|
|
@@ -0,0 +1,142 @@
|
|
|
+#include "filefunctions.h"
|
|
|
+#include "debugging.h"
|
|
|
+
|
|
|
+#include <string>
|
|
|
+
|
|
|
+using namespace filefunctions;
|
|
|
+
|
|
|
+std::string filefunctions::filefunctions_wd;
|
|
|
+
|
|
|
+#ifdef __linux__
|
|
|
+ // Linux methods
|
|
|
+ Ej implementerat
|
|
|
+#elif _WIN32
|
|
|
+ // Windows methods
|
|
|
+#include <windows.h>
|
|
|
+
|
|
|
+STATUS_CODE filefunctions::cd(std::string const &path_arg, COMMAND_FLAGS flags)
|
|
|
+{
|
|
|
+ DWORD attr;
|
|
|
+ std::string path;
|
|
|
+
|
|
|
+ if(flags&CF_RELATIVE)
|
|
|
+ {
|
|
|
+ int path_len = GetCurrentDirectory(0, nullptr);
|
|
|
+ LPTSTR temp = (LPTSTR)malloc((path_len+1)*sizeof(char));
|
|
|
+ if(!GetCurrentDirectory(path_len+1, temp))
|
|
|
+ return SC_INVALID_PATH;
|
|
|
+ path = std::string(temp);
|
|
|
+ DEBUG("path: " << temp);
|
|
|
+ free(temp);
|
|
|
+ }
|
|
|
+
|
|
|
+ path += "\\" + path_arg;
|
|
|
+
|
|
|
+ attr = GetFileAttributes(path.c_str());
|
|
|
+
|
|
|
+ DEBUG("__CD__ Path: " << path);
|
|
|
+ DEBUG("__CD__ Attr: " << std::hex << attr);
|
|
|
+
|
|
|
+ if(attr == FILE_ATTRIBUTE_DIRECTORY)
|
|
|
+ {
|
|
|
+ filefunctions_wd = path;
|
|
|
+ return SC_SUCCESS;
|
|
|
+ }
|
|
|
+
|
|
|
+ return SC_INVALID_PATH;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+STATUS_CODE filefunctions::mv(std::string const &src_arg, std::string const &dest_arg, COMMAND_FLAGS flags)
|
|
|
+{
|
|
|
+ std::string src, dest;
|
|
|
+ DWORD attr;
|
|
|
+
|
|
|
+ if(flags & CF_RELATIVE)
|
|
|
+ {
|
|
|
+ src = filefunctions_wd;
|
|
|
+ dest = filefunctions_wd;
|
|
|
+ }
|
|
|
+ src += "\\" + src_arg;
|
|
|
+ dest += "\\" + dest_arg;
|
|
|
+
|
|
|
+ attr = GetFileAttributes(src.c_str());
|
|
|
+ if (attr == INVALID_FILE_ATTRIBUTES)
|
|
|
+ {
|
|
|
+ return SC_DOES_NOT_EXIST;
|
|
|
+ }
|
|
|
+ else if(attr & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
+ {
|
|
|
+ return SC_IS_DIRECTORY;
|
|
|
+ }
|
|
|
+
|
|
|
+ mkdir(dest.substr(0,dest.find_last_of('\\')));
|
|
|
+
|
|
|
+ return SC_SUCCESS;
|
|
|
+}
|
|
|
+
|
|
|
+STATUS_CODE filefunctions::mkdir(std::string const &arg_path, COMMAND_FLAGS flags)
|
|
|
+{
|
|
|
+ std::string path;
|
|
|
+
|
|
|
+ if(flags & CF_RELATIVE)
|
|
|
+ path = filefunctions_wd + "\\" + arg_path;
|
|
|
+ else
|
|
|
+ path = arg_path;
|
|
|
+ DEBUG("__MKDIR__ Path: " << path);
|
|
|
+
|
|
|
+ DWORD attr = GetFileAttributes(path.c_str());
|
|
|
+
|
|
|
+ if(attr == FILE_ATTRIBUTE_DIRECTORY)
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ SUCCESS Attr: " << attr);
|
|
|
+ return SC_SUCCESS;
|
|
|
+ }
|
|
|
+ else if(attr != INVALID_FILE_ATTRIBUTES)
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ FAILED Attr: " << attr);
|
|
|
+ return SC_IS_NOT_DIRECTORY;
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t parent_separator = path.find_last_of('\\');
|
|
|
+ if(parent_separator == std::string::npos)
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ Invalid path");
|
|
|
+ return SC_INVALID_PATH;
|
|
|
+ }
|
|
|
+ std::string parent = path.substr(0,parent_separator);
|
|
|
+ DEBUG("__MKDIR__ Parent: " << parent);
|
|
|
+
|
|
|
+ if(mkdir(parent) == SC_SUCCESS)
|
|
|
+ {
|
|
|
+ if(CreateDirectory(path.c_str(), nullptr))
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ Created directory: " << path);
|
|
|
+ return SC_SUCCESS;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ CreateDirectory failed");
|
|
|
+ return SC_OTHER_FAILIURE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ DEBUG("__MKDIR__ mkdir(" << parent << ") failed");
|
|
|
+ return SC_OTHER_FAILIURE;
|
|
|
+ }
|
|
|
+}
|
|
|
+//STATUS_CODE filefunctions::rm(std::string const &path, COMMAND_FLAGS flags = CF_NONE);
|
|
|
+
|
|
|
+std::string filefunctions::pwd(void)
|
|
|
+{
|
|
|
+ return filefunctions_wd;
|
|
|
+}
|
|
|
+
|
|
|
+//std::queue<File_Struct> filefunctions::ls(std::string path = filefunctions_wd);
|
|
|
+
|
|
|
+
|
|
|
+#else
|
|
|
+ // Fail?
|
|
|
+ Hård fail som fan!
|
|
|
+#endif // __linux__
|