Sfoglia il codice sorgente

Fixat så tiderna är typ rätt. Photo taken är alltid rätt (mktime ställde till det tidigare). Modified tid sätts alltid till UTC+1, oavsett sommartid/plats den modifierades.

Jonatan Gezelius 8 anni fa
parent
commit
a9375bc4bc
5 ha cambiato i file con 69 aggiunte e 12 eliminazioni
  1. 3 0
      debugging.h
  2. 2 1
      exif.cpp
  3. 48 10
      filefunctions.cpp
  4. 1 0
      image_sort.cbp
  5. 15 1
      main.cpp

+ 3 - 0
debugging.h

@@ -1,6 +1,9 @@
 #include <iostream>
 
 
+//#define ASSERT(x) do { std::cout << x << std::endl; } while (0); char asd; std::cin >> (asd); std::cin.ignore(2000, '\n')// exit(-1)
+#define ASSERT(x)
+
 //#define DEBUG(x) do { std::cerr << x; } while (0)
 //#define DEBUG(x) do { std::cout << x << std::endl; } while (0)
 #define DEBUG(x)

+ 2 - 1
exif.cpp

@@ -424,6 +424,7 @@ int easyexif::EXIFInfo::parseFrom(const unsigned char *buf, unsigned len) {
   // except 0xD9 at the end of the image buffer, keep decrementing len until
   // an 0xFFD9 is found. If JPEG end marker 0xFFD9 is not found,
   // then we can be reasonably sure that the buffer is not a JPEG.
+  /*
   while (len > 2) {
     if (buf[len - 1] == 0xD9 && buf[len - 2] == 0xFF)
       break;
@@ -431,7 +432,7 @@ int easyexif::EXIFInfo::parseFrom(const unsigned char *buf, unsigned len) {
   }
   if (len <= 2)
     return PARSE_EXIF_ERROR_NO_JPEG;
-
+*/
   clear();
 
   // Scan for EXIF header (bytes 0xFF 0xE1) and do a sanity check by

+ 48 - 10
filefunctions.cpp

@@ -28,16 +28,20 @@ COMMAND_FLAGS filefunctions::operator|(filefunctions::COMMAND_FLAGS a, filefunct
 
 time_t filetime_to_unixtime(FILETIME const &t)
 {
+    //FILETIME t;
+    //FileTimeToLocalFileTime(&utc_t, &t);
     LARGE_INTEGER big_time;
     time_t unix_time;
 
     big_time.HighPart = t.dwHighDateTime;
     big_time.LowPart = t.dwLowDateTime;
     unix_time = big_time.QuadPart/10000000-11644473600;
+    unix_time += 3600; // UTC+1
+    DEBUG("__FILETIME__ " << big_time.QuadPart);
     return unix_time;
 }
 
-tm* get_exif_time(std::string path)
+tm* get_exif_time(std::string path, long file_size)
 {
     FILE *filen = fopen(path.c_str(), "rb");
     if (filen == nullptr)
@@ -46,7 +50,7 @@ tm* get_exif_time(std::string path)
         return nullptr;
     }
     DEBUG("__GET_EXIF_TIME__ Opened file: " << path);
-
+/*
     fseek(filen, 0, SEEK_END);
     unsigned long file_size = ftell(filen);
     rewind(filen);
@@ -61,22 +65,41 @@ tm* get_exif_time(std::string path)
         fclose(filen);
         return nullptr;
     }
+    fclose(filen);*/
+
+    DEBUG("__GET_EXIF_TIME__ File size: " << file_size);
+    unsigned int shortened = file_size > 0x10000 ? 0x10000 : file_size;
+    DEBUG("__GET_EXIF_TIME__ Shortened size: " << shortened);
+    unsigned char *buf = new unsigned char[shortened];
+
+    if (fread(buf, 1, shortened, filen) != shortened)
+    {
+        delete[] buf;
+        fclose(filen);
+        ASSERT("__GET_EXIF_TIME__ Could not read file");
+        return nullptr;
+    }
     fclose(filen);
 
     easyexif::EXIFInfo exif;
-    if(exif.parseFrom(buf, file_size))
+    if(exif.parseFrom(buf, shortened))
     {
-        DEBUG("__GET_EXIF_TIME__ Could not parse file");
         delete[] buf;
+        ASSERT("__GET_EXIF_TIME__ Could not parse file");
         return nullptr;
     }
     delete[] buf;
 
     DEBUG("__GET_EXIF_TIME__ File parsed!");
+    if(exif.DateTimeOriginal.size()==0)
+    {
+        DEBUG("__GET_EXIF_TIME__ No original datetime found!");
+        delete[] buf;
+        return nullptr;
+    }
 
     tm *result = new tm;
     //tm *result = (tm*)malloc(sizeof(tm));
-    int bajs;
     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);
 
     DEBUG("__GET_EXIF_TIME__ DateTime: " << exif.DateTimeOriginal.c_str());
@@ -91,7 +114,9 @@ tm* get_exif_time(std::string path)
     result->tm_year -= 1900;
     result->tm_mon -= 1;
 
+    tm dont_touch_my_time = *result;
     time_t unix_time = mktime(result);
+    *result = dont_touch_my_time;
     DEBUG("__GET_EXIF_TIME__ Time: " << unix_time);
 
     // Sort of a sanity check
@@ -175,7 +200,7 @@ STATUS_CODE filefunctions::mv(std::string const &src_arg, std::string const &des
         return SC_DOES_NOT_EXIST;
     }
 
-    if(mkdir(dest.substr(0,dest.find_last_of('/\\'))))
+    if(mkdir(dest.substr(0,dest.find_last_of("/\\"))))
     {
         DEBUG("__MV__ Could not determine destination directory");
         return SC_INVALID_DESTINATION;
@@ -225,7 +250,7 @@ STATUS_CODE filefunctions::mkdir(std::string const &arg_path, COMMAND_FLAGS flag
         return SC_IS_NOT_DIRECTORY;
     }
 
-    size_t parent_separator = path.find_last_of('/\\');
+    size_t parent_separator = path.find_last_of("/\\");
     if(parent_separator == std::string::npos)
     {
         DEBUG("__MKDIR__ Invalid path");
@@ -340,7 +365,7 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
             else
             {
                 temp.modified = new tm;
-                memcpy(temp.modified, localtime(&unix_time), sizeof(tm));
+                memcpy(temp.modified, gmtime(&unix_time), sizeof(tm));
             }
 
             temp.file_ending = temp.path.substr(temp.path.find_last_of('.')+1);
@@ -349,8 +374,21 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
 
             if(temp.file_ending == "jpeg" || temp.file_ending == "jpg")
             {
+                temp.file_ending = "jpg";
                 DEBUG("__LS__ Found JPEG-file, decoding EXIF");
-                temp.taken = get_exif_time(temp.path);
+                LARGE_INTEGER dryg;
+                dryg.HighPart = file_data.nFileSizeHigh;
+                dryg.LowPart = file_data.nFileSizeLow;
+                temp.taken = get_exif_time(temp.path, dryg.QuadPart);
+                if(temp.taken)
+                {
+                    DEBUG("__LS__ year: " << temp.taken->tm_year);
+                    DEBUG("__LS__ month: " << temp.taken->tm_mon);
+                    DEBUG("__LS__ date: " << temp.taken->tm_mday);
+                    DEBUG("__LS__ hour: " << temp.taken->tm_hour);
+                    DEBUG("__LS__ minute: " << temp.taken->tm_min);
+                    DEBUG("__LS__ second: " << temp.taken->tm_sec);
+                }
             }
 
             content.push(temp);
@@ -365,5 +403,5 @@ STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &con
 
 #else
     // Fail?
-    Hård fail som fan!
+    HÃ¥rd fail som fan!
 #endif // __linux__

+ 1 - 0
image_sort.cbp

@@ -43,6 +43,7 @@
 		</Build>
 		<Compiler>
 			<Add option="-Wall" />
+			<Add option="-std=c++11" />
 			<Add option="-fexceptions" />
 		</Compiler>
 		<Unit filename="debugging.h" />

+ 15 - 1
main.cpp

@@ -25,7 +25,7 @@ int main(int argc, char **argv)
     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;
+    std::cout << "Klar med listning av filer. Flyttar nu enl. önskemål" << std::endl;
 
     while(files.size())
     {
@@ -51,12 +51,26 @@ int main(int argc, char **argv)
         if(temp.taken)
         {
             INFO("Moving based on date taken");
+
+            DEBUG("__GET_EXIF_TIME__ year: " << temp.taken->tm_year);
+            DEBUG("__GET_EXIF_TIME__ month: " << temp.taken->tm_mon);
+            DEBUG("__GET_EXIF_TIME__ date: " << temp.taken->tm_mday);
+            DEBUG("__GET_EXIF_TIME__ hour: " << temp.taken->tm_hour);
+            DEBUG("__GET_EXIF_TIME__ minute: " << temp.taken->tm_min);
+            DEBUG("__GET_EXIF_TIME__ second: " << temp.taken->tm_sec);
             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)
         {
             INFO("Moving based on date modified");
+
+            DEBUG("__GET_EXIF_TIME__ year: " << temp.modified->tm_year);
+            DEBUG("__GET_EXIF_TIME__ month: " << temp.modified->tm_mon);
+            DEBUG("__GET_EXIF_TIME__ date: " << temp.modified->tm_mday);
+            DEBUG("__GET_EXIF_TIME__ hour: " << temp.modified->tm_hour);
+            DEBUG("__GET_EXIF_TIME__ minute: " << temp.modified->tm_min);
+            DEBUG("__GET_EXIF_TIME__ second: " << temp.modified->tm_sec);
             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;
         }