int count_line_config() { int fd; char *line; int cp; int line_nbr; if ((fd = open(CONFIG_FILE_NAME, O_RDONLY)) == -1) { my_printf("Unalbe to open %s's file\n", CONFIG_FILE_NAME); return (-1); } cp = 0; line_nbr = 1; while ((line = get_next_line(fd)) != NULL) { if (!is_empty_line(line) && !is_comment_line(line)) { if (is_correct_command_config(line, line_nbr)) cp++; else return (-1); } free(line); line_nbr++; } return (cp); }
int parse_keys(FILE* fp,config_t* cfg) { char line[1024] = {0}; char* r; int i; int ret = 0; char* p; while ((r = fgets(line,1023,fp))) { r = skip_space(r); if(is_comment_line(r)) continue; if(*r == '}'){ break; } for (i = 0;i < sizeof(_keys)/sizeof(ppe_st);i++) { p = strstr(r, _keys[i].key); if (p) { p = strchr(p,'='); if(p){ p++; _keys[i].handler(cfg,(void*)p,_keys[i].offset,fp); } } } } return ret; }
rdConfUnit* rdConfLoader::load_conf(const char* conf_filename) { std::ifstream conf_file(conf_filename, std::ifstream::in); if (conf_file.fail()) { return NULL; } char line[MAX_LINE_LEN]; rdConfUnit* root = new(std::nothrow) rdConfUnit(rdConfUnit::UT_GROUP); if (NULL != root) { rdConfUnit* current = root; while (conf_file.good()) { conf_file.getline(line, MAX_LINE_LEN); if (is_white_line(line) || is_comment_line(line)) { continue; } if (0 != handle_line(line, root, current)) { delete root; root = NULL; break; } } } conf_file.close(); return root; }
static sect_types_t get_section_type ( char *text_line, sect_types_t current_section_type ) { sect_types_t sect_type; /* The order of the first two if-tests cannot be changed! */ if (is_comment_line(text_line)) sect_type = ZEROTH_SECT; else if (is_format(text_line)) sect_type = FMT_SECT; else if (is_equiv_section(text_line, current_section_type)) sect_type = kind_of_equiv_section(text_line); else if (is_last_sect(text_line)) sect_type = LAST_SECT; /* else if (add_your_BOOLEAN_test_here(text_line)) sect_type = ADD_YOUR_SECTION_TYPE_HERE; */ else { if (current_section_type == ZEROTH_SECT) sect_type = FMT_SECT; else sect_type = IN_SECT; } return(sect_type); }
/* * Reads the next line from the file. * * Returns 0 if the line is a comment * 1 if the line is not a comment * <0 on error */ static int next_comment_line(struct libmnt_parser *pa, char **last) { if (getline(&pa->buf, &pa->bufsiz, pa->f) < 0) return feof(pa->f) ? 1 : -errno; pa->line++; *last = strchr(pa->buf, '\n'); return is_comment_line(pa->buf) ? 0 : 1; }
/* * Reads the next line from the file. * * Returns 0 if the line is a comment * 1 if the line is not a comment * <0 on error */ static int next_comment_line(char *buf, size_t bufsz, FILE *f, char **last, int *nlines) { if (fgets(buf, bufsz, f) == NULL) return feof(f) ? 1 : -EINVAL; ++*nlines; *last = strchr(buf, '\n'); return is_comment_line(buf) ? 0 : 1; }
void Stencil::parse_body(std::ifstream& infile) { std::string line; int row = 0; while ( getline (infile, line) ) { if (is_comment_line(line) || is_blank_line(line)) { continue; } else { parse_stencil_line(line, row); row++; } } }
char is_valid_line(char *line, int line_number) { if (ft_strlen(line) > 0) { if (is_name_descr(line) || is_comment_descr(line) || is_empty_line(line) || is_comment_line(line) || is_command(line) || is_label(line)) return (1); ft_putstr_fd("Line n ", 2); ft_putnbr_fd(line_number, 2); ft_putendl_fd(" is wrong formated", 2); return (0); } return (1); }
tree texmacs_invarianted_extend (tree id, string src) { int b, e, n= N(src); get_range (id, b, e, src); //cout << "Extending" << LF << HRULE << src (b, e) << LF << HRULE; rewind_spaces (src, b); while (b>0 && src[b-1] == '\n') { int prev= b-1; rewind_line (src, prev); if (is_comment_line (src (prev, b-1))) b= prev; else break; } skip_spaces (src, e); if (e<n && src[e] == '%') skip_line (src, e); while (e<n && src[e] == '\n') { int next= e+1; forward_line (src, next); if (is_comment_line (src (e+1, next))) e= next; else break; } //cout << "Extended to" << LF << HRULE << src (b, e) << LF << HRULE; return as_string (b) * ":" * as_string (e); }
int read_list_file(const string& filename, const map<string, string>& variables, vector<FileRecord>* files, vector<string>* excludes) { int err = 0; FILE* f = NULL; long size; char* buf = NULL; char *p, *q; int i, lineCount; f = fopen(filename.c_str(), "r"); if (f == NULL) { fprintf(stderr, "Could not open list file (%s): %s\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } err = fseek(f, 0, SEEK_END); if (err != 0) { fprintf(stderr, "Could not seek to the end of file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } size = ftell(f); err = fseek(f, 0, SEEK_SET); if (err != 0) { fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } buf = (char*)malloc(size+1); if (buf == NULL) { // (potentially large) fprintf(stderr, "out of memory (%ld)\n", size); err = ENOMEM; goto cleanup; } if (1 != fread(buf, size, 1, f)) { fprintf(stderr, "error reading file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } // split on lines p = buf; q = buf+size; lineCount = 0; while (p<q) { if (*p == '\r' || *p == '\n') { *p = '\0'; lineCount++; } p++; } // read lines p = buf; for (i=0; i<lineCount; i++) { int len = strlen(p); q = p + len + 1; if (is_whitespace_line(p) || is_comment_line(p)) { ; } else if (is_exclude_line(p)) { while (*p != '-') p++; p++; excludes->push_back(string(p)); } else { vector<string> words; split_line(p, &words); #if 0 printf("[ "); for (size_t k=0; k<words.size(); k++) { printf("'%s' ", words[k].c_str()); } printf("]\n"); #endif if (words.size() == 1) { // pattern: DEST bool error = false; string w0 = replace_variables(words[0], variables, &error); if (error) { err = 1; goto cleanup; } add_file(files, filename, i+1, w0, w0); } else if (words.size() == 2) { // pattern: SRC DEST bool error = false; string w0, w1; w0 = replace_variables(words[0], variables, &error); if (!error) { w1 = replace_variables(words[1], variables, &error); } if (error) { err = 1; goto cleanup; } add_file(files, filename, i+1, w0, w1); } else { fprintf(stderr, "%s:%d: bad format: %s\n", filename.c_str(), i+1, p); err = 1; } } p = q; } cleanup: if (buf != NULL) { free(buf); } if (f != NULL) { fclose(f); } return err; }
/* * Read and parse the next line from {fs,m}tab or mountinfo */ static int mnt_table_parse_next(struct libmnt_table *tb, FILE *f, struct libmnt_fs *fs, const char *filename, int *nlines) { char buf[BUFSIZ]; char *s; int rc; assert(tb); assert(f); assert(fs); /* read the next non-blank non-comment line */ next_line: do { if (fgets(buf, sizeof(buf), f) == NULL) return -EINVAL; ++*nlines; s = strchr (buf, '\n'); if (!s) { /* Missing final newline? Otherwise an extremely */ /* long line - assume file was corrupted */ if (feof(f)) { DBG(TAB, mnt_debug_h(tb, "%s: no final newline", filename)); s = strchr (buf, '\0'); } else { DBG(TAB, mnt_debug_h(tb, "%s:%d: missing newline at line", filename, *nlines)); goto err; } } /* comments parser */ if (tb->comms && (tb->fmt == MNT_FMT_GUESS || tb->fmt == MNT_FMT_FSTAB) && is_comment_line(buf)) { do { rc = append_comment(tb, fs, buf, feof(f)); if (!rc) rc = next_comment_line(buf, sizeof(buf), f, &s, nlines); } while (rc == 0); if (rc == 1 && feof(f)) rc = append_comment(tb, fs, NULL, 1); if (rc < 0) return rc; } *s = '\0'; if (--s >= buf && *s == '\r') *s = '\0'; s = (char *) skip_blank(buf); } while (*s == '\0' || *s == '#'); if (tb->fmt == MNT_FMT_GUESS) { tb->fmt = guess_table_format(s); if (tb->fmt == MNT_FMT_SWAPS) goto next_line; /* skip swap header */ } switch (tb->fmt) { case MNT_FMT_FSTAB: rc = mnt_parse_table_line(fs, s); break; case MNT_FMT_MOUNTINFO: rc = mnt_parse_mountinfo_line(fs, s); break; case MNT_FMT_UTAB: rc = mnt_parse_utab_line(fs, s); break; case MNT_FMT_SWAPS: if (strncmp(s, "Filename\t", 9) == 0) goto next_line; /* skip swap header */ rc = mnt_parse_swaps_line(fs, s); break; default: rc = -1; /* unknown format */ break; } if (rc == 0) return 0; err: DBG(TAB, mnt_debug_h(tb, "%s:%d: %s parse error", filename, *nlines, tb->fmt == MNT_FMT_MOUNTINFO ? "mountinfo" : tb->fmt == MNT_FMT_SWAPS ? "swaps" : tb->fmt == MNT_FMT_FSTAB ? "tab" : "utab")); /* by default all errors are recoverable, otherwise behavior depends on * the errcb() function. See mnt_table_set_parser_errcb(). */ return tb->errcb ? tb->errcb(tb, filename, *nlines) : 1; }
/* * API * * initializes an INI-file holder object, loaded from the given file. this function * reads the entire file, parses it, and stores it as a key-value array */ int ini_load(inifile_t * ini, const char * filename) { char line[MAX_INPUT_BUFFER]; char * key, * value; int allocated_lines = 50; keyvalue_t * tmp = NULL; FILE * f = fopen(filename, "r"); if (f == NULL) { perror(filename); return INI_STATUS_ERROR; } ini->num_of_lines = 0; ini->lines = (keyvalue_t*)malloc(sizeof(keyvalue_t) * allocated_lines); if (ini->lines == NULL) { fprintf(stderr, "allocating ini->lines failed\n"); } while (1) { if (fgets(line, sizeof(line)-1, f) == NULL) { if (feof(f)) { break; } else { perror("ini_load: fgets failed"); goto cleanup; } } if (is_comment_line(line)) { continue; } if (parse_line(line, &key, &value) != INI_STATUS_OK) { goto cleanup; } /* grow lines array if needed */ if (ini->num_of_lines >= allocated_lines) { allocated_lines *= 2; tmp = realloc(ini->lines, sizeof(keyvalue_t) * allocated_lines); if (tmp == NULL) { fprintf(stderr, "reallocating ini->lines failed\n"); goto cleanup; } ini->lines = tmp; } strncpy(ini->lines[ini->num_of_lines].key, key, MAX_INPUT_BUFFER); strncpy(ini->lines[ini->num_of_lines].value, value, MAX_INPUT_BUFFER); ini->num_of_lines += 1; } fclose(f); return INI_STATUS_OK; cleanup: if (ini->lines != NULL) { free(ini->lines); ini->lines = NULL; } return INI_STATUS_ERROR; }
static bool isCommentLine(const std::string& line) { return (bool)is_comment_line(line.c_str(),line.length()); }
static int is_key_value_line ( const char *line, size_t line_len, size_t *key_pos_out, size_t *key_len_out, size_t *value_pos_out, size_t *value_len_out ) { const char *p = line; const char *pe = p + line_len; const char *key_end = NULL; const char *value_end = NULL; size_t key_pos = 0; size_t key_len = 0; size_t value_pos = 0; size_t value_len = 0; int valid = 1; while (p < pe) { char ch = *p; if (ch == '"') { ++ p; key_pos = (size_t)(p - line); break; } else if (ch == ' ' || ch == '\t') { ++ p; } else { valid = 0; break; } } if (!valid) { goto out; } key_end = (const char *)memmem(p,pe - p,"\"=\"",3); if (!key_end) { valid = 0; goto out; } key_len = key_end - p; value_pos = key_end + 3 - line; value_end = (const char *)memmem(key_end + 3, pe - key_end - 3, "\"", 1); if (!value_end) { valid = 0; goto out; } value_len = value_end - (line + value_pos); valid = is_comment_line(value_end + 1, pe - value_end - 1); out: *key_pos_out = key_pos; *key_len_out = key_len; *value_pos_out = value_pos; *value_len_out = value_len; return valid; }
static int is_section_line(const char *line, size_t line_len, size_t *pos_out, size_t *len_out) { const char *p = line; const char *pe = line + line_len; size_t pos = 0; size_t len = 0; int valid = 1; while (p < pe) { char ch = *p; if (ch == '[') { ++ p; pos = (size_t)(p - line); break; } else if (ch == ' ' || ch == '\t') { ++ p; } else { valid = 0; break; } } if (!valid) { goto out; } while (p < pe) { char ch = *p; if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '/') { p ++; } else { break; } } if (p < pe) { if (*p != ']') { valid = 0; } else { len = (size_t)(p - (line + pos)); } } p = line + pos + len + 1; valid = is_comment_line(p, pe - p); out: *pos_out = pos; *len_out = len; return valid; }
static int make_format ( char *origin, FF_BUFSIZE_PTR bufsize, FORMAT_HANDLE hformat ) { char *text_line = NULL; char *fmt_name; int error = 0; assert(hformat); assert(*hformat == NULL); *hformat = NULL; text_line = get_first_line(bufsize->buffer); if (!text_line) return(err_push(ERR_MISSING_TOKEN, "Expecting a newline")); else { FF_TYPES_t check_format_type; if (get_format_type_and_name(text_line, &check_format_type, &fmt_name)) { char *cp; char save_char = STR_END; cp = find_EOL(fmt_name); if (cp) { save_char = *cp; *cp = STR_END; } *hformat = ff_create_format(fmt_name, origin); if (!*hformat) return(ERR_MEM_LACK); (*hformat)->type = check_format_type; if (cp) *cp = save_char; if ((*hformat)->name[0] == '\"') (*hformat)->name[0] = ' '; cp = strrchr((*hformat)->name, '\"'); if (cp) *cp = ' '; os_str_trim_whitespace((*hformat)->name, (*hformat)->name); text_line = get_next_line(text_line); if (!text_line) return(err_push(ERR_VARIABLE_DESC, "Expecting a variable description")); } else { char *cp; char save_char = STR_END; cp = find_EOL(text_line); if (cp) { save_char = *cp; *cp = STR_END; } error = err_push(ERR_UNKNOWN_FORMAT_TYPE, text_line); if (cp) *cp = save_char; return(error); } } while (strlen(text_line)) { if (!is_comment_line(text_line) && strlen(text_line)) { error = add_to_variable_list(text_line, *hformat); if (error && error < ERR_WARNING_ONLY) return(error); } text_line = get_next_line(text_line); } if (IS_ARRAY(*hformat) && IS_RECORD_FORMAT(*hformat)) return(err_push(ERR_MAKE_FORM, "You cannot define a record format containing an array (%s)", (*hformat)->name)); if (!(*hformat)->variables) return(err_push(ERR_NO_VARIABLES, "%s", (*hformat)->name)); if (IS_ASCII(*hformat) && !IS_ARRAY(*hformat) && !IS_VARIED(*hformat)) error = append_EOL_to_format(*hformat); /* experimental */ if (IS_ARRAY(*hformat)) (*hformat)->length = 0; return(error); }