💾 Archived View for thebackupbox.net › ~epoch › csv.txt captured on 2023-04-19 at 22:34:08.
-=-=-=-=-=-=-
#include <stdio.h> #include <string.h> // this is supposed to be an attempt at implementing the type of stuff in: // https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6 // but I can't be fucked to shave this yak all the way right now. // pipe this into jq -r to get the string unquoted and unescaped I guess int main(int argc,char *argv[]) { char *name; char *nend; char *value; char *vend; if(argc < 2) { printf("usage: csv derp name\n"); return 1; } name=argv[1]; for(;;) { while(*name && (*name == ' ' || *name == '\t')) name++;//skip over whitespace /before/ the name value=strchr(name,'=');//value will ALWAYS start after first =s after the name. if(!value) return __LINE__;//there wasn't a = after what should be a name. nend=value-1; while(*nend == ' ' || *nend == '\t') nend--; //work backwards from the = nend++; *nend=0;//null out the byte after the name *value=0; value++; while(*value && (*value == ' ' || *value == '\t')) value++;//skip over whitespace if(*value != '"') return __LINE__;//malformed because missing a double-quote after the = value++; vend=strchr(value,'"'); if(!vend) return __LINE__;//malformed because missing a closing double-quote while(vend && *vend && *(vend-1)=='\\') vend=strchr(vend+1,'"'); if(!vend) return __LINE__;//malformed because missing an unescape double-quote to close value. *vend=0; //fprintf(stderr,"'%s' = '%s'\n",name,value); if(argc == 2) { printf("%s\n",name); } if(argc == 3) { if(!strcmp(name,argv[2])) { printf("\"%s\"\n",value); } } vend++; while(*vend && (*vend == ' ' || *vend == '\t')) vend++; if(*vend == '\0') break;//we're at the end of the string, exit. if(*vend != ',') return __LINE__;//need a comma after the closing string if not at end name=vend+1; if(!*name) return __LINE__;//there was a comma at the end of the input } return 0; }