void __for_each_file(const std::string& dirname, const std::string& suff, const List& order_list, const List& disable_list, Function f) { GDir *dir = g_dir_open(dirname.c_str(), 0, NULL); if (dir) { const gchar *filename; while ((filename = g_dir_read_name(dir))!=NULL) { std::string fullfilename(dirname+G_DIR_SEPARATOR_S+filename); if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR)) //folder __for_each_file(fullfilename, suff, order_list, disable_list, f); else if (g_str_has_suffix(filename, suff.c_str()) && std::find(order_list.begin(), order_list.end(), fullfilename)==order_list.end()) { bool disable=std::find(disable_list.begin(), disable_list.end(), fullfilename)!=disable_list.end(); f(fullfilename, disable); } } g_dir_close(dir); } }
void ParCheckerMock::CorruptFile(const char* filename, int offset) { std::string fullfilename(TestUtil::WorkingDir() + "/" + filename); FILE* file = fopen(fullfilename.c_str(), FOPEN_RBP); REQUIRE(file != nullptr); fseek(file, offset, SEEK_SET); char b = 0; int written = fwrite(&b, 1, 1, file); REQUIRE(written == 1); fclose(file); }
Library::Error Library::load(const char exename[], const char path[]) { if (std::strchr(path,',') != nullptr) { std::string p(path); while (p.find(",") != std::string::npos) { const std::string::size_type pos = p.find(","); const Error &e = load(exename, p.substr(0,pos).c_str()); if (e.errorcode != OK) return e; p = p.substr(pos+1); } if (!p.empty()) return load(exename, p.c_str()); return Error(); } // open file.. tinyxml2::XMLDocument doc; tinyxml2::XMLError error = doc.LoadFile(path); if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { // failed to open file.. is there no extension? std::string fullfilename(path); if (Path::getFilenameExtension(fullfilename) == "") { fullfilename += ".cfg"; error = doc.LoadFile(fullfilename.c_str()); } if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { // Try to locate the library configuration in the installation folder.. #ifdef CFGDIR const std::string cfgfolder(CFGDIR); #else if (!exename) return Error(FILE_NOT_FOUND); const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(exename)) + "cfg"); #endif const char *sep = (!cfgfolder.empty() && cfgfolder[cfgfolder.size()-1U]=='/' ? "" : "/"); const std::string filename(cfgfolder + sep + fullfilename); error = doc.LoadFile(filename.c_str()); } } return (error == tinyxml2::XML_NO_ERROR) ? load(doc) : Error(error == tinyxml2::XML_ERROR_FILE_NOT_FOUND ? FILE_NOT_FOUND : BAD_XML); }
CachedTexture::CachedTexture( const string &f_,int flags,int w,int h,int first,int cnt ){ string f=f_; if( f.substr(0,2)==".\\" ) f=f.substr(2); if( path.size() ){ string t=path+tolower( filenamefile( f ) ); if( rep=findRep( t,flags,w,h,first,cnt ) ) return; rep=d_new Rep( t,flags,w,h,first,cnt ); if( rep->frames.size() ){ rep_set.insert( rep ); return; } delete rep; } string t=tolower( fullfilename( f ) ); if( rep=findRep( t,flags,w,h,first,cnt ) ) return; rep=d_new Rep( t,flags,w,h,first,cnt ); rep_set.insert( rep ); }
bool Library::load(const char exename[], const char path[]) { if (std::strchr(path,',') != NULL) { bool ret = true; std::string p(path); while (p.find(",") != std::string::npos) { const std::string::size_type pos = p.find(","); ret &= load(exename, p.substr(0,pos).c_str()); p = p.substr(pos+1); } if (!p.empty()) ret &= load(exename, p.c_str()); return ret; } // open file.. tinyxml2::XMLDocument doc; tinyxml2::XMLError error = doc.LoadFile(path); if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { // failed to open file.. is there no extension? std::string fullfilename(path); if (Path::getFilenameExtension(fullfilename) == "") { fullfilename += ".cfg"; error = doc.LoadFile(fullfilename.c_str()); } if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) { // Try to locate the library configuration in the installation folder.. const std::string installfolder = Path::fromNativeSeparators(Path::getPathFromFilename(exename)); const std::string filename = installfolder + "cfg/" + fullfilename; error = doc.LoadFile(filename.c_str()); } } return (error == tinyxml2::XML_NO_ERROR) && load(doc); }
bool TreeDict::load(const std::string& ifofilename) { gulong tdxfilesize; if (!load_ifofile(ifofilename, &tdxfilesize)) return false; std::string fullfilename(ifofilename); fullfilename.replace(fullfilename.length()-sizeof("ifo")+1, sizeof("ifo")-1, "dict.dz"); if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) { dictdzfile.reset(new dictData); if (!dictdzfile->open(fullfilename, 0)) { //g_print("open file %s failed!\n",fullfilename); return false; } } else { fullfilename.erase(fullfilename.length()-sizeof(".dz")+1, sizeof(".dz")-1); dictfile = fopen(fullfilename.c_str(),"rb"); if (!dictfile) { //g_print("open file %s failed!\n",fullfilename); return false; } } fullfilename=ifofilename; fullfilename.replace(fullfilename.length()-sizeof("ifo")+1, sizeof("ifo")-1, "tdx.gz"); gchar *buffer= NULL; if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) { gzFile in; in = gzopen(fullfilename.c_str(),"rb"); if (in == NULL) { //g_print("Open file %s failed!\n",idxfilename); return false; } buffer = (gchar *)g_malloc(tdxfilesize); gulong len; len = gzread(in, buffer, tdxfilesize); if (len < 0) { g_free(buffer); return false; } gzclose(in); if (len != tdxfilesize) { g_free(buffer); return false; } } else { fullfilename.erase(fullfilename.length()-sizeof(".gz")+1, sizeof(".gz")-1); FILE *file; if (!(file = fopen (fullfilename.c_str(), "rb"))) { //g_print("Open file %s failed!\n",fullfilename); return false; } buffer = (gchar *)g_malloc(tdxfilesize); gulong read_len; read_len = fread(buffer, 1, tdxfilesize, file); fclose(file); if (read_len!=tdxfilesize) { g_free(buffer); return false; } } gchar *tmp_buffer = buffer; load_model(&tmp_buffer, NULL, 1); // tmp_buffer will be changed. g_free(buffer); return true; }
bool mdk_dict::load(const std::string& ifofilename) { if (! load_ifo(ifofilename.c_str())) return false; std::string fullfilename(ifofilename); fullfilename.replace(fullfilename.length() - sizeof("ifo") + 1, sizeof("ifo") - 1, "dict.dz"); if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) { char extract_cmd[256]; sprintf(extract_cmd, "/usr/bin/gunzip -S .dz %s", fullfilename.c_str()); system(extract_cmd); } fullfilename.erase(fullfilename.length() - sizeof(".dz") + 1, sizeof(".dz") - 1); dictfile = fopen(fullfilename.c_str(), "rb"); if (! dictfile) { g_print("open file %s failed!\n", fullfilename.c_str()); return false; } fullfilename = ifofilename; fullfilename.replace(fullfilename.length() - sizeof("ifo")+1, sizeof("ifo") - 1, "idx.gz"); if (! g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) fullfilename.erase(fullfilename.length() - sizeof(".gz") + 1, sizeof(".gz") - 1); index = new mdk_index(); if (! index->load(fullfilename.c_str(), wordcount, index_file_size)) return false; bool has_res = false; gchar *dirname = g_path_get_dirname(ifofilename.c_str()); fullfilename = dirname; fullfilename += G_DIR_SEPARATOR_S "res"; if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR)) { has_res = true; } else { fullfilename = dirname; fullfilename += G_DIR_SEPARATOR_S "res.rifo"; if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) { has_res = true; } } if (has_res) { storage = new ResourceStorage(); bool failed = storage->load(dirname); if (failed) { delete storage; storage = NULL; } } g_free(dirname); return true; }
static int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password, const char *basedir) { char filename_inzip[256]; char* filename_withoutpath; char* filename_withpath; char* p; int err = UNZ_OK; FILE *fout = NULL; void* buf; uInt size_buf; unz_file_info file_info; err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); if (err != UNZ_OK) { // printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); return err; } size_buf = WRITEBUFFERSIZE; buf = (void*) malloc(size_buf); if (buf == NULL) { // printf("Error allocating memory\n"); return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; filename_withpath = fullfilename(basedir, filename_inzip); while ((*p) != '\0') { if (((*p) == '/') || ((*p) == '\\')) filename_withoutpath = p + 1; p++; } if ((*filename_withoutpath) == '\0') { if ((*popt_extract_without_path) == 0) { // Fix the path, this will fail if the directoryname is the same as the first filename in the zip char *path = (char *) malloc(strlen(filename_withpath)); strcpy(path, filename_withpath); char *ptr = strstr(path, filename_withoutpath); *ptr = '\0'; // printf("creating directory: %s\n",path); mymkdir(path); free(path); } } else { char* write_filename; int skip = 0; if ((*popt_extract_without_path) == 0) write_filename = filename_withpath; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf, password); if (err != UNZ_OK) { // printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); } if (((*popt_overwrite) == 0) && (err == UNZ_OK)) { char rep = 0; FILE* ftestexist; ftestexist = fopen(write_filename, "rb"); if (ftestexist != NULL) { fclose(ftestexist); do { char answer[128]; int ret; // printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); ret = scanf("%1s", answer); if (ret != 1) { exit(EXIT_FAILURE); } rep = answer[0]; if ((rep >= 'a') && (rep <= 'z')) rep -= 0x20; } while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); } if (rep == 'N') skip = 1; if (rep == 'A') *popt_overwrite = 1; } if ((skip == 0) && (err == UNZ_OK)) { fout = fopen(write_filename, "wb"); /* some zipfile don't contain directory alone before file */ if ((fout == NULL) && ((*popt_extract_without_path) == 0) && (filename_withoutpath != (char*) filename_inzip)) { char c = *(filename_withoutpath - 1); *(filename_withoutpath - 1) = '\0'; // Fix the path, this will fail if the directoryname is the same as the first filename in the zip char *path = (char *) malloc(strlen(write_filename)); strcpy(path, write_filename); char *ptr = strstr(path, filename_withoutpath); *ptr = '\0'; makedir(path); free(path); *(filename_withoutpath - 1) = c; fout = fopen(write_filename, "wb"); } if (fout == NULL) { // printf("error opening %s\n",write_filename); } } if (fout != NULL) { // printf(" extracting: %s\n",write_filename); do { err = unzReadCurrentFile(uf, buf, size_buf); if (err < 0) { // printf("error %d with zipfile in unzReadCurrentFile\n",err); break; } if (err > 0) if (fwrite(buf, err, 1, fout) != 1) { // printf("error in writing extracted file\n"); err = UNZ_ERRNO; break; } } while (err > 0); if (fout) fclose(fout); } if (err == UNZ_OK) { err = unzCloseCurrentFile(uf); if (err != UNZ_OK) { // printf("error %d with zipfile in unzCloseCurrentFile\n",err); } } else unzCloseCurrentFile(uf); /* don't lose the error */ } free(filename_withpath); free(buf); return err; }
gxFont *gxGraphics::loadFont( const string &f,int height,int flags ){ int bold=flags & gxFont::FONT_BOLD ? FW_BOLD : FW_REGULAR; int italic=flags & gxFont::FONT_ITALIC ? 1 : 0; int underline=flags & gxFont::FONT_UNDERLINE ? 1 : 0; int strikeout=0; string t; int n=f.find('.'); if( n!=string::npos ){ t=fullfilename(f); if( !font_res.count(t) && AddFontResource( t.c_str() ) ) font_res.insert( t ); t=filenamefile( f.substr(0,n) ); }else{ t=f; } //save and turn off font smoothing.... BOOL smoothing=FALSE; SystemParametersInfo( SPI_GETFONTSMOOTHING,0,&smoothing,0 ); SystemParametersInfo( SPI_SETFONTSMOOTHING,FALSE,0,0 ); HFONT hfont=CreateFont( height,0,0,0, bold,italic,underline,strikeout, ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE,t.c_str() ); if( !hfont ){ //restore font smoothing SystemParametersInfo( SPI_SETFONTSMOOTHING,smoothing,0,0 ); return 0; } HDC hdc=CreateCompatibleDC( 0 ); HFONT pfont=(HFONT)SelectObject( hdc,hfont ); TEXTMETRIC tm={0}; if( !GetTextMetrics( hdc,&tm ) ){ SelectObject( hdc,pfont ); DeleteDC( hdc ); DeleteObject( hfont ); SystemParametersInfo( SPI_SETFONTSMOOTHING,smoothing,0,0 ); return 0; } height=tm.tmHeight; int first=tm.tmFirstChar,last=tm.tmLastChar; int sz=last-first+1; int *offs=d_new int[sz]; int *widths=d_new int[sz]; int *as=d_new int[sz]; //calc size of canvas to hold font. int x=0,y=0,max_x=0; for( int k=0;k<sz;++k ){ char t=k+first; SIZE sz; GetTextExtentPoint32( hdc,&t,1,&sz ); int w=sz.cx; as[k]=0; ABC abc; if( GetCharABCWidths( hdc,t,t,&abc ) ){ if( abc.abcA<0 ){ as[k]=ceil(-abc.abcA); w+=as[k]; } if( abc.abcC<0 ) w+=ceil(-abc.abcC); } if( x && x+w>getWidth() ){ x=0;y+=height; } offs[k]=(x<<16)|y; widths[k]=w; x+=w;if( x>max_x ) max_x=x; } SelectObject( hdc,pfont ); DeleteDC( hdc ); int cw=max_x,ch=y+height; if( gxCanvas *c=createCanvas( cw,ch,0 ) ){ ddSurf *surf=c->getSurface(); HDC surf_hdc; if( surf->GetDC( &surf_hdc )>=0 ){ HFONT pfont=(HFONT)SelectObject( surf_hdc,hfont ); SetBkColor( surf_hdc,0x000000 ); SetTextColor( surf_hdc,0xffffff ); for( int k=0;k<sz;++k ){ int x=(offs[k]>>16)&0xffff,y=offs[k]&0xffff; char t=k+first; RECT rect={x,y,x+widths[k],y+height}; ExtTextOut( surf_hdc,x+as[k],y,ETO_CLIPPED,&rect,&t,1,0 ); } SelectObject( surf_hdc,pfont ); surf->ReleaseDC( surf_hdc ); DeleteObject( hfont ); delete[] as; c->backup(); gxFont *font=d_new gxFont( this,c,tm.tmMaxCharWidth,height,first,last+1,tm.tmDefaultChar,offs,widths ); font_set.insert( font ); //restore font smoothing SystemParametersInfo( SPI_SETFONTSMOOTHING,smoothing,0,0 ); return font; }else{