Changeset 17393


Ignore:
Timestamp:
11/26/13 19:16:38 (11 years ago)
Author:
tbretz
Message:
Fixed a bug which caused replace() to fail. Memory was reallocated and so the pointer was wrong; simplified the retrieval of table names.
File:
1 edited

Legend:

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

    r17291 r17393  
    7272    /// Lists all columns of an open file
    7373    void List();                         
    74     void ListFileContent(const string& filename);
     74    void ListFileContent() const;
    7575    void ListHeader(const string& filename);
    7676    void ListKeywords(ostream &);
     
    166166}
    167167
    168 void FitsDumper::ListFileContent(const string& filename)
    169 {
    170     ofstream fout(filename=="-"?"/dev/stdout":filename);
    171     if (!fout)
    172     {
    173         cerr << "Cannot open file " << filename << ": " << strerror(errno) << endl;
    174         return;
    175     }
    176 
    177     int table_id = 0;
    178 
    179     vector<string> table_names;
    180     vector<uint32_t> table_rows;
    181     try
    182     {
    183 
    184         while (true)
    185         {
    186             clear();
    187             seekg(0);
    188             fTable = Table();
    189             Constructor(fFilename, "", "", false, table_id);
    190 
    191             table_names.push_back(fTable.Get<string>("EXTNAME"));
    192             table_rows.push_back(GetNumRows());
    193 
    194             table_id++;
    195         }
    196     }
    197     catch (runtime_error e)
    198     {
    199         //nothing to see here, just catching the error thrown by EOF
    200     }
    201 
    202     fout << "File " << fFilename << " has " << table_names.size() << " tables: " << endl;
    203     for (uint32_t i=0; i<table_names.size(); i++)
    204     {
    205         fout << "#" << i << ": \"" << table_names[i];
    206         fout << "\" num. rows = " << table_rows[i] << endl;
    207     }
    208 }
     168void FitsDumper::ListFileContent() const
     169{
     170    const std::vector<std::string> &tables = GetTables();
     171
     172    cout << "File " << fFilename << " has " << tables.size() << " table(s): " << endl;
     173    for (auto it=tables.begin(); it!=tables.end(); it++)
     174        cout << " * " << *it << endl;
     175}
     176
    209177void FitsDumper::ListHeader(const string& filename)
    210178{
     
    607575                    break;
    608576
    609             ostringstream id;
    610             id << '[' << p << ']';
    611 
    612             it->replace(ibeg-it->begin()+what.position(1), what.length()-1, id.str());
    613 
    614             ibeg = what[0].first+3;
     577            const string id = '['+to_string(p)+']';
     578
     579            // Replace might reallocate the memory. Therefore, we cannot use what[0].first
     580            // directly but have to store the offset
     581            const size_t offset = what[0].first - it->begin();
     582
     583            it->replace(ibeg-it->begin()+what.position(1), what.length()-1, id);
     584
     585            ibeg = it->begin() + offset + id.size();
    615586            iend = it->end();
    616587
     
    972943
    973944    if (conf.Get<bool>("filecontent"))
    974         ListFileContent(conf.Get<string>("outfile"));
     945        ListFileContent();
    975946
    976947    if (conf.Get<bool>("header"))
     
    11351106    po::options_description configs("Fitsdump options");
    11361107    configs.add_options()
    1137         ("fitsfile,f",  var<string>()
     1108        ("filecontent", po_switch(),            "List the number of tables in the file, along with their name")
     1109        ("header,h",    po_switch(),            "Dump header of given table")
     1110        ("list,l",      po_switch(),            "List all tables and columns in file")
     1111        ("fitsfile",    var<string>()
    11381112#if BOOST_VERSION >= 104200
    11391113         ->required()
    11401114#endif
    11411115                                              , "Name of FITS file")
    1142         ("col,c",       vars<string>(),         "List of columns to dump\narg is a list of columns, separated by a space.\nAdditionnally, a list of sub-columns can be added\ne.g. Data[3] will dump sub-column 3 of column Data\nData[3:4] will dump sub-columns 3 and 4\nOmitting this argument dump the entire column\nnota: all indices start at zero")
     1116        ("col,c",       vars<string>(),         "List of columns to dump\narg is a list of columns, separated by a space.\nAdditionnally, a list of sub-columns can be added\ne.g. Data[3] will dump sub-column 3 of column Data\nData[3:4] will dump sub-columns 3 and 4\nOmitting this argument dump the entire column\nnote: all indices start at zero")
    11431117        ("outfile,o",   var<string>("-"),       "Name of output file (-:/dev/stdout)")
    11441118        ("precision,p", var<int>(20),           "Precision of ofstream")
    1145         ("list,l",      po_switch(),            "List all tables and columns in file")
    1146         ("header,h",    po_switch(),            "Dump header of given table")
    11471119        ("stat,s",      po_switch(),            "Perform statistics instead of dump")
    11481120        ("minmax,m",    po_switch(),            "Calculates min and max of data")
     
    11551127        ("limit",       var<size_t>(size_t(0)), "Limit for the maximum number of rows to read (0=unlimited)")
    11561128        ("tablename,t", var<string>(""),        "Name of the table to open. If not specified, first binary table is opened")
    1157         ("filecontent", po_switch(),            "List the number of tables in the file, along with their name")
    11581129#ifdef HAVE_ROOT
    11591130        ("root,r",      po_switch(),            "Enable root mode")
Note: See TracChangeset for help on using the changeset viewer.