Example #1
0
void ConsoleTools::viewFile(){
  try{
    // Source file text lines store.
    TextLinesStore textLinesStore;
    textLinesStore.loadFile(inputFileName, inputEncoding, true);
    // parsers factory
    ParserFactory pf(catalogPath);
    // Base editor to make primary parse
    BaseEditor baseEditor(&pf, &textLinesStore);
    // HRD RegionMapper linking
    baseEditor.setRegionMapper(&DString("console"), hrdName);
    FileType *type = selectType(pf.getHRCParser(), &textLinesStore);
    baseEditor.setFileType(type);
    // Initial line count notify
    baseEditor.lineCountEvent(textLinesStore.getLineCount());

    int background;
    const StyledRegion *rd = StyledRegion::cast(baseEditor.rd_def_Text);
    if (rd != null && rd->bfore && rd->bback) background = rd->fore + (rd->back<<4);
    else background = 0x1F;
    // File viewing in console window
    TextConsoleViewer viewer(&baseEditor, &textLinesStore, background, outputEncodingIndex);
    viewer.view();
  }catch(Exception &e){
    fprintf(stderr, "%s\n", e.getMessage()->getChars());
  }catch(...){
    fprintf(stderr, "unknown exception ...\n");
  }
}
Example #2
0
void ConsoleTools::profile(int loopCount){
  clock_t msecs;

  // parsers factory
  ParserFactory pf(catalogPath);
  // Source file text lines store.
  TextLinesStore textLinesStore;
  textLinesStore.loadFile(inputFileName, inputEncoding, true);
  // Base editor to make primary parse
  BaseEditor baseEditor(&pf, &textLinesStore);
  // HRD RegionMapper linking
  baseEditor.setRegionMapper(&DString("console"), hrdName);
  FileType *type = selectType(pf.getHRCParser(), &textLinesStore);
  type->getBaseScheme();
  baseEditor.setFileType(type);

  msecs = clock();
  while(loopCount--){
    baseEditor.modifyLineEvent(0);
    baseEditor.lineCountEvent(textLinesStore.getLineCount());
    baseEditor.validate(-1, false);
  }
  msecs = clock() - msecs;

  printf("%ld\n", (msecs*1000)/CLOCKS_PER_SEC );
}
int ColorerFilter::SendOutput() {
    DString styleType = DString("rgb");

    ap_set_content_type(kFilter->r,"text/html; charset=utf-8");

    try {
        // parsers factory
        ParserFactory pf(catalogPath);
        // HRC loading
        HRCParser *hrcParser = pf.getHRCParser();

        RegionMapper *mapper = null;
        bool useMarkup = false;

        try {
            mapper = pf.createStyledMapper(&styleType, hrdName);
        }
        catch(ParserFactoryException &e) {
            useMarkup = true;
            mapper = pf.createTextMapper(hrdName);
        };

        // Base editor to make primary parse
        BaseEditor baseEditor(&pf, pSource);
        // Using compact regions
        baseEditor.setRegionCompact(true);
        baseEditor.setRegionMapper(mapper);
        baseEditor.lineCountEvent(pSource->getLineCount());
        // Choosing file type
        FileType *type = selectType(hrcParser, pSource->getLine(0));
        baseEditor.setFileType(type);
        // parsing all the text
        baseEditor.validate(-1);

        Hashtable<String*> *docLinkHash = new Hashtable<String*>;
        BucketWriter* myWriter = new BucketWriter(kFilter);
        Writer* escapedWriter = new HtmlEscapesWriter(myWriter);
        Writer* cwrite = myWriter;

#if 0 /* Add Configuration Options to add HTML Header.. etc */
        if()
            cwrite->write(DString("<html>\n<head>\n<style></style>\n</head>\n<body><pre>\n"));
#endif

        cwrite->write(DString("<pre>\n"));
        for(int i = 0; i < pSource->getLineCount(); i++){
            htmlRGBWrite(myWriter, escapedWriter, docLinkHash, 
                         pSource->getLine(i), baseEditor.getLineRegions(i));
        }

#if 0 /* Add Configuration Options to add HTML Header.. etc */
        if()
            cwrite->write(DString("</pre></body></html>\n"));
#endif
        cwrite->write(DString("</pre>\n"));

        myWriter->SendBrigade();
        delete docLinkHash;
        delete escapedWriter;
        delete myWriter;
    }
    catch(Exception &e) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, kFilter->r, 
                      "ColorerFilter::SendOutput(): uncaught exception: %s",
                      e.getMessage()->getChars());    
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    catch(...) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, kFilter->r, "ColorerFilter::SendOutput(): unknown exception!");    
        return HTTP_INTERNAL_SERVER_ERROR;
    };

    return OK;
}
Example #4
0
void ConsoleTools::genOutput(bool useTokens){
  try{
    // Source file text lines store.
    TextLinesStore textLinesStore;
    textLinesStore.loadFile(inputFileName, inputEncoding, true);
    // parsers factory
    ParserFactory pf(catalogPath);
    // HRC loading
    HRCParser *hrcParser = pf.getHRCParser();
    // HRD RegionMapper creation
    bool useMarkup = false;
    RegionMapper *mapper = null;
    if (!useTokens){
      try{
        mapper = pf.createStyledMapper(&DString("rgb"), hrdName);
      }catch(ParserFactoryException &){
        useMarkup = true;
        mapper = pf.createTextMapper(hrdName);
      }
    }
    // Base editor to make primary parse
    BaseEditor baseEditor(&pf, &textLinesStore);
    // Using compact regions
    baseEditor.setRegionCompact(true);
    baseEditor.setRegionMapper(mapper);
    baseEditor.lineCountEvent(textLinesStore.getLineCount());
    // Choosing file type
    FileType *type = selectType(hrcParser, &textLinesStore);
    baseEditor.setFileType(type);

    //  writing result into HTML colored stream...
    const RegionDefine *rd = null;
    if (mapper != null) rd = baseEditor.rd_def_Text;

    Writer *escapedWriter = null;
    Writer *commonWriter = null;
    try{
      if (outputFileName != null) commonWriter = new FileWriter(outputFileName, outputEncodingIndex, bomOutput);
      else commonWriter = new StreamWriter(stdout, outputEncodingIndex, bomOutput);
      if (htmlEscaping) escapedWriter = new HtmlEscapesWriter(commonWriter);
      else escapedWriter = commonWriter;
    }catch(Exception &e){
      fprintf(stderr, "can't open file '%s' for writing:\n", outputFileName->getChars());
      fprintf(stderr, e.getMessage()->getChars());
      return;
    }

    if (htmlWrapping && useTokens){
      commonWriter->write(DString("<html>\n<head>\n<style></style>\n</head>\n<body><pre>\n"));
    }else if (htmlWrapping && rd != null){
      if (useMarkup){
        commonWriter->write(TextRegion::cast(rd)->stext);
      }else{
        commonWriter->write(DString("<html><body style='"));
        ParsedLineWriter::writeStyle(commonWriter, StyledRegion::cast(rd));
        commonWriter->write(DString("'><pre>\n"));
      }
    }

    if (copyrightHeader){
      commonWriter->write(DString("Created with colorer-take5 library. Type '"));
      commonWriter->write(type->getName());
      commonWriter->write(DString("'\n\n"));
    }

    int lni = 0;
    int lwidth = 1;
    int lncount = textLinesStore.getLineCount();
    for(lni = lncount/10; lni > 0; lni = lni/10, lwidth++);

    for(int i = 0; i < lncount; i++){
      if (lineNumbers){
        int iwidth = 1;
        for(lni = i/10; lni > 0; lni = lni/10, iwidth++);
        for(lni = iwidth; lni < lwidth; lni++) commonWriter->write(0x0020);
        commonWriter->write(SString(i));
        commonWriter->write(DString(": "));
      }
      if (useTokens){
        ParsedLineWriter::tokenWrite(commonWriter, escapedWriter, docLinkHash, textLinesStore.getLine(i), baseEditor.getLineRegions(i));
      }else if (useMarkup){
        ParsedLineWriter::markupWrite(commonWriter, escapedWriter, docLinkHash, textLinesStore.getLine(i), baseEditor.getLineRegions(i));
      }else{
        ParsedLineWriter::htmlRGBWrite(commonWriter, escapedWriter, docLinkHash, textLinesStore.getLine(i), baseEditor.getLineRegions(i));
      }
      commonWriter->write(DString("\n"));
    }

    if (htmlWrapping && useTokens){
      commonWriter->write(DString("</pre></body></html>\n"));
    }else if (htmlWrapping && rd != null){
      if (useMarkup){
        commonWriter->write(TextRegion::cast(rd)->etext);
      }else{
        commonWriter->write(DString("</pre></body></html>\n"));
      }
    }

    if (htmlEscaping) delete commonWriter;
    delete escapedWriter;

    delete mapper;
  }catch(Exception &e){
    fprintf(stderr, "%s\n", e.getMessage()->getChars());
  }catch(...){
    fprintf(stderr, "unknown exception ...\n");
  }
}