Example #1
0
static int handle_literal(const char *literal, size_t len, void *ctx) {
    tfs_token_array_t *tokens = (void *)ctx;

    int in_i = 0, out_i = 0;
    int was_slash = 0;
    tfs_token_t *new_token = tfs_append_token(tokens);
    new_token->is_literal = 1;
    char *out_text = new_token->text;

    if (literal[0] == '"') {
        in_i = 1;
        len--;
    }

    /* TODO bounds check */
    while (in_i < len) {
        if (was_slash) {
            out_text[out_i++] = literal[in_i];
        } else if (literal[in_i] == '\\') {
            was_slash = 1;
        } else {
            out_text[out_i++] = literal[in_i];
        }
        in_i++;
    }
    out_text[out_i] = '\0';

    return 0;
}
Example #2
0
static int handle_code(const char *code, size_t len, void *ctx) {
    tfs_token_array_t *tokens = (void *)ctx;

    int i;
    tfs_token_t *new_token = NULL;
    if (code[0] == 's') {
        size_t fractional_len = 0;
        new_token = tfs_append_token(tokens);
        new_token->time_unit = TFS_SECOND;
        if (len > 1 && code[1] == 's') {
            new_token->style = TFS_2DIGIT;
            if (len > 3 && code[2] == '.') {
                fractional_len = len - 3;
            }
        } else {
            new_token->style = TFS_NUMBER;
            if (len > 2 && code[1] == '.') {
                fractional_len = len - 2;
            }
        }
        if (fractional_len) {
            new_token = tfs_append_token(tokens);
            new_token->time_unit = TFS_FRACTIONAL_SECOND;
            new_token->style = TFS_NUMBER;
            new_token->truncate_len = fractional_len;
            new_token->add_dots = 1;
        }
    } else if ((code[0] == 'A' || code[0] == 'P' || code[0] == 'a' || code[0] == 'p') && len < 3) {
        new_token = tfs_append_token(tokens);
        new_token->time_unit = TFS_PERIOD;
        new_token->style = len == 2 ? TFS_ABBREV : TFS_NARROW;
        new_token->uppercase = (code[0] == 'A' || code[0] == 'P');
        new_token->lowercase = (code[0] == 'a' || code[0] == 'p');
    } else {
        for (i=0; i<sizeof(excel_tokens)/sizeof(excel_tokens[0]); i++) {
            if (len == strlen(excel_tokens[i].text) && strncmp(excel_tokens[i].text, code, len) == 0) {
                new_token = tfs_append_token(tokens);
                memcpy(new_token, &excel_tokens[i].token, sizeof(tfs_token_t));
                break;
            }
        }
    }

    return 0;
}
Example #3
0
tfs_token_array_t *tfs_posix_parse(const char *bytes, tfs_handle_string_callback handle_error, tfs_error_e *outError) {
    tfs_token_array_t *tokens = tfs_init_token_array(10);
    tfs_token_t *new_token = NULL;
    int literal_len = 0;
    const char *p = bytes;
    while (*p) {
        if (*p == '%') {
            if (literal_len) {
                new_token = tfs_append_token(tokens);
                new_token->is_literal = 1;
                if (literal_len > sizeof(new_token->text))
                    literal_len = sizeof(new_token->text);
                memcpy(new_token->text, p-literal_len, literal_len);
                literal_len = 0;
            }
            p++;
            if (*p == '\0') {
                *outError = TFS_PARSE_ERROR;
                tfs_free_token_array(tokens);
                return NULL;
            }

            if (*p == 'n' || *p == 't' || *p == '%') {
                new_token = tfs_append_token(tokens);
                new_token->is_literal = 1;
                if (*p == 'n') {
                    new_token->text[0] = '\n';
                } else if (*p == 't') {
                    new_token->text[0] = '\t';
                } else {
                    new_token->text[0] = *p;
                }
            } else if (*p == 'D') {
                new_token = append_month(tokens, TFS_2DIGIT);
                new_token = append_literal_char(tokens, '/');
                new_token = append_day(tokens, TFS_MONTH, TFS_2DIGIT);
                new_token = append_literal_char(tokens, '/');
                new_token = append_year(tokens, TFS_CENTURY, TFS_2DIGIT);
            } else if (*p == 'F') {
                new_token = append_year(tokens, TFS_ERA, TFS_NUMBER);
                new_token = append_literal_char(tokens, '-');
                new_token = append_month(tokens, TFS_2DIGIT);
                new_token = append_literal_char(tokens, '-');
                new_token = append_day(tokens, TFS_MONTH, TFS_2DIGIT);
            } else if (*p == 'R') {
                new_token = append_hour(tokens, TFS_DAY, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ':');
                new_token = append_minute(tokens, TFS_HOUR, TFS_2DIGIT);
            } else if (*p == 'r') {
                new_token = append_hour(tokens, TFS_PERIOD, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ':');
                new_token = append_minute(tokens, TFS_HOUR, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ':');
                new_token = append_second(tokens, TFS_MINUTE, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ' ');
                new_token = append_ampm(tokens, TFS_ABBREV);
            } else if (*p == 'T') {
                new_token = append_hour(tokens, TFS_DAY, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ':');
                new_token = append_minute(tokens, TFS_HOUR, TFS_2DIGIT);
                new_token = append_literal_char(tokens, ':');
                new_token = append_second(tokens, TFS_MINUTE, TFS_2DIGIT);
            } else if (*p == 'v') {
                new_token = append_day(tokens, TFS_MONTH, TFS_NUMBER);
                new_token = append_literal_char(tokens, '-');
                new_token = append_month(tokens, TFS_ABBREV);
                new_token = append_literal_char(tokens, '-');
                new_token = append_year(tokens, TFS_ERA, TFS_NUMBER);
            } else {
                int i;
                for (i=0; i<sizeof(posix_tokens)/sizeof(posix_tokens[0]); i++) {
                    if (posix_tokens[i].text[1] == *p) {
                        new_token = tfs_append_token(tokens);
                        memcpy(new_token, &posix_tokens[i].token, sizeof(tfs_token_t));
                        break;
                    }
                }
                if (i == sizeof(posix_tokens)/sizeof(posix_tokens[0])) {
                    if (handle_error) {
                        char error_buf[1024];
                        snprintf(error_buf, sizeof(error_buf), "Unrecognized percent code: %c", *p);
                        handle_error(error_buf, sizeof(error_buf), tokens);
                    }
                    *outError = TFS_PARSE_ERROR;
                    tfs_free_token_array(tokens);
                    return NULL;
                }
            }
        } else {
            literal_len++;
        }
        p++;
    }
    if (literal_len) {
        new_token = tfs_append_token(tokens);
        new_token->is_literal = 1;
        if (literal_len > sizeof(new_token->text))
            literal_len = sizeof(new_token->text);
        memcpy(new_token->text, p-literal_len, literal_len);
    }
    return tokens;
}