//database functions void dbsave() { dprintf("Saving database..."); DWORD ticks = GetTickCount(); JSON root = json_object(); CommentCacheSave(root); LabelCacheSave(root); BookmarkCacheSave(root); FunctionCacheSave(root); LoopCacheSave(root); BpCacheSave(root); WString wdbpath = StringUtils::Utf8ToUtf16(dbpath); if(json_object_size(root)) { Handle hFile = CreateFileW(wdbpath.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); if(!hFile) { dputs("\nFailed to open database for writing!"); json_decref(root); //free root return; } SetEndOfFile(hFile); char* jsonText = json_dumps(root, JSON_INDENT(4)); DWORD written = 0; if(!WriteFile(hFile, jsonText, (DWORD)strlen(jsonText), &written, 0)) { json_free(jsonText); dputs("\nFailed to write database file!"); json_decref(root); //free root return; } hFile.Close(); json_free(jsonText); if(!settingboolget("Engine", "DisableDatabaseCompression")) LZ4_compress_fileW(wdbpath.c_str(), wdbpath.c_str()); } else //remove database when nothing is in there DeleteFileW(wdbpath.c_str()); dprintf("%ums\n", GetTickCount() - ticks); json_decref(root); //free root }
//database functions void dbsave() { dprintf("saving database..."); DWORD ticks = GetTickCount(); JSON root = json_object(); CommentCacheSave(root); LabelCacheSave(root); BookmarkCacheSave(root); FunctionCacheSave(root); LoopCacheSave(root); BpCacheSave(root); WString wdbpath = StringUtils::Utf8ToUtf16(dbpath); if(json_object_size(root)) { FILE* jsonFile = 0; if(_wfopen_s(&jsonFile, wdbpath.c_str(), L"wb")) { dputs("failed to open database file for editing!"); json_decref(root); //free root return; } if(json_dumpf(root, jsonFile, JSON_INDENT(4)) == -1) { dputs("couldn't write JSON to database file..."); json_decref(root); //free root return; } fclose(jsonFile); if(!settingboolget("Engine", "DisableCompression")) LZ4_compress_fileW(wdbpath.c_str(), wdbpath.c_str()); } else //remove database when nothing is in there DeleteFileW(wdbpath.c_str()); dprintf("%ums\n", GetTickCount() - ticks); json_decref(root); //free root }
void dbload() { // If the file doesn't exist, there is no DB to load if(!FileExists(dbpath)) return; dprintf("Loading database..."); DWORD ticks = GetTickCount(); // Multi-byte (UTF8) file path converted to UTF16 WString databasePathW = StringUtils::Utf8ToUtf16(dbpath); // Decompress the file if compression was enabled bool useCompression = !settingboolget("Engine", "DisableCompression"); LZ4_STATUS lzmaStatus = LZ4_INVALID_ARCHIVE; { lzmaStatus = LZ4_decompress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Check return code if(useCompression && lzmaStatus != LZ4_SUCCESS && lzmaStatus != LZ4_INVALID_ARCHIVE) { dputs("\nInvalid database file!"); return; } } // Open the file for reading by the JSON parser FILE* jsonFile = nullptr; long jsonFileSize = 0; if(_wfopen_s(&jsonFile, databasePathW.c_str(), L"rb")) { dputs("\nFailed to open database file!"); return; } // Get the current file size fseek(jsonFile, 0, SEEK_END); jsonFileSize = ftell(jsonFile); fseek(jsonFile, 0, SEEK_SET); // Verify that the file size is greater than 0. // This corrects a bug when a file exists, but there is no data inside. JSON root = nullptr; if(jsonFileSize > 0) root = json_loadf(jsonFile, 0, 0); // Release the file handle and re-compress fclose(jsonFile); if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression) LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Validate JSON load status if(!root) { dputs("\nInvalid database file (JSON)!"); return; } // Finally load all structures CommentCacheLoad(root); LabelCacheLoad(root); BookmarkCacheLoad(root); FunctionCacheLoad(root); LoopCacheLoad(root); BpCacheLoad(root); // Free root json_decref(root); dprintf("%ums\n", GetTickCount() - ticks); }
void dbload() { // If the file doesn't exist, there is no DB to load if(!FileExists(dbpath)) return; dprintf("Loading database..."); DWORD ticks = GetTickCount(); // Multi-byte (UTF8) file path converted to UTF16 WString databasePathW = StringUtils::Utf8ToUtf16(dbpath); // Decompress the file if compression was enabled bool useCompression = !settingboolget("Engine", "DisableDatabaseCompression"); LZ4_STATUS lzmaStatus = LZ4_INVALID_ARCHIVE; { lzmaStatus = LZ4_decompress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Check return code if(useCompression && lzmaStatus != LZ4_SUCCESS && lzmaStatus != LZ4_INVALID_ARCHIVE) { dputs("\nInvalid database file!"); return; } } // Read the database file Handle hFile = CreateFileW(databasePathW.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if(!hFile) { dputs("\nFailed to open database file!"); return; } unsigned int jsonFileSize = GetFileSize(hFile, 0); if(!jsonFileSize) { dputs("\nEmpty database file!"); return; } Memory<char*> jsonText(jsonFileSize + 1); DWORD read = 0; if(!ReadFile(hFile, jsonText(), jsonFileSize, &read, 0)) { dputs("\nFailed to read database file!"); return; } hFile.Close(); // Deserialize JSON JSON root = json_loads(jsonText(), 0, 0); if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression) LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Validate JSON load status if(!root) { dputs("\nInvalid database file (JSON)!"); return; } // Finally load all structures CommentCacheLoad(root); LabelCacheLoad(root); BookmarkCacheLoad(root); FunctionCacheLoad(root); LoopCacheLoad(root); BpCacheLoad(root); // Free root json_decref(root); dprintf("%ums\n", GetTickCount() - ticks); }