Added some exception handling. Marked all the functions throwing
[emufront] / src / utils / unziphelper.cpp
index e58ed03..0304e5b 100644 (file)
 
 const QString UnzipHelper::UNZIP_COMMAND = "unzip ";
 const QString UnzipHelper::UNZIP_LIST_ARGS = "-lv ";
+const QString UnzipHelper::UNZIP_EXTRACT_ARGS= "-qqo ";
 
 UnzipHelper::UnzipHelper(QObject *parent) :
     ProcessHelper(parent)
 {
 }
 
-QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
+/* Throws EmuFrontException */
+QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
 {
     if (!fp->getSetup()){
         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
@@ -40,11 +42,11 @@ QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FileP
 
     QFile fl(filePath);
     if (!fl.open(QIODevice::ReadOnly)) {
-        throw new EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
+        throw EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
     }
 
-    Setup *sup = fp->getSetup();
-    QList<MediaImage*>  fileList;
+    //Setup *sup = fp->getSetup();
+    QMap<QString, EmuFrontObject*>  fileList;
 
     QString command;
     command.append(UNZIP_COMMAND);
@@ -52,7 +54,7 @@ QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FileP
     command.append("\"");
     command.append(filePath);
     command.append("\"");
-    qDebug() << command;
+    //qDebug() << command;
     start(command);
     // TODO: slot(s) for (start and) error signal(s)
     bool procOk = waitForFinished();
@@ -61,7 +63,7 @@ QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FileP
     }
     QString err = readAllStandardError();
     QString msg = readAllStandardOutput();
-    qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
+    //qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
 
     /*
 
@@ -98,45 +100,52 @@ QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FileP
         //QRegExp("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$")
         );
     QStringList entries;
-    QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
-    QRegExp regExEntries("^\\s+(\\d+)\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+([0-9a-f]{8})\\s+(\\S.+)$");
+    //QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
+    QRegExp regExEntries(
+        "^"             // line starts
+        "\\s*"          // 1st empty space is optional!
+        "(\\d+)"        // uncompressed "Length" in digits
+        "\\s+"
+        "[A-Za-z:]*"    // "Method"
+        "\\s+"
+        "\\d+"          // compressed "Size"
+        "\\s+"
+        "\\d{1,3}%"     // compression ratio
+        "\\s+"
+        "\\d{2,4}-\\d{2}-\\d{2,4}" // date
+        "\\s+"
+        "\\d{2}:\\d{2}" // time
+        "\\s+"
+        "([0-9a-f]{8})" // CRC-32
+        "\\s+"
+        "(\\S.*)"       // at least one non whitespace character + optional other chars (including whitespace)
+        "$"             // line ends
+        );
     foreach(QString ln, lines) {
-        if (!test.exactMatch(ln)) continue;
+        //if (!test.exactMatch(ln)) continue;
+        //qDebug() << "Current line is " << ln;
         int pos = regExEntries.indexIn(ln);
+        if (pos == -1) {
+            //qDebug() << "Regex didn't match any entries.";
+            continue; // > no entries
+        }
         entries = regExEntries.capturedTexts();
+        //qDebug() << "Got " << entries.count() << " entries.";
         if (entries.count() < 4) continue;
         QString filename = entries[3];
         QString checksum = entries[2];
         QString lenStr = entries[1];
         bool ok;
         int length = lenStr.toInt(&ok);
+        /*qDebug() << "Filename is " << filename << " checksum "
+            << checksum << " length " << length;*/
         if (!ok) continue;
         MediaImage *effo = new MediaImage(filename, checksum, length);
-        fileList << effo;
+        fileList[checksum] = effo;
     }
 
-    /*UnZip uz;
-    UnZip::ErrorCode ec = uz.openArchive(filePath);
-    if (ec != UnZip::Ok)
-        throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
-
-    QList<UnZip::ZipEntry> list = uz.entryList();
-    foreach(UnZip::ZipEntry entry, list)
-    {
-        qDebug() << "Zip entry " << entry.filename;
-        if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
-        {
-            QString checksum = QString("%1").arg(entry.crc32, 0, 16);
-            qDebug() << "Checksum " << checksum;
-            MediaImage *effo = new MediaImage(entry.filename,
-                checksum, entry.uncompressedSize);
-            fileList << effo;
-        }
-    }*/
-
-    qDebug() << "File list has " << fileList.size() << " entries.";
+    //qDebug() << "File list has " << fileList.size() << " entries.";
     return fileList;
-
 }
 
 /*
@@ -158,14 +167,15 @@ int UnzipHelper::extractAll(QString filePath, QString targetPath)
     // unzip filepath -d targetpath
     QString command;
     command.append(UNZIP_COMMAND);
+    command.append(UNZIP_EXTRACT_ARGS);
     command.append("\"");
     command.append(filePath);
     command.append("\"");
     command.append(" -d ");
     command.append(targetPath);
-    qDebug() << "Starting unzip command: " << command;
+    //qDebug() << "Starting unzip command: " << command;
     start(command);
-    bool procOk = waitForFinished(); // TODO: set timeout, now using default 30000ms
+    bool procOk = waitForFinished( ); // TODO: set timeout, now using default 30000ms
     if (!procOk) {
         throw EmuFrontException(tr("Failed unzipping file '%1' to '%2'.").arg(filePath).arg(targetPath));
     }