// refresh the preferences when a different SumatraPDF process saves them // or if they are edited by the user using a text editor bool Reload() { ScopedMem<WCHAR> path(GetSettingsPath()); if (!file::Exists(path)) return false; // make sure that the settings file is readable - else wait // a short while to prevent accidental dataloss int tryAgainCount = 5; HANDLE h = file::OpenReadOnly(path); while (INVALID_HANDLE_VALUE == h && tryAgainCount-- > 0) { Sleep(200); h = file::OpenReadOnly(path); } if (INVALID_HANDLE_VALUE == h) { // prefer not reloading to resetting all settings return false; } ScopedHandle hScope(h); FILETIME time = file::GetModificationTime(path); if (FileTimeEq(time, gGlobalPrefs->lastPrefUpdate)) return true; ScopedMem<char> uiLanguage(str::Dup(gGlobalPrefs->uiLanguage)); bool showToolbar = gGlobalPrefs->showToolbar; bool invertColors = gGlobalPrefs->fixedPageUI.invertColors; gFileHistory.UpdateStatesSource(NULL); CleanUp(); bool ok = Load(); CrashAlwaysIf(!ok || !gGlobalPrefs); gGlobalPrefs->fixedPageUI.invertColors = invertColors; // TODO: about window doesn't have to be at position 0 if (gWindows.Count() > 0 && gWindows.At(0)->IsAboutWindow()) { gWindows.At(0)->DeleteInfotip(); gWindows.At(0)->staticLinks.Reset(); gWindows.At(0)->RedrawAll(true); } if (!str::Eq(uiLanguage, gGlobalPrefs->uiLanguage)) SetCurrentLanguageAndRefreshUi(gGlobalPrefs->uiLanguage); if (gGlobalPrefs->showToolbar != showToolbar) ShowOrHideToolbarGlobally(); UpdateDocumentColors(); UpdateFavoritesTreeForAllWindows(); return true; }
static bool AppendEntry(str::Str<char>& data, str::Str<char>& content, const WCHAR *filePath, const char *inArchiveName, lzma::FileInfo *fi=NULL) { size_t nameLen = str::Len(inArchiveName); CrashIf(nameLen > UINT32_MAX - 25); uint32_t headerSize = 25 + (uint32_t)nameLen; FILETIME ft = file::GetModificationTime(filePath); if (fi && FileTimeEq(ft, fi->ftModified)) { ReusePrevious: ByteWriterLE meta(data.AppendBlanks(24), 24); meta.Write32(headerSize); meta.Write32(fi->compressedSize); meta.Write32(fi->uncompressedSize); meta.Write32(fi->uncompressedCrc32); meta.Write32(ft.dwLowDateTime); meta.Write32(ft.dwHighDateTime); data.Append(inArchiveName, nameLen + 1); return content.AppendChecked(fi->compressedData, fi->compressedSize); } size_t fileDataLen; ScopedMem<char> fileData(file::ReadAll(filePath, &fileDataLen)); if (!fileData || fileDataLen >= UINT32_MAX) { fprintf(stderr, "Failed to read \"%S\" for compression\n", filePath); return false; } uint32_t fileDataCrc = crc32(0, (const uint8_t *)fileData.Get(), (uint32_t)fileDataLen); if (fi && fi->uncompressedCrc32 == fileDataCrc && fi->uncompressedSize == fileDataLen) goto ReusePrevious; size_t compressedSize = fileDataLen + 1; ScopedMem<char> compressed((char *)malloc(compressedSize)); if (!compressed) return false; if (!Compress(fileData, fileDataLen, compressed, &compressedSize)) return false; ByteWriterLE meta(data.AppendBlanks(24), 24); meta.Write32(headerSize); meta.Write32((uint32_t)compressedSize); meta.Write32((uint32_t)fileDataLen); meta.Write32(fileDataCrc); meta.Write32(ft.dwLowDateTime); meta.Write32(ft.dwHighDateTime); data.Append(inArchiveName, nameLen + 1); return content.AppendChecked(compressed, compressedSize); }
static void hexstrTest() { unsigned char buf[6] = {1, 2, 33, 255, 0, 18}; unsigned char buf2[6] = {0}; AutoFree s(_MemToHex(&buf)); utassert(str::Eq(s, "010221ff0012")); bool ok = _HexToMem(s, &buf2); utassert(ok); utassert(memeq(buf, buf2, sizeof(buf))); FILETIME ft1, ft2; GetSystemTimeAsFileTime(&ft1); s.Set(_MemToHex(&ft1)); _HexToMem(s, &ft2); DWORD diff = FileTimeDiffInSecs(ft1, ft2); utassert(0 == diff); utassert(FileTimeEq(ft1, ft2)); s.Set(str::MemToHex(nullptr, 0)); utassert(str::Eq(s, "")); ok = str::HexToMem(s, nullptr, 0); utassert(ok); }