Преглед на файлове

Add functions mkdir and cd. Seem to be working

Jonatan Gezelius преди 8 години
родител
ревизия
0028c66d8b
променени са 4 файла, в които са добавени 221 реда и са изтрити 4 реда
  1. 6 0
      debugging.h
  2. 142 0
      filefunctions.cpp
  3. 51 0
      filefunctions.h
  4. 22 4
      main.cpp

+ 6 - 0
debugging.h

@@ -0,0 +1,6 @@
+#include <iostream>
+
+
+//#define DEBUG(x) do { std::cerr << x; } while (0)
+#define DEBUG(x) do { std::cout << x << std::endl; } while (0)
+//#define DEBUG(x)

+ 142 - 0
filefunctions.cpp

@@ -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__

+ 51 - 0
filefunctions.h

@@ -0,0 +1,51 @@
+#ifndef FILEFUNCTIONS_H
+#define FILEFUNCTIONS_H
+
+#include <string>
+#include <ctime>
+#include <queue>
+
+namespace filefunctions
+{
+    enum STATUS_CODE
+    {
+        SC_SUCCESS = 0,
+        SC_INVALID_WD,           // No or invalid working dir
+        SC_INVALID_PATH,
+        SC_DISK_FULL,
+        SC_DOES_NOT_EXIST,
+        SC_ALREADY_EXISTS,
+        SC_READ_ONLY,
+        SC_IS_DIRECTORY,
+        SC_IS_NOT_DIRECTORY,
+        SC_OTHER_FAILIURE
+        // And so on
+    };
+
+    enum COMMAND_FLAGS
+    {
+        CF_NONE         = 0,
+        CF_FORCE        = 1,
+        CF_RELATIVE     = 2
+    };
+
+    struct File_Struct
+    {
+        std::string     path;
+        unsigned int    size;
+        tm              *modified   = nullptr;
+        tm              *taken      = nullptr;
+    };
+
+    extern std::string filefunctions_wd;
+
+    STATUS_CODE cd      (std::string const &path = "", COMMAND_FLAGS flags = CF_NONE);
+    STATUS_CODE mv      (std::string const &src, std::string const &dest, COMMAND_FLAGS flags = CF_NONE);
+    STATUS_CODE mkdir   (std::string const &path, COMMAND_FLAGS flags = CF_NONE);
+    STATUS_CODE rm      (std::string const &path, COMMAND_FLAGS flags = CF_NONE);
+
+    std::string             pwd (void);
+    std::queue<File_Struct> ls  (std::string path = filefunctions_wd);
+
+}
+#endif // FILEFUNCTIONS_H

+ 22 - 4
main.cpp

@@ -1,9 +1,27 @@
-#include <iostream>
+#include <cstdio>
+#include "filefunctions.h"
+#include "debugging.h"
+#include <windows.h>
 
-using namespace std;
+using namespace filefunctions;
 
-int main()
+int main(int argc, char **argv)
 {
-    cout << "Hello world!" << endl;
+    printf("Yo dude!\n");
+
+    DEBUG(std::hex << INVALID_FILE_ATTRIBUTES << std::endl << argv[0]);
+
+    if(cd("testfiler", CF_RELATIVE))
+        printf("FAIL!\n");
+    else
+        printf("SUCCESS! Current dir: %s\n", pwd().c_str());
+
+    printf("\n\n\n\n");
+
+    if(mkdir("manick\\bajs\\snorkråka\\hitler\\adolf", CF_RELATIVE))
+        printf("FAIL!\n");
+    else
+        printf("Success!");
+
     return 0;
 }