Explorar o código

Förra fungerade inte alls bra. Denna verkar nu fungera hjälpligt. Långsam.

Jonatan Gezelius %!s(int64=8) %!d(string=hai) anos
pai
achega
7d03eb9d9b
Modificáronse 3 ficheiros con 58 adicións e 26 borrados
  1. 4 2
      debugging.h
  2. 39 14
      filefunctions.cpp
  3. 15 10
      main.cpp

+ 4 - 2
debugging.h

@@ -2,5 +2,7 @@
 
 
 //#define DEBUG(x) do { std::cerr << x; } while (0)
-#define DEBUG(x) do { std::cout << x << std::endl; } while (0)
-//#define DEBUG(x)
+//#define DEBUG(x) do { std::cout << x << std::endl; } while (0)
+#define DEBUG(x)
+
+#define INFO(x) do { std::cout << x << std::endl; } while (0)

+ 39 - 14
filefunctions.cpp

@@ -75,19 +75,32 @@ tm* get_exif_time(std::string path)
     DEBUG("__GET_EXIF_TIME__ File parsed!");
 
     tm *result = new tm;
+    //tm *result = (tm*)malloc(sizeof(tm));
     int bajs;
-    sscanf(exif.DateTimeOriginal.c_str(), "%i:%i:%i %i:%i:%i", &result->tm_year, &result->tm_mon, &result->tm_mday, &result->tm_hour, &result->tm_min, &result->tm_sec);
+    sscanf(exif.DateTimeOriginal.c_str(), "%d:%d:%d %d:%d:%d", &result->tm_year, &result->tm_mon, &result->tm_mday, &result->tm_hour, &result->tm_min, &result->tm_sec);
 
-    // Coarse sanity check to avoid very invalid dates
-    if(result->tm_year < 70 || result->tm_yday > 200)
-    {
-        delete result;
-        return nullptr;
-    }
+    DEBUG("__GET_EXIF_TIME__ DateTime: " << exif.DateTimeOriginal.c_str());
+
+    DEBUG("__GET_EXIF_TIME__ year: " << result->tm_year);
+    DEBUG("__GET_EXIF_TIME__ month: " << result->tm_mon);
+    DEBUG("__GET_EXIF_TIME__ date: " << result->tm_mday);
+    DEBUG("__GET_EXIF_TIME__ hour: " << result->tm_hour);
+    DEBUG("__GET_EXIF_TIME__ minute: " << result->tm_min);
+    DEBUG("__GET_EXIF_TIME__ second: " << result->tm_sec);
 
     result->tm_year -= 1900;
     result->tm_mon -= 1;
 
+    time_t unix_time = mktime(result);
+    DEBUG("__GET_EXIF_TIME__ Time: " << unix_time);
+
+    // Sort of a sanity check
+    if(unix_time == -1)
+    {
+        delete result;
+        return nullptr;
+    }//*/
+
     return result;
 }
 
@@ -122,7 +135,7 @@ STATUS_CODE filefunctions::cd(std::string const &path_arg, COMMAND_FLAGS flags)
     DEBUG("__CD__ Path: " << path);
     DEBUG("__CD__ Attr: " << std::hex << attr);
 
-    if(attr == FILE_ATTRIBUTE_DIRECTORY)
+    if(attr != INVALID_FILE_ATTRIBUTES && attr&FILE_ATTRIBUTE_DIRECTORY)
     {
         filefunctions_wd = path;
         return SC_SUCCESS;
@@ -201,7 +214,7 @@ STATUS_CODE filefunctions::mkdir(std::string const &arg_path, COMMAND_FLAGS flag
 
     DWORD attr = GetFileAttributes(path.c_str());
 
-    if(attr == FILE_ATTRIBUTE_DIRECTORY)
+    if(attr != INVALID_FILE_ATTRIBUTES && attr&FILE_ATTRIBUTE_DIRECTORY)
     {
         DEBUG("__MKDIR__ SUCCESS Attr: " << attr);
         return SC_SUCCESS;
@@ -265,7 +278,7 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
     DEBUG("__LS__ ls(" << path << ")");
 
     DWORD attr = GetFileAttributes(path.c_str());
-    if(attr != FILE_ATTRIBUTE_DIRECTORY)
+    if(attr != INVALID_FILE_ATTRIBUTES && !(attr&FILE_ATTRIBUTE_DIRECTORY))
         return SC_IS_NOT_DIRECTORY;
 
     std::string path_joker = path + "\\*";
@@ -282,6 +295,10 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
     do
     {
         File_Struct temp;
+        temp.modified = nullptr;
+        temp.taken = nullptr;
+        temp.file_ending = "";
+
         temp.path = path + "\\" + file_data.cFileName;
 
         if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
@@ -297,7 +314,7 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
                 }
                 else
                 {
-                    DEBUG("__LS__ Which is not . or ..\nGoing in!\n");
+                    INFO("__LS__ Found directory. Going in!\n");
                     ls(temp.path, content, CF_RECURSIVE);
                 }
             }
@@ -309,14 +326,22 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
         else
         {
             DEBUG("__LS__ Found file!");
-            DEBUG("__LS__ File: \"" << temp.path << "\"");
+            INFO("__LS__ File: \"" << temp.path << "\"");
             time_t unix_time = filetime_to_unixtime(file_data.ftLastWriteTime);
 
 
             DEBUG("__LS__ Modified time: " << std::dec << unix_time);
 
-            temp.modified = new tm;
-            memcpy(temp.modified, localtime(&unix_time), sizeof(tm));
+            if(unix_time < 800000000 || unix_time > 3500000000)
+            {
+                delete temp.modified;
+                temp.modified = nullptr;
+            }
+            else
+            {
+                temp.modified = new tm;
+                memcpy(temp.modified, localtime(&unix_time), sizeof(tm));
+            }
 
             temp.file_ending = temp.path.substr(temp.path.find_last_of('.')+1);
             std::transform(temp.file_ending.begin(), temp.file_ending.end(), temp.file_ending.begin(), ::tolower);

+ 15 - 10
main.cpp

@@ -21,22 +21,22 @@ int main(int argc, char **argv)
     else
         printf("SUCCESS! Current dir: %s\n", pwd().c_str());
 */
-    printf("\n\n\n\n");
-
-    DEBUG("Test operator: " << (CF_RECURSIVE | CF_RELATIVE));
 
     std::queue<File_Struct> files;
     ls("", files, CF_RECURSIVE | CF_RELATIVE);
 
+    std::cout << "Klar med listning av filer. Flyttar nu enl. önskemål" << std::endl;
+
     while(files.size())
     {
         File_Struct temp = files.front();
-        files.pop();
         DEBUG("Fil: " << temp.path);
         if(temp.file_ending == "exe")
         {
             if(temp.modified)
                 delete temp.modified;
+
+            files.pop();
             continue;
         }
         //DEBUG("Modified: " << asctime(temp.modified));
@@ -45,24 +45,25 @@ int main(int argc, char **argv)
             DEBUG("Tagen: " << asctime(temp.taken));
             */
 
+        INFO("Moving file: \"" << temp.path << "\"");
         std::ostringstream new_path;
-        new_path << pwd() << "\\sorterat\\";
+        new_path << std::dec << pwd() << "\\sorterat\\";
         if(temp.taken)
         {
-            DEBUG("Moving based on date taken");
+            INFO("Moving based on date taken");
             new_path << (temp.taken->tm_year+1900) << "\\" << std::setfill('0') << std::setw(2) << (temp.taken->tm_mon+1) << "\\" << (temp.taken->tm_year+1900) << "-" << std::setw(2) << (temp.taken->tm_mon+1) << "-" \
                      << std::setw(2) << temp.taken->tm_mday << "_" << std::setw(2) << temp.taken->tm_hour << "'" << std::setw(2) << temp.taken->tm_min << "'" << std::setw(2) << temp.taken->tm_sec;
         }
         else if(temp.modified)
         {
-            DEBUG("Moving based on date modified");
+            INFO("Moving based on date modified");
             new_path << (temp.modified->tm_year+1900) << "\\" << std::setfill('0') << std::setw(2) << (temp.modified->tm_mon+1) << "\\" << (temp.modified->tm_year+1900) << "-" << std::setw(2) << (temp.modified->tm_mon+1) << "-" \
                      << std::setw(2) << temp.modified->tm_mday << "_" << std::setw(2) << temp.modified->tm_hour << "'" << std::setw(2) << temp.modified->tm_min << "'" << std::setw(2) << temp.modified->tm_sec;
         }
         else
         {
             DEBUG("No date found");
-            new_path << "osorterat/" << temp.path.substr(temp.path.find_last_of('/\\')+1, temp.path.find_last_of('.'));
+            new_path << "osorterat\\" << temp.path.substr(temp.path.find_last_of('/\\')+1, temp.path.find_last_of('.'));
         }
 
         std::ostringstream path_postfix;
@@ -76,17 +77,21 @@ int main(int argc, char **argv)
                 path_postfix << new_path.str() << "(" << postfix++ << ")" << "." << temp.file_ending;
             } while(mv(temp.path, path_postfix.str()) == SC_ALREADY_EXISTS);
         }
-        DEBUG("New filename: " << new_path.str() << "\n\n");
+        INFO("New filename: " << path_postfix.str() << "\n\n");
 
         // Free memory
+
         if(temp.taken)
             delete temp.taken;
         if(temp.modified)
             delete temp.modified;
 
+
+        files.pop();
+
     }
 
-    printf("Klart utan jättefatala fel troligtvis! :D\n");
+    printf("Klart! :D\n");
     char c;
     std::cin >> c;