rdbxx  1.0.10
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 std::istream&
38 operator>>(
39  std::istream& is,
40  RDBComment& rdbcomment
41  ) {
42 
43  std::string line;
44  getline( is, line, '\n' );
45  try {
46  rdbcomment.setComment( line );
47 
48  } catch ( RDBErr& rdberr ) {
49  rdberr.set_message( "operator>>(std::istream&,RDBComment&): " );
50  throw( rdberr );
51 
52  } catch ( ... ) {
53  throw( RDBErr( "operator>>(std::istream&,RDBComment&): unexpected exception caught" ) );
54 
55  }
56 
57  return is;
58 
59 }
60 
72 std::ostream&
73 operator<<(
74  std::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 std::string& comment
104  ) {
105 
106  try {
107  setCommentText( comment );
108 
109  } catch ( RDBErr& rdberr ) {
110  rdberr.set_message( "RDBComment::RDBComment(std::string&)" );
111  throw( rdberr );
112 
113  } catch ( ... ) {
114  throw( RDBErr( "RDBComment::RDBComment(std::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 std::string& comment
174  ) {
175 
176  setCommentText( comment );
177 
178  return *this;
179 
180 }
181 
189 void
191  const std::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(std::string&): Leading characters not `# '" ) );
198 
199  }
200  } else if ( comment.npos != pos ) {
201  if ( '#' != comment[pos] ) {
202  throw( RDBErr( "RDBComment::setComment(std::string&): First non-space character not `#'" ) );
203 
204  }
205  } else {
206  throw( RDBErr( "RDBComment::setComment(std::string&): No comment found" ) );
207 
208  }
209 
210  setCommentText( comment );
211 
212 }
213 
221 void
223  const std::string& comment
224  ) {
225 
226  if ( comment.size() == 0 )
227  return;
228 
229  size_t pos = comment.find_first_not_of( " " );
230  _comment = comment;
231  _keyword.erase( );
232  _value.erase( );
233 
234  // if the line has a leading '#' character, remove it...
235  // users creating comments need not include leading '#'.
236  if ( '#' == _comment[pos] ) {
237  _comment.erase( 0, pos + 1 );
238 
239  }
240 
241  // see if its a keyword value pair...
242  if ( ':' == _comment[0] ) {
243  _comment.erase( 0, 1 );
244 
245  pos = _comment.find( "=", 0 );
246  if ( pos > _comment.size( ) )
247  pos = _comment.size( );
248 
249  _keyword = _comment;
250  _keyword.erase( pos );
251 
252  size_t wspos = _keyword.find_first_not_of( " " );
253  _keyword.erase( 0, wspos );
254  wspos = _keyword.find_last_not_of( " " );
255  _keyword.erase( wspos + 1 );
256 
257  _value = _comment;
258  _value.replace( 0, pos+1, "" );
259 
260  wspos = _value.find_first_not_of( " " );
261  _value.erase( 0, wspos );
262  wspos = _value.find_last_not_of( " " );
263  _value.erase( wspos + 1 );
264 
265  } else {
266  pos = _comment.find_first_not_of( "\t " );
267  if ( _comment.npos != pos ) {
268  _comment.erase( 0, pos );
269 
270  }
271  }
272 }
273 
280 void
282  const std::string& keyword
283  ) {
284 
285  try {
286  setComment( std::string("#:") + keyword + " = " + _value );
287 
288  } catch ( RDBErr& rdberr ) {
289  rdberr.set_message( "RDBComment::setKeyword(std::string&): " );
290  throw( rdberr );
291 
292  } catch ( ... ) {
293  throw( RDBErr( "RDBComment::setKeyword(std::string&): unexpected exception caught" ) );
294 
295  }
296 }
297 
304 void
306  const std::string& value
307  ) {
308 
309  try {
310  setComment( std::string("#:") + _keyword + " = " + value );
311 
312  } catch ( RDBErr& rdberr ) {
313  rdberr.set_message( "RDBComment::setValue(std::string&): " );
314  throw( rdberr );
315 
316  } catch ( ... ) {
317  throw( RDBErr( "RDBComment::setValue(std::string&): unexpected exception caught" ) );
318 
319  }
320 }
321 
326 std::string
328  void
329  ) const {
330 
331  return
332  (
333  (0 != _keyword.size( ) || 0 != _value.size( ))
334  ?
335  ("#:")
336  :
337  ("# ")
338  +
339  _comment
340  );
341 
342 }
343 
348 std::string
350  void
351  ) const {
352 
353  return _comment;
354 
355 }
356 
361 std::string
363  void
364  ) const {
365 
366  return _keyword;
367 
368 }
369 
374 std::string
376  void
377  ) const {
378 
379  return _value;
380 
381 }
RDBComment(const std::string &comment="")
Parses comment string for RDB comment structure.
Definition: RDBComment.cc:102
std::string getValue(void) const
Return the keyword's value, if any.
Definition: RDBComment.cc:375
The parent class for all RDB related exceptions.
Definition: RDBErr.h:31
std::string getKeyword(void) const
Return the keyword, if any.
Definition: RDBComment.cc:362
Provides interface for manipulating RDB comments.
Definition: RDBComment.h:33
void setCommentText(const std::string &comment)
Parses string for RDB comment elements.
Definition: RDBComment.cc:222
void setComment(const std::string &comment)
Parses comment string for RDB comment elements.
Definition: RDBComment.cc:190
const RDBComment & operator=(const RDBComment &rdbcomment)
Copy RDBComment object.
Definition: RDBComment.cc:150
std::string getCommentText(void) const
Return the full comment text.
Definition: RDBComment.cc:349
void setValue(const std::string &value)
Set just the comment value.
Definition: RDBComment.cc:305
void setKeyword(const std::string &keyword)
Set just the comment keyword.
Definition: RDBComment.cc:281
~RDBComment(void)
Destructor has nothing to do.
Definition: RDBComment.cc:137
std::string getComment(void) const
Return the full comment.
Definition: RDBComment.cc:327