filefunctions.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "filefunctions.h"
  2. #include "debugging.h"
  3. #include <string>
  4. using namespace filefunctions;
  5. std::string filefunctions::filefunctions_wd;
  6. #ifdef __linux__
  7. // Linux methods
  8. Ej implementerat
  9. #elif _WIN32
  10. // Windows methods
  11. #include <windows.h>
  12. STATUS_CODE filefunctions::cd(std::string const &path_arg, COMMAND_FLAGS flags)
  13. {
  14. DWORD attr;
  15. std::string path;
  16. if(flags&CF_RELATIVE)
  17. {
  18. int path_len = GetCurrentDirectory(0, nullptr);
  19. LPTSTR temp = (LPTSTR)malloc((path_len+1)*sizeof(char));
  20. if(!GetCurrentDirectory(path_len+1, temp))
  21. return SC_INVALID_PATH;
  22. path = std::string(temp);
  23. DEBUG("path: " << temp);
  24. free(temp);
  25. }
  26. path += "\\" + path_arg;
  27. attr = GetFileAttributes(path.c_str());
  28. DEBUG("__CD__ Path: " << path);
  29. DEBUG("__CD__ Attr: " << std::hex << attr);
  30. if(attr == FILE_ATTRIBUTE_DIRECTORY)
  31. {
  32. filefunctions_wd = path;
  33. return SC_SUCCESS;
  34. }
  35. return SC_INVALID_PATH;
  36. }
  37. STATUS_CODE filefunctions::mv(std::string const &src_arg, std::string const &dest_arg, COMMAND_FLAGS flags)
  38. {
  39. std::string src, dest;
  40. DWORD attr;
  41. if(flags & CF_RELATIVE)
  42. {
  43. src = filefunctions_wd;
  44. dest = filefunctions_wd;
  45. }
  46. src += "\\" + src_arg;
  47. dest += "\\" + dest_arg;
  48. attr = GetFileAttributes(src.c_str());
  49. if (attr == INVALID_FILE_ATTRIBUTES)
  50. {
  51. return SC_DOES_NOT_EXIST;
  52. }
  53. else if(attr & FILE_ATTRIBUTE_DIRECTORY)
  54. {
  55. return SC_IS_DIRECTORY;
  56. }
  57. mkdir(dest.substr(0,dest.find_last_of('\\')));
  58. return SC_SUCCESS;
  59. }
  60. STATUS_CODE filefunctions::mkdir(std::string const &arg_path, COMMAND_FLAGS flags)
  61. {
  62. std::string path;
  63. if(flags & CF_RELATIVE)
  64. path = filefunctions_wd + "\\" + arg_path;
  65. else
  66. path = arg_path;
  67. DEBUG("__MKDIR__ Path: " << path);
  68. DWORD attr = GetFileAttributes(path.c_str());
  69. if(attr == FILE_ATTRIBUTE_DIRECTORY)
  70. {
  71. DEBUG("__MKDIR__ SUCCESS Attr: " << attr);
  72. return SC_SUCCESS;
  73. }
  74. else if(attr != INVALID_FILE_ATTRIBUTES)
  75. {
  76. DEBUG("__MKDIR__ FAILED Attr: " << attr);
  77. return SC_IS_NOT_DIRECTORY;
  78. }
  79. size_t parent_separator = path.find_last_of('\\');
  80. if(parent_separator == std::string::npos)
  81. {
  82. DEBUG("__MKDIR__ Invalid path");
  83. return SC_INVALID_PATH;
  84. }
  85. std::string parent = path.substr(0,parent_separator);
  86. DEBUG("__MKDIR__ Parent: " << parent);
  87. if(mkdir(parent) == SC_SUCCESS)
  88. {
  89. if(CreateDirectory(path.c_str(), nullptr))
  90. {
  91. DEBUG("__MKDIR__ Created directory: " << path);
  92. return SC_SUCCESS;
  93. }
  94. else
  95. {
  96. DEBUG("__MKDIR__ CreateDirectory failed");
  97. return SC_OTHER_FAILIURE;
  98. }
  99. }
  100. else
  101. {
  102. DEBUG("__MKDIR__ mkdir(" << parent << ") failed");
  103. return SC_OTHER_FAILIURE;
  104. }
  105. }
  106. //STATUS_CODE filefunctions::rm(std::string const &path, COMMAND_FLAGS flags = CF_NONE);
  107. std::string filefunctions::pwd(void)
  108. {
  109. return filefunctions_wd;
  110. }
  111. //std::queue<File_Struct> filefunctions::ls(std::string path = filefunctions_wd);
  112. #else
  113. // Fail?
  114. Hård fail som fan!
  115. #endif // __linux__