Last active
September 18, 2023 01:17
-
-
Save niemasd/0a100fae63ff6058fb2fe43ffc3f401d to your computer and use it in GitHub Desktop.
Parse TSV data loaded as a C string (char*)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile: g++ -o tsv_parse tsv_parse.c | |
// Run: ./tsv_parse | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char** argv) { | |
// example TSV dataset as C string | |
char* profile_data; | |
profile_data = (char*)"a\t1\nb\t2.3\nc\thello\n"; | |
printf("%s\n", profile_data); | |
// declare the variables I'll be reading from the TSV (equivalent to the struct) | |
char a; | |
float b; | |
unsigned int c_len = 1024; // whatever string allocation you want | |
char c[c_len]; | |
// parse TSV | |
int profile_data_len = strlen(profile_data); | |
unsigned int key_start; | |
unsigned int key_len; | |
unsigned int val_start; | |
unsigned int val_len; | |
unsigned int i, j; // i is for index in TSV data, and j is for handling string copying | |
i = 0; | |
while(i < profile_data_len-1) { | |
// find start and end of key and value | |
key_start = i; key_len = 0; | |
while(profile_data[i++] != '\t') { ++key_len; } | |
val_start = i; val_len = 0; | |
while(profile_data[i++] != '\n') { ++val_len; } | |
// extract new C strings (probably not necessary, but easier for string stuff) | |
char key[key_len+1]; memcpy(key, &profile_data[key_start], key_len); key[key_len] = '\0'; | |
char val[val_len+1]; memcpy(val, &profile_data[val_start], val_len); val[val_len] = '\0'; | |
// parse to store variables | |
if(strcmp(key, "a") == 0) { | |
a = (char)strtol(val, NULL, 10); | |
} else if(strcmp(key, "b") == 0) { | |
b = (float)strtod(val, NULL); | |
} else if(strcmp(key, "c") == 0) { | |
memcpy(c, &val, val_len); | |
for(j = val_len; j < c_len; ++j) { c[j] = '\0'; } | |
} | |
// put additional strcmp(key, "KEY_NAME") == 0) checks for each "KEY_NAME" the user can provide in the TSV | |
} | |
// print variables that were loaded | |
printf("a = %d\n", a); | |
printf("b = %f\n", b); | |
printf("c = %s\n", c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment