Пример #1
0
tfs_token_array_t *tfs_excel_parse(const char *bytes, int *outError) {
    tfs_token_array_t *token_array = tfs_init_token_array(10);
    int error = 0;
    int i;
    size_t len = strlen(bytes);

    error = tfs_parse_excel_format_string_internal((const u_char *)bytes, len,
        &handle_literal, &handle_code, token_array);

    if (error) {
        *outError = error;
        tfs_free_token_array(token_array);
        return NULL;
    }

    tfs_time_unit_e last_unit = 0;
    int has_ampm = 0;

    /* Various stupid fix-ups because the Excel format is retarded */
    for (i=0; i<token_array->count; i++) {
        tfs_token_t *token = &token_array->tokens[i];
        if (!token->is_literal && token->time_unit == TFS_PERIOD) {
            has_ampm = 1;
            break;
        }
    }

    for (i=0; i<token_array->count; i++) {
        tfs_token_t *token = &token_array->tokens[i];

        if (!token->is_literal) {
            if (token->time_unit == TFS_HOUR && has_ampm) {
                token->relative_to = TFS_PERIOD;
                token->start_at_one = 1;
            }
            if (token->time_unit == TFS_MONTH && last_unit == TFS_HOUR) {
                token->time_unit = TFS_MINUTE;
            }
            last_unit = token->time_unit;
        }
    }

    last_unit = 0;

    for (i=token_array->count-1; i>=0; i--) {
        tfs_token_t *token = &token_array->tokens[i];

        if (!token->is_literal) {
            if (token->time_unit == TFS_MONTH && last_unit == TFS_SECOND) {
                token->time_unit = TFS_MINUTE;
            }

            last_unit = token->time_unit;
        }
    }

    return token_array;
}
Пример #2
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;
}