"Fossies" - the Fresh Open Source Software Archive

Member "ansifilter-2.18/src/cmdlineoptions.cpp" (30 Jan 2021, 13639 Bytes) of package /linux/privat/ansifilter-2.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cmdlineoptions.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.16_vs_2.17.

    1 /***************************************************************************
    2                           cmdlineoptions.cpp  -  description
    3                              -------------------
    4     begin                : Sun Oct 13 2007
    5     copyright            : (C) 2007-2020 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 /*
   10 This file is part of ANSIFilter.
   11 
   12 ANSIFilter is free software: you can redistribute it and/or modify
   13 it under the terms of the GNU General Public License as published by
   14 the Free Software Foundation, either version 3 of the License, or
   15 (at your option) any later version.
   16 
   17 ANSIFilter is distributed in the hope that it will be useful,
   18 but WITHOUT ANY WARRANTY; without even the implied warranty of
   19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   20 GNU General Public License for more details.
   21 
   22 You should have received a copy of the GNU General Public License
   23 along with ANSIFilter.  If not, see <http://www.gnu.org/licenses/>.
   24 */
   25 
   26 #include <cstring>
   27 #include <string>
   28 #include <vector>
   29 #include <list>
   30 
   31 #include "arg_parser.h"
   32 #include "cmdlineoptions.h"
   33 #include "platform_fs.h"
   34 #include "stringtools.h"
   35 
   36 using namespace std;  
   37     
   38 const Arg_parser::Option options[] = {
   39     { 'a', "anchors",    Arg_parser::maybe },
   40     { 'd', "doc-title",  Arg_parser::yes },
   41     { 'e', "encoding",   Arg_parser::yes },
   42     { 'f', "fragment",   Arg_parser::no  },
   43     { 'F', "font",       Arg_parser::yes },
   44     { 'h', "help",       Arg_parser::no  },
   45     { 'H', "html",       Arg_parser::no  },
   46     { 'M', "pango",      Arg_parser::no  },
   47     { 'i', "input",      Arg_parser::yes },
   48     { 'l', "line-numbers", Arg_parser::no },
   49     { 'L', "latex",      Arg_parser::no  },
   50     { 'P', "tex",        Arg_parser::no  },
   51     { 'B', "bbcode",     Arg_parser::no  },
   52     { 'o', "output",     Arg_parser::yes },
   53     { 'O', "outdir",     Arg_parser::yes },
   54     { 'p', "plain",      Arg_parser::no  },
   55     { 'r', "style-ref",  Arg_parser::yes },
   56     { 'R', "rtf",        Arg_parser::no  },
   57     { 's', "font-size",  Arg_parser::yes },
   58     { 't', "tail",       Arg_parser::no  },
   59     { 'T', "text",       Arg_parser::no  },
   60     { 'w', "wrap",       Arg_parser::yes },
   61     { 'v', "version",    Arg_parser::no  },
   62     { 'V', "version",    Arg_parser::no  },
   63     { 'W', "wrap-no-numbers", Arg_parser::no  },
   64     { 'X', "art-cp437",  Arg_parser::no  },
   65     { 'U', "art-bin",    Arg_parser::no  },
   66     { 'D', "art-tundra", Arg_parser::no  },
   67     { 'Y', "art-width",  Arg_parser::yes  },
   68     { 'Z', "art-height", Arg_parser::yes  },
   69     { 'm', "map",        Arg_parser::yes },
   70     { 'N', "no-trailing-nl",  Arg_parser::no  },
   71     { 'C', "no-version-info", Arg_parser::no  },
   72     { 'k', "ignore-clear",    Arg_parser::maybe  },
   73     { 'c', "ignore-csi",      Arg_parser::no  },
   74     { 'y', "derived-styles",  Arg_parser::no  },
   75     { 'S', "svg",        Arg_parser::no  },
   76     { 'Q', "width",      Arg_parser::yes  },
   77     { 'E', "height",     Arg_parser::yes  },
   78     { 'x', "max-size",     Arg_parser::yes  },
   79 
   80     {  0,  0,           Arg_parser::no  }
   81 };
   82 
   83 CmdLineOptions::CmdLineOptions( const int argc, const char *argv[] ):
   84     outputType (ansifilter::TEXT),
   85     opt_help(false),
   86     opt_version(false),
   87     opt_fragment(false),
   88     opt_plain(false),
   89     opt_ignoreEOF(false),
   90     opt_linenum(false),
   91     opt_wrapNoNum(false),
   92     opt_anchors(false),
   93     opt_cp437(false),
   94     opt_asciiBin(false),
   95     opt_asciiTundra(false),
   96     opt_omit_trailing_cr(false),
   97     opt_omit_version_info(false),
   98     opt_ignoreClear(true),
   99     opt_ignoreCSI(false),
  100     opt_applyDynStyles(false),
  101     opt_genDynStyles(false),
  102     opt_funny_anchors(false),
  103     encodingName("ISO-8859-1"),
  104     font("Courier New"),
  105     fontSize("10pt"),
  106     wrapLineLen(0),
  107     asciiArtWidth(80),
  108     asciiArtHeight(100),
  109     maxFileSize(268435456)
  110 {
  111     char* hlEnvOptions=getenv("ANSIFILTER_OPTIONS");
  112     if (hlEnvOptions!=NULL) {
  113         std::ostringstream envos;
  114         envos<<argv[0]<<" "<<hlEnvOptions;
  115         std::istringstream ss( envos.str());
  116         std::string arg;
  117         std::list<std::string> ls;
  118         std::vector<char*> options;
  119         while (ss >> arg)
  120         {
  121             ls.push_back(arg); 
  122             options.push_back(const_cast<char*>(ls.back().c_str()));
  123         }
  124         options.push_back(0); 
  125         parseRuntimeOptions(options.size()-1, (const char**) &options[0], false);
  126     }
  127     
  128     parseRuntimeOptions(argc, argv);
  129 }
  130 
  131 CmdLineOptions::~CmdLineOptions() {}
  132 
  133 void CmdLineOptions::parseRuntimeOptions( const int argc, const char *argv[], bool readInputFilenames) {
  134         Arg_parser parser( argc, argv, options );
  135     if( parser.error().size() ) {           // bad option
  136         cerr << "ansifilter: "<< parser.error()<<"\n";
  137         cerr << "Try 'ansifilter --help' for more information.\n";
  138         exit( 1 );
  139     }
  140 
  141     int argind = 0;
  142     for( ; argind < parser.arguments(); ++argind ) {
  143         const int code = parser.code( argind );
  144         const std::string & arg = parser.argument( argind );
  145         if( !code ) break;
  146         switch( code ) {
  147         /* tbd
  148         case 'O':
  149                     {
  150                     const string tmp = StringTools::change_case ( arg );
  151                     if ( tmp == "xhtml" )
  152                         outputType = highlight::XHTML;
  153                     else if ( tmp == "tex" )
  154                         outputType = highlight::TEX;
  155                     else if ( tmp == "latex" )
  156                         outputType = highlight::LATEX;
  157                     else if ( tmp == "rtf" )
  158                         outputType = highlight::RTF;
  159                     else if ( tmp == "svg" )
  160                         outputType = highlight::SVG;
  161                     else if ( tmp == "bbcode" )
  162                         outputType = highlight::BBCODE;
  163                     else if ( tmp == "odt" )
  164                         outputType = highlight::ODTFLAT;
  165                     else
  166                         outputType = highlight::HTML;
  167                     }
  168                     break;
  169         */
  170         case 'a':
  171             opt_anchors = true;
  172             if ( arg=="self" ) opt_funny_anchors=true;
  173             break;
  174         case 'B':
  175             outputType = ansifilter::BBCODE;
  176             break;
  177         case 'd':
  178             docTitle = arg;
  179             break;
  180         case 'e':
  181             encodingName = arg;
  182             break;
  183         case 'f':
  184             opt_fragment = true;
  185             break;
  186         case 'F':
  187             font = arg;
  188             break;
  189         case 'h':
  190             opt_help = true;
  191             break;
  192         case 'H':
  193             outputType = ansifilter::HTML;
  194             break;
  195         case 'i':
  196             inputFileNames.push_back( arg );
  197             break;
  198         case 'l':
  199             opt_linenum=true;
  200             break;
  201         case 'L':
  202             outputType = ansifilter::LATEX;
  203             break;
  204         case 'm':
  205             colorMapPath = arg;
  206             break;
  207         case 'M':
  208             outputType = ansifilter::PANGO;
  209             break;
  210         case 'P':
  211             outputType = ansifilter::TEX;
  212             break;
  213         case 'o':
  214             outFilename = arg;
  215             break;
  216         case 'p':
  217             opt_plain = true;
  218             break;
  219         case 'r':
  220             styleSheetPath = arg;
  221             break;
  222         case 'R':
  223             outputType = ansifilter::RTF;
  224             break;
  225         case 'S':
  226             outputType = ansifilter::SVG;
  227             break;
  228         case 's':
  229             fontSize = arg;
  230             break;
  231         case 't':
  232             opt_ignoreEOF = true;
  233             break;
  234         case 'T':
  235             outputType = ansifilter::TEXT;
  236             break;
  237         case 'v':
  238         case 'V':
  239             opt_version = true;
  240             break;
  241         case 'O':
  242             outDirectory = validateDirPath( arg );
  243             break;
  244         case 'w':
  245             wrapLineLen=atoi(arg.c_str());
  246             break;
  247         case 'W':
  248             opt_wrapNoNum=true;
  249             break;
  250         case 'X':
  251             opt_cp437=true;
  252             break;
  253         case 'U':
  254             opt_asciiBin=true;
  255             break;
  256         case 'D':
  257             opt_asciiTundra=true;
  258             break;
  259         case 'Y':
  260             asciiArtWidth=atoi(arg.c_str());
  261             break;
  262         case 'Z':
  263             asciiArtHeight=atoi(arg.c_str());
  264             break;
  265         case 'N':
  266             opt_omit_trailing_cr=true;
  267             break;
  268         case 'C':
  269             opt_omit_version_info=true;
  270             break;
  271         case 'k':
  272             if (arg.size())
  273                 opt_ignoreClear = ( arg=="true" || arg=="1" ) ;
  274             break;
  275         case 'c':
  276             opt_ignoreCSI = true;
  277             break;
  278         case 'y':
  279             opt_applyDynStyles=true;
  280             break;
  281         case 'Q':
  282             width=arg;
  283             break;
  284         case 'E':
  285             height=arg;
  286             break;
  287         
  288         case 'x': {
  289             StringTools::str2num<off_t> ( maxFileSize, arg, std::dec );
  290             switch (arg[arg.size()-1]) {
  291                 case 'G': maxFileSize *= 1024;
  292                 case 'M': maxFileSize *= 1024;
  293                 case 'K': maxFileSize *= 1024;
  294             }
  295             break;
  296         }
  297         default:
  298             cerr << "ansifilter: option parsing failed" << endl;
  299         }
  300     }
  301 
  302     if (readInputFilenames) {
  303         if (argind < parser.arguments()) { //still args left
  304             if  (inputFileNames.empty()) {
  305                 while (argind < parser.arguments()) {
  306                     inputFileNames.push_back( parser.argument( argind++ ) );
  307                 }
  308             }
  309         } else if (inputFileNames.empty()) {
  310             inputFileNames.push_back("");
  311         }
  312     }
  313 }
  314 
  315 string CmdLineOptions::validateDirPath(const string & path)
  316 {
  317     return (path[path.length()-1] !=Platform::pathSeparator)?
  318            path+Platform::pathSeparator : path;
  319 }
  320 
  321 string CmdLineOptions::getSingleOutFilename()
  322 {
  323     if (!inputFileNames.empty() && !outDirectory.empty()) {
  324         if (outFilename.empty()) {
  325             outFilename = outDirectory;
  326             int delim = getSingleInFilename().find_last_of(Platform::pathSeparator)+1;
  327             outFilename += getSingleInFilename().substr((delim>-1)?delim:0)
  328                            + getOutFileSuffix();
  329         }
  330     }
  331     return outFilename;
  332 }
  333 
  334 string CmdLineOptions::getSingleInFilename()  const
  335 {
  336     return inputFileNames[0];
  337 }
  338 
  339 string CmdLineOptions::getOutDirectory()
  340 {
  341     if (!outFilename.empty() && !inputFileNames.size()) {
  342         outDirectory=getDirName(outFilename);
  343     }
  344     return outDirectory;
  345 }
  346 
  347 string CmdLineOptions::getDirName(const string & path)
  348 {
  349     size_t dirNameLength=path.rfind(Platform::pathSeparator);
  350     return (dirNameLength==string::npos)?string():path.substr(0, dirNameLength+1);
  351 }
  352 
  353 bool CmdLineOptions::printVersion()const
  354 {
  355     return opt_version;
  356 }
  357 
  358 bool CmdLineOptions::printHelp()const
  359 {
  360     return opt_help;
  361 }
  362 
  363 bool CmdLineOptions::fragmentOutput()const
  364 {
  365     return opt_fragment;
  366 }
  367 
  368 bool CmdLineOptions::showLineNumbers()const
  369 {
  370     return opt_linenum;
  371 }
  372 
  373 bool CmdLineOptions::parseCP437() const
  374 {
  375   return opt_cp437;
  376 }
  377 
  378 bool CmdLineOptions::parseAsciiBin() const{
  379   return opt_asciiBin;
  380 }
  381 
  382 bool CmdLineOptions::parseAsciiTundra() const{
  383   return opt_asciiTundra;
  384 }
  385 
  386 bool CmdLineOptions::ignoreClearSeq() const {
  387     return opt_ignoreClear;
  388 }
  389 
  390 bool CmdLineOptions::ignoreCSISeq() const {
  391     return opt_ignoreCSI;
  392 }
  393 
  394 int CmdLineOptions::getAsciiArtWidth() const {
  395   return asciiArtWidth;   
  396 }
  397 int CmdLineOptions::getAsciiArtHeight() const{
  398   return asciiArtHeight;   
  399 }
  400 
  401 string CmdLineOptions::getOutFileSuffix()const
  402 {
  403     switch (outputType) {
  404     case ansifilter::HTML:
  405         return ".html";
  406     case ansifilter::PANGO:
  407         return ".pango";
  408     case ansifilter::XHTML:
  409         return ".xhtml";
  410     case ansifilter::RTF:
  411         return ".rtf";
  412     case ansifilter::TEX:
  413     case ansifilter::LATEX:
  414         return ".tex";
  415     case ansifilter::BBCODE:
  416         return ".bbcode";
  417     case ansifilter::SVG:
  418         return ".svg";
  419     
  420     default:
  421         return ".txt";
  422     }
  423 }
  424 
  425 string CmdLineOptions::getEncoding() const
  426 {
  427     return encodingName;
  428 }
  429 
  430 string CmdLineOptions::getFont() const
  431 {
  432     return font;
  433 }
  434 
  435 string CmdLineOptions::getFontSize() const
  436 {
  437     return fontSize;
  438 }
  439 
  440 string CmdLineOptions::getMapPath() const
  441 {
  442   return colorMapPath;
  443 }
  444 
  445 bool CmdLineOptions::plainOutput() const
  446 {
  447     return opt_plain;
  448 }
  449 
  450 bool CmdLineOptions::ignoreInputEOF() const
  451 {
  452     return opt_ignoreEOF;
  453 }
  454 
  455 bool CmdLineOptions::wrapNoNumbers() const
  456 {
  457     return opt_wrapNoNum;
  458 }
  459 
  460 bool CmdLineOptions::addAnchors() const
  461 {
  462     return opt_anchors;
  463 }
  464 bool CmdLineOptions::addFunnyAnchors() const
  465 {
  466     return opt_funny_anchors;
  467 }
  468 
  469 bool CmdLineOptions::omitEncoding() const
  470 {
  471     return StringTools::lowerCase(encodingName)=="none";
  472 }
  473 
  474 bool CmdLineOptions::omitTrailingCR() const
  475 {
  476     return opt_omit_trailing_cr;
  477 }
  478 bool CmdLineOptions::omitVersionInfo() const
  479 {
  480     return opt_omit_version_info;
  481 }
  482 bool CmdLineOptions::applyDynStyles() const {
  483     return opt_applyDynStyles;
  484 }
  485     
  486     
  487 string CmdLineOptions::getDocumentTitle() const
  488 {
  489     return docTitle;
  490 }
  491 
  492 string CmdLineOptions::getStyleSheetPath() const
  493 {
  494     return styleSheetPath;
  495 }
  496 
  497 const vector <string> & CmdLineOptions::getInputFileNames() const
  498 {
  499     return inputFileNames;
  500 }
  501 
  502 ansifilter::OutputType CmdLineOptions::getOutputType() const
  503 {
  504     return outputType;
  505 }
  506 
  507 int CmdLineOptions::getWrapLineLength() const
  508 {
  509     return wrapLineLen;
  510 }
  511 
  512 string CmdLineOptions::getWidth() const
  513 {
  514     return width;
  515 }
  516 
  517 string CmdLineOptions::getHeight() const
  518 {
  519     return height;
  520 }
  521 
  522 off_t CmdLineOptions::getMaxFileSize() const
  523 {
  524     return maxFileSize;
  525 }
  526