scanner::token_type scanner::scan_body() { wchar c = get_char(); value_length = 0; bool ws = false; if(c == 0) return TT_EOF; else if(c == '<') return scan_tag(); else if(c == '&') c = scan_entity(); else ws = is_whitespace(c); while(true) { append_value(c); c = input.get_char(); if(c == 0) { push_back(c); break; } if(c == '<') { push_back(c); break; } if(c == '&') { push_back(c); break; } if(is_whitespace(c) != ws) { push_back(c); break; } } return ws? TT_SPACE:TT_WORD; }
static value_object eval_function(char* name, expr_list* args) { expr_list* head = args; value_list* arg_vals = NULL; while (head != NULL) { value_object v = eval_aux(head->expression); arg_vals = append_value(arg_vals, v); head = head->next; } fpl_function fun; symbol_def* symbol = find_symbol(name); if (symbol == NULL) { fprintf(stderr, "Error: function `%s' does not exist\n", name); return make_null(); } if (symbol->type != SYM_FUNCTION) { fprintf(stderr, "Error: `%s' is not a function\n", name); return make_null(); } value_object val = (symbol->function)(arg_vals); return val; }
static bool append_document(bson_t* bson, object_t* object) { for (size_t i = 0; i < object->l; ++i) { object_t* key = object->children + i * 2; object_t* value = object->children + i * 2 + 1; if (!append_value(bson, key->str, key->l, value)) return false; } return true; }
static bool append_array(bson_t* bson, object_t* object) { for (size_t i = 0; i < object->l; ++i) { char str[16]; const char* key; size_t len = bson_uint32_to_string(i, &key, str, sizeof(str)); if (!append_value(bson, key, len, object->children + i)) return false; } return true; }
int ltLuaToJSON(lua_State *L) { ltLuaCheckNArgs(L, 1); lua_pushvalue(L, 1); write_state state; state.buf = (char*)malloc(INIT_CAPACITY); state.ptr = state.buf; state.capacity = INIT_CAPACITY; append_value(L, &state); lua_pushlstring(L, state.buf, (state.ptr - state.buf)); free(state.buf); return 1; }
// caller consumed '&' wchar scanner::scan_entity() { char buf[32]; int i = 0; wchar t; for(; i < 31 ; ++i ) { t = get_char(); if(t == 0) return TT_EOF; if( !isalnum(t) ) { push_back(t); break; // appears a erroneous entity token. // but we try to use it. } buf[i] = char(t); if(t == ';') break; } buf[i] = 0; if(i == 2) { if(equal(buf,"gt",2)) return '>'; if(equal(buf,"lt",2)) return '<'; } else if(i == 3 && equal(buf,"amp",3)) return '&'; else if(i == 4) { if(equal(buf,"apos",4)) return '\''; if(equal(buf,"quot",4)) return '\"'; } t = resolve_entity(buf,i); if(t) return t; // no luck ... append_value('&'); for(int n = 0; n < i; ++n) append_value(buf[n]); return ';'; }
std::pair<size_t, bool> StreamWriter<StreamType>::append_array(const Value& value) { std::pair<size_t, bool> rtn(2, false); const std::size_t size = value.size(); write(Marker::Array_Start); for(size_t i=0; i < size; i++) update(append_value(value[i]), rtn); write(Marker::Array_End); return rtn; //! \todo TODO... detect homogenepus arrays and treat accordingly //! WORK in progress }
std::pair<size_t, bool> StreamWriter<StreamType>::append_object(const Value& value) { auto keys = value.keys(); std::pair<size_t, bool> rtn(1, false); write(Marker::Object_Start); //update(append_size(keys.size()), rtn); for(const auto& key : keys) { decltype(rtn) k(0, false); k = append_key(key); update(k, rtn); k = append_value(value[key]); update(k, rtn); } write(Marker::Object_End); rtn.first += 1; return rtn; }
static int append_row(std::string *str, const struct row_data *row) { for (uint16_t i = 0; i < row->field_count; i++) { low_data_struct *low = &row->datas[i]; str->append(" "); str->append(low->field_name); str->append("=\""); const char *type_name = g_type_name_map.get(low->type); if (!type_name) { log_error("unknown type %d", low->type); return -1; } str->append(type_name); str->append(":"); append_value(str, low); str->append("\""); } return 0; }
std::pair<size_t, bool> StreamWriter<StreamType>::writeValue(const Value& value) { return append_value(value); }
int to_RPN(char *src) { char buf0[1024]; int i; int f; int top; int type; int value; double d_value; init_stack(); strncpy(buf0, src, sizeof(buf0)); if(!strncmp(src, "$", 1)) { f = 0; for(i = 1; src[i] != '\0'; i++) { if(src[i] == '=') f = 1; if(f == 0) variable_name[i - 1] = src[i]; if(src[i] == '=') { strncpy(buf0, src + i + 1, sizeof(buf0)); break; } } if(f == 0) { variable_name[0] = '\0'; } else { variable_name[i - 1] = '\0'; } } for( ;; ) { type = get_next_token(buf0, &value, &d_value); switch(type) { case 0: // End of String goto end_of_formula; case 1: // NUMBER append_value(d_value); break; case 2: // OPERATOR case 3: // SPLIT case 4: // SYMBOL if(pop(&top)) { /* empty stack */ if(value == ')') { fprintf(stderr, "Error: Invalid syntax\n"); } push(value); } else if(value == '(') { push(top); push(value); } else if(value == ')') { for( ;; ) { append_operator(top); if(pop(&top)) break; if(top == '(') break; } } else { if(order(value) == -1) { fprintf(stderr, "Error: Syntax error\n"); break; } while(order(top) >= order(value)) { append_operator(top); if(pop(&top)) goto end_loop; } push(top); end_loop: push(value); } break; default: break; } } end_of_formula: while(!pop(&top)) { if(top == '(') { fprintf(stderr, "Error: Invalid syntax\n"); return -1; } append_operator(top); } term_stack(); return 0; }
static void append_value(lua_State *L, write_state *state) { char numbuf[NUM_BUF_SIZE]; int ltype = lua_type(L, -1); switch (ltype) { case LUA_TNIL: { appends("null", state); break; } case LUA_TNUMBER: { LTdouble n = lua_tonumber(L, -1); snprintf(numbuf, NUM_BUF_SIZE, "%.14g", n); appends(numbuf, state); break; } case LUA_TBOOLEAN: { int b = lua_toboolean(L, -1); if (b) appends("true", state); else appends("false", state); break; } case LUA_TSTRING: { const char *s = lua_tostring(L, -1); appendc('"', state); while (*s != 0) { switch (*s) { case '\n': appends("\\n", state); break; case '\r': appends("\\r", state); break; case '\t': appends("\\t", state); break; case '\b': appends("\\b", state); break; case '\f': appends("\\f", state); break; case '\\': appends("\\\\", state); break; case '\"': appends("\\\"", state); break; default: appendc(*s, state); break; } s++; } appendc('"', state); break; } case LUA_TTABLE: { int is_array = 1; lua_pushnil(L); if (!lua_next(L, -2)) { // empty table. write out as empty array, since // that seems like it would be the more common case. appends("[]", state); break; } else { if (lua_type(L, -2) == LUA_TSTRING) is_array = 0; lua_pop(L, 2); // pop key and value } if (is_array) { appendc('[', state); int i = 1; lua_rawgeti(L, -1, i++); while (1) { append_value(L, state); lua_rawgeti(L, -1, i++); if (lua_isnil(L, -1)) { lua_pop(L, 1); // pop nil break; } appendc(',', state); } appendc(']', state); } else { appendc('{', state); lua_pushnil(L); lua_next(L, -2); while (1) { if (lua_type(L, -2) != LUA_TSTRING) { // ignore entries without string fields. lua_pop(L, 1); // pop value, leaving key if (!lua_next(L, -2)) break; continue; } lua_pushvalue(L, -2); // push key append_value(L, state); // write key appendc(':', state); append_value(L, state); // write value, key now on top if (!lua_next(L, -2)) break; appendc(',', state); } appendc('}', state); } break; } default: appends("null", state); } lua_pop(L, 1); // pop value }
void append_byte( Byte byte1 ) { append_value( byte1, 1 ); }
void append_word( int word ) { append_value( word, 2 ); }
void append_long( long dword ) { append_value( dword, 4 ); }
static void osec_empty_symlink(struct record *rec) { char t = '\0'; append_value(OVALUE_LINK, &t, (size_t) 1, rec); }
static void osec_empty_digest(struct record *rec) { char fdigest[digest_len]; bzero(&fdigest, (size_t) digest_len); append_value(OVALUE_CSUM, &fdigest, (size_t) digest_len, rec); }
void append_2bytes( Byte byte1, Byte byte2 ) { append_value( byte1, 1 ); append_value( byte2, 1 ); }
scanner::token_type scanner::scan_head() { wchar c = skip_whitespace(); if(c == '>') { c_scan = &scanner::scan_body; return scan_body(); } if(c == '/') { wchar t = get_char(); if(t == '>') { c_scan = &scanner::scan_body; return TT_TAG_END; } else { push_back(t); return TT_ERROR; } // erroneous situtation - standalone '/' } attr_name_length = 0; value_length = 0; // attribute name... while(c != '=') { if( c == 0) return TT_EOF; if( c == '>' ) { push_back(c); return TT_ATTR; } // attribute without value (HTML style) if( is_whitespace(c) ) { c = skip_whitespace(); if(c != '=') { push_back(c); return TT_ATTR; } // attribute without value (HTML style) else break; } if( c == '<') return TT_ERROR; append_attr_name(c); c = get_char(); } c = skip_whitespace(); // attribute value... if(c == '\"') while(c = get_char()) { if(c == '\"') return TT_ATTR; if(c == '&') c = scan_entity(); append_value(c); } else if(c == '\'') // allowed in html while(c = get_char()) { if(c == '\'') return TT_ATTR; if(c == '&') c = scan_entity(); append_value(c); } else // scan token, allowed in html: e.g. align=center do { if( is_whitespace(c) ) return TT_ATTR; /* these two removed in favour of better html support: if( c == '/' || c == '>' ) { push_back(c); return TT_ATTR; } if( c == '&' ) c = scan_entity();*/ if( c == '>' ) { push_back(c); return TT_ATTR; } append_value(c); } while(c = get_char()); return TT_ERROR; }
int main(int argc, char **argv) { FILE * fp; struct stat sb; size_t len; ssize_t read; char *line = NULL; char *outdir = NULL; char *dumpfile = NULL; const char datafile[MAXPATHLEN]; const char metafile[MAXPATHLEN]; int rc = EXIT_SUCCESS; item_value_u* values = NULL; int values_buf = 0; int values_count = 0; int o_type, o_delay; hfs_time_t o_stamp, b_stamp; if (argc != 3) { printf("Usage %s <outdir> <hfs-dump-file>\n", argv[0]); return EXIT_SUCCESS; } if (stat(argv[1], &sb) == -1) { perror("stat"); return EXIT_FAILURE; } if (!S_ISDIR(sb.st_mode)) { fprintf(stderr, "%s: first argument should be a directory.\n", argv[1]); return EXIT_FAILURE; } outdir = argv[1]; if (strcmp("-", argv[2]) != 0) { if (stat(argv[2], &sb) == -1) { perror("stat"); return EXIT_FAILURE; } if (!S_ISREG(sb.st_mode)) { fprintf(stderr, "%s: first argument should be a regular file.\n", argv[2]); return EXIT_FAILURE; } dumpfile = argv[2]; if ((fp = fopen(dumpfile, "r")) == NULL) { perror("fopen"); return EXIT_FAILURE; } } else { fp = stdin; } snprintf(metafile, MAXPATHLEN, "%s/history.meta", outdir); snprintf(datafile, MAXPATHLEN, "%s/history.data", outdir); if (access(metafile, R_OK) == 0) unlink(metafile); if (access(datafile, R_OK) == 0) unlink(datafile); while ((read = getline(&line, &len, fp)) != -1) { char *ch, *start; hfs_time_t stamp = -1; int rc, type = -1, delay = -1; item_value_u value; start = line; line[read-1] = '\0'; // Expected: time=1217851317 delay=30 type=0 value=1 while (start != NULL) { if ((ch = strchr(start, '\t')) != NULL) *ch = '\0'; if (strncmp("time=", start, 5) == 0) sscanf(start, "time=%lld", &stamp); else if (strncmp("delay=", start, 6) == 0) sscanf(start, "delay=%d", &delay); else if (strncmp("type=", start, 5) == 0) sscanf(start, "type=%d", &type); else if (strncmp("value=", start, 6) == 0) { if (type == -1 || delay == -1 || stamp == -1) { fprintf(stderr, "Wrong value order!\n"); goto out; } if (type == IT_UINT64) rc = sscanf(start, "value=%lld", &value.l); else if (type == IT_DOUBLE) rc = sscanf(start, "value=%lf", &value.d); /* check that we must start a new block */ if (values_count) { if (delay != o_delay || o_type != type || o_stamp > stamp) { /* dump old data */ rc = hfs_store_values(metafile, datafile, b_stamp, o_delay, values, sizeof(item_value_u), values_count, o_type); values_count = 0; o_type = type; o_delay = delay; b_stamp = stamp; } /* check that data value appeared before it's time */ if (stamp - o_stamp < delay) continue; /* drop data item */ if (stamp - o_stamp > delay) { /* append needed amount of invalid data values */ item_value_u v; int i; hfs_time_t extra = (stamp - o_stamp) / delay; v.l = 0xFFFFFFFFFFFFFFFFULL; for (i = 0; i < extra-1; i++) append_value (&values, &values_buf, &values_count, &v); } } else { o_type = type; o_delay = delay; b_stamp = stamp; } o_stamp = stamp; append_value (&values, &values_buf, &values_count, &value); // printf("TIME: %lld\n", stamp); // printf("TYPE: %d\n", type); // printf("DELAY: %d\n", delay); // printf("VALUE: %lld\n", value); } start = (ch != NULL) ? (ch + 1) : NULL; } } /* dump data (if any) */ if (values_count) hfs_store_values(metafile, datafile, b_stamp, o_delay, values, sizeof(item_value_u), values_count, o_type); if (values) free (values); out: if (line) free(line); fclose(fp); return rc; }