oval_schema_version_t oval_schema_version_from_cstr(const char *ver_str) { oval_schema_version_t version; memset(&version, 0, sizeof(oval_schema_version_t)); if (ver_str == NULL) { return version; } #if defined USE_REGEX_PCRE const char *pattern = "([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?(?::([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?)?"; const char *error; int erroffset; pcre *re = pcre_compile(pattern, 0, &error, &erroffset, NULL); if (re == NULL) { dE("Regular expression compilation failed with %s\n", pattern); return version; } const int ovector_size = 30; // must be a multiple of 30 int ovector[ovector_size]; int rc = pcre_exec(re, NULL, ver_str, strlen(ver_str), 0, 0, ovector, ovector_size); pcre_free(re); if (rc < 0) { dE("Regular expression %s did not match string %s\n", pattern, ver_str); return version; } for (int i = 1; i < rc; i++) { const char *substring = ver_str + ovector[2 * i]; size_t substring_length = ovector[2 * i + 1] - ovector[2 * i]; version.component[i - 1] = _parse_int(substring, substring_length); } #elif defined USE_REGEX_POSIX const char *pattern = "([0-9]+)\\.([0-9]+)(\\.([0-9]+))?(:([0-9]+)\\.([0-9]+)(\\.([0-9]+))?)?"; int groups[OVAL_SCHEMA_VERSION_COMPONENTS_COUNT] = {1, 2, 4, 6, 7, 9}; regex_t re; if (regcomp(&re, pattern, REG_EXTENDED) != 0) { dE("Regular expression compilation failed with %s\n", pattern); return version; } size_t max_matches = 10; regmatch_t matches[max_matches]; int match = regexec(&re, ver_str, max_matches, matches, 0); regfree(&re); if (match != 0) { dE("Regular expression %s did not match string %s\n", pattern, ver_str); return version; } for (int i = 0; i < OVAL_SCHEMA_VERSION_COMPONENTS_COUNT; i++) { int group = groups[i]; if (matches[group].rm_so >= 0) { const char *substring = ver_str + matches[group].rm_so; size_t substring_length = matches[group].rm_eo - matches[group].rm_so; version.component[i] = _parse_int(substring, substring_length); } } #endif return version; }
oval_schema_version_t oval_schema_version_from_cstr(const char *ver_str) { oval_schema_version_t version; memset(&version, 0, sizeof(oval_schema_version_t)); if (ver_str == NULL) { return version; } const char *pattern = "([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?(?::([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?)?"; const char *error; int erroffset; pcre *re = pcre_compile(pattern, 0, &error, &erroffset, NULL); if (re == NULL) { dE("Regular expression compilation failed with %s", pattern); return version; } const int ovector_size = 30; // must be a multiple of 30 int ovector[ovector_size]; int rc = pcre_exec(re, NULL, ver_str, strlen(ver_str), 0, 0, ovector, ovector_size); pcre_free(re); if (rc < 0) { dE("Regular expression %s did not match string %s", pattern, ver_str); return version; } for (int i = 1; i < rc; i++) { const char *substring = ver_str + ovector[2 * i]; size_t substring_length = ovector[2 * i + 1] - ovector[2 * i]; version.component[i - 1] = _parse_int(substring, substring_length); } return version; }
static bool _parse_time(JsLex *lex, TimeInDay *time, int initialChars) { time->hour = (int)stringToIntWithRadix(&jslGetTokenValueAsString(lex)[initialChars], 10, 0); jslGetNextToken(lex); if (lex->tk==':') { jslGetNextToken(lex); if (lex->tk == LEX_INT) { time->min = _parse_int(lex); jslGetNextToken(lex); if (lex->tk==':') { jslGetNextToken(lex); if (lex->tk == LEX_INT || lex->tk == LEX_FLOAT) { JsVarFloat f = stringToFloat(jslGetTokenValueAsString(lex)); time->sec = (int)f; time->ms = (int)(f*1000) % 1000; jslGetNextToken(lex); if (lex->tk == LEX_ID && strcmp(jslGetTokenValueAsString(lex),"GMT")==0) { jslGetNextToken(lex); } if (lex->tk == '+' || lex->tk == '-') { int sign = lex->tk == '+' ? 1 : -1; jslGetNextToken(lex); if (lex->tk == LEX_INT) { int i = _parse_int(lex); // correct the fact that it's HHMM and turn it into just minutes i = (i%100) + ((i/100)*60); time->zone = i*sign; jslGetNextToken(lex); } } return true; } } } } return false; }
/*JSON{ "type" : "staticmethod", "class" : "Date", "name" : "parse", "generate" : "jswrap_date_parse", "params" : [ ["str","JsVar","A String"] ], "return" : ["float","The number of milliseconds since 1970"] } Parse a date string and return milliseconds since 1970. Data can be either '2011-10-20T14:48:00', '2011-10-20' or 'Mon, 25 Dec 1995 13:30:00 +0430' */ JsVarFloat jswrap_date_parse(JsVar *str) { if (!jsvIsString(str)) return 0; TimeInDay time; time.daysSinceEpoch = 0; time.hour = 0; time.min = 0; time.sec = 0; time.ms = 0; time.zone = 0; CalendarDate date = getCalendarDate(0); JsLex lex; jslInit(&lex, str); if (lex.tk == LEX_ID) { date.month = getMonth(jslGetTokenValueAsString(&lex)); date.dow = getDay(jslGetTokenValueAsString(&lex)); if (date.month>=0) { // Aug 9, 1995 jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk==',') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { _parse_time(&lex, &time, 0); } } } } } else if (date.dow>=0) { date.month = 0; jslGetNextToken(&lex); if (lex.tk==',') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_ID && getMonth(jslGetTokenValueAsString(&lex))>=0) { date.month = getMonth(jslGetTokenValueAsString(&lex)); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_INT) { _parse_time(&lex, &time, 0); } } } } } } else { date.dow = 0; date.month = 0; } } else if (lex.tk == LEX_INT) { // assume 2011-10-10T14:48:00 format date.year = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk=='-') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.month = _parse_int(&lex) - 1; jslGetNextToken(&lex); if (lex.tk=='-') { jslGetNextToken(&lex); if (lex.tk == LEX_INT) { date.day = _parse_int(&lex); jslGetNextToken(&lex); if (lex.tk == LEX_ID && jslGetTokenValueAsString(&lex)[0]=='T') { _parse_time(&lex, &time, 1); } } } } } } jslKill(&lex); time.daysSinceEpoch = fromCalenderDate(&date); return fromTimeInDay(&time); }