rdbstats  2.0.7
Row.cc
1 // File: Row.cc
2 
3 // --8<--8<--8<--8<--
4 //
5 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
6 //
7 // This file is part of rdbstats
8 //
9 // rdbstats is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // rdbstats is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.
22 // 51 Franklin Street, Fifth Floor
23 // Boston, MA 02110-1301, USA
24 //
25 // -->8-->8-->8-->8--
26 
27 #include <functional>
28 #include <sstream>
29 
30 #include <algorithm>
31 
32 #include <Exception/Exception.h>
33 #include <suplib/range.h>
34 
35 #include "Row.h"
36 
37 inline bool mycmp( const std::pair< long, long >& a, const std::pair< long, long >& b ) {
38  return a.first < b.first;
39 }
40 
41 Row::Row( const std::vector< std::string >& row_options ) {
42 
43  std::vector< std::string >::const_iterator iter = row_options.begin( );
44  std::vector< std::string >::const_iterator the_end = row_options.end( );
45 
46  if ( iter == the_end ) {
47  // If the user did not request a specific row then use the entire file.
48  start_end.push_back( std::pair< long, long > ( 1, LONG_MAX ) );
49  return;
50  }
51 
52  for ( ; iter != the_end; ++iter ) {
53 
54  RangeLList* rll = NULL;
55 
56  RangeOpts opt =
57  (RangeOpts) ( Range_SORT | Range_MERGE | Range_INCOMPLETE );
58  long where, start = 1, end = LONG_MAX;
59 
60  RangeErr myerr;
61  if ( (myerr=rangel_parse( &rll, (char*) iter->c_str( ), opt,
62  start, end, &where )) ) {
63  rangel_del( rll );
64  std::ostringstream ost;
65  ost << "Row::Row( ) : Unable to parse '" << *iter << "'\n";
66  throw Exception( ost.str( ) );
67  }
68 
69  for ( size_t ii = 0; ii < rll->n ; ii++ )
70  start_end.push_back( std::pair< long, long > ( rll->range[ ii ].start,
71  rll->range[ ii ].end ) );
72 
73  sort( start_end.begin( ), start_end.end( ), ptr_fun( mycmp ) );
74 
75  rangel_del( rll );
76 
77  }
78 
79 }
80 
81 void Row::print( std::ostream& os ) const {
82 
83  std::vector< std::pair< long, long > > ::const_iterator iter = start_end.begin( );
84  std::vector< std::pair< long, long > >::const_iterator end = start_end.end( );
85  for ( ; iter != end; ++iter ) {
86  os << '[' << iter->first << ',' << iter->second << ']';
87  if ( iter + 1 != end )
88  os << '\n';
89  }
90 
91 }