suplibxx  1.3.9
getrecord.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of suplibxx
6 //
7 // suplibxx is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // suplibxx is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.
20 // 51 Franklin Street, Fifth Floor
21 // Boston, MA 02110-1301, USA
22 //
23 // -->8-->8-->8-->8--
24 
25 #ifdef sgi
26 #include <ctype.h>
27 #else
28 #include <cctype>
29 #endif
30 #include <fstream>
31 #include <iostream>
32 #include <string>
33 using namespace std;
34 
35 #include "io.h"
36 using namespace suplib;
37 
38 #define WS " \t\n\r"
39 
61  istream& is,
62  string& str,
63  int opt,
64  char delim,
65  char continuation
66  ) {
67 
68  str.erase( );
69 
70  // split appart logical and physical reads here. in the latter
71  // case, can optimize a bit by reading directly into target string
72 
73  if ( opt & suplib::READ_LOGICAL ) {
74 
75  string tmp;
76 
77  // are we there yet?
78  bool done = false;
79 
80  while ( !done && getline( is, tmp, delim ) ) {
81 
82  // find last continuation char
83  size_t last = tmp.find_last_of( continuation );
84 
85  // find last non whitespace char
86  size_t eows = tmp.find_last_not_of( WS );
87 
88  // if there is nothing but whitespace after continuation... a
89  // logical line, and lo, a continuation character. what shall
90  // we do?
91  if ( eows == last ) {
92 
93  // well, if this CLEAN thing is requested,
94  // remove any whitespace after the continuation character
95  // and insert the delimiter, since it's been scrubbed
96  // by getline()
97 
98  if ( opt & suplib::CLEAN )
99  {
100  tmp.erase( last + 1 );
101  tmp += delim;
102  }
103 
104  // this really isn't raw, as we don't know if we
105  // ran out of characters or if the delimiter was
106  // removed by getline. add the delimiter just to
107  // make things peachy.
108  else if (opt & suplib::RAW )
109  tmp += delim;
110 
111  // normally, we just zap the continuation character and all
112  // trailing white space
113  else
114  tmp.erase( last );
115 
116  }
117 
118  // ok, it's not a continuation character, just worry about
119  // cleaning out the end of the line. note that we
120  // bail here, as we've hit the end of the record.
121  else {
122 
123  // zap end of line (no white space)
124  if ( opt & suplib::STRIP )
125  tmp.erase( eows + 1 );
126 
127  // same bogus argument as before
128  else if (opt & suplib::RAW )
129  tmp += delim;
130 
131  // by default, just keep everything but the delimiter,
132  // already removed by getline()
133 
134  // we're done here, break out of the getline() loop
135  done = true;
136  }
137 
138  // save the current line
139  str.append( tmp );
140  }
141 
142  }
143 
144 
145  // read physical line
146  else if ( getline( is, str, delim ) ) {
147 
148  // strip trailing white space
149  if ( opt & suplib::STRIP )
150  {
151  size_t eows = str.find_last_not_of( WS );
152  str.erase( eows + 1 );
153  }
154 
155  // same silliness as above
156  else if ( opt & suplib::RAW )
157  str += delim;
158  }
159 
160 
161  return is;
162 }
The suplib namespace encompasses all of the functions in the suplib++ library.
istream & getrecord(istream &is, string &str, int opt=READ_PHYS, char delim='\n', char continuation='\\')
Reads physical and logical lines.
Definition: getrecord.cc:60
Definition: io.h:96