rdbxx  1.0.7_02
RDBComment.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of RDB
6 //
7 // RDB 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 // RDB 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 #include "RDBComment.h"
26 
37 istream&
38 operator>>(
39  istream& is,
40  RDBComment& rdbcomment
41  ) {
42 
43  string line;
44  getline( is, line, '\n' );
45  try {
46  rdbcomment.setComment( line );
47 
48  } catch ( RDBErr& rdberr ) {
49  rdberr.set_message( "operator>>(istream&,RDBComment&): " );
50  throw( rdberr );
51 
52  } catch ( ... ) {
53  throw( RDBErr( "operator>>(istream&,RDBComment&): unexpected exception caught" ) );
54 
55  }
56 
57  return is;
58 
59 }
60 
72 ostream&
73 operator<<(
74  ostream& os,
75  const RDBComment& rdbcomment
76  ) {
77 
78  os <<
79  (
80  (
81  0 != rdbcomment._keyword.size( )
82  ||
83  0 != rdbcomment._value.size( )
84  )
85  ?
86  ("#:")
87  :
88  ("# ")
89  ) << rdbcomment._comment;
90 
91  return os;
92 
93 }
94 
103  const string& comment
104  ) {
105 
106  try {
107  setCommentText( comment );
108 
109  } catch ( RDBErr& rdberr ) {
110  rdberr.set_message( "RDBComment::RDBComment(string&)" );
111  throw( rdberr );
112 
113  } catch ( ... ) {
114  throw( RDBErr( "RDBComment::RDBComment(string&): unexpected exception caught" ) );
115 
116  }
117 }
118 
126  const RDBComment& rdbcomment
127  ) {
128 
129  operator=( rdbcomment );
130 
131 }
132 
138  void
139  ){
140 
141 }
142 
149 const RDBComment&
151  const RDBComment& rdbcomment
152  ) {
153 
154  if ( this != &rdbcomment ) {
155  _comment = rdbcomment._comment;
156  _keyword = rdbcomment._keyword;
157  _value = rdbcomment._value;
158 
159  }
160 
161  return *this;
162 
163 }
164 
171 const RDBComment&
173  const string& comment
174  ) {
175 
176  setCommentText( comment );
177 
178  return *this;
179 
180 }
181 
189 void
191  const string& comment
192  ) {
193 
194  size_t pos = comment.find_first_not_of( " " );
195  if ( 0 == pos ) {
196  if ( '#' != comment[0] || ' ' != comment[1] ) {
197  throw( RDBErr( "RDBComment::setComment(string&): Leading characters not `# '" ) );
198 
199  }
200  } else if ( comment.npos != pos ) {
201  if ( '#' != comment[pos] ) {
202  throw( RDBErr( "RDBComment::setComment(string&): First non-space character not `#'" ) );
203 
204  }
205  } else {
206  throw( RDBErr( "RDBComment::setComment(string&): No comment found" ) );
207 
208  }
209 
210  setCommentText( comment );
211 
212 }
213 
221 void
223  const string& comment
224  ) {
225 
226  size_t pos = comment.find_first_not_of( " " );
227  _comment = comment;
228  _keyword.erase( );
229  _value.erase( );
230 
231  // if the line has a leading '#' character, remove it...
232  // users creating comments need not include leading '#'.
233  if ( '#' == _comment[pos] ) {
234  _comment.erase( 0, pos + 1 );
235 
236  }
237 
238  // see if its a keyword value pair...
239  if ( ':' == _comment[0] ) {
240  _comment.erase( 0, 1 );
241 
242  pos = _comment.find( "=", 0 );
243  if ( pos > _comment.size( ) )
244  pos = _comment.size( );
245 
246  _keyword = _comment;
247  _keyword.erase( pos );
248 
249  size_t wspos = _keyword.find_first_not_of( " " );
250  _keyword.erase( 0, wspos );
251  wspos = _keyword.find_last_not_of( " " );
252  _keyword.erase( wspos + 1 );
253 
254  _value = _comment;
255  _value.replace( 0, pos+1, "" );
256 
257  wspos = _value.find_first_not_of( " " );
258  _value.erase( 0, wspos );
259  wspos = _value.find_last_not_of( " " );
260  _value.erase( wspos + 1 );
261 
262  } else {
263  pos = _comment.find_first_not_of( "\t " );
264  if ( _comment.npos != pos ) {
265  _comment.erase( 0, pos );
266 
267  }
268  }
269 }
270 
277 void
279  const string& keyword
280  ) {
281 
282  try {
283  setComment( string("#:") + keyword + " = " + _value );
284 
285  } catch ( RDBErr& rdberr ) {
286  rdberr.set_message( "RDBComment::setKeyword(string&): " );
287  throw( rdberr );
288 
289  } catch ( ... ) {
290  throw( RDBErr( "RDBComment::setKeyword(string&): unexpected exception caught" ) );
291 
292  }
293 }
294 
301 void
303  const string& value
304  ) {
305 
306  try {
307  setComment( string("#:") + _keyword + " = " + value );
308 
309  } catch ( RDBErr& rdberr ) {
310  rdberr.set_message( "RDBComment::setValue(string&): " );
311  throw( rdberr );
312 
313  } catch ( ... ) {
314  throw( RDBErr( "RDBComment::setValue(string&): unexpected exception caught" ) );
315 
316  }
317 }
318 
323 string
325  void
326  ) const {
327 
328  return
329  (
330  (0 != _keyword.size( ) || 0 != _value.size( ))
331  ?
332  ("#:")
333  :
334  ("# ")
335  +
336  _comment
337  );
338 
339 }
340 
345 string
347  void
348  ) const {
349 
350  return _comment;
351 
352 }
353 
358 string
360  void
361  ) const {
362 
363  return _keyword;
364 
365 }
366 
371 string
373  void
374  ) const {
375 
376  return _value;
377 
378 }
string getValue(void) const
Return the keyword's value, if any.
Definition: RDBComment.cc:372
void setCommentText(const string &comment)
Parses string for RDB comment elements.
Definition: RDBComment.cc:222
void setKeyword(const string &keyword)
Set just the comment keyword.
Definition: RDBComment.cc:278
string getComment(void) const
Return the full comment.
Definition: RDBComment.cc:324
void setComment(const string &comment)
Parses comment string for RDB comment elements.
Definition: RDBComment.cc:190
The parent class for all RDB related exceptions.
Definition: RDBErr.h:33
Provides interface for manipulating RDB comments.
Definition: RDBComment.h:35
RDBComment(const string &comment="")
Parses comment string for RDB comment structure.
Definition: RDBComment.cc:102
string getKeyword(void) const
Return the keyword, if any.
Definition: RDBComment.cc:359
void setValue(const string &value)
Set just the comment value.
Definition: RDBComment.cc:302
const RDBComment & operator=(const RDBComment &rdbcomment)
Copy RDBComment object.
Definition: RDBComment.cc:150
string getCommentText(void) const
Return the full comment text.
Definition: RDBComment.cc:346
~RDBComment(void)
Destructor has nothing to do.
Definition: RDBComment.cc:137