Ignore:
Timestamp:
08/01/18 08:25:00 (6 years ago)
Author:
tbretz
Message:
Allow to write output to an ascii file, minor changes to replacing the environment values.
File:
1 edited

Legend:

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

    r19117 r19118  
    3535        ("ignore",        vars<string>(),              "Ignore the given columns")
    3636        ("display,d",     po_switch(),                 "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
     37        ("write",         var<string>(""),             "Write output to an ascii file")
    3738        ("null,n",        po_switch(),                 "Redirect the output file to /dev/null (mainly for debugging purposes, e.g. performance studies)")
    3839        ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
     
    9394        "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword. "
    9495        "You can also read the values for an enviroment substitution from a file "
    95         "(one element per line), e.g. --env.TEST=file.txt\n"
     96        "(one element per line), e.g. --env.TEST=file.txt. Lines starting with a # "
     97        "are skipped.\n"
    9698        "\n"
    9799        "Comments in the query-file can be placed according to the SQL standard inline "
     
    373375    const bool     update      = conf.Get<bool>("update");
    374376    const bool     display     = conf.Get<bool>("display");
     377    const string   write       = conf.Get<string>("write");
    375378    const bool     noout       = conf.Get<bool>("null");
    376379    const bool     nofill      = conf.Get<bool>("no-fill");
     
    420423
    421424    map<string, vector<string>> envs;
     425
    422426    for (const auto &env : conf.GetWildcardOptions("env.*"))
    423         envs.emplace(env.substr(4), conf.Vec<string>(env));
     427        envs[env.substr(4)] = conf.Vec<string>(env);
    424428
    425429    for (const auto &env : conf.GetWildcardOptions("list.*"))
    426430    {
    427         const string fname = conf.Get<string>(env);
     431        const string  fname = conf.Get<string>(env);
     432        const string &ident = env.substr(5);
    428433
    429434        ifstream fin(fname);
    430435        if (!fin)
    431436        {
    432             cerr << "Could not open environment in '" << fname << "': " << strerror(errno) << endl;
     437            cerr << "Could not open environment in '" << fname << "' for ${" << ident << "}: " << strerror(errno) << endl;
    433438            return 3;
    434439        }
    435440
    436         const string &ident = env.substr(5);
    437 
    438         string repl;
    439441        for (string line; getline(fin, line); )
    440             envs[ident].push_back(line);
     442            if (Tools::Trim(line)[0]!='#')
     443                envs[ident].push_back(line);
     444
     445        if (verbose>0)
     446            cout << "Found " << envs[ident].size() << " list element(s) for ${" << ident << "}" << endl;
    441447    }
    442448
    443449    for (const auto &env : envs)
    444450    {
    445         regex rexpr("\\$\\{"+env.first+"\\}|\\$"+env.first+"\\b");
     451        regex rexpr("\\$(\\{"+env.first+"\\}|"+env.first+"\\b)");
    446452        query = regex_replace(query, rexpr, boost::join(env.second, ", "));
    447453    }
     
    460466    {
    461467        FileStat_t stat;
    462         const Int_t  exist = !gSystem->GetPathInfo(path, stat);
    463         const Bool_t write = !gSystem->AccessPathName(path,  kWritePermission) && R_ISREG(stat.fMode);
    464 
    465         if ((update && !exist) || (update && exist && !write) || (force && exist && !write))
     468        const Int_t  exist  = !gSystem->GetPathInfo(path, stat);
     469        const Bool_t _write = !gSystem->AccessPathName(path,  kWritePermission) && R_ISREG(stat.fMode);
     470
     471        if ((update && !exist) || (update && exist && !_write) || (force && exist && !_write))
    466472        {
    467473            cerr << "File '" << path << "' is not writable." << endl;
     
    723729    }
    724730
     731    ofstream fout(write);
     732    if (!fout)
     733        cout << "WARNING: Writing to '" << write << "' failed: " << strerror(errno) << endl;
     734
    725735    if (display)
    726736    {
     
    732742    }
    733743
     744    if (!write.empty())
     745    {
     746        fout << "#";
     747        for (size_t i=0; i<l.size(); i++)
     748            fout << ' ' << l[i].c_str();
     749        fout << endl;
     750    }
     751
    734752    // ---------------------- Fill TTree with DB data --------------------------
    735753    size_t count = 0;
     
    744762        for (auto col=row.begin(); col!=row.end(); col++, idx++)
    745763        {
    746             if (display)
     764            if (display || !write.empty())
    747765            {
    748766                if (idx>0)
     
    789807            if (display)
    790808                cout << sout.str() << endl;
     809            if (!write.empty())
     810                fout << sout.str() << '\n';
    791811        }
    792812
Note: See TracChangeset for help on using the changeset viewer.