#include #include #if defined(TEST_TARGET_txls) #include #include #else #include "init.h" #include "command.h" #include "txls.h" #endif #define READ_FILE "test/file/read.md" #define WRITE_FILE "test/file/write.md" void txls_preview(txls_t txls) { char* s; int len = 0; if (!txls) return; s = txls_dumps(txls, 1, &len); printf("s %p\r\n", s); if (!s) return; printf("len = %d, <%d, %d>\r\n%s\r\n", len, txls_col(txls), txls_row(txls), s); free(s); } static void test_read(void) { txls_t x = NULL; // Define the txls object, initialized to NULL as usual /* Load the txls file */ x = txls_file_load(READ_FILE); if (!x) // Loading failed, locating error { int line, type; type = txls_error_info(&line); printf("txls parse error! line %d, error %d.\r\n", line, type); return; } /* Iterate over the header to locate the column*/ int col = 0; for (col = 1; col <= txls_col(x); col++) { if (strcmp("Li Si", txls_get_head(x, col)) == 0) { break; } } if (col > txls_col(x)) // Not found { printf("Lookup failed\r\n"); return; } /* print info */ printf("name: %s, age=%s, gender: %s, height=%s, weight=%s, email:%s\r\n", txls_get_text(x, col, 0), txls_get_text(x, col, 1), txls_get_text(x, col, 2), txls_get_text(x, col, 3), txls_get_text(x, col, 4), txls_get_text(x, col, 5)); txls_delete(x); } static void test_write(void) { txls_t x = NULL; // Define the txls object, initialized to NULL as usual x = txls_create(4, 5); // Create a 4x5 txls if (!x) { return; } /* Sets the header of the table, leaving the first column blank */ txls_set_head(x, 2, "Zhang San"); txls_set_head(x, 3, "Li Si"); txls_set_head(x, 4, "Wang Wu"); /* Set the alignment */ txls_set_align(x, 2, TXLS_ALIGN_LEFT); txls_set_align(x, 3, TXLS_ALIGN_CENTER); txls_set_align(x, 4, TXLS_ALIGN_RIGHT); /* The first column serves as the information category */ txls_set_text(x, 1, 1, "age"); txls_set_text(x, 1, 2, "gender"); txls_set_text(x, 1, 3, "height"); txls_set_text(x, 1, 4, "weight"); txls_set_text(x, 1, 5, "email"); /* Write per-person information */ // Zhang San txls_set_text(x, 2, 1, "18"); txls_set_text(x, 2, 2, "man"); txls_set_text(x, 2, 3, "178.5"); txls_set_text(x, 2, 4, "65"); txls_set_text(x, 2, 5, "123321@qq.com"); // Li Si txls_set_text(x, 3, 1, "24"); txls_set_text(x, 3, 2, "woman"); txls_set_text(x, 3, 3, "165"); txls_set_text(x, 3, 4, "48"); txls_set_text(x, 3, 5, "lisi@163.com"); // Wang Wu txls_set_text(x, 4, 1, "20"); txls_set_text(x, 4, 2, "man"); txls_set_text(x, 4, 3, "175"); txls_set_text(x, 4, 4, "75"); txls_set_text(x, 4, 5, "ww1234567890@qq.com"); txls_file_dump(x, WRITE_FILE); txls_delete(x); } static void usage(void) { printf( "Usage: txls [opt] [arg]\n" "\n" "options:\n" " -h Print help\n" " -v Print version\n" "\n" "argument:\n" " Test default read function\n" " Test default write function\n" ); } static int test(int argc, char *argv[]) { /* reset getopt */ command_opt_init(); while (1) { int opt = command_getopt(argc, argv, "hv"); if (opt == -1) break; switch (opt) { case 'v' : printf("txls version %d.%d.%d\r\n", TXLS_V_MAJOR, TXLS_V_MINOR, TXLS_V_PATCH); return 0; case '?': printf("Unknown option `%c`\r\n", command_optopt); return -1; case 'h' : default: usage(); return 0; } } for (int index = command_optind; index < argc; index++) { if (!strcmp(argv[index], "read")) { test_read(); } else if (!strcmp(argv[index], "write")) { test_write(); } } return 0; } #if defined(TEST_TARGET_txls) int main(int argc, char *argv[]) { return test(argc, argv); } #else void test_txls(void) { command_export("txls", test); } init_export_app(test_txls); #endif