# sconf **Repository Path**: sky-heaven/sconf ## Basic Information - **Project Name**: sconf - **Description**: A simple string configure parser. - **Primary Language**: C - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-08-25 - **Last Updated**: 2022-07-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # String configure (sconf) A simple string configure parser. It use CSV fromat. # The data struct item0, item1, item2, item3, item4, item5, item6, item7, item8, # How to use? 1. you should save the data to a string or load from a file. ```c const char *file_str = "abcd, 123, -1024, true, false,\n" "line1, date, 2021, data, line one,\n" "line2, date, 2021, data, line two,\n"; ``` ```c FILE *in_fp = fopen("config", "r"); fseek(in_fp, 0, SEEK_END); long size = ftell(in_fp); fseek(in_fp, 0, SEEK_SET); char *file_buf = malloc(size + 1); fread(file_buf, size, 1, in_fp); fclose(in_fp); printf("buf:%s\n", file_buf); ``` 2. read from the string. ```c char *p = (char *) file_str; /* read one line */ char line[128] = {0}; p = sconf_readline(p, line, 128); if (!p) printf("no line left"); /* read each item */ char *q = line; while (1) { char buf[32] = {0}; /* read item */ q = sconf_read(q, buf, 32); if (!q) // no item left break; printf("item: %s\n", buf); } ====output==== item: abcd item: 123 item: -1024 item: true item: false item: line1 item: date item: 2021 item: data item: line one item: line2 item: date item: 2021 item: data item: line two ``` 3. write to the string. ```c char *p = (char *) buf_str; /* write one line */ char line[128] = {0}; sconf_write(line, "abcd"); sconf_write(line, "123"); sconf_write(line, "-1024"); sconf_write(line, "true"); sconf_write(line, "false"); sconf_writeline(line); printf(line); ====output==== abcd, 123, -1024, true, false, ``` # Example exp1: username, root, password, 1234, exp2: PATH, /bin, /sbin, /usr/local/bin, /usr/local/sbin, exp3: ip, 192.168.0.1, gateway, 255.255.0.0,