Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
FileFunctions.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 
14 
15 #include "FileFunctions.h"
16 
17 #include "LogFunctions.h"
18 #include "StringFunctions.h"
19 #include "CoreMacros.h"
20 #include "ErrorCode.h"
21 
22 #include <vector>
23 #include <list>
24 #include <boost/filesystem/path.hpp>
25 #include <boost/filesystem/operations.hpp>
26 #include <boost/bind.hpp>
27 
28 #ifdef __unix
29  #include <sys/stat.h>
30 #else
31  #include <direct.h>
32 #endif //__unix
33 
34 
35 #ifndef _LARGEFILE_SOURCE
36  #define _LARGEFILE_SOURCE
37 #endif // _LARGEFILE_SOURCE
38 
39 
41 std::vector<std::string> g_arguments;
42 
44 std::string g_moduleFile;
45 
47 std::string g_homeDir;
48 
49 
50 // judge where file exists.
51 bool fileexists( const char* path ) {
52  // check parameter
53  if( path == NULL ) {
54  return false;
55  }
56 
57  // create path object
58  boost::filesystem::path p( path, boost::filesystem::native );
59 
60  // judge
61  bool ret = false;
62  try{
63  ret = boost::filesystem::exists( p );
64  }
65  catch( ... ) {
66  ret = false;
67  }
68 
69  return ret;
70 }
71 
72 // judge where path is directory.
73 bool isdirectory( const char* path ) {
74  // check parameter
75  if( !fileexists( path ) ) {
76  return false;
77  }
78 
79  // create path object
80  boost::filesystem::path p( path, boost::filesystem::native );
81 
82  // check existing
83  if( !boost::filesystem::exists( p ) ) {
84  return false;
85  }
86 
87  // judgement
88  return boost::filesystem::is_directory( p );
89 }
90 
91 // check file before open
92 bool checkfile( const char* path ) {
93  // check parameter
94  if( path == NULL ) {
95  LOG_ERROR( FMT( "The file path is null." ) );
96  return false;
97  }
98 
99  //
100  if( !fileexists( path ) ) {
101  LOG_ERROR_CODE( FMT( "The file does not exist. [%s]", path ), ERR_FILE_NOT_FOUND );
102  return false;
103  }
104 
105  if( isdirectory( path ) ){
106  LOG_ERROR_CODE( FMT( "The path is a directory. A file should be specified. [%s]", path ), ERR_PATH_IS_DIRECTORY );
107  return false;
108  }
109 
110  return true;
111 }
112 
113 // get file name
114 std::string getfilename( const char* path ) {
115  // check parameter
116  if( path == NULL ) {
117  return "";
118  }
119 
120  // create path object
121  boost::filesystem::path p( path, boost::filesystem::native );
122 
123  // leaf
124  std::string name = p.leaf().string();
125  return name;
126 }
127 
128 // get dir name
129 std::string getdir( const char* path ) {
130  // check parameter
131  if( path == NULL ) {
132  return "";
133  }
134 
135  // create path object
136  boost::filesystem::path p( path, boost::filesystem::native );
137 
138  // directory
139  if( p.has_branch_path() ) {
140  p = p.branch_path();
141  return p.string();
142  }
143 
144  return std::string( "" );
145 }
146 
147 // get path
148 std::string getpath( const char* dir, const char* file ) {
149  // check parameter
150  if( dir == NULL && file == NULL ) {
151  return std::string( "" );
152  }
153  if( dir == NULL ) {
154  return std::string( file );
155  }
156  if( file == NULL ) {
157  return std::string( dir );
158  }
159 
160  // create path object
161  boost::filesystem::path filePath( file, boost::filesystem::native );
162 
163  std::string mergeFileName = FMT( "%s%s%s", dir, DIR_SEPARATOR, file );
164  boost::filesystem::path mergePath( mergeFileName.c_str(), boost::filesystem::native );
165 
166  // return path
167  boost::filesystem::path p = filePath.is_complete() ? filePath : mergePath;
168  p.normalize();
169  std::string ret = p.string();
170  ret = replacestring( ret.c_str(), "/", DIR_SEPARATOR );
171  ret = replacestring( ret.c_str(), "\\", DIR_SEPARATOR );
172  return ret;
173 }
174 
175 // get absolute path
176 std::string absolutepath( const char* path ) {
177  // check parameter
178  if( path == NULL ) {
179  return "";
180  }
181 
182  // create path object
183  boost::filesystem::path p( path, boost::filesystem::native );
184 
185  // get absolute path
186  boost::filesystem::path absPath = boost::filesystem::absolute( p );
187 
188  return absPath.string();
189 }
190 
191 // get absolute path
192 std::string getabsolutepath( const char* dir, const char* file ) {
193  // create path object
194  boost::filesystem::path p( getpath( dir, file ), boost::filesystem::native );
195  p.normalize();
196  std::string ret = p.string();
197  ret = replacestring( ret.c_str(), "/", DIR_SEPARATOR );
198 
199  // return path
200  if( p.is_complete() ) { // dir has root
201  return ret;
202  }
203 
204  return absolutepath( ret.c_str() );
205 }
206 
207 // judge absolute file path or not
208 bool isabsolutepath( const char* file ) {
209  // create path object
210  boost::filesystem::path p( file, boost::filesystem::native );
211  p.normalize();
212 
213  // judge
214  return p.is_complete();
215 }
216 
217 // set arguments
218 void setarguments( int argc, char** argv ) {
219  // initialize the array
220  g_arguments.clear();
221 
222  // keep arguments
223  if( argc > 0 ){
224  g_arguments.reserve( argc );
225  }
226  for( int i = 0; i < argc; i++ ){
227  g_arguments.push_back( std::string( argv[i] ) );
228  }
229 }
230 
231 // get number of arguments
232 int getargc() {
233  // check variable
234  if( g_arguments.size() <= 0 ) {
235  LOG_WARN( FMT( "\"setarguments\" function has not been called yet." ) );
236  }
237 
238  return (int)g_arguments.size();
239 }
240 
241 // get argument
242 const char* getargv( int index ) {
243  // check variable
244  if( g_arguments.size() <= 0 ) {
245  LOG_WARN( FMT( "\"setarguments\" function has not been called yet." ) );
246  }
247 
248  // check index
249  if( index < 0 || index >= (int)g_arguments.size() ) {
250  return NULL;
251  }
252 
253  return g_arguments[index].c_str();
254 }
255 
256 // get extension
257 std::string getext( const char* path ) {
258  // check parameter
259  if( path == NULL ) {
260  return std::string( "" );
261  }
262 
263  // find '.'
264  std::string p( path );
265  size_t pos = p.find_last_of( "." );
266  if( pos == p.npos ) {
267  return "";
268  }
269 
270  return p.substr( pos + 1 );
271 }
272 
273 // get environment variable
274 std::string getenvariable( const char* name ) {
275  // check parameter
276  if( name == NULL ) {
277  return std::string( "" );
278  }
279 
280  // get environment variable
281  std::string value;
282 
283 #ifdef _MSC_VER
284  size_t size = 0;
285  char* v = NULL;
286  _dupenv_s( &v, &size, name );
287  if( v != NULL ) {
288  value = std::string( v );
289  delete[] v;
290  }
291 #else
292  char* v = getenv( name );
293  if( v != NULL ) {
294  value = std::string( v );
295  }
296 
297 #endif // _MSC_VER
298 
299  return value;
300 }
301 
302 // get module file path
303 std::string getmodulefile() {
304  // check global variables
305  if( !g_moduleFile.empty() ) {
306  return g_moduleFile;
307  }
308  if( g_arguments.size() <= 0 ) {
309  LOG_WARN( FMT( "\"setarguments\" function has not been called yet." ) );
310  return std::string( "" );
311  }
312 
313  // get file name
314  std::string program = getfilename( g_arguments[0].c_str() );
315  std::string fileName = program;
316 
317 #ifndef __unix // Windows
318 
319  std::string ext = getext( fileName.c_str() );
320  if( compareignorecase( ext.c_str(), "exe" ) != 0 ) {
321  fileName.append( ".exe" );
322  }
323 
324 #endif // __unix
325 
326  if( strcmp( program.c_str(), g_arguments[0].c_str() ) == 0 ) { // search PATH
327  // get environment variable "PATH"
328 #ifdef __unix // unix
329  std::string p = getenvariable( "PATH" );
330 #else // windows
331  std::string p = getenvariable( "Path" );
332 #endif // __unix
333 
334  // creating search path list
335  std::vector<std::string> paths;
336  stringtoken(
337  p.c_str(),
339  paths
340  );
341 
342 #ifndef __unix // Windows
343  paths.insert(paths.begin(), std::string("."));
344 #endif // __unix
345 
346  // search path
347  for( unsigned int xx = 0; g_moduleFile.empty() && xx < paths.size(); xx++) {
348 
349  if( !paths[xx].empty() ) {
350  try {
351  std::string pgPath = getabsolutepath( paths[xx].c_str(), fileName.c_str() );
352  if( fileexists( pgPath.c_str() ) ) {
353  g_moduleFile = pgPath;
354  }
355  }
356  catch( ... ) {
357  }
358  }
359  }
360  }
361  else { // specified directory
362  g_moduleFile = getabsolutepath( getdir( g_arguments[0].c_str() ).c_str(), fileName.c_str() );
363  }
364 
365  return g_moduleFile;
366 }
367 
368 // get module file dir
369 std::string getmoduledir() {
370  return getdir( getmodulefile().c_str() );
371 }
372 
373 // get home directory
374 std::string gethomedir() {
375  // check global variable
376  if( !g_homeDir.empty() ) {
377  return g_homeDir;
378  }
379 
380  // get home dir
381  std::string homeDir;
382 
383 #ifdef __unix
384  homeDir = getenvariable( "HOME" );
385  if( homeDir.empty() ) {
386  homeDir = "~";
387  }
388 #else
389  std::string homeDrive = getenvariable( "HOMEDRIVE" );
390  std::string homePath = getenvariable( "HOMEPATH" );
391  homeDir = homeDrive + homePath;
392 #endif // __unix
393  g_homeDir = absolutepath( homeDir.c_str() );
394 
395  return g_homeDir;
396 }
397 
398 // file open
399 FILE* fileopen( const char* path, const char* mode ) {
400  // file descriptor
401  FILE* fp = NULL;
402 
403 #ifdef _MSC_VER
404  errno_t ret = fopen_s( &fp, path, mode );
405  if( ret != 0 ) {
406  fp = NULL;
407  }
408 #else
409  fp = fopen( path, mode );
410 #endif // MSC_VER
411 
412  return fp;
413 }
414 
415 // file seek
416 void fileseek( FILE* fp, long long offset, int origin ) {
417 
418 #ifdef _MSC_VER
419  _fseeki64( fp, offset, origin );
420 #else
421  fseeko64( fp, offset, origin );
422 #endif
423 }
424 
425 // file tell
426 long long filetell( FILE* fp ) {
427  long long pos = 0;
428 #ifdef _MSC_VER
429  pos = _ftelli64( fp );
430 #else
431  pos = ftello64( fp );
432 #endif
433 
434  return pos;
435 }
436 
437 // copy file
438 bool copyfile( const char* src, const char* dst ) {
439  // buffer
440  char buff[ 0x10000 ];
441 
442  // file open
443  FILE* srcFp = fileopen( src, "rb" );
444  if( srcFp == NULL ) {
445  return false;
446  }
447 
448  FILE* dstFp = fileopen( dst, "wb" );
449  if( dstFp == NULL ) {
450  return false;
451  }
452 
453  // copy
454  size_t len = int();
455  while( ( len = fread( buff, 1, 0x10000, srcFp ) ) > 0 ) {
456  fwrite( buff, 1, len, dstFp );
457  }
458 
459  // close
460  fflush( dstFp );
461  fclose( dstFp );
462  fclose( srcFp );
463 
464  return true;
465 }
466 
467 // remove file
468 bool removefile( const char* path ) {
469  // result
470  int ret = 0;
471 
472 #ifdef _MSC_VER
473  ret = _unlink( path );
474 #else
475  ret = unlink( path );
476 #endif
477 
478  if( ret != 0 ) {
479  LOG_ERROR( FMT( "Failed to remove the file. [%s]", NVL( path, "" ) ) );
480  }
481 
482  return ( ret == 0 );
483 }
484 
485 // make directory
486 bool makedirectory( const char* path ) {
487  // result
488  int ret = 0;
489 
490 #ifdef _MSC_VER
491  ret = _mkdir( path );
492 #else
493  ret = mkdir( path, 0755 );
494 #endif
495 
496  if( ret != 0 ) {
497  LOG_ERROR( FMT( "Failed to create the directory. [%s]", NVL( path, "" ) ) );
498  }
499 
500  return ( ret == 0 );
501 }
502 
503 // remove directories
504 bool removedirs( const char* path ) {
505  // path object
506  boost::filesystem::path p( path, boost::filesystem::native );
507  boost::filesystem::directory_iterator end;
508 
509  // check
510  if( !boost::filesystem::exists( p ) ) {
511  LOG_WARN( FMT( "%s does not exist.", path ) );
512  return false;
513  }
514 
515  // remove
516  bool ret = true;
517  if( boost::filesystem::is_directory( p ) ) { // directory
518  for( boost::filesystem::directory_iterator it( p ); it != end; it++ ) {
519  boost::filesystem::path child = boost::filesystem::absolute( *it );
520  if( !removedirs( child.string().c_str() ) ) {
521  ret = false;
522  }
523  }
524 
525 #ifdef _MSC_VER
526  _rmdir( path );
527 #else
528  rmdir( path );
529 #endif
530  }
531  else { // file
532  if( !removefile( path ) ) {
533  ret = false;
534  }
535  }
536 
537  return ret;
538 }
539 
540 // make directories
541 bool makedirs( const char* path ) {
542  // path object
543  boost::filesystem::path p( path, boost::filesystem::native );
544  p = boost::filesystem::absolute( p );
545 
546  // check
547  if( boost::filesystem::exists( p ) ) {
548  return false;
549  }
550 
551  // branch
552  bool ret = true;
553  boost::filesystem::path branche = p.branch_path();
554  if( !boost::filesystem::exists( branche ) ) {
555  if( !makedirs( branche.string().c_str() ) ) {
556  ret = false;
557  }
558  }
559 
560  // make directory
561  if( ret ) {
562  ret = makedirectory( p.string().c_str() );
563  }
564 
565  return ret;
566 }
567 
568 // get mspp plugin dir
569 std::string getplugindir(){
570  std::string strDir;
571  strDir = getenvariable( "MSPP_PLUGIN_DIR" );
572  return strDir;
573 }
574 
575 // file size
576 unsigned long long filesize( const char* path ) {
577  // path object
578  boost::filesystem::path p( path, boost::filesystem::native );
579 
580  // size
581  unsigned long long size = (unsigned long long)boost::filesystem::file_size( p );
582 
583  return size;
584 }
585 
586 // search file
587 std::string searchfile( const char* dir, const char* fileName, const bool recursive ) {
588  // return value
589  std::string filePath;
590 
591  // search files
592  std::vector< std::string > dirs;
593 
594  boost::filesystem::path p( dir, boost::filesystem::native );
595  if( !boost::filesystem::exists( p ) ) {
596  return filePath;
597  }
598 
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() );
603 
604  if( childName.compare( fileName ) == 0 ) {
605  filePath = child;
606  }
607  else if( isdirectory( child.c_str() ) ) {
608  dirs.push_back( child );
609  }
610  }
611 
612  // search folders
613  if( recursive ) {
614  for( unsigned int i = 0; i < dirs.size() && filePath.empty(); i++ ) {
615  filePath = searchfile( dirs[ i ].c_str(), fileName, true );
616  }
617  }
618 
619  return filePath;
620 
621 }
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
std::string g_homeDir
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.
#define PATH_SEPARATOR
Definition: CoreMacros.h:83
bool makedirs(const char *path)
make directories
basic macro collection
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
#define DIR_SEPARATOR
Definition: CoreMacros.h:77
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)
Definition: CoreMacros.h:99
#define NULL
Definition: CoreMacros.h:18
bool makedirectory(const char *path)
creates directory
const unsigned int ERR_PATH_IS_DIRECTORY
Definition: ErrorCode.h:57
FILE * fileopen(const char *path, const char *mode)
opens file
int getargc()
get number of arguments
std::string g_moduleFile
const unsigned int ERR_FILE_NOT_FOUND
Definition: ErrorCode.h:42
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