| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #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__
|