All pastes #617399 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-07-13 06:29 UTC
rendered paste body
int
main(void)
{
    int expecting_lbrace = 0, expecting_rbrace = 0;
    int expecting_lparen = 0, expecting_rparen = 0;
    int expecting_quote = 1, expecting_scolon = 0;
    unsigned int ii;
    char string[64];
    FILE *conf_file;
    int c;

    if (!(conf_file = fopen("test.conf", "r"))) {
        printf("Error opening test.conf: %d (%s).\n", errno, strerror(errno));
        return 0;
    }

    while ((c = fgetc(conf_file)) != EOF) {
    switch (c) {
        case '{':
            if (!expecting_lbrace) {
                printf("Unexpected ' { ' reached\n");
                getchar();
                exit(0);
            }
            expecting_rbrace = 1;
            break;
        case '}':
            if (!expecting_rbrace) {
                printf("Unexpected ' } ' reached\n");
                getchar();
                exit(0);
            }
            expecting_lbrace = 0;
            break;
        case '(':
            if (!expecting_lparen) {
                printf("Unexpected ' ( ' reached\n");
                getchar();
                exit(0);
            }
            expecting_rparen = 1;
            break;
        case ')':
            if (!expecting_rparen) {
                printf("Unexpected ' ) ' reached\n");
                getchar();
                exit(0);
            }
            expecting_lparen = 0;
            break;
        case ';':
            if (!expecting_scolon) {
                printf("Unexpected ' ; ' reached\n");
                getchar();
                exit(0);
            }
            break;
        case '\"':
            if (!expecting_quote) {
                printf("Unexpected ' \" ' reached\n");
                getchar();
                exit(0);
            }
            ii = 0;
            while ((c = fgetc(conf_file)) != '\"')
                string[ii++] = c;
            string[ii] = 0;
            printf("string: %s\n", string);
            expecting_lbrace = expecting_lparen = expecting_quote = 1;
            break;
        default:
            /* Assume it's a whitespace character */
            break;
        }
    }
    return getchar();
}