Changeset 19235


Ignore:
Timestamp:
10/14/18 16:04:25 (6 years ago)
Author:
tbretz
Message:
Added --copy-* resources to be able to copy additional contents from the input file to the output.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/rootifysql.cc

    r19205 r19235  
    2828        ("ignore-null,i", po_switch(),                 "Do not skip rows containing any NULL field")
    2929        ("display,d",     po_switch(),                 "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
    30         ("write,w",       var<string>(""),             "Write output to an ascii file")
    31         ("delimiter",     var<string>(),               "The delimiter used if contents are displayed with --display (default=\\t)")
    3230        ("explain",       po_switch(),                 "Requests an EXPLAIN from the server (shows the server optimized query)\nsee also https://dev.mysql.com/doc/refman/explain-output.html")
    3331        ("profiling",     po_switch(),                 "Turn on profiling and print profile")
     
    3735        ("print-connection", po_switch(),              "Print database connection information")
    3836        ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
     37        ;
     38
     39    po::options_description ascii("ASCII output");
     40    ascii.add_options()
     41        ("write,w",       var<string>(""),             "Write output to an ascii file")
     42        ("delimiter",     var<string>(),               "The delimiter used if contents are displayed with --display (default=\\t)")
     43        ("copy-shabang",  po_switch(),                 "Copy the sha-bang line if exists to the output file")
     44        ("copy-header",   po_switch(),                 "Copy the header (all line starting with '#'  up to the first non-comment line to the output file")
     45        ("copy-query",    po_switch(),                 "Copy the query to the ascii output file")
     46        ("copy-comments", po_switch(),                 "Copy all lines starting with '#' to the output file which are not part of header")
     47        ("copy-all",      po_switch(),                 "An alias for --copy-header --copy-query --copy-comments")
    3948        ;
    4049
     
    5665
    5766    conf.AddOptions(control);
     67    conf.AddOptions(ascii);
    5868    conf.AddOptions(root);
    5969    conf.SetArgumentPositions(p);
     
    481491    const uint16_t compression = conf.Get<uint16_t>("compression");
    482492    const string   delimiter   = conf.Has("delimiter") ? conf.Get<string>("delimiter") : "\t";
     493
     494    const bool copy_all        = conf.Get<bool>("copy-all");
     495    const bool copy_shabang    = conf.Get<bool>("copy-shabang");
     496    const bool copy_header     = copy_all || conf.Get<bool>("copy-header");
     497    const bool copy_query      = copy_all || conf.Get<bool>("copy-query");
     498    const bool copy_comments   = copy_all || conf.Get<bool>("copy-comments");
     499
    483500    const vector<string> _ignore = conf.Vec<string>("ignore");
    484501    const bool  print_connection = conf.Get<bool>("print-connection");
     
    513530            return 4;
    514531        }
     532
    515533        getline(fin, query, (char)fin.eof());
    516534    }
     
    861879    }
    862880
     881    // -------------------------------------------------------------------------
     882
     883    string contents;
     884
     885    istringstream istr(query);
     886    size_t line = 0;
     887    bool header = true;
     888    while (istr)
     889    {
     890        string ibuf;
     891        getline(istr, ibuf);
     892        const string sbuf = Tools::Trim(ibuf);
     893
     894        const bool shabang = line==0 && ibuf[0]=='#' && ibuf[1]=='!';
     895        const bool comment = sbuf[0]=='#' && !shabang;
     896        const bool isquery = !shabang && !comment;
     897        if (isquery)
     898            header = false;
     899
     900        line++;
     901
     902        if ((copy_shabang  && shabang) ||
     903            (copy_header   && comment && header)  ||
     904            (copy_query    && isquery) ||
     905            (copy_comments && comment && !header))
     906            contents += '#' + ibuf + '\n';
     907
     908    }
     909
    863910    ofstream fout(write);
    864911    if (!write.empty() && !fout)
     
    868915    {
    869916        cout << endl;
     917        cout << contents << endl;
    870918        cout << "# " << row.field_list(delimiter.c_str()) << endl;
    871919    }
    872920
    873921    if (!write.empty())
     922    {
     923        fout << contents;
    874924        fout << "# " << row.field_list(delimiter.c_str()) << endl;
     925    }
    875926
    876927    // ---------------------- Fill TTree with DB data --------------------------
Note: See TracChangeset for help on using the changeset viewer.