Added some exception handling. Marked all the functions throwing
[emufront] / src / utils / unziphelper.cpp
index 7472247..0304e5b 100644 (file)
@@ -33,6 +33,7 @@ UnzipHelper::UnzipHelper(QObject *parent) :
 {
 }
 
+/* Throws EmuFrontException */
 QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
 {
     if (!fp->getSetup()){
@@ -41,10 +42,10 @@ QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath,
 
     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();
+    //Setup *sup = fp->getSetup();
     QMap<QString, EmuFrontObject*>  fileList;
 
     QString command;
@@ -53,7 +54,7 @@ QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath,
     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();
@@ -62,7 +63,7 @@ QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath,
     }
     QString err = readAllStandardError();
     QString msg = readAllStandardOutput();
-    qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
+    //qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
 
     /*
 
@@ -99,26 +100,52 @@ QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath,
         //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[checksum] = effo;
     }
 
-    qDebug() << "File list has " << fileList.size() << " entries.";
+    //qDebug() << "File list has " << fileList.size() << " entries.";
     return fileList;
-
 }
 
 /*
@@ -146,9 +173,9 @@ int UnzipHelper::extractAll(QString filePath, QString targetPath)
     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));
     }