/**************************************************** 函数名: sab_train_arrive_approach 功能描述: 判断列车是否已到达接近区段 返回值: 参数: int16_t index 作者 : CY 日期 : 2012/3/28 ****************************************************/ CI_BOOL sab_train_arrive_approach(int16_t index) { CI_BOOL result = CI_FALSE; if (semi_auto_block_config[index].belong_route != NO_INDEX ) { if (IsFALSE(is_approach_cleared(semi_auto_block_config[index].belong_route,CI_TRUE))) { result = CI_TRUE; } } else { if(gn_direction(semi_auto_block_config[index].entry_signal) == DIR_UP) { if(IsTRUE(is_section(signal_nodes[semi_auto_block_config[index].entry_signal].next_node_index)) && (SCS_CLEARED != gn_section_state(signal_nodes[semi_auto_block_config[index].entry_signal].next_node_index))) { result = CI_TRUE; } } else { if(IsTRUE(is_section(signal_nodes[semi_auto_block_config[index].entry_signal].previous_node_index)) && (SCS_CLEARED != gn_section_state(signal_nodes[semi_auto_block_config[index].entry_signal].previous_node_index))) { result = CI_TRUE; } } } return result; }
/**************************************************** 函数名: sab_first_section_clear 功能描述: 判断进站信号机内方第一区段空闲 返回值: CI_BOOL 参数: int16_t index 作者: hejh 日期: 2015/08/19 ****************************************************/ CI_BOOL sab_first_section_clear(int16_t index) { CI_BOOL result = CI_FALSE; node_t entry_signal = semi_auto_block_config[index].entry_signal; node_t first_section = gn_forword(gn_direction(entry_signal),entry_signal); int16_t i; for (i = 0; i < TOTAL_SIGNAL_NODE; i++) { if (IsTRUE(is_section(first_section))) { break; } else { first_section = gn_forword(gn_direction(entry_signal),first_section); } } if ((IsTRUE(is_section(first_section))) && (gn_section_state(first_section) == SCS_CLEARED)) { result = CI_TRUE; } return result; }
bool ini_find_string_section(FFILE f, const char *p_section, const char *p_template, long *p_file_start, long *p_file_end) { char line[MAX_TOKEN_LEN]; char section[MAX_TOKEN_LEN]; bool section_found = FALSE; long file_pos; long file_last_line_start = 0; long file_last_line_end = 0; file_pos = 0; fseek(f, 0, SEEK_SET); while (fgets(line, MAX_TOKEN_LEN, f)) { if(!section_found) { section_found = (bool)read_section(line, section, MAX_TOKEN_LEN); if(section_found) { section_found = !strncasecmp(p_section, section, MAX_TOKEN_LEN); } if(section_found) { file_pos = ftell(f); continue; } } // Cache last non-section line from recent section if(section_found && !is_section(line) && !is_empty(line)) { file_last_line_start = file_pos; file_last_line_end = ftell(f); } if(section_found && is_section(line)) { // we hit next section - so it's not found, // create a new entry *p_file_start = file_last_line_start; *p_file_end = file_last_line_end; return(TRUE); } // Replace this line if(section_found && is_token(line, p_template)) { *p_file_start = file_pos; *p_file_end = ftell(f); return(TRUE); } file_pos = ftell(f); } // no section -> create a new one return(FALSE); }
char *ini_read_string_section(FFILE f, const char *p_section, const char *p_template, char *p_out, int max_len, const char *p_default) { char line[MAX_TOKEN_LEN]; char section[MAX_TOKEN_LEN]; bool section_found = FALSE; fseek(f, 0, SEEK_SET); while (fgets(line, MAX_TOKEN_LEN, f)) { if(!section_found) { section_found = (bool)read_section(line, section, MAX_TOKEN_LEN); if(section_found) { section_found = !strncasecmp(p_section, section, MAX_TOKEN_LEN); } if(section_found) continue; } if(section_found && is_section(line)) { // we hit next section - so it's not found break; } if(section_found) { int len = is_token(line, p_template); char *p_rest; if (len && (p_rest = ini_skip_separator(line + len))) { return (ini_read_param(p_rest, p_out, max_len)); } } } return p_default ? (strcpy(p_out, p_default)) : NULL; }
int _config_del(config_t *cfg, const char *section, const char *entry) { int start; char *tmp, *value; unsigned int line = 0, end; if ( ! entry ) { start = search_section(cfg, section, 0); if ( start < 0 ) return start; for ( end = (unsigned int) start + 1; end < cfg->elements && ! is_section(cfg->content[end]); end++ ); while ( start >= 2 && ! *cfg->content[start - 1] && ! *cfg->content[start - 2] ) start--; } else { start = search_entry(cfg, section, entry, &line, &tmp, &value); if ( start < 0 ) return start; free_val(&tmp); free_val(&value); end = (unsigned int) start + 1; } cfg->need_sync = TRUE; return op_delete_line(cfg, start, end); }
static int search_section(config_t *cfg, const char *section, unsigned int i) { int ret; char *entry, *value, *wentry, *wvalue; if ( ! cfg->content ) return -1; ret = parse_section_buffer(section, &wentry, &wvalue, TRUE); if ( ret < 0 ) return ret; for ( ; i < cfg->elements; i++ ) { if ( is_line_commented(cfg->content[i]) ) continue; if ( ! is_section(cfg->content[i]) ) continue; ret = parse_section_buffer(cfg->content[i], &entry, &value, TRUE); if ( ret < 0 ) continue; ret = strcasecmp(entry, wentry); free(entry); if ( ret != 0 ) { free(value); continue; } ret = strcasecmp(value, wvalue); free(value); if ( ret == 0 ) { free(wentry); free(wvalue); return i; } } free(wentry); free(wvalue); return -1; }
static unsigned int adjust_insertion_point(config_t *cfg, unsigned int l) { /* * We want to insert this entry at the end of the current section. */ while ( l < cfg->elements ) { l++; if ( l == cfg->elements ) break; if ( is_section(cfg->content[l]) ) break; } return l; }
/* * Search an entry (delimited by '=' character) in content. * returns the line number matching 'entry' or -1. */ static int search_entry(config_t *cfg, const char *section, const char *entry, unsigned int *index, char **eout, char **vout) { int ret; unsigned int i = *index; if ( ! cfg->content || i >= cfg->elements ) return -1; if ( section && ! index ) { ret = search_section(cfg, section, 0); if ( ret < 0 ) return ret; i = (unsigned int) ret + 1; } for ( ; i < cfg->elements; i++ ) { if ( section && is_section(cfg->content[i]) ) return -1; ret = parse_buffer(cfg->content[i], eout, vout); if ( ret < 0 || ! *eout ) continue; ret = strcmp(entry, *eout); if ( ret == 0 ) { *index = i; return 0; } free_val(eout); free_val(vout); } return -1; }
/* * _config_get_next: * @cfg: Configuration file identifier. * @section: Pointer address where the current section should be stored. * @entry: Pointer address where the current entry should be stored. * @value: Pointer address where the current value should be stored. * @line: Pointer to a line number we should start the search at. * * Parses the whole configuration file starting at @line, * and stores the current section, entry and value within the * provided argument. * * The caller has to call config_get_next() until it returns -1 * or memory will be leaked. * * If the value gathered starts with a '$', which means it is * a variable, the variable is automatically looked up. * * Returns: 0 on success, -1 if there is nothing more to read. */ int _config_get_next(config_t *cfg, char **section, char **entry, char **value, unsigned int *line) { int ret; char *ptr; free_val(entry); free_val(value); free_val(section); if ( ! cfg->content || *line >= cfg->elements ) return -1; while ( *line < cfg->elements ) { ptr = cfg->content[*line]; ptr += strspn(ptr, " \t\r"); (*line)++; if ( ! *ptr || is_line_commented(ptr) ) continue; if ( is_section(ptr) ) return parse_section_buffer(ptr, section, value, FALSE); ret = parse_buffer(ptr, entry, value); if ( ret >= 0 && **entry == '$' ) { free_val(entry); free_val(value); continue; } return ret; } (*line)--; return -1; }
static void process_text( struct textfile *txt, int lang_count, int *lang_map, unsigned char **lang_shorts) { int i; int lang_sect_lineno = -1; int lang_id_lineno = -1; int res; unsigned char short_name_buf[1024]; int j, lineno; int *lang_id_linenos; unsigned char id_buf[1024]; int id_buf_len; short_name_buf[0] = 0; XCALLOC(lang_id_linenos, lang_count); for (i = 0; i < txt->u; ++i) { res = is_section(txt->v[i].s, i + 1); if (res >= 0) { if (lang_sect_lineno > 0) { if (lang_id_lineno <= 0) { fprintf(stderr, "language section at line %d has no id\n", lang_sect_lineno); return; } if (!short_name_buf[0]) { fprintf(stderr, "language section at line %d has no short_name\n", lang_sect_lineno); return; } } for (j = 1; j < lang_count; ++j) { if (lang_shorts[j] && lang_map[j] > 0 && !strcmp(lang_shorts[j], short_name_buf)) break; } if (j < lang_count) lang_id_linenos[j] = lang_id_lineno; } if (res == 1) { lang_sect_lineno = i + 1; lang_id_lineno = -1; short_name_buf[0] = 0; } else if (!res) { lang_sect_lineno = -1; } if (lang_sect_lineno > 0 && is_id(txt->v[i].s, i + 1)) { //fprintf(stderr, "id: %d\n", i + 1); lang_id_lineno = i + 1; } if (lang_sect_lineno > 0 && is_short_name(txt->v[i].s, short_name_buf, sizeof(short_name_buf), i + 1)) { //fprintf(stderr, "short_name: %s, %d\n", short_name_buf, i + 1); //map_lang_aliases(short_name_buf, sizeof(short_name_buf)); } } if (lang_sect_lineno > 0) { if (lang_id_lineno <= 0) { fprintf(stderr, "language section at line %d has no id\n", lang_sect_lineno); return; } if (!short_name_buf[0]) { fprintf(stderr, "language section at line %d has no short_name\n", lang_sect_lineno); return; } } for (j = 1; j < lang_count; ++j) { if (lang_shorts[j] && lang_map[j] > 0 && !strcmp(lang_shorts[j], short_name_buf)) break; } if (j < lang_count) lang_id_linenos[j] = lang_id_lineno; for (j = 1; j < lang_count; ++j) { if (lang_map[j] > 0 && lang_id_linenos[j] <= 0) { fprintf(stderr, "language %s lineno is not found\n", lang_shorts[j]); return; } } for (j = 1; j < lang_count; ++j) { if (lang_map[j] > 0) { lineno = lang_id_linenos[j] - 1; if (lineno < 0 || lineno >= txt->u) { die("invalid lineno %d", lineno); } snprintf(id_buf, sizeof(id_buf), "id = %d", lang_map[j]); id_buf_len = strlen(id_buf); txt->v[lineno].a = id_buf_len + 1; txt->v[lineno].u = id_buf_len; xfree(txt->v[lineno].s); txt->v[lineno].s = xstrdup(id_buf); } } }
/// reads the file bool Leo::Ini::read() { if( !is_open ) return false; if( file_readed ) return false; MemElement current_section; bool in_section = false; while( true ) { Line line = read_line(); if( eof_reached && line.str.empty() ) break; if( line.tag.str.empty() ) { // simple comment comments.push_back( line ); continue; } if( is_section( line.tag.str ) ) { if( in_section ) { elements.push_back( current_section ); current_section.clear(); } // create a new section current_section.line = line; current_section.section = extract_section_name( line.tag.str ); current_section.type = Element::SECTION; in_section = true; } else if( is_key( line.tag.str ) && in_section ) { // create a new key MemElement me; me.line = line; me.section = current_section.section; me.key = extract_key_name( line.tag.str ); me.value = extract_key_data( line.tag.str ); me.type = Element::KEY; current_section.mem_elements.push_back( me ); } } if( !current_section.section.empty() ) elements.push_back( current_section ); file_readed = true; return true; }
// Puts "field" from "section" from .ini file "f" into "s". // If "section" does not exist or "field" does not exist in // section then s=""; void ini_fgets(FILE *f, const char *section, const char *field, char *s) { int i; long start_pos,string_start_pos; char ts[INI_STRING_SIZE*2]; if (f!=current_file) reset_buffer(f); // Default to "Not found" s[0]=0; // See if section is in buffer for (i=0;i<NUM_SECTION_BUFFERS;i++) if (strnicmp(section_buffers[i].name,section,MAX_SECTION_WIDTH)==0) break; // If section is in buffer, seek to it if necessary if (i<NUM_SECTION_BUFFERS) { if (i!=current_section) { current_section=i; fseek(f,section_buffers[i].offset,SEEK_SET); } } // else look through .ini file for it. else { // Make sure we are not at eof or this will cause trouble. if (feof(f)) rewind(f); start_pos=ftell(f); while (1) { stripped_fgets(ts,INI_STRING_SIZE*2,f); // If it is a section, add it to the section buffer if (is_section(ts,"*")) current_section=add_section(ts,ftell(f)); // If it is the section we are looking for, break. if (is_section(ts,section)) break; // If we reach the end of the file, rewind to the start. if (feof(f)) rewind(f); if (ftell(f)==start_pos) return; } } // See if field is in buffer for (i=0;i<NUM_FIELD_BUFFERS;i++) if (field_buffers[i].section==current_section) if (strnicmp(field_buffers[i].name,field,MAX_FIELD_WIDTH)==0) break; // If field is in buffer, seek to it and read it if (i<NUM_FIELD_BUFFERS) { fseek(f,field_buffers[i].offset,SEEK_SET); stripped_fgets(ts,INI_STRING_SIZE*2,f); get_field_string(s,ts); } else // else search through section for field. { // Make sure we do not start at eof or this will cause problems. if (feof(f)) fseek(f,section_buffers[current_section].offset,SEEK_SET); start_pos=ftell(f); while (1) { string_start_pos=ftell(f); stripped_fgets(ts,INI_STRING_SIZE*2,f); // If it is a field, add it to the buffer if (is_field(ts,"*")) add_field(ts,current_section,string_start_pos); // If it is the field we are looking for, save it if (is_field(ts,field)) { get_field_string(s,ts); break; } // If we reach the end of the section, start over if (feof(f) || is_section(ts,"*")) fseek(f,section_buffers[current_section].offset,SEEK_SET); if (ftell(f)==start_pos) return; } } }
static int parse_one_file(config_t *config, const char *filename, char **section) { _cleanup_(fclosep) FILE *fp = NULL; _cleanup_free_ char *line = NULL; size_t n = 0; int in_options = 0; if(*section) { in_options = strcmp(*section, "options") == 0; } fp = fopen(filename, "r"); if(fp == NULL) { return -errno; } for(;;) { ssize_t len; errno = 0; len = getline(&line, &n, fp); if(len < 0) { if(errno != 0) { return -errno; } /* EOF */ break; } len = strtrim(line); if(len == 0 || line[0] == '#') { continue; } if(is_section(line, len)) { free(*section); *section = strndup(&line[1], len - 2); if(*section == NULL) { return -ENOMEM; } in_options = strcmp(*section, "options") == 0; if(!in_options) { int r; r = config_add_repo(config, *section); if(r < 0) { return r; } } continue; } if(in_options && memchr(line, '=', len)) { char *val = line; strsep(&val, "="); strtrim(line); strtrim(val); if(strcmp(line, "Include") == 0) { int k; k = parse_include(config, val, section); if(k < 0) { return k; } } else if(strcmp(line, "DBPath") == 0) { config->dbpath = strdup(val); if(config->dbpath == NULL) { return -ENOMEM; } } else if(strcmp(line, "RootDir") == 0) { config->dbroot = strdup(val); if(config->dbpath == NULL) { return -ENOMEM; } } } } return 0; }
int main (int argc, char **argv) { int status = 0; char *nextarg; char *tmp; char *section = 0; #ifdef __CYGWIN__ extern int optind; #endif #if 0 { /* There are no known cases of buffer overflow caused by excessively long environment variables. In case you find one, the simplistic way to fix is to enable this stopgap. */ char *s; #define CHECK(p,l) s=getenv(p); if(s && strlen(s)>(l)) { fprintf(stderr, "ERROR: Environment variable %s too long!\n", p); exit(1); } CHECK("LANG", 32); CHECK("LANGUAGE", 128); CHECK("LC_MESSAGES", 128); CHECK("MANPAGER", 128); CHECK("MANPL", 128); CHECK("MANROFFSEQ", 128); CHECK("MANSECT", 128); CHECK("MAN_HP_DIREXT", 128); CHECK("PAGER", 128); CHECK("SYSTEM", 64); CHECK("BROWSER", 64); CHECK("HTMLPAGER", 64); /* COLUMNS, LC_ALL, LC_CTYPE, MANPATH, MANWIDTH, MAN_IRIX_CATNAMES, MAN_ICONV_PATH, MAN_ICONV_OPT, MAN_ICONV_INPUT_CHARSET, MAN_ICONV_OUTPUT_CHARSET, NLSPATH, PATH */ } #endif #ifndef __FreeBSD__ /* Slaven Rezif: FreeBSD-2.2-SNAP does not recognize LC_MESSAGES. */ setlocale(LC_CTYPE, ""); /* used anywhere? maybe only isdigit()? */ setlocale(LC_MESSAGES, ""); #endif /* No doubt we'll need some generic language code here later. For the moment only Japanese support. */ setlang(); /* Handle /usr/man/man1.Z/name.1 nonsense from HP */ dohp = getenv("MAN_HP_DIREXT"); /* .Z */ /* Handle ls.z (instead of ls.1.z) cat page naming from IRIX */ if (getenv("MAN_IRIX_CATNAMES")) do_irix = 1; /* Handle lack of ':' in NTFS file names */ #if defined(_WIN32) || defined(__CYGWIN__) do_win32 = 1; #endif progname = mkprogname (argv[0]); get_permissions (); get_line_length(); /* * read command line options and man.conf */ man_getopt (argc, argv); /* * manpath or man --path or man -w will only print the manpath */ if (!strcmp (progname, "manpath") || (optind == argc && print_where)) { init_manpath(); prmanpath(); exit(0); } if (optind == argc) gripe(NO_NAME_NO_SECTION); section_list = get_section_list (); while (optind < argc) { nextarg = argv[optind++]; /* is_section correctly accepts 3Xt as section, but also 9wm, so we should not believe is_section() for the last arg. */ tmp = is_section (nextarg); if (tmp && optind < argc) { section = tmp; if (debug) gripe (SECTION, section); continue; } if (global_apropos) status = !do_global_apropos (nextarg, section); else if (apropos) status = !do_apropos (nextarg); else if (whatis) status = !do_whatis (nextarg); else { status = man (nextarg, section); if (status == 0) { if (section) gripe (NO_SUCH_ENTRY_IN_SECTION, nextarg, section); else gripe (NO_SUCH_ENTRY, nextarg); } } } return status ? EXIT_SUCCESS : EXIT_FAILURE; }