1
0

wa_files.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "WAT.h"
  2. bool wa::files::file_exists( const char *p_filename )
  3. {
  4. if ( p_filename == NULL )
  5. return false;
  6. struct stat l_buffer;
  7. return ( stat( p_filename, &l_buffer ) == 0 );
  8. }
  9. bool wa::files::file_exists( const std::string &p_filename )
  10. {
  11. return wa::files::file_exists( p_filename.c_str() );
  12. }
  13. bool wa::files::file_exists( const wchar_t *p_filename )
  14. {
  15. return wa::files::file_exists( wa::strings::convert::to_string( p_filename ) );
  16. }
  17. int wa::files::file_size( const char *p_filename )
  18. {
  19. int l_file_size = -1;
  20. struct stat l_file_info{};
  21. if ( !stat( p_filename, &l_file_info ) )
  22. l_file_size = l_file_info.st_size;
  23. return l_file_size;
  24. }
  25. int wa::files::file_size( const wchar_t *p_filename )
  26. {
  27. std::string l_filename = wa::strings::convert::to_string( p_filename );
  28. return file_size( l_filename.c_str() );
  29. }
  30. bool wa::files::folder_exists( const char *p_folder )
  31. {
  32. struct stat info;
  33. if ( stat( p_folder, &info) != 0 )
  34. return false;
  35. else if ( info.st_mode & S_IFDIR )
  36. return true;
  37. else
  38. return false;
  39. }
  40. bool wa::files::getFilenamesFromFolder( std::vector<std::string> &p_result, const std::string &p_folder_path, const std::string &p_reg_ex, const size_t p_limit )
  41. {
  42. _finddata_t l_file_info;
  43. std::string l_file_pattern = p_folder_path + "\\" + p_reg_ex;
  44. intptr_t l_handle = _findfirst( l_file_pattern.c_str(), &l_file_info );
  45. //If folder_path exsist, using l_file_pattern will find at least two files "." and "..",
  46. //of which "." means current dir and ".." means parent dir
  47. if ( l_handle != -1 )
  48. {
  49. //iteratively check each file or sub_directory in current folder
  50. do
  51. {
  52. std::string l_file_name = l_file_info.name; //from char array to string
  53. //check whtether it is a sub direcotry or a file
  54. if ( l_file_info.attrib & _A_SUBDIR )
  55. {
  56. if ( l_file_name != "." && l_file_name != ".." )
  57. wa::files::getFilenamesFromFolder( p_result, p_folder_path + "\\" + l_file_name, p_reg_ex );
  58. }
  59. else
  60. p_result.push_back( p_folder_path + "\\" + l_file_name );
  61. } while ( _findnext( l_handle, &l_file_info ) == 0 && p_result.size() < p_limit - 1 );
  62. _findclose( l_handle );
  63. return true;
  64. }
  65. //
  66. _findclose( l_handle );
  67. return false;
  68. }