Ignore:
Timestamp:
Dec 9, 2017, 5:05:16 PM (17 months ago)
Author:
nmedfort
Message:

Minor changes and hopefully a fix for bug exposed by base64 test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5770 r5771  
    8585    const unsigned n = filenames.size();
    8686    mResultStrs.resize(n);
    87     mFileStatus.resize(n);
    88     for (unsigned i = 0; i < n; i++) {
    89         mResultStrs[i] = make_unique<std::stringstream>();
    90         mFileStatus[i] = FileStatus::Pending;
    91     }
     87    mFileStatus.resize(n, FileStatus::Pending);
    9288    inputFiles = filenames;
    9389}
     
    221217    friend class EmitMatchesEngine;
    222218public:
    223     EmitMatch(std::string linePrefix, std::stringstream * strm) : mLinePrefix(linePrefix), mLineCount(0), mTerminated(true), mResultStr(strm) {}
     219    EmitMatch(std::string linePrefix, std::ostringstream & strm) : mLinePrefix(linePrefix), mLineCount(0), mTerminated(true), mResultStr(strm) {}
    224220    void accumulate_match(const size_t lineNum, char * line_start, char * line_end) override;
    225221    void finalize_match(char * buffer_end) override;
     
    228224    size_t mLineCount;
    229225    bool mTerminated;
    230     std::stringstream* mResultStr;
     226    std::ostringstream & mResultStr;
    231227};
    232228
     
    237233void EmitMatch::accumulate_match (const size_t lineNum, char * line_start, char * line_end) {
    238234    if (WithFilenameFlag) {
    239         *mResultStr << mLinePrefix;
     235        mResultStr << mLinePrefix;
    240236    }
    241237    if (LineNumberFlag) {
     
    243239        // the line number so that lines are numbered from 1.
    244240        if (InitialTabFlag) {
    245             *mResultStr << lineNum+1 << "\t:";
     241            mResultStr << lineNum+1 << "\t:";
    246242        }
    247243        else {
    248             *mResultStr << lineNum+1 << ":";
     244            mResultStr << lineNum+1 << ":";
    249245        }
    250246    }
    251247    size_t bytes = line_end - line_start + 1;
    252     mResultStr->write(line_start, bytes);
     248    mResultStr.write(line_start, bytes);
    253249    mLineCount++;
    254250    unsigned last_byte = *line_end;
     
    268264
    269265void EmitMatch::finalize_match(char * buffer_end) {
    270     if (!mTerminated) *mResultStr << "\n";
     266    if (!mTerminated) mResultStr << "\n";
    271267}
    272268
     
    321317    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
    322318
    323     int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx].get());
     319    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
    324320    if (fileDescriptor == -1) return 0;
    325321
     
    331327uint64_t CountOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
    332328    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
    333     if (WithFilenameFlag) *mResultStrs[fileIdx] << linePrefix(fileName);
    334     *mResultStrs[fileIdx] << grepResult << "\n";
     329    if (WithFilenameFlag) mResultStrs[fileIdx] << linePrefix(fileName);
     330    mResultStrs[fileIdx] << grepResult << "\n";
    335331    return grepResult;
    336332}
     
    348344    uint64_t grepResult = GrepEngine::doGrep(fileName, fileIdx);
    349345    if (grepResult == mRequiredCount) {
    350        *mResultStrs[fileIdx] << linePrefix(fileName);
     346       mResultStrs[fileIdx] << linePrefix(fileName);
    351347    }
    352348    return grepResult;
     
    357353    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
    358354
    359     int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx].get());
     355    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
    360356    if (fileDescriptor == -1) return 0;
    361     EmitMatch accum(linePrefix(fileName), mResultStrs[fileIdx].get());
     357    EmitMatch accum(linePrefix(fileName), mResultStrs[fileIdx]);
    362358    f(fileDescriptor, reinterpret_cast<intptr_t>(&accum));
    363359    close(fileDescriptor);
     
    367363
    368364// Open a file and return its file desciptor.
    369 int32_t GrepEngine::openFile(const std::string & fileName, std::stringstream * msgstrm) {
     365int32_t GrepEngine::openFile(const std::string & fileName, std::ostringstream & msgstrm) {
    370366    if (fileName == "-") {
    371367        return STDIN_FILENO;
     
    377373            if (!NoMessagesFlag) {
    378374                if (errno == EACCES) {
    379                     *msgstrm << "icgrep: " << fileName << ": Permission denied.\n";
     375                    msgstrm << "icgrep: " << fileName << ": Permission denied.\n";
    380376                }
    381377                else if (errno == ENOENT) {
    382                     *msgstrm << "icgrep: " << fileName << ": No such file.\n";
     378                    msgstrm << "icgrep: " << fileName << ": No such file.\n";
    383379                }
    384380                else {
    385                     *msgstrm << "icgrep: " << fileName << ": Failed.\n";
     381                    msgstrm << "icgrep: " << fileName << ": Failed.\n";
    386382                }
    387383            }
     
    390386        if (stat(fileName.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
    391387            if (!NoMessagesFlag) {
    392                 *msgstrm << "icgrep: " << fileName << ": Is a directory.\n";
     388                msgstrm << "icgrep: " << fileName << ": Is a directory.\n";
    393389            }
    394390            close(fileDescriptor);
     
    433429void * GrepEngine::DoGrepThreadMethod() {
    434430
    435     auto fileIdx = mNextFileToGrep++;
     431    unsigned fileIdx = mNextFileToGrep++;
    436432    while (fileIdx < inputFiles.size()) {
    437         const size_t grepResult = doGrep(inputFiles[fileIdx], fileIdx);
     433        const auto grepResult = doGrep(inputFiles[fileIdx], fileIdx);
    438434        mFileStatus[fileIdx] = FileStatus::GrepComplete;
    439435        if (grepResult > 0) {
     
    449445    }
    450446
    451     auto printIdx = mNextFileToPrint++;
     447    unsigned printIdx = mNextFileToPrint++;
    452448    while (printIdx < inputFiles.size()) {
    453449        const bool readyToPrint = ((printIdx == 0) || (mFileStatus[printIdx - 1] == FileStatus::PrintComplete)) && (mFileStatus[printIdx] == FileStatus::GrepComplete);
    454450        if (readyToPrint) {
    455             const auto output = mResultStrs[printIdx]->str();
     451            const auto output = mResultStrs[printIdx].str();
    456452            if (!output.empty()) {
    457                 mWriteMutex.lock();
    458                 std::cout << output;
    459                 mWriteMutex.unlock();
     453                llvm::outs() << output;
    460454            }
    461455            mFileStatus[printIdx] = FileStatus::PrintComplete;
    462456            printIdx = mNextFileToPrint++;
    463457        } else {
    464             mCacheMutex.lock();
    465458            mGrepDriver->performIncrementalCacheCleanupStep();
    466             mCacheMutex.unlock();
    467459        }
    468460        sched_yield();
Note: See TracChangeset for help on using the changeset viewer.