Changeset 5020


Ignore:
Timestamp:
May 2, 2016, 12:52:09 PM (17 months ago)
Author:
cameron
Message:

Set fieldwith based on maximum line/word/char/byte count reported.

File:
1 edited

Legend:

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

    r5019 r5020  
    7272
    7373
    74 static int fieldWidth = 9;  // default field width
     74static int defaultFieldWidth = 7;  // default field width
     75
     76std::vector<uint64_t> lineCount;
     77std::vector<uint64_t> wordCount;
     78std::vector<uint64_t> charCount;
     79std::vector<uint64_t> byteCount;
    7580
    7681uint64_t TotalLines = 0;
     
    8388extern "C" {
    8489    void report_counts(uint64_t lines, uint64_t words, uint64_t chars, uint64_t bytes, uint64_t fileIdx) {
    85         if (CountLines) {
    86             std::cout << std::setw(fieldWidth) << lines;
    87             TotalLines += lines;
    88         }
    89         // FIXME Temporary Hack because null bytes at EOF are considered word chars.
    90         if (CountWords) {
    91             std::cout << std::setw(fieldWidth) << words - 1;
    92             TotalWords += words - 1;
    93         }
    94         if (CountChars) {
    95             std::cout << std::setw(fieldWidth) << chars ;
    96             TotalChars += chars;
    97         }
    98         if (CountBytes) {
    99             std::cout << std::setw(fieldWidth) << bytes;
    100             TotalBytes += bytes;
    101         }
    102         std::cout << " " << inputFiles[fileIdx] << std::endl;
     90        lineCount[fileIdx] = lines;
     91        wordCount[fileIdx] = words;
     92        charCount[fileIdx] = chars;
     93        byteCount[fileIdx] = bytes;
     94        TotalLines += lines;
     95        TotalWords += words;
     96        TotalChars += chars;
     97        TotalBytes += bytes;
    10398    }
    10499}
     
    481476    wcFunctionType fn_ptr = wcCodeGen();
    482477
    483     for (unsigned i = 0; i != inputFiles.size(); ++i) {
     478    int fileCount = inputFiles.size();
     479    lineCount.resize(fileCount);
     480    wordCount.resize(fileCount);
     481    charCount.resize(fileCount);
     482    byteCount.resize(fileCount);
     483   
     484    for (unsigned i = 0; i < inputFiles.size(); ++i) {
    484485        wc(fn_ptr, i);
    485486    }
     
    487488    delete wcEngine;
    488489   
     490    int maxCount = 0;
     491    if (CountLines) maxCount = TotalLines;
     492    if (CountWords) maxCount = TotalWords;
     493    if (CountChars) maxCount = TotalChars;
     494    if (CountBytes) maxCount = TotalBytes;
     495   
     496    int fieldWidth = std::to_string(maxCount).size() + 1;
     497    if (fieldWidth < defaultFieldWidth) fieldWidth = defaultFieldWidth;
     498
     499    for (unsigned i = 0; i < inputFiles.size(); ++i) {
     500        if (CountLines) {
     501            std::cout << std::setw(fieldWidth) << lineCount[i];
     502        }
     503        if (CountWords) {
     504            std::cout << std::setw(fieldWidth) << wordCount[i];
     505        }
     506        if (CountChars) {
     507            std::cout << std::setw(fieldWidth) << charCount[i];
     508        }
     509        if (CountBytes) {
     510            std::cout << std::setw(fieldWidth) << byteCount[i];
     511        }
     512        std::cout << " " << inputFiles[i] << std::endl;
     513    }
    489514    if (inputFiles.size() > 1) {
    490515        if (CountLines) {
Note: See TracChangeset for help on using the changeset viewer.