• 让每一步都跳出,这样不破坏整体流程,可以对结果随心所欲的控制(线程中断也可采用这种方法)


    中间彻底中断整个流程的话,就不知道整体到底怎么回事了。而且会造成资源泄漏、程序员得不到预定的结果(哪怕是失败的结果,也要按照程序员的格式来记录)。

    此外,内部原子操作不必记录错误(否则对同一个文件错误,会有太多的记录,反而也没法再细究了),记录外部的逻辑结果错误即可。

    这样,还可以一次性返回 return success1 && success2 && success3 是个好方法,不中断整体流程:

    QString MyTool::ZipAES(QString strSrcFile, QString passwd, int bCompress)
    {
        bool res = ZipAES(szDestFile, szSrcFile, szPasswd, bCompress, szNewFile);
        if (!res) {
            InsertToolLog("debug", m_taskname, "compress", strSrcFile+" "+tr("cannot be compressed"));
            // return "";
        }
    
        QString strNewFile = QString::fromLocal8Bit(szDestFile); // codage
    
        return strNewFile; // 返回的是全路径
    }
    
    bool MyTool::ZipAES(char* szDestFile, char* szSrcFile, char* passwd, int bCompress, char* szNewFileName)
    {
        // ------------------------------- 准备工作 -------------------------------
        QString file_full=QString::fromLocal8Bit(szSrcFile); // codage error
    
        const QFileInfo fi = QFileInfo(file_full);     
        if (!fi.exists()) {
            InsertToolLog("debug", m_taskname, "compress", QString(file_full)+" file doesnot exist ");
            return false;
        }
        QDateTime last = fi.lastModified();
        QString strTime = last.toString("yyyyMMddhhmmss");
        if (strTime.isEmpty()) {
            InsertToolLog("debug", m_taskname, "compress", QString(file_full)+" time is null ");
            return false;
        }
    
        QString strFileName = "DATET"+strTime+"-"+fi.fileName()+".zip";
        QString strFinal = m_strTempFolder+"/"+strFileName;
    
        QByteArray ba = strFinal.toLocal8Bit();
        char* tempPath=ba.data();
        strcpy(szDestFile, tempPath);
        ba = strFileName.toLocal8Bit();
        tempPath=ba.data();
        strcpy(szNewFileName, tempPath);
    
        // ------------------------------- 流程控制 -------------------------------
        // hang exp 开始压缩后,三个动作是一气呵成的,不必单独判断三次,只要看整体结果即可。压缩过程中,则有回调函数帮忙
        m_thread->UpdateStop("try to zip "+QString::fromLocal8Bit(szSrcFile)); if (m_ignore) return false;
    
        // ------------------------------- 正式压缩 -------------------------------
        // m_zip->put_Utf8 // codage
        bool success1 = m_zip->NewZip(szDestFile);
        if (success1 != true) {
            qDebug() << QString(m_zip->lastErrorText());
            InsertToolLog("debug", m_taskname, "compress", QString(szDestFile)+" NewZip fails");
            //return false;
        }
    
        bool success2 = m_zip->AppendOneFileOrDir(szSrcFile, false);
        if (success2 != true) {
            qDebug() << QString(m_zip->lastErrorText());
            // hang exp 外边包装函数也记录结果,那么这里就不用再记录了,虽然不是具体到每一步,不过也已经很清楚了。就算知道是哪个函数,也没用,没法再分割了
            //return false; // 这里就更不必返回了
        }
    
        bool success3 = m_zip->WriteZipAndClose();
        if (success3 != true) {
            qDebug() << QString(m_zip->lastErrorText());
        }
    
        return success1 && success2 && success1; // Archi exp 这个方法好,既不会中断前面的各个步骤,也可联合报告结果,供外部使用结果
    }

    另外,线程的中断也可采用这个方法,让每一个子步骤不断跳出。因为QT里不让直接中断线程,采取这种方法也没有问题。

  • 相关阅读:
    IPC总结学习
    机器学习中的范数规则
    机器学习的几个误区-转载
    来几道大数据的面试题吧
    海量数据随机抽样问题(蓄水池问题)
    字符串类算法题目总结
    RPC学习
    如何做出健壮的系统设计
    关于bind函数和connect函数的测试结论
    [置顶] Codeforces Round #197 (Div. 2)(完全)
  • 原文地址:https://www.cnblogs.com/findumars/p/4722796.html
Copyright © 2020-2023  润新知