WidthAndHeight JpgImageInfo::getSize(boost::filesystem::path & aPath) { WidthAndHeight result; FILE *f = fopen(aPath.file_string().data(), "rb"); if (!f) { throw std::runtime_error("cannot open file [" + aPath.file_string() + "]"); } struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, f); ImageInfoException::throwIf(jpeg_read_header(&cinfo, TRUE) == 0, "unable to read jpeg header"); result.width = cinfo.image_width; result.height = cinfo.image_height; jpeg_destroy_decompress(&cinfo); fclose(f); return result; }
size_t IOFactory::loadFile( std::list<Chunk> &ret, const boost::filesystem::path &filename, std::string suffix_override, std::string dialect ) { FileFormatList formatReader; formatReader = getFileFormatList( filename.file_string(), suffix_override, dialect ); const size_t nimgs_old = ret.size(); // save number of chunks const std::string with_dialect = dialect.empty() ? std::string( "" ) : std::string( " with dialect \"" ) + dialect + "\""; if ( formatReader.empty() ) { if( !boost::filesystem::exists( filename ) ) { LOG( Runtime, error ) << util::MSubject( filename ) << " does not exist as file, and no suitable plugin was found to generate data from " << ( suffix_override.empty() ? std::string( "that name" ) : std::string( "the suffix \"" ) + suffix_override + "\"" ); } else if( suffix_override.empty() ) { LOG( Runtime, error ) << "No plugin found to read " << filename.file_string() << with_dialect; } else { LOG( Runtime, error ) << "No plugin supporting the requested suffix " << suffix_override << with_dialect << " was found"; } } else { BOOST_FOREACH( FileFormatList::const_reference it, formatReader ) { LOG( ImageIoDebug, info ) << "plugin to load file" << with_dialect << " " << util::MSubject( filename ) << ": " << it->getName(); try { return it->load( ret, filename.file_string(), dialect ); } catch ( std::runtime_error &e ) { LOG( Runtime, formatReader.size() > 1 ? warning : error ) << "Failed to load " << filename << " using " << it->getName() << with_dialect << " ( " << e.what() << " )"; } } LOG_IF( boost::filesystem::exists( filename ) && formatReader.size() > 1, Runtime, error ) << "No plugin was able to load: " << util::MSubject( filename ) << with_dialect; }
WidthAndHeight TifImageInfo::getSize(boost::filesystem::path & aPath) { WidthAndHeight result; static BOOL bFirstRun = true; if (bFirstRun) { TIFFSetWarningHandler(NULL); TIFFSetErrorHandler(NULL); bFirstRun = false; } TIFF *tif = TIFFOpen(aPath.file_string().data(), "r"); if (!tif) { throw std::runtime_error("cannot open file [" + aPath.file_string() + "]"); } BOOL success = FALSE; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &result.width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &result.height); TIFFClose(tif); return result; }
WidthAndHeight PngImageInfo::getSize(boost::filesystem::path & aPath) { WidthAndHeight result; FILE *fp; fopen_s(&fp, aPath.file_string().data(), "rb"); if (!fp) { throw std::runtime_error("cannot open file [" + aPath.file_string() + "]"); } char header[8]; // 8 is the maximum size that can be checked fread(header, 1, 8, fp); if (png_sig_cmp((png_bytep)header, 0, 8)) { throw std::runtime_error("cannot open file [" + aPath.file_string() + "]"); } // create and initialize the png_struct png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { fclose(fp); throw std::runtime_error("cannot create png structure for file [" + aPath.file_string() + "]"); } // allocate the memory for image information png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); fclose(fp); throw std::exception("cannot create png information structure"); } if (setjmp(png_jmpbuf(png_ptr))) { // free all of the memory associated with the png_ptr and info_ptr png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); fclose(fp); throw std::exception("unexpected error while reading png file information"); } // set up the input control (use standard C streams) png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); // read information from the PNG file png_read_info(png_ptr, info_ptr); result.width = info_ptr->width; result.height = info_ptr->height; // clean up after the read, and free any memory allocated png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); fclose(fp); return result; }
inline void remove_all(const boost::filesystem::path& path) { if (__is_directory(path)) { std::vector<boost::filesystem::path> paths; #if BOOST_FILESYSTEM_VERSION == 2 DIR* dirp = (path.empty() ? ::opendir(".") : ::opendir(path.directory_string().c_str())); #else DIR* dirp = (path.empty() ? ::opendir(".") : ::opendir(path.string().c_str())); #endif if (dirp) { struct dirent* dp; do { errno = 0; if ((dp = readdir(dirp)) != 0) { const std::string path_leaf(dp->d_name); if (path_leaf != "." && path_leaf != "..") paths.push_back(path / path_leaf); } } while (dp); ::closedir(dirp); } std::for_each(paths.begin(), paths.end(), utils::filesystem::remove_all); } #if BOOST_FILESYSTEM_VERSION == 2 if (__exists(path)) ::remove(path.file_string().c_str()); #else if (__exists(path)) ::remove(path.string().c_str()); #endif errno = 0; }
TEST_F(GeneralTest, DISABLED_memcpy_test) { const fs::path filename("/tmp/metadatstoretest"); TCBDB* database_(tcbdbnew()); HANDLE(tcbdbsetcache(database_, 0, 1024)); HANDLE(tcbdbsetcmpfunc(database_,tccmpint64,NULL)); HANDLE(tcbdbopen(database_, filename.file_string().c_str(), BDBOWRITER)); for(int page_size = 10; page_size < 17; ++page_size) { const uint64_t size = 1 << page_size; const uint64_t times = 1 << 10; std::vector<void*> bufs1; const uint64_t num_bufs = 1 << 10; void * b1 = valloc(size); for(uint64_t i = 0; i < num_bufs; ++i) { HANDLE(tcbdbput(database_,&i, sizeof(i),b1, size)); } youtils::wall_timer wt; for(uint64_t i = 0; i < times; ++i) { for(uint64_t j = 0; j < num_bufs; j++) { int _sz = 0; const void* buf = tcbdbget3(database_, &j, sizeof(j), &_sz); ASSERT((unsigned)_sz == size); memcpy(b1, buf, size); ((uint64_t*)b1)[3] = 15; tcbdbput(database_,&j, sizeof(j), buf, size); } } double time = wt.elapsed(); std::cout << "page_size is " << size << std::endl; std::cout << "To copy " << size << " bytes " << times * num_bufs << " times takes " << time << " seconds" << std::endl; std::cout << "this limit voldrv performance to " << (1 << 12) * (times*num_bufs) / ((1 << 21) * time) << "Meg per second" << std::endl ; // for(uint64_t i = 0; i < num_bufs; ++i) // { // free(buf1); // free(buf2); // } } }
/*! * Load all modules in a given path. * This will recurse into sub-directories. * Does not throw, prints to std error. * \param path the filesystem path */ static void load_module_path(const fs::path &path){ if (not fs::exists(path)){ //std::cerr << boost::format("Module path \"%s\" not found.") % path.file_string() << std::endl; return; } //try to load the files in this path if (fs::is_directory(path)){ for( fs::directory_iterator dir_itr(path); dir_itr != fs::directory_iterator(); ++dir_itr ){ load_module_path(dir_itr->path()); } return; } //its not a directory, try to load it try{ load_module(path.file_string()); } catch(const std::exception &err){ std::cerr << boost::format("Error: %s") % err.what() << std::endl; } }
Ruleset::Ruleset(boost::filesystem::path filename) { mDb = NULL; mBeforeReplacementsPtr = NULL; mAfterReplacementsPtr = NULL; mFilename = filename.file_string(); loadDb(filename); };
VirtualDBFVirtualTable::VirtualDBFVirtualTable( const boost::filesystem::path& path, const std::string& codePage ): SQLiteVirtualTable( GetTableName(path), "VirtualDBF(\"" + replace_all_copy(path.file_string(), "\\", "\\\\") + "\"," + codePage + ")" ) {}
inline std::string native_file_string(boost::filesystem::path const& p) { #if BOOST_VERSION >= 104600 return p.string(); #else return p.file_string(); #endif }
WidthAndHeight BmpImageInfo::getSize(boost::filesystem::path & aPath) { WidthAndHeight result; FILE *f; BITMAPINFOHEADER bmi; fopen_s(&f, aPath.file_string().data(), "rb"); if (!f) { throw std::runtime_error("cannot open file [" + aPath.file_string() + "]"); } fseek(f, sizeof(BITMAPFILEHEADER), SEEK_SET); fread(&bmi, 1, sizeof(BITMAPINFOHEADER), f); fclose(f); result.width = bmi.biWidth; result.height = bmi.biHeight; return result; }
BlockData::BlockData(const boost::filesystem::path& block_file_path) { block_data_bytes_ = 0; bytes_ = 0; int file_descriptor = open(block_file_path.file_string().c_str(), O_RDONLY | O_DIRECT); if (file_descriptor < 0) { LOG4CPLUS_ERROR(Loggers::Service(), "Failed to open file "<<block_file_path<<" to read."); return; } block_data_bytes_ = reinterpret_cast<char*>(BlockDataAllocator::Instance().Allocate()); if (!block_data_bytes_) { LOG4CPLUS_ERROR(Loggers::Service(), "Failed to allocate aligned memory buffer."); close(file_descriptor); return; } //read up to MaxBlockSize bytes to the buffer size_t offset(0); int bytes_read(0); int bytes_left(BlockData::MaxBlockSize); do { bytes_read = pread(file_descriptor, block_data_bytes_ + offset, bytes_left, offset); if (bytes_read > 0) { bytes_left -= bytes_read; offset += bytes_read; } } while (bytes_left > 0 && bytes_read > 0); if (bytes_read < 0) { LOG4CPLUS_ERROR(Loggers::Service(), "An error occurred while reading file file "<<block_file_path); close(file_descriptor); BlockDataAllocator::Instance().Free(block_data_bytes_); block_data_bytes_ = 0; return; } close(file_descriptor); bytes_ = offset; }
void display( const bfs::path & p) { long int fc=0,dc=0,ec=0,oc=0; if( bfs::is_directory(p)) { cout<<"In Directory:"<< p.directory_string()<<"\n\n"; bfs::directory_iterator i(p), e; for(i;i!=e;++i) { if(bfs::is_directory( i->status())) { dc++; display(*i); //cout<< i->path().filename()<<"[directory]\n"; } else if(bfs::is_regular_file(i->status())) { fc++; cout<<i->path().filename()<<"[file]\n"; } else { oc++; cout<<i->path().filename()<<"[others]\n"; } } cout<<"File Count:"<<fc<<"\n"; cout<<"Directory COunt"<<dc<<"\n"; cout<<"Others Count"<<oc<<"\n"; } else { cout<<"File Found"<<p.file_string()<<"\n\n"; } }
void *PionPlugin::loadDynamicLibrary(const std::string& plugin_file) { #ifdef PION_WIN32 #ifdef _MSC_VER return LoadLibraryA(plugin_file.c_str()); #else return LoadLibrary(plugin_file.c_str()); #endif #else // convert into a full/absolute/complete path since dlopen() // does not always search the CWD on some operating systems const boost::filesystem::path full_path = boost::filesystem::complete(plugin_file); // NOTE: you must load shared libraries using RTLD_GLOBAL on Unix platforms // due to a bug in GCC (or Boost::any, depending on which crowd you want to believe). // see: http://svn.boost.org/trac/boost/ticket/754 return dlopen(full_path.file_string().c_str(), RTLD_LAZY | RTLD_GLOBAL); #endif }
inline bool __exists(const boost::filesystem::path& path) { if (path.empty()) return false; struct stat statbuf; #if BOOST_FILESYSTEM_VERSION == 2 if (::lstat(path.file_string().c_str(), &statbuf) >= 0) return true; else { errno = 0; return false; } #else if (::lstat(path.string().c_str(), &statbuf) >= 0) return true; else { errno = 0; return false; } #endif }
inline bool __is_directory(const boost::filesystem::path& path) { if (path.empty()) return false; struct stat statbuf; #if BOOST_FILESYSTEM_VERSION == 2 if (::lstat(path.file_string().c_str(), &statbuf) >= 0) return ! S_ISLNK(statbuf.st_mode) && S_ISDIR(statbuf.st_mode); else { errno = 0; return false; } #else if (::lstat(path.string().c_str(), &statbuf) >= 0) return ! S_ISLNK(statbuf.st_mode) && S_ISDIR(statbuf.st_mode); else { errno = 0; return false; } #endif }
void OMW::Engine::addResourcesDirectory (const boost::filesystem::path& path) { mOgre.getRoot()->addResourceLocation (path.file_string(), "FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true); }