Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Functions
FileFunctions.h File Reference

interfaces of file system function. More...

#include <string>
#include <vector>
Include dependency graph for FileFunctions.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool fileexists (const char *path)
 judge whether file exists More...
 
bool isdirectory (const char *path)
 judge whether file is directory More...
 
bool checkfile (const char *path)
 check file before open. (This function calls fileexists and isdirectory ) More...
 
std::string getfilename (const char *path)
 get file name More...
 
std::string getdir (const char *path)
 get dir that the file exists More...
 
std::string getpath (const char *dir, const char *file)
 get file path More...
 
std::string absolutepath (const char *path)
 get absolute path More...
 
std::string getabsolutepath (const char *dir, const char *file)
 get absolute path More...
 
bool isabsolutepath (const char *file)
 judges whether the specified file path is absolute file path or not. More...
 
void setarguments (int argc, char **argv)
 set arguments More...
 
int getargc ()
 get number of arguments More...
 
const char * getargv (int index)
 get argument More...
 
std::string getext (const char *path)
 get the extension of the file More...
 
std::string getenvariable (const char *name)
 get environment variable More...
 
std::string getmodulefile ()
 gets module file path More...
 
std::string getmoduledir ()
 gets module file directory More...
 
std::string gethomedir ()
 gets current user's home directory More...
 
std::string getplugindir ()
 gets mspp plugin paths
 
FILE * fileopen (const char *path, const char *mode)
 opens file More...
 
void fileseek (FILE *fp, long long offset, int origin)
 sets file position More...
 
long long filetell (FILE *fp)
 gets file position More...
 
bool copyfile (const char *src, const char *dst)
 copies file More...
 
bool removefile (const char *path)
 removes file More...
 
bool makedirectory (const char *path)
 creates directory More...
 
bool removedirs (const char *path)
 remove directories More...
 
bool makedirs (const char *path)
 make directories More...
 
unsigned long long filesize (const char *path)
 gets the file size More...
 
std::string searchfile (const char *dir, const char *fileName, const bool recursive)
 searches file More...
 

Detailed Description

interfaces of file system function.

Author
S.Tanaka
Date
2006.06.28

Copyright(C) 2006-2014 Eisai Co., Ltd. All rights reserved.

Definition in file FileFunctions.h.

Function Documentation

std::string absolutepath ( const char *  path)

get absolute path

Parameters
[in]pathfile path
Returns
absolute path

Definition at line 176 of file FileFunctions.cpp.

176  {
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 }
#define NULL
Definition: CoreMacros.h:18
bool checkfile ( const char *  path)

check file before open. (This function calls fileexists and isdirectory )

Parameters
[in]pathfile path
Returns
true: The path specified exists and is not directory.

Definition at line 92 of file FileFunctions.cpp.

92  {
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 }
bool fileexists(const char *path)
judge whether file exists
#define NULL
Definition: CoreMacros.h:18
const unsigned int ERR_PATH_IS_DIRECTORY
Definition: ErrorCode.h:57
const unsigned int ERR_FILE_NOT_FOUND
Definition: ErrorCode.h:42
bool isdirectory(const char *path)
judge whether file is directory

Here is the call graph for this function:

bool copyfile ( const char *  src,
const char *  dst 
)

copies file

Parameters
[in]srcfile path to be copied
[in]dstcopy file path
Returns
If true, it succeeded to copy a file.

Definition at line 438 of file FileFunctions.cpp.

438  {
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 }
#define NULL
Definition: CoreMacros.h:18
FILE * fileopen(const char *path, const char *mode)
opens file

Here is the call graph for this function:

bool fileexists ( const char *  path)

judge whether file exists

Parameters
[in]pathfile path
Returns
true: file exists. | false: file doesn't exist

Definition at line 51 of file FileFunctions.cpp.

51  {
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 }
#define NULL
Definition: CoreMacros.h:18
FILE* fileopen ( const char *  path,
const char *  mode 
)

opens file

Parameters
[in]pathfile path to be opened
[in]modeopen mode
Returns
file descriptor

Definition at line 399 of file FileFunctions.cpp.

399  {
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 }
#define NULL
Definition: CoreMacros.h:18
void fileseek ( FILE *  fp,
long long  offset,
int  origin 
)

sets file position

Parameters
[in]fpfile descriptor
[in]offsetposition from origin
[in]originSEEK_SET, SEEK_CUR or SEEK_END (see "fseek" document)

Definition at line 416 of file FileFunctions.cpp.

416  {
417 
418 #ifdef _MSC_VER
419  _fseeki64( fp, offset, origin );
420 #else
421  fseeko64( fp, offset, origin );
422 #endif
423 }
unsigned long long filesize ( const char *  path)

gets the file size

Parameters
[in]pathfile path
Returns
file size

Definition at line 576 of file FileFunctions.cpp.

576  {
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 }
long long filetell ( FILE *  fp)

gets file position

Parameters
[in]fpfile description
Returns
the file position

Definition at line 426 of file FileFunctions.cpp.

426  {
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 }
std::string getabsolutepath ( const char *  dir,
const char *  file 
)

get absolute path

  • If dir has root, this function returns getpath( dir, file )
  • If dir doesn't have root, this function returns absolutepath( getpath( dir, file ) );
    Parameters
    [in]dirdirectory name
    [in]filefile name
    Returns
    file path

Definition at line 192 of file FileFunctions.cpp.

192  {
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 }
#define DIR_SEPARATOR
Definition: CoreMacros.h:77
std::string replacestring(const char *s, const char *oldStr, const char *newStr)
replaces specified substring with another one
std::string absolutepath(const char *path)
get absolute path
std::string getpath(const char *dir, const char *file)
get file path

Here is the call graph for this function:

int getargc ( )

get number of arguments

Returns
number of arguments

Definition at line 232 of file FileFunctions.cpp.

232  {
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 }
std::vector< std::string > g_arguments
const char* getargv ( int  index)

get argument

Parameters
[in]indexargument index
Returns
argument ( if index < 0 or index > 0 returns NULL )

Definition at line 242 of file FileFunctions.cpp.

242  {
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 }
std::vector< std::string > g_arguments
#define NULL
Definition: CoreMacros.h:18
std::string getdir ( const char *  path)

get dir that the file exists

Parameters
[in]pathfile path
Returns
directory part

Definition at line 129 of file FileFunctions.cpp.

129  {
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 }
#define NULL
Definition: CoreMacros.h:18
std::string getenvariable ( const char *  name)

get environment variable

Parameters
[in]nameenvironment variable name
Returns
environment vaiable value

Definition at line 274 of file FileFunctions.cpp.

274  {
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 }
#define NULL
Definition: CoreMacros.h:18
std::string getext ( const char *  path)

get the extension of the file

Parameters
[in]pathfile path
Returns
the extension of the file

Definition at line 257 of file FileFunctions.cpp.

257  {
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 }
#define NULL
Definition: CoreMacros.h:18
std::string getfilename ( const char *  path)

get file name

Parameters
[in]pathfile path
Returns
file name (excluding directory part)

Definition at line 114 of file FileFunctions.cpp.

114  {
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 }
#define NULL
Definition: CoreMacros.h:18
std::string gethomedir ( )

gets current user's home directory

Returns
current user's home directory

Definition at line 374 of file FileFunctions.cpp.

374  {
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 }
std::string getenvariable(const char *name)
get environment variable
std::string g_homeDir
std::string absolutepath(const char *path)
get absolute path

Here is the call graph for this function:

std::string getmoduledir ( )

gets module file directory

Returns
module file directory

Definition at line 369 of file FileFunctions.cpp.

369  {
370  return getdir( getmodulefile().c_str() );
371 }
std::string getdir(const char *path)
get dir that the file exists
std::string getmodulefile()
gets module file path

Here is the call graph for this function:

std::string getmodulefile ( )

gets module file path

Returns
module file path

Definition at line 303 of file FileFunctions.cpp.

303  {
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 }
std::string getfilename(const char *path)
get file name
std::string getenvariable(const char *name)
get environment variable
std::vector< std::string > g_arguments
#define PATH_SEPARATOR
Definition: CoreMacros.h:83
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
unsigned int stringtoken(const char *s, const char *delim, std::vector< std::string > &tokens)
get tokens from string
std::string g_moduleFile
std::string getdir(const char *path)
get dir that the file exists
std::string getext(const char *path)
get the extension of the file

Here is the call graph for this function:

std::string getpath ( const char *  dir,
const char *  file 
)

get file path

Parameters
dirdirectory name
[in]filefile name
Returns
file path

Definition at line 148 of file FileFunctions.cpp.

148  {
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 }
#define DIR_SEPARATOR
Definition: CoreMacros.h:77
#define NULL
Definition: CoreMacros.h:18
std::string replacestring(const char *s, const char *oldStr, const char *newStr)
replaces specified substring with another one

Here is the call graph for this function:

bool isabsolutepath ( const char *  file)

judges whether the specified file path is absolute file path or not.

Parameters
[in]filefile path
Returns
If true, the specified file path is absolute file path.

Definition at line 208 of file FileFunctions.cpp.

208  {
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 }
bool isdirectory ( const char *  path)

judge whether file is directory

Parameters
[in]pathfile path
Returns
true: path is directory. | false: path is not directory.

Definition at line 73 of file FileFunctions.cpp.

73  {
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 }
bool fileexists(const char *path)
judge whether file exists

Here is the call graph for this function:

bool makedirectory ( const char *  path)

creates directory

Parameters
[in]pathdirectory path to be maken
Returns
If true, it succeeded to make a directory.

Definition at line 486 of file FileFunctions.cpp.

486  {
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 }
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
bool makedirs ( const char *  path)

make directories

Parameters
[in]pathdirectory path

Definition at line 541 of file FileFunctions.cpp.

541  {
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 }
bool makedirs(const char *path)
make directories
bool makedirectory(const char *path)
creates directory

Here is the call graph for this function:

bool removedirs ( const char *  path)

remove directories

Parameters
[in]pathdirectory path

Definition at line 504 of file FileFunctions.cpp.

504  {
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 }
bool removedirs(const char *path)
remove directories
bool removefile(const char *path)
removes file

Here is the call graph for this function:

bool removefile ( const char *  path)

removes file

Parameters
[in]pathfile path to be removed
Returns
If true, it succeeded to remove a file.

Definition at line 468 of file FileFunctions.cpp.

468  {
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 }
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
std::string searchfile ( const char *  dir,
const char *  fileName,
const bool  recursive 
)

searches file

Parameters
[in]dirfile directory
[in]fileNamefile name
[in]recursiveIf true, search file recursively.
Returns
If the file is found, this function returns file path. Otherwise, empty string.

Definition at line 587 of file FileFunctions.cpp.

587  {
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 searchfile(const char *dir, const char *fileName, const bool recursive)
searches file
bool isdirectory(const char *path)
judge whether file is directory
std::string getpath(const char *dir, const char *file)
get file path

Here is the call graph for this function:

void setarguments ( int  argc,
char **  argv 
)

set arguments

Parameters
[in]argcthe number of arguments
[in]argvthe array of arguments

Definition at line 218 of file FileFunctions.cpp.

218  {
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 }
std::vector< std::string > g_arguments