0

gcc 4.5.1 c89

次のコードを使用して、構成ファイルからテキスト行を読み取ります。設定ファイルは現時点では小さく、新しいフィールドを追加すると大きくなります。そして、構成ファイルがどのように見えるかを自分でほとんど設計できます。だから私は次の方法でそれをしました:

config.cfg

# Configuration file to be loaded 

# protocol to use either ISDN, SIP, 
protocol: ISDN

# run application in the following mode, makecall or waitcall
mode: makecall

必要な構成のタイプを検索する方法としてコロンを使用しました。これを行うためのより良い方法があるかどうか疑問に思っていますか?

私が使用したコードは次のとおりです。

static int load_config(FILE *fp)
{
    char line_read[LINE_SIZE] = {0};
    char *type = NULL;
    char field[LINE_SIZE] = {0};
    char *carriage_return = NULL;

    /* Read each line */
    while(fgets(line_read, LINE_SIZE, fp) != NULL) {
        if(line_read != NULL) {
            /* Ignore any hashs and blank lines */
            if((line_read[0] != '#') && (strlen(line_read) > 1)) {
                /* I don't like the carriage return, so remove it. */
                carriage_return = strrchr(line_read, '\n');
                if(carriage_return != NULL) {
                    /* carriage return found so relace with a null */
                    *carriage_return = '\0';
                }

                /* Parse line_read to extract the field name i.e. protocol, mode, etc */
                parse_string(field, line_read);
                if(field != NULL) {
                    type = strchr(line_read, ':');
                    type+=2; /* Point to the first character after the space */

                    if(strcmp("protocol", field) == 0) {
                        /* Check the protocol type */
                        printf("protocol [ %s ]\n", type);
                    }
                    else if (strcmp("mode", field) == 0) {
                        /* Check the mode type */
                        printf("mode [ %s ]\n", type);
                    }
                }
            }
        }
    }

    return TRUE;
}

/* Extract the field name for the read in line from the configuration file. */
static void parse_string(char *dest, const char *string)
{
    /* Copy string up to the colon to determine configuration type */
    while(*string != ':') {
        *dest++ = *string++;
    }
    /* Insert nul terminator */
    *dest = '\0';
}
4

4 に答える 4

1

単純な構文解析の問題に対する標準的な答えは、lex と yacc を使用することです。

ただし、構成ファイルの形式を自由に設定できるため、さまざまな構成ファイル形式を実装する多数のライブラリのいずれかを使用し、そのライブラリのみを使用する必要があります。

http://www.google.com/search?q=構成+ファイル+パーサー

たとえば、http://www.nongnu.org/confuse/はニーズを十分にカバーしているように見えますが、より単純な他のさまざまなオプションも見てください。

于 2010-11-20T12:02:00.810 に答える
1

簡単にする必要があります:

static int load_config(FILE *fp)
{
  int r=0;
  char line[LINE_SIZE], field[LINE_SIZE], type[LINE_SIZE], dummy[LINE_SIZE];

  /* Read each line */
  while( fgets(line, LINE_SIZE, fp) )
  {
    if( strchr(line,'\n') ) *strchr(line,'\n')=0;
    if( 3==sscanf(line,"%[^: ]%[: ]%s,field,dummy,type) )
      ++r,printf("\n %s [ %s ]",field,type);
  }
  return r;
}
于 2010-11-20T13:23:14.120 に答える
1

<regex.h>あなたの友達です!

http://www.gnu.org/s/libc/manual/html_node/Regular-Expressions.html

于 2010-11-20T11:50:39.367 に答える
1

If you can design what the configuration file will look like i'd go for XML, parsing it with Expat. It's painless.

于 2010-11-20T11:54:15.860 に答える