char *stringCombine(char *string1, char *string2) { unsigned long length1 = 0; unsigned long length2 = 0; char *out = NULL; if (!string1 || !string2) { return string1; } length1 = stringLen(string1); length2 = stringLen(string2); out = memMgrChunkNew(length1 + length2 + 1); if (!out) { return string1; } stringPrintF(out, "%s%s", string1, string2); out[length1 + length2] = '\0'; memMgrChunkFree(string1); return out; }
std::string formatHex(const std::vector<uint8_t>& data) { // FIXME: not very efficient std::string ret; for (uint8_t b : data) ret += stringPrintF("%02x ", b); return std::move(ret); }
ErrnoError::ErrnoError(const std::string& context, int errnoC) : std::runtime_error( stringPrintF("%s errno: %s", context.c_str(), strerror(errnoC))) {}
std::vector<uint8_t> readIntelHexFile(const std::string& path) { std::vector<uint8_t> result; std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), fclose); if (!fp) throw ErrnoError("fopen", errno); int expNextAddr = 0; while (true) { char header[9]; if (fread(header, 1, sizeof(header), fp.get()) != sizeof(header)) { if (feof(fp.get())) break; throw ErrnoError("fread", errno); } if (header[0] != ':') { throw std::runtime_error("failed to find start code"); } char byteCountStr[3]; memcpy(byteCountStr, &header[1], 2); byteCountStr[2] = '\0'; int byteCount = strtol(byteCountStr, nullptr, 16); if (g_verbose) printf("byteCount: %d\n", byteCount); char addrStr[5]; memcpy(addrStr, &header[3], 4); addrStr[4] = '\0'; int addr = strtol(addrStr, nullptr, 16); if (g_verbose) printf("addr: %04x\n", addr); if (header[7] != '0') { throw std::runtime_error("record type doesn't start with 0"); } if (header[8] == '1') { // EOF record break; } else if (header[8] != '0') { throw std::runtime_error( stringPrintF("encountered unsupported record type 0%c!", header[8])); } if (addr != expNextAddr) throw std::runtime_error(stringPrintF( "Expected addr record %04x, but read %04x", expNextAddr, addr)); char byteStr[3]; byteStr[2] = '\0'; for (int i = 0; i < byteCount; ++i) { if (fread(byteStr, 1, 2, fp.get()) != 2) throw ErrnoError("fread byteStr", errno); int byte = strtol(byteStr, nullptr, 16); result.push_back(static_cast<uint8_t>(byte)); } expNextAddr = addr + byteCount; if (fread(byteStr, 1, 2, fp.get()) != 2) throw ErrnoError("fread checksum", errno); while (true) { int c = fgetc(fp.get()); if (c == EOF) break; else if (!isspace(c)) { ungetc(c, fp.get()); break; } } } return result; }
void StoreTest(int_t count, bool ram){ srand(1251971); int_t i; long_t veryStart = lucene::util::Misc::currentTimeMillis(); long_t start = lucene::util::Misc::currentTimeMillis(); Directory& store = (ram?(Directory&)*new RAMDirectory(): (Directory&)FSDirectory::getDirectory(_T("test.store"), true) ); int_t LENGTH_MASK = 0xFFF; char_t name[260]; for (i = 0; i < count; i++) { stringPrintF(name,_T("%d.dat"),i); int_t length = rand() & LENGTH_MASK; l_byte_t b = (l_byte_t)(rand() & 0x7F); OutputStream& file = store.createFile(name); for (int_t j = 0; j < length; j++) file.writeByte(b); file.close(); delete &file; } _cout << (lucene::util::Misc::currentTimeMillis() - start) << _T(" total milliseconds to create") << endl; if (!ram){ //store.close(); store = (Directory&)FSDirectory::getDirectory(_T("test.store"), false); } srand(1251971); start = lucene::util::Misc::currentTimeMillis(); for (i = 0; i < count; i++) { stringPrintF(name,_T("%d.dat"),i); int_t length = rand() & LENGTH_MASK; l_byte_t b = (l_byte_t)(rand() & 0x7F); InputStream& file = store.openFile(name); if (file.Length() != length) throw; // exception( "length incorrect" ); for (int_t j = 0; j < length; j++) if (file.readByte() != b) throw;// exception( "contents incorrect" ); file.close(); delete &file; } _cout << (lucene::util::Misc::currentTimeMillis() - start) << _T(" total milliseconds to read") << endl; srand(1251971); start = lucene::util::Misc::currentTimeMillis(); for (i = 0; i < count; i++) { stringPrintF(name,_T("%d.dat"),i); store.deleteFile(name); } _cout << (lucene::util::Misc::currentTimeMillis() - start) << _T(" total milliseconds to delete") << endl; _cout << (lucene::util::Misc::currentTimeMillis() - veryStart) << _T(" total milliseconds") << endl; store.close(); if ( ram ) delete &store; }