24 #include <boost/filesystem/path.hpp>
25 #include <boost/filesystem/operations.hpp>
26 #include <boost/bind.hpp>
35 #ifndef _LARGEFILE_SOURCE
36 #define _LARGEFILE_SOURCE
37 #endif // _LARGEFILE_SOURCE
58 boost::filesystem::path p( path, boost::filesystem::native );
63 ret = boost::filesystem::exists( p );
80 boost::filesystem::path p( path, boost::filesystem::native );
83 if( !boost::filesystem::exists( p ) ) {
88 return boost::filesystem::is_directory( p );
95 LOG_ERROR( FMT(
"The file path is null." ) );
106 LOG_ERROR_CODE( FMT(
"The path is a directory. A file should be specified. [%s]", path ),
ERR_PATH_IS_DIRECTORY );
121 boost::filesystem::path p( path, boost::filesystem::native );
124 std::string name = p.leaf().string();
136 boost::filesystem::path p( path, boost::filesystem::native );
139 if( p.has_branch_path() ) {
144 return std::string(
"" );
148 std::string
getpath(
const char* dir,
const char* file ) {
151 return std::string(
"" );
154 return std::string( file );
157 return std::string( dir );
161 boost::filesystem::path filePath( file, boost::filesystem::native );
163 std::string mergeFileName = FMT(
"%s%s%s", dir,
DIR_SEPARATOR, file );
164 boost::filesystem::path mergePath( mergeFileName.c_str(), boost::filesystem::native );
167 boost::filesystem::path p = filePath.is_complete() ? filePath : mergePath;
169 std::string ret = p.string();
183 boost::filesystem::path p( path, boost::filesystem::native );
186 boost::filesystem::path absPath = boost::filesystem::absolute( p );
188 return absPath.string();
194 boost::filesystem::path p(
getpath( dir, file ), boost::filesystem::native );
196 std::string ret = p.string();
200 if( p.is_complete() ) {
210 boost::filesystem::path p( file, boost::filesystem::native );
214 return p.is_complete();
226 for(
int i = 0; i < argc; i++ ){
235 LOG_WARN( FMT(
"\"setarguments\" function has not been called yet." ) );
245 LOG_WARN( FMT(
"\"setarguments\" function has not been called yet." ) );
249 if( index < 0 || index >= (
int)
g_arguments.size() ) {
260 return std::string(
"" );
264 std::string p( path );
265 size_t pos = p.find_last_of(
"." );
266 if( pos == p.npos ) {
270 return p.substr( pos + 1 );
277 return std::string(
"" );
286 _dupenv_s( &v, &size, name );
288 value = std::string( v );
292 char* v = getenv( name );
294 value = std::string( v );
309 LOG_WARN( FMT(
"\"setarguments\" function has not been called yet." ) );
310 return std::string(
"" );
315 std::string fileName = program;
317 #ifndef __unix // Windows
319 std::string ext =
getext( fileName.c_str() );
321 fileName.append(
".exe" );
326 if( strcmp( program.c_str(),
g_arguments[0].c_str() ) == 0 ) {
328 #ifdef __unix // unix
335 std::vector<std::string> paths;
342 #ifndef __unix // Windows
343 paths.insert(paths.begin(), std::string(
"."));
347 for(
unsigned int xx = 0;
g_moduleFile.empty() && xx < paths.size(); xx++) {
349 if( !paths[xx].empty() ) {
351 std::string pgPath =
getabsolutepath( paths[xx].c_str(), fileName.c_str() );
385 if( homeDir.empty() ) {
391 homeDir = homeDrive + homePath;
399 FILE*
fileopen(
const char* path,
const char* mode ) {
404 errno_t ret = fopen_s( &fp, path, mode );
409 fp = fopen( path, mode );
416 void fileseek( FILE* fp,
long long offset,
int origin ) {
419 _fseeki64( fp, offset, origin );
421 fseeko64( fp, offset, origin );
429 pos = _ftelli64( fp );
431 pos = ftello64( fp );
438 bool copyfile(
const char* src,
const char* dst ) {
440 char buff[ 0x10000 ];
443 FILE* srcFp =
fileopen( src,
"rb" );
444 if( srcFp ==
NULL ) {
448 FILE* dstFp =
fileopen( dst,
"wb" );
449 if( dstFp ==
NULL ) {
455 while( ( len = fread( buff, 1, 0x10000, srcFp ) ) > 0 ) {
456 fwrite( buff, 1, len, dstFp );
473 ret = _unlink( path );
475 ret = unlink( path );
479 LOG_ERROR( FMT(
"Failed to remove the file. [%s]",
NVL( path,
"" ) ) );
491 ret = _mkdir( path );
493 ret = mkdir( path, 0755 );
497 LOG_ERROR( FMT(
"Failed to create the directory. [%s]",
NVL( path,
"" ) ) );
506 boost::filesystem::path p( path, boost::filesystem::native );
507 boost::filesystem::directory_iterator end;
510 if( !boost::filesystem::exists( p ) ) {
511 LOG_WARN( FMT(
"%s does not exist.", path ) );
517 if( boost::filesystem::is_directory( p ) ) {
518 for( boost::filesystem::directory_iterator it( p ); it != end; it++ ) {
519 boost::filesystem::path child = boost::filesystem::absolute( *it );
543 boost::filesystem::path p( path, boost::filesystem::native );
544 p = boost::filesystem::absolute( p );
547 if( boost::filesystem::exists( p ) ) {
553 boost::filesystem::path branche = p.branch_path();
554 if( !boost::filesystem::exists( branche ) ) {
555 if( !
makedirs( branche.string().c_str() ) ) {
578 boost::filesystem::path p( path, boost::filesystem::native );
581 unsigned long long size = (
unsigned long long)boost::filesystem::file_size( p );
587 std::string
searchfile(
const char* dir,
const char* fileName,
const bool recursive ) {
589 std::string filePath;
592 std::vector< std::string > dirs;
594 boost::filesystem::path p( dir, boost::filesystem::native );
595 if( !boost::filesystem::exists( p ) ) {
599 boost::filesystem::directory_iterator end;
600 for( boost::filesystem::directory_iterator it( p ); it != end && filePath.empty(); it++ ) {
601 std::string childName = (*it).path().leaf().string();
602 std::string child =
getpath( dir, (*it).path().leaf().string().c_str() );
604 if( childName.compare( fileName ) == 0 ) {
608 dirs.push_back( child );
614 for(
unsigned int i = 0; i < dirs.size() && filePath.empty(); i++ ) {
615 filePath =
searchfile( dirs[ i ].c_str(), fileName,
true );
std::string getfilename(const char *path)
get file name
std::string getenvariable(const char *name)
get environment variable
bool isabsolutepath(const char *file)
judges whether the specified file path is absolute file path or not.
std::vector< std::string > g_arguments
bool removedirs(const char *path)
remove directories
long long filetell(FILE *fp)
gets file position
std::string getmoduledir()
gets module file directory
interfaces of file system function.
bool makedirs(const char *path)
make directories
void fileseek(FILE *fp, long long offset, int origin)
sets file position
std::string searchfile(const char *dir, const char *fileName, const bool recursive)
searches file
int compareignorecase(const char *s1, const char *s2)
compare two strings ignoring case considerations
bool fileexists(const char *path)
judge whether file exists
std::string getabsolutepath(const char *dir, const char *file)
get absolute path
bool checkfile(const char *path)
check file before open. (This function calls fileexists and isdirectory )
common error code definition
bool copyfile(const char *src, const char *dst)
copies file
unsigned int stringtoken(const char *s, const char *delim, std::vector< std::string > &tokens)
get tokens from string
const char * getargv(int index)
get argument
#define NVL(checkVal, replaceVal)
bool makedirectory(const char *path)
creates directory
const unsigned int ERR_PATH_IS_DIRECTORY
FILE * fileopen(const char *path, const char *mode)
opens file
int getargc()
get number of arguments
const unsigned int ERR_FILE_NOT_FOUND
std::string getdir(const char *path)
get dir that the file exists
interfaces of log function
interfaces of string function
std::string replacestring(const char *s, const char *oldStr, const char *newStr)
replaces specified substring with another one
std::string getplugindir()
gets mspp plugin paths
std::string absolutepath(const char *path)
get absolute path
bool isdirectory(const char *path)
judge whether file is directory
std::string getmodulefile()
gets module file path
std::string getext(const char *path)
get the extension of the file
bool removefile(const char *path)
removes file
std::string gethomedir()
gets current user's home directory
std::string getpath(const char *dir, const char *file)
get file path
void setarguments(int argc, char **argv)
set arguments
unsigned long long filesize(const char *path)
gets the file size