void Compressor::serialize(char* s, streamsize n) { switch (s[0]) { case '"': serialize_str_val(s + 1, n - 2); break; case 't': serialize_char(T_BOOL | 1); break; case 'f': serialize_char(T_BOOL | 0); break; case 'n': serialize_char(T_NULL); break; default: if (s[0] >= '0' && s[0] < '9' || s[0] == '-' || s[0] == '.') { if (memchr((void*)s, '.', n)) { serialize_float_val(parse_float(s, n)); } else { serialize_int_val(parse_int(s, n), T_INT); } } break; } }
void Compressor::serialize_float_val(float f) { serialize_char(T_FLOAT | FLOAT_SIZE); char bytes[FLOAT_SIZE]; memset((void*)bytes, 0, FLOAT_SIZE); memcpy(bytes, (void*)&f, FLOAT_SIZE); for (int i = 0; i < FLOAT_SIZE; i++) { serialize_char(bytes[i]); } }
void Compressor::close(Source&) { parser.feeding_done(); serialize_char(START_DICTIONARY); for (auto& kv : keyDictionary) { serialize_str_val((char*)kv.first.c_str(), (int)kv.first.length()); serialize_int_val(kv.second, T_INT); } }
void Compressor::serialize_int_val(int num, SerializedConstants sconst) { char byte_count = -1; char bytes[INT_SIZE]; memset((void*)bytes, 0, INT_SIZE); int fn = 0xff << (8 * (INT_SIZE - 1)); for (int i = 0; i < INT_SIZE; i++) { bytes[i] = (num & fn) >> ((INT_SIZE - i - 1) * 8); fn = fn >> 8; if (byte_count == -1 && bytes[i]) byte_count = INT_SIZE - i; } if (byte_count == -1) byte_count = 1; // supporting num == 0 serialize_char(sconst | byte_count); for (int i = INT_SIZE - byte_count; i < INT_SIZE; i++) serialize_char(bytes[i]); }
void Test_serialize_char(CuTest * tc){ printf("Char serialization tests and experiments \n"); char a = 'a'; unsigned char buffer[32]; serialize_char(buffer, a); printf("Size of char = %zu\n",sizeof(char)); printf("Size of a = %zu\n",sizeof(a)); printf("Size of 'a' = %zu\n",sizeof('a')); printf("Serialized value is {%c}\n",buffer[0]); CuAssertIntEquals(tc,0,0); }
streamsize Compressor::write(Sink& dest, const char* s, streamsize n) { streamsize bytesToCopy = n + buffLen > JSC_LINE_BUFF_SIZE ? JSC_LINE_BUFF_SIZE - buffLen : n; if (bytesToCopy == 0) throw runtime_error("buffer overflow"); memcpy(buff + buffLen, s, bytesToCopy); buffLen += bytesToCopy; sinkWrite = [&dest](char* buff, streamsize n) { io::write(dest, buff, n); }; char* newLine = (char*)memchr((void*)buff, 10, buffLen); while (newLine) { // newline included streamsize llen = newLine - buff + 1; parser.reset(); try { // ignoring bad lines for now parser.feed(buff, llen); } catch (const exception& ex) { cerr << ex.what() << endl; } // TODO: this is not safe yet; should finish // any started token serialize_char(END_RECORD); memcpy(buff, buff + llen, buffLen - llen); buffLen = buffLen - llen; memset(buff + buffLen, 0, JSC_LINE_BUFF_SIZE - buffLen); newLine = (char*)memchr((void*)buff, 10, buffLen); } return n; }