// Needs buffer of size BBI_MAX_LINE_LENGTH. Could possibly make it variable size char *config_read_line(char *buf, FILE* in_file) { if (!fgets(buf, BBI_MAX_LINE_LENGTH, in_file)) return NULL; trim_back(buf); if (*buf == NULL) return buf; char *p; while ( *(p = buf +(strlen(buf)-1) ) == '\\') { char line[BBI_MAX_LINE_LENGTH]; if (fgets(line, sizeof line, in_file)) { char *newline = trim_front(line); trim_back(newline); if (strlen(buf) + strlen(newline) < BBI_MAX_LINE_LENGTH) strcpy(p,newline); else { return buf; // line too long, possible error message. for now it's just an error during later parsing } } else { return buf; // FIX, possible error message. } } return buf; }
/* Splits data in place into strings containing section-headers, keys and * values using one or more '\0' as a delimiter. Unescapes quoted values */ static void split_data(ini_t *ini) { char *value_start, *line_start; char *p = ini->data; while (p < ini->end) { switch (*p) { case '\r': case '\n': case '\t': case ' ': *p = '\0'; /* Fall through */ case '\0': p++; break; case '[': p += strcspn(p, "]\n"); *p = '\0'; break; case ';': p = discard_line(ini, p); break; default: line_start = p; p += strcspn(p, "=\n"); /* Is line missing a '='? */ if (*p != '=') { p = discard_line(ini, line_start); break; } trim_back(ini, p - 1); /* Replace '=' and whitespace after it with '\0' */ do { *p++ = '\0'; } while (*p == ' ' || *p == '\r' || *p == '\t'); /* Is a value after '=' missing? */ if (*p == '\n' || *p == '\0') { p = discard_line(ini, line_start); break; } if (*p == '"') { /* Handle quoted string value */ value_start = p; p = unescape_quoted_value(ini, p); /* Was the string empty? */ if (p == value_start) { p = discard_line(ini, line_start); break; } /* Discard the rest of the line after the string value */ p = discard_line(ini, p); } else { /* Handle normal value */ p += strcspn(p, "\n"); trim_back(ini, p - 1); } break; } } }
void trim(std::string& s) { trim_front(s); trim_back(s); }
void parse_field( char const*& p, char const* last, string_view& name, string_view& value, static_string<N>& buf, error_code& ec) { /* header-field = field-name ":" OWS field-value OWS field-name = token field-value = *( field-content / obs-fold ) field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] field-vchar = VCHAR / obs-text obs-fold = CRLF 1*( SP / HTAB ) ; obsolete line folding ; see Section 3.2.4 token = 1*<any CHAR except CTLs or separators> CHAR = <any US-ASCII character (octets 0 - 127)> sep = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT */ static char const* is_token = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0" "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1" "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // name BOOST_ALIGNMENT(16) static const char ranges1[] = "\x00 " /* control chars and up to SP */ "\"\"" /* 0x22 */ "()" /* 0x28,0x29 */ ",," /* 0x2c */ "//" /* 0x2f */ ":@" /* 0x3a-0x40 */ "[]" /* 0x5b-0x5d */ "{\377"; /* 0x7b-0xff */ auto first = p; bool found; std::tie(p, found) = find_fast( p, last, ranges1, sizeof(ranges1)-1); if(! found && p >= last) { ec = error::need_more; return; } for(;;) { if(*p == ':') break; if(! is_token[static_cast< unsigned char>(*p)]) { ec = error::bad_field; return; } ++p; if(p >= last) { ec = error::need_more; return; } } if(p == first) { // empty name ec = error::bad_field; return; } name = make_string(first, p); ++p; // eat ':' char const* token_last; for(;;) { // eat leading ' ' and '\t' for(;;++p) { if(p + 1 > last) { ec = error::need_more; return; } if(! (*p == ' ' || *p == '\t')) break; } // parse to CRLF first = p; p = parse_token_to_eol(p, last, token_last, ec); if(ec) return; if(! p) { ec = error::bad_value; return; } // Look 1 char past the CRLF to handle obs-fold. if(p + 1 > last) { ec = error::need_more; return; } token_last = trim_back(token_last, first); if(*p != ' ' && *p != '\t') { value = make_string(first, token_last); return; } ++p; if(token_last != first) break; } buf.resize(0); buf.append(first, token_last); BOOST_ASSERT(! buf.empty()); try { for(;;) { // eat leading ' ' and '\t' for(;;++p) { if(p + 1 > last) { ec = error::need_more; return; } if(! (*p == ' ' || *p == '\t')) break; } // parse to CRLF first = p; p = parse_token_to_eol(p, last, token_last, ec); if(ec) return; if(! p) { ec = error::bad_value; return; } // Look 1 char past the CRLF to handle obs-fold. if(p + 1 > last) { ec = error::need_more; return; } token_last = trim_back(token_last, first); if(first != token_last) { buf.push_back(' '); buf.append(first, token_last); } if(*p != ' ' && *p != '\t') { value = {buf.data(), buf.size()}; return; } ++p; } } catch(std::length_error const&) { ec = error::header_limit; return; } }
static void trim (std::string & str) { trim_back(str); trim_front(str); }
int main(int argc, char **argv){ char buffer[1024],*buffptr; char buffer2[1024]; int i; char *filein=NULL,*fileout=NULL,*prog; FILE *streamin=NULL,*streamout=NULL; int lendata; buffptr=buffer; prog=argv[0]; for(i=1;i<argc;i++){ int lenarg; char *arg; arg=argv[i]; lenarg=strlen(arg); if(arg[0]=='-'&&lenarg>1){ switch(arg[1]){ case 'h': usage(prog); exit(1); break; default: usage(prog); exit(1); break; } } else{ if(filein==NULL){ filein=arg; continue; } if(fileout==NULL){ fileout=arg; continue; } } } if(filein==NULL||fileout==NULL){ usage(prog); exit(1); } streamin=fopen(filein,"r"); streamout=fopen(fileout,"w"); if(streamin==NULL||streamout==NULL){ if(streamin==NULL){ fprintf(stderr,"unable to open %s for input\n",filein); } if(streamout==NULL){ fprintf(stderr,"unable to open %s for output\n",fileout); } exit(1); } fprintf(streamout,"@echo off\n"); for(;;){ if(fgets(buffer,1024,streamin)==NULL)break; trim_back(buffer); if(strlen(buffer)==0){ fprintf(streamout,"\n"); continue; } if(buffer[0]=='#'){ if(strlen(buffer)>0){ fprintf(streamout,":: %s\n",buffer+1); } else{ fprintf(streamout,"::\n"); } continue; } if(buffer[0]=='$'){ char *comm_beg, *comm_end, *data; char *casename; int j; char *datato, *datafrom; comm_beg=buffer+1; comm_end=strchr(buffer,' '); data = comm_end+1; *comm_end=0; trim_back(data); fprintf(streamout,"%s%s%s %s\n","%",comm_beg,"%",data); continue; } fprintf(streamout,"%s\n",buffer); } }