Changeset 5029


Ignore:
Timestamp:
May 5, 2016, 7:18:02 AM (17 months ago)
Author:
cameron
Message:

Command line and output format improvements for wc - Posix compatibility

File:
1 edited

Legend:

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

    r5026 r5029  
    5656static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore, cl::cat(wcFlags));
    5757
    58 static cl::opt<bool> CountLines("l", cl::desc("Report the number of lines in each input file."), cl::init(false), cl::cat(wcFlags));
    59 static cl::opt<bool> CountWords("w", cl::desc("Report the number of lines in each input file."), cl::init(false), cl::cat(wcFlags));
    60 static cl::opt<bool> CountBytes("c", cl::desc("Report the number of bytes in each input file."), cl::init(false), cl::cat(wcFlags));
    61 static cl::opt<bool> CountChars("m", cl::desc("Report the number of characters in each input file."), cl::init(false), cl::cat(wcFlags));
    62 
    63 
     58enum CountOptions {
     59    LineOption, WordOption, CharOption, ByteOption
     60};
     61
     62static cl::list<CountOptions> wcOptions(cl::desc("Counting options."),
     63  cl::values(clEnumValN(LineOption, "l", "Report the number of lines in each input file."),
     64             clEnumValN(WordOption, "w", "Report the number of words in each input file."),
     65             clEnumValN(CharOption, "m", "Report the number of characters in each input file (override -c)."),
     66             clEnumValN(ByteOption, "c", "Report the number of bytes in each input file (override -m)."),
     67             clEnumValEnd), cl::cat(wcFlags), cl::Grouping);
     68                                                 
    6469static cl::OptionCategory eIRDumpOptions("LLVM IR Dump Options", "These options control dumping of LLVM IR.");
    6570static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(eIRDumpOptions));
     
    7479
    7580static int defaultFieldWidth = 7;  // default field width
     81
     82
     83bool CountLines = false;
     84bool CountWords = false;
     85bool CountChars = false;
     86bool CountBytes = false;
    7687
    7788std::vector<uint64_t> lineCount;
     
    8596uint64_t TotalBytes = 0;
    8697
    87 //
     98
     99//  The callback routine that records counts in progress.
    88100//
    89101extern "C" {
    90     void report_counts(uint64_t lines, uint64_t words, uint64_t chars, uint64_t bytes, uint64_t fileIdx) {
     102    void record_counts(uint64_t lines, uint64_t words, uint64_t chars, uint64_t bytes, uint64_t fileIdx) {
    91103        lineCount[fileIdx] = lines;
    92104        wordCount[fileIdx] = words;
     
    99111    }
    100112}
    101 
    102 
    103113
    104114//
     
    251261
    252262Function * wcPipelineBuilder::ExecuteKernels() {
    253     Constant * report_counts_routine;
     263    Constant * record_counts_routine;
    254264    Type * const int64ty = iBuilder->getInt64Ty();
    255265    Type * const voidTy = Type::getVoidTy(mMod->getContext());
    256     report_counts_routine = mMod->getOrInsertFunction("report_counts", voidTy, int64ty, int64ty, int64ty, int64ty, int64ty, nullptr);
     266    record_counts_routine = mMod->getOrInsertFunction("record_counts", voidTy, int64ty, int64ty, int64ty, int64ty, int64ty, nullptr);
    257267    Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
    258268   
     
    356366    Value * charCount = iBuilder->CreateExtractElement(iBuilder->CreateBlockAlignedLoad(wcInstance->getOutputStream(2)), iBuilder->getInt32(0));
    357367   
    358     iBuilder->CreateCall(report_counts_routine, std::vector<Value *>({lineCount, wordCount, charCount, bufferSize, fileIdx}));
     368    iBuilder->CreateCall(record_counts_routine, std::vector<Value *>({lineCount, wordCount, charCount, bufferSize, fileIdx}));
    359369   
    360370    iBuilder->CreateRetVoid();
     
    442452
    443453    cl::ParseCommandLineOptions(argc, argv);
    444     if (!(CountLines || CountWords || CountChars || CountBytes)) {
     454    if (wcOptions.size() == 0) {
    445455        CountLines = true;
    446456        CountWords = true;
    447457        CountBytes = true;
    448458    }
     459    else {
     460        CountLines = false;
     461        CountWords = false;
     462        CountBytes = false;
     463        CountChars = false;
     464        for (unsigned i = 0; i < wcOptions.size(); i++) {
     465            switch (wcOptions[i]) {
     466                case WordOption: CountWords = true; break;
     467                case LineOption: CountLines = true; break;
     468                case CharOption: CountBytes = true; CountChars = false; break;
     469                case ByteOption: CountChars = true; CountBytes = false; break;
     470            }
     471        }
     472    }
     473   
    449474   
    450475    wcFunctionType fn_ptr = wcCodeGen();
     
    472497
    473498    for (unsigned i = 0; i < inputFiles.size(); ++i) {
     499        std::cout << std::setw(fieldWidth-1);
    474500        if (CountLines) {
    475             std::cout << std::setw(fieldWidth) << lineCount[i];
     501            std::cout << lineCount[i] << std::setw(fieldWidth);
    476502        }
    477503        if (CountWords) {
    478             std::cout << std::setw(fieldWidth) << wordCount[i];
     504            std::cout << wordCount[i] << std::setw(fieldWidth);
    479505        }
    480506        if (CountChars) {
    481             std::cout << std::setw(fieldWidth) << charCount[i];
     507            std::cout << charCount[i] << std::setw(fieldWidth);
    482508        }
    483509        if (CountBytes) {
    484             std::cout << std::setw(fieldWidth) << byteCount[i];
     510            std::cout << byteCount[i];
    485511        }
    486512        std::cout << " " << inputFiles[i] << std::endl;
    487513    }
    488514    if (inputFiles.size() > 1) {
     515        std::cout << std::setw(fieldWidth-1);
    489516        if (CountLines) {
    490             std::cout << std::setw(fieldWidth) << TotalLines;
     517            std::cout << TotalLines << std::setw(fieldWidth);
    491518        }
    492519        if (CountWords) {
    493             std::cout << std::setw(fieldWidth) << TotalWords;
     520            std::cout << TotalWords << std::setw(fieldWidth);
    494521        }
    495522        if (CountChars) {
    496             std::cout << std::setw(fieldWidth) << TotalChars;
     523            std::cout << TotalChars << std::setw(fieldWidth);
    497524        }
    498525        if (CountBytes) {
    499             std::cout << std::setw(fieldWidth) << TotalBytes;
     526            std::cout << TotalBytes;
    500527        }
    501528        std::cout << " total" << std::endl;
Note: See TracChangeset for help on using the changeset viewer.