void HttpSession::parseHeader() { istream is(&impl->data); string line; getline(is, line); auto rc = split(line, " "); impl->code = stol(rc[1]); LOGD(">>%s", line); while(!is.eof()) { getline(is, line); if(line == "" || line == "\r") break; LOGD("%s", line); auto parts = split(line, ": ", 2); if(parts.size() == 2) { parts[1] = rstrip(rstrip(parts[1], '\n'), '\r'); impl->header[parts[0]] = parts[1]; } } auto cl = impl->header["Content-Length"]; impl->cntSize = cl != "" ? stol(impl->header["Content-Length"]) : 0; if(isRedirect()) { LOGD("Should redirect to '%s'", impl->header["Location"]); //sleepms(500); } }
int CConfigHandler::parseFile(FILE *pFile, int (*handler)(void *, const char *, const char *, const char *), void *object) { char line[MAX_LINE]; char section[MAX_SECTION]; char *start; char *end; char *name; char *value; char *chr; int lineno = 0; memset(line, 0, sizeof(line)); while (fgets(line, MAX_LINE, pFile) != NULL) { start = line; start = lstrip(rstrip(start)); if (*start == ';' || *start == '#' || 0 >= strlen(start)) { memset(line, 0, sizeof(line)); continue; } /** * [section] line */ chr = strchr(line, '['); if (chr) { end = strchr(chr + 1, ']'); if (end) { *end = '\0'; memset(section, 0, sizeof(section)); strcpy(section, chr + 1); } continue; } /** * name = value */ end = strchr(line, '='); if (end && strlen(section)) { *end = '\0'; name = lstrip(rstrip(start)); value = lstrip(end + 1); rstrip(value); handler(object, section, name, value); ++lineno; } } return lineno; }
int extinputs_parse_2D(const char *filename, struct pulse_2D **cfginputs, int N1, int N2) { FILE *fin; char buf[MAX_LINE]; char *start; char *end; char *values; int error = 0; fin = fopen(filename, "r"); if (!fin) return -1; int lineno = 0; double t, q1, q2, i, d, m; /* the values to be read */ /* Scan through file line by line */ while (fgets(buf, sizeof(buf), fin) != NULL) { lineno++; start = lskip(rstrip(buf)); /* chop whites */ if (*start && *start != '#') { /* Remove possible comments at the end */ end = find_char_or_comment(start, '#'); if (*end == '#') *end = '\0'; values = rstrip(start); /* Not a comment, must be a line containing 6 numbers */ /* Scan and check if the inputs is correctly formatted */ if (sscanf(values, "%lf %lf %lf %lf %lf %lf", &t, &d, &q1, &q2, &i, &m) == 6) { if (t >= 0 && d > 0 && m >= 0) { *cfginputs = append_pulse_2D(*cfginputs, new_pulse_2D(N1, N2, t, d, -q1, -q2, i, m)); } else { printf("Durations and time onsets should be positive.\n"); printf("Check configuration file for external inputs.\n"); error = lineno; } } else if (!error) { /* No '=' found on name=value line */ error = lineno; } } } fclose(fin); return error; }
int conf_parse(const char *filename, int (*handler) (void *, const char *, const char *), void *usercfg) { FILE *fin; char buf[MAX_LINE]; char *start; char *end; char *name; char *value; int error = 0; fin = fopen(filename, "r"); if (!fin) return -1; int lineno = 0; /* Scan through file line by line */ while (fgets(buf, sizeof(buf), fin) != NULL) { lineno++; start = lskip(rstrip(buf)); /* chop whites */ if (*start && *start != '#') { /* Not a comment, must be a name = value pair */ end = find_char_or_comment(start, '='); if (*end == '=') { *end = '\0'; name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '#'); if (*end == '#') *end = '\0'; value = rstrip(value); value = strip_quotes(value); /* Valid name=value pair found, call handler */ if (handler(usercfg, name, value) && !error) error = lineno; } else if (!error) { /* No '=' found on name=value line */ error = lineno; } } } fclose(fin); return error; }
void gadget_print(gadget_t *gadgets) { /* If we're at a leaf node in the tree... */ if (gadgets->previous.head == NULL) { /* Print instructions from the leaf up to the root as a gadget. */ x86_insn_t instr; char line[1000]; gadget_t *cursor = gadgets; while (cursor != NULL) { x86_disasm(cursor->instr, cursor->instr_len, 0, 0, &instr); x86_format_insn(&instr, line, sizeof(line), intel_syntax); rstrip(line); tab_to_space(line); printf("0x%08x: %-50s\n", cursor->virtual_address, line); /* Set cursor to its parent in the tree (closer to the RET). */ cursor = cursor->next; } printf("-----------------------\n"); } else { /* We're not at a leaf, so recursively print all of the children. */ gadget_list_item_t *cursor = gadgets->previous.head; while (cursor != NULL) { gadget_print(cursor->gadget); cursor = cursor->next; } } }
void strip( char *s, const char *delimiters) { lstrip(s, delimiters); rstrip(s, delimiters); }
static void flytec_pbrrts(flytec_t *flytec) { flytec_puts_nmea(flytec, "PBRRTS,"); flytec_expectc(flytec, XOFF); route_head *route = 0; char line[128]; while (flytec_gets_nmea(flytec, line, sizeof line)) { const char *p = line; p = match_literal(p, "PBRRTS,"); int index = 0, count = 0, routepoint_index = 0; p = match_unsigned(p, &index); p = match_char(p, ','); p = match_unsigned(p, &count); p = match_char(p, ','); p = match_unsigned(p, &routepoint_index); p = match_char(p, ','); if (!p) continue; if (routepoint_index == 0) { char *name = 0; p = match_string_until(p, '\0', 0, &name); p = match_eos(p); if (p) { route = route_head_alloc(); route->rte_num = index + 1; route->rte_name = rstrip(name); route_add_head(route); } else { free(name); } } else { char *name = 0; p = match_string_until(p, ',', 1, 0); p = match_string_until(p, '\0', 0, &name); p = match_eos(p); if (p) { const waypoint *w = find_waypt_by_name(rstrip(name)); if (w) route_add_wpt(route, waypt_dupe(w)); } free(name); } } flytec_expectc(flytec, XON); }
bool initLemmaLexicon ( const std::string sInputFile ) { std::ifstream *is; is = new std::ifstream(sInputFile.c_str()); if ( is->fail() ) return false; bool bReadSuccessful; std::string line; std::string curToken; getline(*is, line); while(is && !lstrip(line).empty()) { std::string form; std::string lemma; std::string tag; std::istringstream iss(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (form not found)"); form = curToken; //iss = std::istringstream(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (lemma not found)"); lemma = curToken; if ( lemma == "=" ) lemma = form; //lexicon uses = to represent that lemma equals form //iss = std::istringstream(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (tag not found)"); tag = curToken; //add to the word to lemma map //wordToLemma.insert(form,lemma); wordToLemma[form]=lemma; CMorphTag morphTag = lexiconTagToMorphTag(tag); std::pair<std::string,CMorphTag> wordMorphPair = std::pair<std::string,CMorphTag>(form,morphTag); wordAndTagToLemma[wordMorphPair] = lemma; getline(*is, line); } is->close(); delete is; //And we are done. return true; }
static char *stripquote(char *s) { s = lskip(s); char *p=NULL; rstrip(s, &p); p--; if( ISQUOTE(*s) && ISQUOTE(*p) ) { s++; *p = '\0'; } return s; }
void LoadExcludes (char ***ex,int *nex,char *filename) { FILE *fp; int x; char **test; static char s[CCHMAXPATHCOMP + 4]; BOOL temp = fSuspend; if(!ex || !nex || !filename) return; fSuspend = TRUE; if(*ex) FreeExcludes(ex, nex); x = 0; sprintf(s, "%s%s", mydir, filename); fp = fopen(s,"r"); if(fp) { while(!feof(fp)) { if(!fgets(s, sizeof(s), fp)) break; s[sizeof(s) - 1] = 0; stripcr(s); lstrip(rstrip(s)); if(*s) { if(x >= *nex - 1) { test = realloc(*ex, sizeof(char *) * (*nex + 2)); if(test) *ex = test; else break; } (*ex)[*nex] = strdup(s); if((*ex)[*nex]) { (*nex)++; (*ex)[*nex] = NULL; } else break; } } fclose(fp); } fSuspend = temp; }
/* print longest input line */ int main() { int len; /* current line length */ int cursor; /* The current position in the array */ char line[MAXLINE]; /* current input line */ while ((len = getline(line, MAXLINE)) > 0) { cursor = rstrip(len,line); if (cursor >= 0) printf("%s", line); } return 0; }
int main (int argc, char *argv[]) { char search_for[80]; /* take input from the user and search */ printf("Search for: "); fgets(search_for, 80, stdin); rstrip(search_for); /*find_track(search_for);*/ find_track_regex(search_for); return 0; }
void main() { char line[MAXLINE]; int length; while(length = getLine(line, MAXLINE) > 0) { printf("input: %s", line); if (length == 0 && line[0] == '\n') continue; rstrip(line); printf(line); } }
static char * polezero_comment_token(char *p) { if(!p) { return p; } p = strchr(p, ':'); if(p) { p = p + 1; p = lstrip(p); p = rstrip(p); } return p; }
void event_init(pmID pmid) { char cmd[MAXPATHLEN]; int i, fd; for (i = 0; i < numlogfiles; i++) { size_t pathlen = strlen(logfiles[i].pathname); /* * We support 2 kinds of PATHNAMEs: * (1) Regular paths. These paths are opened normally. * (2) Pipes. If the path ends in '|', the filename is * interpreted as a command which pipes input to us. */ if (logfiles[i].pathname[pathlen - 1] != '|') { fd = open(logfiles[i].pathname, O_RDONLY|O_NONBLOCK); if (fd < 0) { if (logfiles[i].fd >= 0) /* log once only */ __pmNotifyErr(LOG_ERR, "open: %s - %s", logfiles[i].pathname, strerror(errno)); } else { if (fstat(fd, &logfiles[i].pathstat) < 0) if (logfiles[i].fd >= 0) /* log once only */ __pmNotifyErr(LOG_ERR, "fstat: %s - %s", logfiles[i].pathname, strerror(errno)); lseek(fd, 0, SEEK_END); } } else { strncpy(cmd, logfiles[i].pathname, sizeof(cmd)); cmd[pathlen - 1] = '\0'; /* get rid of the '|' */ rstrip(cmd); /* Remove all trailing whitespace. */ fd = start_cmd(cmd, &logfiles[i].pid); if (fd < 0) { if (logfiles[i].fd >= 0) /* log once only */ __pmNotifyErr(LOG_ERR, "pipe: %s - %s", logfiles[i].pathname, strerror(errno)); } else { if (fd > maxfd) maxfd = fd; FD_SET(fd, &fds); } } logfiles[i].fd = fd; /* keep file descriptor (or error) */ logfiles[i].pmid = pmid; /* string param metric identifier */ logfiles[i].queueid = pmdaEventNewQueue(logfiles[i].pmnsname, maxmem); } }
static void flytec_pbrwps(flytec_t *flytec) { flytec_puts_nmea(flytec, "PBRWPS,"); flytec_expectc(flytec, XOFF); char line[128]; while (flytec_gets_nmea(flytec, line, sizeof line)) { const char *p = line; p = match_literal(p, "PBRWPS,"); int lat_deg = 0, lat_min = 0, lat_mmin = 0; p = match_n_digits(p, 2, &lat_deg); p = match_n_digits(p, 2, &lat_min); p = match_char(p, '.'); p = match_n_digits(p, 3, &lat_mmin); p = match_char(p, ','); char lat_hemi = '\0'; p = match_one_of(p, "NS", &lat_hemi); p = match_char(p, ','); int lon_deg = 0, lon_min = 0, lon_mmin = 0; p = match_n_digits(p, 3, &lon_deg); p = match_n_digits(p, 2, &lon_min); p = match_char(p, '.'); p = match_n_digits(p, 3, &lon_mmin); p = match_char(p, ','); char lon_hemi = '\0'; p = match_one_of(p, "EW", &lon_hemi); p = match_char(p, ','); char *name = 0; p = match_string_until(p, ',', 1, 0); p = match_string_until(p, ',', 1, &name); int ele = 0; p = match_unsigned(p, &ele); p = match_eos(p); if (p) { waypoint *w = waypt_new(); w->latitude = lat_deg + lat_min / 60.0 + lat_mmin / 60000.0; if (lat_hemi == 'S') w->latitude = -w->latitude; w->longitude = lon_deg + lon_min / 60.0 + lon_mmin / 60000.0; if (lon_hemi == 'W') w->longitude = -w->longitude; w->altitude = ele; w->shortname = rstrip(name); waypt_add(w); } else { free(name); } } flytec_expectc(flytec, XON); }
char *read_clipped_line(FILE *f, char **buffer, size_t *maxlen) { while(readline(f, buffer, maxlen)) { char *line, *comment; line = lstrip(*buffer); comment = strchr(line, '#'); if(comment) *(comment++) = 0; line = rstrip(line); if(line[0]) return line; } return NULL; }
int main(int argc, char *argv[]) { char buf[LEN]; char *t = NULL; char **list = NULL; int word = 1; int i; FILE *fp; fp = fopen(argv[1], "r"); assert(fp); /* this leaks you need to fix*/ while(fgets(buf, LEN, fp)) { /* remove new line */ rstrip(buf); tolowercase(buf); t = malloc((strlen(buf) + 1) * sizeof(char)); assert(t); strncpy(t, buf, strlen(buf) + 1); /* printf("%s\n", t); */ list = realloc(list, word * sizeof(char *)); list[word - 1] = t; word++; } /* overcounted */ word--; /* print the list */ for(i = 0; i < word; i++) printf("%s\n", list[i]); printf("\n"); heapsort(list, word, sizeof(list[0]), genericStrcmp); /* print the sorted list */ for(i = 0; i < word; i++) { printf("%s\n", list[i]); free(list[i]); } free(list); fclose(fp); return 0; }
void joinall(char *baseuri, char **uris, int size){ int i; char *parsed = NULL; for (i = 0; i < size; i++){ lstrip(uris[i]); rstrip(uris[i]); parsed = join(baseuri,uris[i]); if (parsed == NULL) { continue; } free(uris[i]); uris[i] = NULL; uris[i] = parsed; parsed = NULL; } }
int main(void) { const char* t1 = "This is a test sentence to see if\nwhitespace removal is working"; const char* t2 = "gggThis tests if lstrip is working"; const char* t3 = "This tests if rstrip is workinghhh"; const char* t4 = "000 this tests is strip is working 000"; const char* t5 = "this tEsts if UpPer is WorkINg"; const char* t6 = "THIS TESTS if LOwEr is WORKInG"; printf("%s\n %s\n\n", t1, removeWhiteSpace(t1)); printf("%s\n %s\n\n", t2, lstrip(t2, 'g')); printf("%s\n %s\n\n", t3, rstrip(t3, 'h')); printf("%s\n %s\n\n", t4, strip(t4, '0')); printf("%s\n %s\n\n", t5, upper(t5)); printf("%s\n %s\n\n", t6, lower(t6)); return 0; }
static void scan_for_hostname(const char *filename, char **hostname, char **port) { if(!filename || (*hostname && *port)) return; FILE *f = fopen(filename, "r"); if(!f) return; while(fgets(line, sizeof line, f)) { if(!rstrip(line)) continue; char *p = line, *q; p += strcspn(p, "\t ="); if(!*p) continue; q = p + strspn(p, "\t "); if(*q == '=') q += 1 + strspn(q + 1, "\t "); *p = 0; p = q + strcspn(q, "\t "); if(*p) *p++ = 0; p += strspn(p, "\t "); p[strcspn(p, "\t ")] = 0; if(!*port && !strcasecmp(line, "Port")) { *port = xstrdup(q); } else if(!*hostname && !strcasecmp(line, "Address")) { *hostname = xstrdup(q); if(*p) { free(*port); *port = xstrdup(p); } } if(*hostname && *port) break; } fclose(f); }
QLogSystem::QLogSystem() : m_nExpire(15) { m_Time = QDateTime::currentDateTime(); QString strDocuments = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); strDocuments = rstrip( strDocuments, "/\\" ); m_strRootPath = strDocuments; m_strRootPath += "/T3kCfgFE/Logs"; makeDirectory(m_strRootPath); QString strLogPathName; getLogPathName( m_Time, strLogPathName ); openLogFile( strLogPathName ); deleteExpiredLogFiles(); write( "INFO", ("==== T3kCfgFE Start! ====") ); write( "INFO", "Exec Path: %s", (const char*)qApp->applicationFilePath().toUtf8() ); }
int TextEditor::getIndentPosition(QString & str) { QString tab = QString('\t'); int indent = 0; QString trimmed = rstrip(str); if(trimmed.size() == 0) { indent = str.size(); } else { for (int i = 0; str.size() - 1 >= i; i++) { bool isTab = str.at(i) == tab; if (!str.at(i).isSpace()) { break; } if(isTab) { indent += tabStopCount; } else { indent++; } } } return indent; }
int ini_parse_string_impl(const std::string & s, int (*handler)(void*, const char*, const char*, const char*), void* user) { std::istringstream input(s); /* Uses a fair bit of stack (use heap instead if you need to) */ char section[MAX_INI_STRING] = ""; bool has_group = false; int lineno = 0; int error = 0; /* Scan through file line by line */ while (!at_end(input)) { std::string newline; get_line(input, newline); char * line = (char*)newline.c_str(); lineno++; char * start = line; char * end; // UTF-8 BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; } start = lskip(rstrip(start)); char c = *start; if (c == ';' || c == '#' || (c == '/' && start[1] == '/')) { /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (c == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); has_group = true; } else if (!error) { /* No ']' found on section line */ error = lineno; } } else if (c && c != ';' && has_group) { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; char * name = rstrip(start); char * value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ if (!handler(user, section, name, value) && !error) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } } return error; }
int write_final_state(control *c, params *p, state *s) { /* Write the final state to the input param file so we can easily restart the model. This function copies the input param file with the exception of anything in the git hash and the state which it replaces with the updated stuff. */ char line[STRING_LENGTH]; char saved_line[STRING_LENGTH]; char section[STRING_LENGTH] = ""; char prev_name[STRING_LENGTH] = ""; char *start; char *end; char *name; char *value; int error = 0; int line_number = 0; int match = FALSE; while (fgets(line, sizeof(line), c->ifp) != NULL) { strcpy(saved_line, line); line_number++; start = lskip(rstrip(line)); if (*start == ';' || *start == '#') { /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (*start == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); *prev_name = '\0'; } else if (!error) { /* No ']' found on section line */ error = line_number; } } else if (*start && *start != ';') { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); if (!ohandler(section, name, value, c, p, s, &match) && !error) error = line_number; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = line_number; break; } } if (match == FALSE) fprintf(c->ofp, "%s", saved_line); else match = FALSE; /* reset match flag */ } return error; }
/* See documentation in header file. */ int ini_parse_file(FILE* file,const struct IniConfig* config, void* user) { /* Uses a fair bit of stack (use heap instead if you need to) */ #if INI_USE_STACK char line[INI_MAX_LINE]; #else char* line; #endif char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; char* start; char* end; char* name; char* value; int lineno = 0; int error = 0; #if !INI_USE_STACK line = (char*)malloc(INI_MAX_LINE); if (!line) { return -2; } #endif /* Scan through file line by line */ while (fgets(line, INI_MAX_LINE, file) != NULL) { lineno++; start = line; #if INI_ALLOW_BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; } #endif start = lskip(rstrip(start)); if (*start == ';' || *start == '#') { start += 1; if (!config->commentHandler(user, start) && !error) error = lineno; /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (config->supportMultiline && *prev_name && *start && start > line) { /* Non-black line with leading whitespace, treat as continuation of previous name's value (as per Python ConfigParser). */ if (!config->keyHandler(user, section, prev_name, start, 1) && !error) error = lineno; } else if (*start == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); *prev_name = '\0'; if(!config->sectionHandler(user, section) && !error) error = lineno; } else if (!error) { /* No ']' found on section line */ error = lineno; } } else if (*start && *start != ';') { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); if (!config->keyHandler(user, section, name, value, 0) && !error) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } #if INI_STOP_ON_FIRST_ERROR if (error) break; #endif } #if !INI_USE_STACK free(line); #endif return error; }
// read and parse int load(const char* filename) { FILE *file = fopen(filename, "r"); if(!file) return false; char *line = new char[INI_MAX_LINE]; std::string prev_value, prev_name; char *section = new char [MAX_SECTION]; *section = '\0'; int bMultiline = 0; int lineno = 0, error=0; char *start, *end, *name, *value; while (fgets(line, INI_MAX_LINE, file) != NULL) { lineno++; start = line; start = lskip(start); // remove comment end = find_char_or_comment(start, '\0'); if( *end != '\0' ) // it points to a comment char *end = '\0'; rstrip(start, &end); if( start == end ) continue; if ( bMultiline ) { if( *(end-1) == '\\' && (end-1 == start || *(end-2) == ' ') ) { // continue reading next line // remove " \" *(--end) = '\0'; if( end > start && *(end-1) == ' ') *(--end) = '\0'; prev_value += start; } else{ prev_value += start; if( !handle_record(section, prev_name.c_str(), prev_value.c_str()) ) error = lineno; bMultiline = 0; prev_value = ""; prev_name = ""; } } else if( *start == '[' ) { // found section end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, MAX_SECTION); } else{ /* No ']' found on section line */ error = lineno; } bMultiline = false; } else { /* Not a comment, must be a name[=:]value pair */ bMultiline = false; char *nend = find_char_or_comment(start, '='); if (*nend != '=') { nend = find_char_or_comment(start, ':'); } if (*nend == '=' || *nend == ':') { *nend = '\0'; value = lskip(nend + 1); name = rstrip(start, &nend); /* Valid name[=:]value pair found, call handler */ if( *(end-1) == '\\' && (end-1 == value || *(end-2) == ' ') ) { // continue reading next line // remove " \" *(--end) = '\0'; if( end > value && *(end-1) == ' ') *(--end) = '\0'; prev_value = value; prev_name = name; bMultiline = true; } if( !bMultiline ) if (!handle_record(section, name, value)) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } if (error) break; } // while getline fclose(file); delete [] line; delete [] section; return error; } // read function
ssize_t outputMsg( struct write_port *wport, char *str, unsigned int len) { ssize_t ret = -1; char *str_copy = NULL; /* * Log a copy of str without newline(s) */ str_copy = strdup(str); if (str_copy) { rstrip(str_copy, "\r\n"); LOG(LOG_INFO, "Sending %s", str_copy); FLUSH_LOG(); free(str_copy); } if (wport->protocol == LOCAL) { ret = write(wport->out_desc, (const void *)str, len); if (ret < 0) perror("write()"); else if (ret != (ssize_t)len) LOG(LOG_ERR, "Wrote %zd bytes instead of %u", ret, len); return (ret); } else if (wport->protocol == TCP) { /* * send it */ ret = write(wport->out_desc, (const void *)str, len); if (ret < 0) perror("write()"); else if (ret != (ssize_t)len) LOG(LOG_ERR, "Wrote %zd bytes instead of %u", ret, len); return (ret); } else if (wport->protocol == UDP) { /* * send it */ ret = sendto(wport->out_desc, (const void *)str, len, 0, (struct sockaddr *)&(wport->server_addr), wport->to_length); if (ret < 0) perror("sendto()"); else if (ret != (ssize_t)len) LOG(LOG_ERR, "Sent %zd bytes instead of %u", ret, len); } else { LOG(LOG_ERR, "unknown protocol specification: %d", wport->protocol); } return (ret); }
static void LoadConfigFromFile(FILE *f) { char curSection[128] = ""; char *buffer = NULL; size_t maxlen = 0; ConfigEntry *ent; while(readline(f, &buffer, &maxlen)) { char *line, *comment; char key[256] = ""; char value[256] = ""; comment = strchr(buffer, '#'); if(comment) *(comment++) = 0; line = rstrip(lstrip(buffer)); if(!line[0]) continue; if(line[0] == '[') { char *section = line+1; char *endsection; endsection = strchr(section, ']'); if(!endsection || section == endsection || endsection[1] != 0) { ERR("config parse error: bad line \"%s\"\n", line); continue; } *endsection = 0; if(strcasecmp(section, "general") == 0) curSection[0] = 0; else { strncpy(curSection, section, sizeof(curSection)-1); curSection[sizeof(curSection)-1] = 0; } continue; } if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 || sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 || sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2) { /* sscanf doesn't handle '' or "" as empty values, so clip it * manually. */ if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0) value[0] = 0; } else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2) { /* Special case for 'key =' */ value[0] = 0; } else { ERR("config parse error: malformed option line: \"%s\"\n\n", line); continue; } rstrip(key); if(curSection[0] != 0) { size_t len = strlen(curSection); memmove(&key[len+1], key, sizeof(key)-1-len); key[len] = '/'; memcpy(key, curSection, len); } /* Check if we already have this option set */ ent = cfgBlock.entries; while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount) { if(strcasecmp(ent->key, key) == 0) break; ent++; } if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount) { /* Allocate a new option entry */ ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry)); if(!ent) { ERR("config parse error: error reallocating config entries\n"); continue; } cfgBlock.entries = ent; ent = cfgBlock.entries + cfgBlock.entryCount; cfgBlock.entryCount++; ent->key = strdup(key); ent->value = NULL; } free(ent->value); ent->value = expdup(value); TRACE("found '%s' = '%s'\n", ent->key, ent->value); } free(buffer); }
static void LoadConfigFromFile(FILE *f) { char curSection[128] = ""; char *buffer = NULL; size_t maxlen = 0; ConfigEntry *ent; while(readline(f, &buffer, &maxlen)) { char *line, *comment; char key[256] = ""; char value[256] = ""; line = rstrip(lstrip(buffer)); if(!line[0]) continue; if(line[0] == '[') { char *section = line+1; char *endsection; endsection = strchr(section, ']'); if(!endsection || section == endsection) { ERR("config parse error: bad line \"%s\"\n", line); continue; } if(endsection[1] != 0) { char *end = endsection+1; while(isspace(*end)) ++end; if(*end != 0 && *end != '#') { ERR("config parse error: bad line \"%s\"\n", line); continue; } } *endsection = 0; if(strcasecmp(section, "general") == 0) curSection[0] = 0; else { size_t len, p = 0; do { char *nextp = strchr(section, '%'); if(!nextp) { strncpy(curSection+p, section, sizeof(curSection)-1-p); break; } len = nextp - section; if(len > sizeof(curSection)-1-p) len = sizeof(curSection)-1-p; strncpy(curSection+p, section, len); p += len; section = nextp; if(((section[1] >= '0' && section[1] <= '9') || (section[1] >= 'a' && section[1] <= 'f') || (section[1] >= 'A' && section[1] <= 'F')) && ((section[2] >= '0' && section[2] <= '9') || (section[2] >= 'a' && section[2] <= 'f') || (section[2] >= 'A' && section[2] <= 'F'))) { unsigned char b = 0; if(section[1] >= '0' && section[1] <= '9') b = (section[1]-'0') << 4; else if(section[1] >= 'a' && section[1] <= 'f') b = (section[1]-'a'+0xa) << 4; else if(section[1] >= 'A' && section[1] <= 'F') b = (section[1]-'A'+0x0a) << 4; if(section[2] >= '0' && section[2] <= '9') b |= (section[2]-'0'); else if(section[2] >= 'a' && section[2] <= 'f') b |= (section[2]-'a'+0xa); else if(section[2] >= 'A' && section[2] <= 'F') b |= (section[2]-'A'+0x0a); if(p < sizeof(curSection)-1) curSection[p++] = b; section += 3; } else if(section[1] == '%') { if(p < sizeof(curSection)-1) curSection[p++] = '%'; section += 2; } else { if(p < sizeof(curSection)-1) curSection[p++] = '%'; section += 1; } if(p < sizeof(curSection)-1) curSection[p] = 0; } while(p < sizeof(curSection)-1 && *section != 0); curSection[sizeof(curSection)-1] = 0; } continue; } comment = strchr(line, '#'); if(comment) *(comment++) = 0; if(!line[0]) continue; if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 || sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 || sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2) { /* sscanf doesn't handle '' or "" as empty values, so clip it * manually. */ if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0) value[0] = 0; } else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2) { /* Special case for 'key =' */ value[0] = 0; } else { ERR("config parse error: malformed option line: \"%s\"\n\n", line); continue; } rstrip(key); if(curSection[0] != 0) { size_t len = strlen(curSection); memmove(&key[len+1], key, sizeof(key)-1-len); key[len] = '/'; memcpy(key, curSection, len); } /* Check if we already have this option set */ ent = cfgBlock.entries; while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount) { if(strcasecmp(ent->key, key) == 0) break; ent++; } if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount) { /* Allocate a new option entry */ ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry)); if(!ent) { ERR("config parse error: error reallocating config entries\n"); continue; } cfgBlock.entries = ent; ent = cfgBlock.entries + cfgBlock.entryCount; cfgBlock.entryCount++; ent->key = strdup(key); ent->value = NULL; } free(ent->value); ent->value = expdup(value); TRACE("found '%s' = '%s'\n", ent->key, ent->value); } free(buffer); }