inline FgString fgNfs(const FgString & path) { #ifdef _WIN32 return path.replace('/','\\'); #else return path.replace('\\','/'); #endif }
static void Construct() { { FgString s; FGASSERT(s.length() == 0); } { FgString s("12345"); FGASSERT(s.length() == 5);} { FgString s(L"12345"); FGASSERT(s.length() == 5); } { FgString s1("12345"); FgString s2(s1); FGASSERT(s1.length() == s2.length() && s1.length() == 5); } }
static bool fffSave3dsFile(const FgString &name, const FffMultiObjectC &model) { FgPath path(name); path.ext = "3ds"; FgString fname = path.str(); #ifdef _WIN32 FILE *fptr = _wfopen(fname.as_wstring().c_str(),L"wb,ccs=UNICODE"); #else FILE *fptr = fopen(fname.m_str.c_str(),"wb"); #endif if (!fptr) { return false; } unsigned short id = M3DMAGIC; long chunkSize = sizeof(unsigned short) + sizeof(long); long chunkStartPos = ftell(fptr); if (!fffWriteChunkHeader_local(fptr,id,chunkSize)) { fclose(fptr); return false; } { id = M3D_VERSION; long version = 3; long verChunkSize = sizeof(unsigned short) + sizeof(long) + sizeof(long); if (!fffWriteChunkHeader_local(fptr,id,verChunkSize)) { fclose(fptr); return false; } if (fwrite(&version,sizeof(long),1,fptr) != 1) { fclose(fptr); return false; } chunkSize += verChunkSize; } long mdataChunkSize=0; if (!fffWriteMdataChunk_local(fptr,mdataChunkSize,model)) { fclose(fptr); return false; } chunkSize += mdataChunkSize; if (!fffUpdateChunkSizeInfo_local(fptr,chunkSize,chunkStartPos)) { fclose(fptr); return false; } fclose(fptr); return true; }
static void Assign() { { FgString s; s = "12345"; FGASSERT(s.length() == 5); } { FgString s; s = L"12345"; FGASSERT(s.length() == 5); } { FgString s; s = L"12345"; FgString s1; s1 = s; FGASSERT(s1.length() == 5); } }
void fgLoadImgAnyFormat( const FgString & fname, FgImgF & img) { if (!fgFileReadable(fname)) fgThrow("Unable to read file",fname); fgEnsureMagick(); FgScopePtr<ExceptionInfo> exception(AcquireExceptionInfo(),DestroyExceptionInfo); FgScopePtr<ImageInfo> image_info(CloneImageInfo(0),DestroyImageInfo); (void) strcpy(image_info->filename,fname.as_utf8_string().c_str()); // TODO: Do we need to convert into a specific colourspace? Image *imgPtr = ReadImage(image_info.get(),exception.get()); if (imgPtr == 0) // exception.description is NULL fgThrow("Unable to read image file",fname + ": " + exception->reason); FgScopeGuard sg0(boost::bind(DestroyImage,imgPtr)); FgScopePtr<CacheView> view(AcquireCacheView(imgPtr),DestroyCacheView); img.resize(uint(imgPtr->columns),uint(imgPtr->rows)); for(uint row = 0; row < imgPtr->rows; ++row) { for(uint column = 0; column < imgPtr->columns; ++column) { PixelPacket pixel; FGASSERT(MagickTrue == GetOneCacheViewAuthenticPixel(view.get(),column,row,&pixel,exception.get())); img.elem(column,row) = pixel.red; // All channels same value if single-channel read } } }
FgImg4UC fgLoadImg4UC(const FgString & fname) { FgImg4UC ret; if (!fgFileReadable(fname)) fgThrow("Unable to read file",fname); fgEnsureMagick(); FgScopePtr<ExceptionInfo> exception(AcquireExceptionInfo(),DestroyExceptionInfo); FgScopePtr<ImageInfo> image_info(CloneImageInfo(0),DestroyImageInfo); (void) strcpy(image_info->filename,fname.as_utf8_string().c_str()); // TODO: Do we need to convert into a specific colourspace? Image *imgPtr = ReadImage(image_info.get(),exception.get()); if (imgPtr == 0) // exception.description is NULL fgThrow("Unable to read image file",fname + ": " + exception->reason); FgScopeGuard sg0(boost::bind(DestroyImage,imgPtr)); FgScopePtr<CacheView> view(AcquireCacheView(imgPtr),DestroyCacheView); ret.resize(uint(imgPtr->columns),uint(imgPtr->rows)); for(uint row= 0; row < imgPtr->rows; ++row) { for(uint column = 0; column < imgPtr->columns; ++column) { PixelPacket pixel; FGASSERT(MagickTrue == GetOneCacheViewAuthenticPixel(view.get(),column,row,&pixel,exception.get())); ret.elem(column,row)[0] = uchar(pixel.red); ret.elem(column,row)[1] = uchar(pixel.green); ret.elem(column,row)[2] = uchar(pixel.blue); ret.elem(column,row)[3] = 255 - uchar(pixel.opacity); } } return ret; }
void fgRemoveFile(const FgString & fname) { string utf8 = fname.as_utf8_string(); if (remove(utf8.c_str()) != 0) fgThrow("Unable to remove file",fname); }
bool fgSetCurrentDir( const FgString & dir, bool throwOnFail) { string sdir = dir.as_utf8_string(); bool ret = (chdir(sdir.c_str()) == 0); if ((!ret) && throwOnFail) fgThrow("Unable to set current directory to",dir); return ret; }
void fgSaveImgAnyFormat( const FgString & fname, const FgImgRgbaUb & img) { FGASSERT(fname.length() > 0); fgEnsureMagick(); FgScopePtr<ImageInfo> image_info(CloneImageInfo(0),DestroyImageInfo); FgScopePtr<ExceptionInfo> exception(AcquireExceptionInfo(),DestroyExceptionInfo); FgScopePtr<Image> image(ConstituteImage(img.width(),img.height(),"RGBA", CharPixel, img.dataPtr(), exception.get()), DestroyImage); (void) strcpy(image_info->filename,fname.as_utf8_string().c_str()); FGASSERT( MagickTrue == WriteImages(image_info.get(),image.get(),fname.as_utf8_string().c_str(),exception.get())); }
FgDgn<T> addInput(const T & defaultVal,const FgString & uid,bool binary=false) { FgDgn<T> node = dg.addNode(defaultVal,uid.as_ascii()); readNode(node,uid,binary); Input inp; inp.nodeIdx = node.idx(); inp.save = boost::bind(&FgGuiGraph::writeNode<T>,this,node,uid,binary); inp.defaultVal = FgVariant(defaultVal); m_inputSaves.push_back(inp); return node; }
bool fgRemoveDirectory( const FgString & dir, bool throwOnFail) { string sdir = dir.as_utf8_string(); int ret = rmdir(sdir.c_str()); if (ret == 0) return true; if (throwOnFail) fgThrow("Unable to remove directory",dir); return false; }
static void textToVal(const FgString & text,FgDgn<double> valN,FgVect2D bounds) { double userVal = fgFromString<double>(text.as_ascii()); // Behaviour for non-numerical strings varies even between debug and release, the // latter returning very small values for null string: if (userVal < bounds[0]) userVal = bounds[0]; if (userVal > bounds[1]) userVal = bounds[1]; g_gg.setVal(valN,userVal); }
LRESULT wndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { switch (message) { case WM_CREATE: { //fgout << fgnl << "Button::WM_CREATE"; // This is the first place we get the hwnd for this instance since this callback // happens before 'fgCreateChild' above returns: hwndThis = hwnd; hwndButton = CreateWindowEx(0, TEXT("button"), // Standard controls class name for all buttons m_api.label.as_wstring().c_str(), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0,0,0,0, // Will be sent MOVEWINDOW messages. hwnd, HMENU(0), s_fgGuiWin.hinst, NULL); // No WM_CREATE parameter FGASSERTWIN(hwndButton != 0); return 0; } case WM_SIZE: { int wid = LOWORD(lParam); int hgt = HIWORD(lParam); if (wid*hgt > 0) { //fgout << fgnl << "Button::WM_SIZE: " << wid << "," << hgt; int buttonHgt = fgMin(int(getMinSize()[1]),hgt), vspace = hgt - buttonHgt; MoveWindow(hwndButton,0,vspace/2,wid,buttonHgt,TRUE); } return 0; } case WM_COMMAND: { WORD ident = LOWORD(wParam); WORD code = HIWORD(wParam); if (code == 0) { FGASSERT(ident == 0); try { m_api.action(); g_gg.updateScreen(); } catch(FgException const & e) { FgString txt = e.tr_message() + "\n"; fgout << txt; MessageBox(hwnd,txt.as_wstring().c_str(),L"Error",MB_OK); } catch(std::exception const & e) { FgString txt = FgString("Internal Program Error (std::exception):\n") + e.what() + "\n"; fgout << txt; MessageBox(hwnd,txt.as_wstring().c_str(),L"Error",MB_OK); } catch(...) { FgString txt = FgString("Internal Program Error (unknown exception).\n"); fgout << txt; MessageBox(hwnd,txt.as_wstring().c_str(),L"Error",MB_OK); } } return 0; } //case WM_PAINT: //fgout << fgnl << "Button::WM_PAINT"; } return DefWindowProc(hwnd,message,wParam,lParam); }
// Can't put this inline without include file recursive dependency: bool FgString::endsWith(const FgString & str) const {return fgEndsWith(as_utf32(),str.as_utf32()); }
// Works for both files and directories: inline std::time_t fgLastWriteTime(const FgString & path) {return boost::filesystem::last_write_time(path.ns()); }
inline bool fgRemove(const FgString & fname) {return boost::filesystem::remove(fname.ns()); }
inline bool fgExists(const FgString & fname) {return boost::filesystem::exists(fname.ns()); }
// Recursive descent remove: inline size_t fgRemoveAll(const FgString & fname) {return size_t(boost::filesystem::remove_all(fname.ns())); }
inline void fgRename(const FgString & from,const FgString & to) {return boost::filesystem::rename(from.ns(),to.ns()); }
Dir(const FgString & dirName) {handle = opendir(dirName.as_utf8_string().c_str()); }
bool fgCreateDirectory(const FgString & dir) { string sdir = dir.as_utf8_string(); return (mkdir(sdir.c_str(),0777) == 0); }
// is_directory doesn't throw: inline bool fgIsDirectory(const FgString & name) {return boost::filesystem::is_directory(name.ns()); }