Row.cc

00001 // File:  Row.cc
00002 
00003 // --8<--8<--8<--8<--
00004 //
00005 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00006 //
00007 // This file is part of rdbstats
00008 //
00009 // rdbstats is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // rdbstats is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the 
00021 //       Free Software Foundation, Inc. 
00022 //       51 Franklin Street, Fifth Floor
00023 //       Boston, MA  02110-1301, USA
00024 //
00025 // -->8-->8-->8-->8--
00026 
00027 #include <functional>
00028 #include <sstream>
00029 
00030 #include <algorithm>
00031 
00032 #include <Exception/Exception.h>
00033 #include <suplib/range.h>
00034 
00035 #include "Row.h"
00036 
00037 inline bool mycmp( const pair< long, long >& a, const pair< long, long >& b ) {
00038   return a.first < b.first;
00039 }
00040 
00041 Row::Row( const vector< string >& row_options ) throw ( Exception ) {
00042 
00043   vector< string >::const_iterator iter = row_options.begin( );
00044   vector< string >::const_iterator the_end = row_options.end( );
00045 
00046   if ( iter == the_end ) {
00047     // If the user did not request a specific row then use the entire file.
00048     start_end.push_back( pair< long, long > ( 1, LONG_MAX ) );
00049     return;
00050   }
00051 
00052   for ( ; iter != the_end; ++iter ) {
00053 
00054     RangeLList* rll = NULL;
00055 
00056     RangeOpts opt =
00057       (RangeOpts) ( Range_SORT | Range_MERGE | Range_INCOMPLETE );
00058     long where, start = 1, end = LONG_MAX;
00059 
00060     RangeErr myerr;
00061     if ( (myerr=rangel_parse( &rll, (char*) iter->c_str( ), opt,
00062                               start, end, &where )) ) {
00063       rangel_del( rll );
00064       ostringstream ost;
00065       ost << "Row::Row( ) : Unable to parse '" << *iter << "'\n";
00066       throw Exception( ost.str( ) );
00067     }
00068 
00069     for ( size_t ii = 0; ii < rll->n ; ii++ )
00070       start_end.push_back( pair< long, long > ( rll->range[ ii ].start,
00071                                                 rll->range[ ii ].end ) );
00072 
00073     sort( start_end.begin( ), start_end.end( ), ptr_fun( mycmp ) );
00074 
00075     rangel_del( rll );
00076     
00077   }
00078 
00079 }
00080 
00081 void Row::print( ostream& os ) const {
00082 
00083   vector< pair< long, long > > ::const_iterator iter = start_end.begin( );
00084   vector< pair< long, long > >::const_iterator end = start_end.end( );
00085   for ( ; iter != end; ++iter ) {
00086     os << '[' << iter->first << ',' << iter->second << ']';
00087     if ( iter + 1 != end )
00088       os << '\n';
00089   }
00090 
00091 }
00092 
00093 #ifdef Row_Testme
00094 
00095 int main( int argc,  char* argv[] ) {
00096 
00097   try {
00098 
00099     vector< string > options;
00100     for ( int ii = 1; ii < argc; ++ii )
00101       options.push_back( argv[ ii ] );
00102 
00103     Row row( options );
00104 
00105     cout << row << '\n';
00106 
00107     return 0;
00108 
00109   } catch( Exception& E ) {
00110 
00111     cerr << E << '\n';
00112     return 1;
00113 
00114   } catch( exception& e ) {
00115 
00116     cerr << e.what( ) << 'n';
00117     return 1;
00118 
00119   } catch( ... ) {
00120 
00121     cerr << "unknown exception caught\n";
00122     return 1;
00123 
00124   }
00125 
00126 }
00127 
00128 /*
00129  CC -DRow_Testme MyRow.cc -I/proj/axaf/simul/include /proj/axaf/simul/lib/sun4u-SunOS-5/libException.a /proj/axaf/simul/lib/sun4u-SunOS-5/libsuplib.so
00130 */
00131 
00132 #endif