StSettings::StSettings(const StHandle<StResourceManager>& /*theResMgr*/, const StString& theSettingsSet) : mySettingsSet(theSettingsSet.toUtfWide()), myRegisterPath(StStringUtfWide("SOFTWARE\\sView\\") + theSettingsSet.toUtfWide()), myToFlush(false) { // }
bool StSettings::saveString(const StString& theParam, const StString& theValue) { StRegKey aKey; if(!aKey.create(myRegisterPath)) { return false; // No write registry success! } StStringUtfWide aValue = theValue.toUtfWide(); return RegSetValueExW(aKey, theParam.toUtfWide().toCString(), 0, REG_SZ, (LPBYTE )aValue.toCString(), DWORD(aValue.getSize() + sizeof(stUtfWide_t))) == ERROR_SUCCESS; }
bool loadStringFromRegister(const StString& theRegisterPath, const StString& theParamPath, StString& theOutValue) { // TODO (Kirill Gavrilov) parse ERROR_MORE_DATA error (increase buffer) stUtfWide_t* aDataOut = new stUtfWide_t[4096U]; HKEY hKey = NULL; DWORD aValueSize = sizeof(stUtfWide_t) * 4096U; RegOpenKeyExW(HKEY_CURRENT_USER, theRegisterPath.toUtfWide().toCString(), 0, KEY_READ, &hKey); if(RegQueryValueExW(hKey, theParamPath.toUtfWide().toCString(), NULL, NULL, (LPBYTE )aDataOut, &aValueSize) == ERROR_SUCCESS) { RegCloseKey(hKey); theOutValue = StString(aDataOut); delete[] aDataOut; return true; } RegCloseKey(hKey); delete[] aDataOut; return false; }
bool StSettings::loadString(const StString& theParam, StString& theValue) { const StStringUtfWide aParam = theParam.toUtfWide(); StRegKey aKey; if(!aKey.open(myRegisterPath)) { return false; } // request size DWORD aValueSize = 0; if(RegQueryValueExW(aKey, aParam.toCString(), NULL, NULL, NULL, &aValueSize) != ERROR_SUCCESS) { return false; } if(aValueSize == 0) { theValue = ""; return true; } DWORD aValueSizeRead = aValueSize; stUtfWide_t* aDataOut = new stUtfWide_t[aValueSize / sizeof(stUtfWide_t) + 1]; if(RegQueryValueExW(aKey, aParam.toCString(), NULL, NULL, (LPBYTE )aDataOut, &aValueSizeRead) != ERROR_SUCCESS) { return false; } aDataOut[aValueSize / sizeof(stUtfWide_t)] = L'\0'; // NULL-terminate theValue = StString(aDataOut); return true; }
StString StFileNode::getCompatibleName(const StString& theFileName) { #ifdef _WIN32 stUtfWide_t aShortNameWide[MAX_PATH]; GetShortPathNameW(theFileName.toUtfWide().toCString(), aShortNameWide, MAX_PATH); return *aShortNameWide != L'\0' ? StString(aShortNameWide) : theFileName; #else return theFileName; #endif }
StString StFileNode::getCompatibleName(const StString& theFileName) { #ifdef _WIN32 /// TODO (Kirill Gavrilov#1) if result is empty - not a filesystem element or no DOS-names enabled stUtfWide_t aShortNameWide[MAX_PATH]; GetShortPathName(theFileName.toUtfWide().toCString(), aShortNameWide, MAX_PATH); return *aShortNameWide != L'\0' ? StString(aShortNameWide) : theFileName; #else return theFileName; #endif }
bool StSettings::saveInt32(const StString& theParam, const int32_t& theValue) { StRegKey aKey; if(!aKey.create(myRegisterPath)) { return false; // No write registry success! } DWORD aData = theValue; return RegSetValueExW(aKey, theParam.toUtfWide().toCString(), 0, REG_DWORD, (LPBYTE )&aData, sizeof(DWORD)) == ERROR_SUCCESS; }
HMODULE StLibrary::DLibLoadFull(const StString& theLibName) { #ifdef _WIN32 HMODULE aModule = LoadLibraryW(theLibName.toUtfWide().toCString()); if(aModule == NULL) { ST_DEBUG_LOG("Failed to load library: \"" + theLibName + "\" (" + (int )GetLastError() + ')'); } else { #ifdef ST_DEBUG_LIBS ST_DEBUG_LOG("Loaded library: \"" + theLibName + "\" " + DLibGetVersion(theLibName.toUtfWide())); #endif } return aModule; #else HMODULE aModule = dlopen(theLibName.toCString(), RTLD_NOW); if(aModule == NULL) { ST_DEBUG_LOG("Failed to load library: \"" + theLibName + "\" (" + dlerror() + ')'); } else { #ifdef ST_DEBUG_LIBS ST_DEBUG_LOG("Loaded library: \"" + theLibName + '\"'); #endif } return aModule; #endif }
StLogger::StLogger(const StString& theLogFile, const StLogger::Level theFilter, const int theOptions) : myMutex((theOptions & StLogger::ST_OPT_LOCK) ? new StMutexSlim() : (StMutexSlim* )NULL), #ifdef _WIN32 myFilePath(theLogFile.toUtfWide()), #else myFilePath(theLogFile), #endif myFileHandle(NULL), myFilter(theFilter), myToLogCout(theOptions & StLogger::ST_OPT_COUT) { // }
void StMessageBox::Error(const StString& theMessage) { StLogger::GetDefault().write(theMessage, StLogger::ST_ERROR); #ifdef _WIN32 MessageBoxW(NULL, theMessage.toUtfWide().toCString(), L"Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND | MB_TOPMOST); #elif defined(__linux__) if(initGlobals()) { gdk_threads_enter(); GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", theMessage.toCString()); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); gdk_flush(); // we need this call! gdk_threads_leave(); } #endif }
void StProcess::openURL(const StString& theUrl) { #if defined(_WIN32) ShellExecuteW(NULL, L"open", theUrl.toUtfWide().toCString(), NULL, NULL, SW_SHOWNORMAL); #elif(defined(__linux__) || defined(__linux)) // we use nice script tool from Xdg-utils package // http://portland.freedesktop.org/wiki/ StArrayList<StString> anArguments(1); anArguments.add(theUrl); if(!StProcess::execProcess("/usr/bin/xdg-open", anArguments)) { ST_DEBUG_LOG("/usr/bin/xdg-open is not found!"); } // also we could use GTK function //gtk_show_uri(NULL, uri, gtk_get_current_event_time(), &err); #endif }
bool StMessageBox::Question(const StString& theMessage) { #ifdef _WIN32 return MessageBoxW(NULL, theMessage.toUtfWide().toCString(), L"Question", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_TOPMOST) == IDYES; #elif defined(__linux__) if(initGlobals()) { gdk_threads_enter(); GtkWidget* aDialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", theMessage.toCString()); gint anAnswer = gtk_dialog_run(GTK_DIALOG(aDialog)); gtk_widget_destroy(aDialog); gdk_flush(); // we need this call! gdk_threads_leave(); return anAnswer == GTK_RESPONSE_YES; } return false; #endif }
bool StSettings::loadInt32(const StString& theParam, int32_t& theValue) { StRegKey aKey; if(!aKey.open(myRegisterPath)) { return false; } DWORD aValueSize = sizeof(DWORD); DWORD aData = 0; if(RegQueryValueExW(aKey, theParam.toUtfWide().toCString(), NULL, NULL, (LPBYTE )&aData, &aValueSize) != ERROR_SUCCESS) { return false; } theValue = aData; return true; }
void StFolder::init(const StArrayList<StString>& theExtensions, int theDeep) { // clean up old list... clear(); StString aSearchFolderPath = getPath(); #ifdef _WIN32 WIN32_FIND_DATAW aFindFile; StString aStrSearchMask = getPath() + StString(SYS_FS_SPLITTER) + '*'; HANDLE hFind = FindFirstFileW(aStrSearchMask.toUtfWide().toCString(), &aFindFile); for(BOOL hasFile = (hFind != INVALID_HANDLE_VALUE); hasFile == TRUE; hasFile = FindNextFileW(hFind, &aFindFile)) { // StString aCurrItemName(aFindFile.cFileName); addItem(theExtensions, theDeep, aSearchFolderPath, aCurrItemName); } FindClose(hFind); #else DIR* aSearchedFolder = opendir(aSearchFolderPath.toCString()); if(aSearchedFolder == NULL) { return; } for(dirent* aDirItem = readdir(aSearchedFolder); aDirItem != NULL; aDirItem = readdir(aSearchedFolder)) { // #if (defined(__APPLE__)) // automatically convert filenames from decomposed form used by Mac OS X file systems StString aCurrItemName = stFromUtf8Mac(aDirItem->d_name); #else StString aCurrItemName(aDirItem->d_name); #endif addItem(theExtensions, theDeep, aSearchFolderPath, aCurrItemName); } closedir(aSearchedFolder); #endif // perform sorting... sort(); }
bool StProcess::execProcess(const StString& theExecutablePath, const StArray<StString>& theArguments) { if(!StFileNode::isFileExists(theExecutablePath)) { return false; } #ifdef _WIN32 // convert to wide strings StStringUtfWide anExecutablePathW = theExecutablePath.toUtfWide(); StArrayList<StStringUtfWide> anArgumentsW(theArguments.size()); StStringUtfWide aSplitter = ' '; StStringUtfWide aCmdLineW = StStringUtfWide('\"') + anExecutablePathW + StStringUtfWide("\" "); for(size_t anElem = 0;;) { // TODO (Kirill Gavrilov#9) we should probably quote arguments with spaces... // how to correctly deal this in the same way for UNIX / Windows? aCmdLineW += theArguments[anElem++].toUtfWide(); if(anElem >= theArguments.size()) { break; } aCmdLineW += aSplitter; } STARTUPINFOW aStartInfo; PROCESS_INFORMATION aProcessInfo; stMemSet(&aStartInfo, 0, sizeof(aStartInfo)); aStartInfo.cb = sizeof(aStartInfo); stMemSet(&aProcessInfo, 0, sizeof(aProcessInfo)); // start the process if(!CreateProcessW(anExecutablePathW.toCString(), (wchar_t* )aCmdLineW.toCString(), NULL, NULL, FALSE, 0, NULL, NULL, &aStartInfo, &aProcessInfo)) { return false; } // close process and thread handles CloseHandle(aProcessInfo.hProcess); CloseHandle(aProcessInfo.hThread); return true; #else char** anArgList = new char*[theArguments.size() + 2]; anArgList[0] = (char* )theExecutablePath.toCString(); for(size_t anArgId = 0; anArgId < theArguments.size(); ++anArgId) { anArgList[anArgId + 1] = (char* )theArguments.getValue(anArgId).toCString(); } anArgList[theArguments.size() + 1] = NULL; pid_t aChildPid = vfork(); if(aChildPid == -1) { // fork fail delete[] anArgList; return false; } else if(aChildPid != 0) { // parent process give the control only after child // calls exit() or exec() functions delete[] anArgList; return true; } // child process execv(theExecutablePath.toCString(), anArgList); // fail _exit(1); #endif }
bool StFreeImage::load(const StString& theFilePath, ImageType theImageType, uint8_t* theDataPtr, int theDataSize) { if(!StFreeImage::init()) { setState("FreeImage library is not initialized"); return false; } // reset current data StImage::nullify(); setState(); close(); FREE_IMAGE_FORMAT aFIF = convertToFIF(theImageType); if(theDataPtr != NULL && theDataSize != 0 && aFIF != FIF_UNKNOWN) { FIMEMORY* aFIMemory = FreeImage_OpenMemory(theDataPtr, theDataSize); if(aFIMemory == NULL) { setState("FreeImage library, internal error"); return false; } myDIB = FreeImage_LoadFromMemory(aFIF, aFIMemory, 0); FreeImage_CloseMemory(aFIMemory); } else { // check the file signature and deduce its format #if defined(_WIN32) StStringUtfWide aFilePathWide = theFilePath.toUtfWide(); aFIF = FreeImage_GetFileType(aFilePathWide.toCString(), 0); #else aFIF = FreeImage_GetFileType(theFilePath.toCString(), 0); #endif if(aFIF == FIF_UNKNOWN) { // no signature? try to guess the file format from the file extension #if defined(_WIN32) aFIF = FreeImage_GetFIFFromFilename(aFilePathWide.toCString()); #else aFIF = FreeImage_GetFIFFromFilename(theFilePath.toCString()); #endif } if((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading(aFIF)) { setState("FreeImage library does not support image format"); return false; } int loadFlags = 0; if(aFIF == FIF_GIF) { // GIF_PLAYBACK - 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading loadFlags = 2; } else if(aFIF == FIF_ICO) { // ICO_MAKEALPHA - convert to 32bpp and create an alpha channel from the AND-mask when loading loadFlags = 1; } #if defined(_WIN32) myDIB = FreeImage_Load(aFIF, aFilePathWide.toCString(), loadFlags); #else myDIB = FreeImage_Load(aFIF, theFilePath.toCString(), loadFlags); #endif } if(myDIB == NULL) { setState("FreeImage library, loading file failed"); return false; } StImagePlane::ImgFormat stImgFormat = convertFromFreeFormat(FreeImage_GetImageType(myDIB), FreeImage_GetColorType(myDIB), FreeImage_GetBPP(myDIB)); if(stImgFormat == StImagePlane::ImgUNKNOWN) { setState(StString("StFreeImage, image format ") + FreeImage_GetImageType(myDIB) + ", " + FreeImage_GetColorType(myDIB) + " doesn't supported by application"); close(); return false; } setColorModelPacked(stImgFormat); changePlane(0).initWrapper(stImgFormat, FreeImage_GetBits(myDIB), FreeImage_GetWidth(myDIB), FreeImage_GetHeight(myDIB), FreeImage_GetPitch(myDIB)); // FreeImage data always bottom-up... changePlane(0).setTopDown(false); // set debug information StString aDummy, aFileName; StFileNode::getFolderAndFile(theFilePath, aDummy, aFileName); setState(StString("FreeImage library, loaded image '") + aFileName + "' " + getDescription()); // we should not close the file because we create a wrapper over FreeImage native object return true; }
bool StFreeImage::save(const StString& theFilePath, ImageType theImageType, StFormatEnum ) { if(!StFreeImage::init()) { setState("FreeImage library is not initialized"); return false; } FREE_IMAGE_FORMAT aFIF = convertToFIF(theImageType); if(aFIF == FIF_UNKNOWN) { setState("FreeImage library, not supported image file format"); return false; } StImage stSaveImage; if(getColorModel() != ImgColor_RGB && getColorModel() != ImgColor_RGBA && getColorModel() != ImgColor_GRAY) { // convert from YUV and so on if(!stSaveImage.initRGB(*this)) { setState("StFreeImage, only RGB image could be saved"); return false; } } else { stSaveImage.initWrapper(*this); } const StImagePlane& stImgPlane = stSaveImage.getPlane(); FREE_IMAGE_TYPE aSaveFormatFI = FIT_UNKNOWN; if(!convertToFreeFormat(stImgPlane.getFormat(), aSaveFormatFI)) { setState("StFreeImage, image format currently not supported"); return false; } // allocate FreeImage native structure FIBITMAP* aSaveDIB = FreeImage_AllocateT(aSaveFormatFI, (int )stImgPlane.getSizeX(), (int )stImgPlane.getSizeY(), (unsigned )stImgPlane.getSizePixelBytes() * 8, 0, 0, 0); if(aSaveDIB == NULL) { setState("FreeImage library, internal error"); FreeImage_Unload(aSaveDIB); return false; } // wrapper the created data StImagePlane stImgPlaneSave; StImagePlane::ImgFormat stImgFormatSave = convertFromFreeFormat(FreeImage_GetImageType(aSaveDIB), FreeImage_GetColorType(aSaveDIB), FreeImage_GetBPP(aSaveDIB)); stImgPlaneSave.initWrapper(stImgFormatSave, FreeImage_GetBits(aSaveDIB), FreeImage_GetWidth(aSaveDIB), FreeImage_GetHeight(aSaveDIB), FreeImage_GetPitch(aSaveDIB)); // FreeImage data should be bottom-up... stImgPlaneSave.setTopDown(false); // copy from local structure to the FreeImage structure size_t aRowInc = (( stImgPlaneSave.isTopDown() && stImgPlane.isTopDown()) || (!stImgPlaneSave.isTopDown() && !stImgPlane.isTopDown())) ? 1 : size_t(-1); size_t aRowTo = (aRowInc == 1) ? 0 : (stImgPlane.getSizeY() - 1); for(size_t aRowFrom = 0; aRowFrom < stImgPlane.getSizeY(); ++aRowFrom, aRowTo += aRowInc) { for(size_t aCol = 0; aCol < stImgPlane.getSizeX(); ++aCol) { stMemCpy(stImgPlaneSave.changeData(aRowTo, aCol), stImgPlane.getData(aRowFrom, aCol), stImgPlane.getSizePixelBytes()); } } // now save the image file! #if defined(_WIN32) if(!FreeImage_Save(aFIF, aSaveDIB, theFilePath.toUtfWide().toCString(), 0)) { #else if(!FreeImage_Save(aFIF, aSaveDIB, theFilePath.toCString(), 0)) { #endif setState("FreeImage library, image save failed"); FreeImage_Unload(aSaveDIB); return false; } // free resources FreeImage_Unload(aSaveDIB); // set debug information StString aDummy, aFileName; StFileNode::getFolderAndFile(theFilePath, aDummy, aFileName); setState(StString("FreeImage library, saved image '") + aFileName + "' " + getDescription()); return true; } bool StFreeImage::resize(size_t , size_t ) { return false; }