1 //
2 // This file is part of the SDTS++ toolkit, written by the U.S.
3 // Geological Survey.  It is experimental software, written to support
4 // USGS research and cartographic data production.
5 //
6 // SDTS++ is public domain software.  It may be freely copied,
7 // distributed, and modified.  The USGS welcomes user feedback, but makes
8 // no committment to any level of support for this code.  See the SDTS
9 // web site at http://mcmcweb.er.usgs.gov/sdts for more information,
10 // including points of contact.
11 //
12 // $Id: sb_Stat.cpp,v 1.7 2002/11/24 22:07:43 mcoletti Exp $
13 //
14 
15 #include <sdts++/builder/sb_Stat.h>
16 
17 
18 
19 #include <iostream>
20 #include <strstream>
21 
22 #include <limits.h>
23 #include <float.h>
24 
25 #ifndef INCLUDED_SB_UTILS_H
26 #include <sdts++/builder/sb_Utils.h>
27 #endif
28 
29 #ifndef INCLUDED_SB_FOREIGNID_H
30 #include <sdts++/builder/sb_ForeignID.h>
31 #endif
32 
33 #ifndef INCLUDED_SC_RECORD_H
34 #include <sdts++/container/sc_Record.h>
35 #endif
36 
37 #ifndef INCLUDED_SC_FIELD_H
38 #include <sdts++/container/sc_Field.h>
39 #endif
40 
41 #ifndef INCLUDED_SC_SUBFIELD_H
42 #include <sdts++/container/sc_Subfield.h>
43 #endif
44 
45 #ifndef INCLUDED_SIO_8211CONVERTER_H
46 #include <sdts++/io/sio_8211Converter.h>
47 #endif
48 
49 
50 using namespace std;
51 
52 
53 static const char* ident_ = "$Id: sb_Stat.cpp,v 1.7 2002/11/24 22:07:43 mcoletti Exp $";
54 
55 // Strings and integers are initialized with these values; they are used
56 // to indicate whether a given module value has been assigned a value or not.
57 
58 // (XXX I arbitrarily chose 0x4 as the sentinal value.  I hate ad hoc crap.)
59 
60 static const string  UNVALUED_STRING(1, static_cast<string::value_type>(0x4) );
61 
62 static const long    UNVALUED_LONG   = INT_MIN;
63 
64 static const double  UNVALUED_DOUBLE = DBL_MAX;
65 
66 struct sb_Stat_Imp
67 {
68   string   ModuleTypeReferred_;
69   string   ModuleNameReferred_;
70   long     ModuleRecordCount_;
71   long     SpatialAddressCount_;
72 
73 
sb_Stat_Impsb_Stat_Imp74   sb_Stat_Imp()
75     :
76     ModuleTypeReferred_( UNVALUED_STRING ),
77     ModuleNameReferred_( UNVALUED_STRING ),
78     ModuleRecordCount_( UNVALUED_LONG ),
79     SpatialAddressCount_( UNVALUED_LONG )
80   {}
81 
82 };
83 
84 
sb_Stat()85 sb_Stat::sb_Stat()
86   : imp_( new sb_Stat_Imp() )
87 {
88   setMnemonic("STAT");
89   setID( 1 );
90 
91 
92   // insert static initializers
93 
94 } // Stat ctor
95 
96 
~sb_Stat()97 sb_Stat::~sb_Stat()
98 {
99   delete imp_;
100 } // Stat dtor
101 
102 
103 
104 
105 static sio_8211Converter_I converter_I; // XXX should define these in
106 static sio_8211Converter_A converter_A; // XXX sio_8211Converter.h
107 static sio_8211Converter_R converter_R;
108 static sio_8211Converter_C converter_C;
109 
110 static sio_8211Schema schema_; // module specific schema
111 
112 
113 sio_8211Schema&
schema_()114 sb_Stat::schema_()
115 {
116    return ::schema_;
117 } // sb_Stat::schema_()
118 
119 
120 
121 void
buildSpecificSchema_()122 sb_Stat::buildSpecificSchema_( )
123 {
124   schema_().push_back( sio_8211FieldFormat() );
125 
126   sio_8211FieldFormat& field_format = schema_().back();
127 
128   field_format.setDataStructCode( sio_8211FieldFormat::vector );
129   field_format.setDataTypeCode( sio_8211FieldFormat::mixed_data_type );
130   field_format.setName( "Transfer Statistics" );
131   field_format.setTag( "STAT" );
132 
133 
134   field_format.push_back( sio_8211SubfieldFormat() );
135 
136   field_format.back().setLabel( "MODN" );
137   field_format.back().setType( sio_8211SubfieldFormat::A );
138   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
139   field_format.back().setConverter( &converter_A );
140 
141   field_format.push_back( sio_8211SubfieldFormat() );
142 
143   field_format.back().setLabel( "RCID" );
144   field_format.back().setType( sio_8211SubfieldFormat::I );
145   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
146   field_format.back().setConverter( &converter_I );
147 
148   field_format.push_back( sio_8211SubfieldFormat() );
149 
150   field_format.back().setLabel( "MNTF" );
151   field_format.back().setType( sio_8211SubfieldFormat::A );
152   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
153   field_format.back().setConverter( &converter_A );
154 
155 
156   field_format.push_back( sio_8211SubfieldFormat() );
157 
158   field_format.back().setLabel( "MNRF" );
159   field_format.back().setType( sio_8211SubfieldFormat::A );
160   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
161   field_format.back().setConverter( &converter_A );
162 
163 
164   field_format.push_back( sio_8211SubfieldFormat() );
165 
166   field_format.back().setLabel( "NREC" );
167   field_format.back().setType( sio_8211SubfieldFormat::I );
168   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
169   field_format.back().setConverter( &converter_I );
170 
171 
172   field_format.push_back( sio_8211SubfieldFormat() );
173 
174   field_format.back().setLabel( "NSAD" );
175   field_format.back().setType( sio_8211SubfieldFormat::I );
176   field_format.back().setFormat( sio_8211SubfieldFormat::variable );
177   field_format.back().setConverter( &converter_I );
178 
179 
180 } // build__schema
181 
182 
183 static
184 bool
ingest_record_(sb_Stat & stat,sb_Stat_Imp & stat_imp,sc_Record const & record)185 ingest_record_( sb_Stat& stat, sb_Stat_Imp &stat_imp, sc_Record const& record )
186 {
187 
188   // Make sure we have a record from an
189   // External Spatial Reference module.
190 
191   sc_FieldCntr::const_iterator curfield;
192 
193   if ( ! sb_Utils::getFieldByMnem( record,"STAT",curfield) )
194   {
195 #ifdef SDTSXX_DEBUG
196     cerr << "sb_Stat::sb_Stat(sc_Record const&): "
197          << "Not an transfer statistics record.";
198     cerr << endl;
199 #endif
200     return false;
201   }
202 
203 
204   // We have a primary field from a  module. Start// picking it apart.
205 
206   sc_SubfieldCntr::const_iterator cursubfield;
207 
208   string tmp_str;
209   long   tmp_int;
210 
211 
212   // MODN
213   if (sb_Utils::getSubfieldByMnem(*curfield,"MODN",cursubfield))
214   {
215     cursubfield->getA( tmp_str );
216     stat.setMnemonic( tmp_str );
217   }
218 
219 
220   // RCID
221   if (sb_Utils::getSubfieldByMnem(*curfield,"RCID",cursubfield))
222   {
223     cursubfield->getI( tmp_int );
224     stat.setID( tmp_int );
225   }
226 
227 
228   // MNTF
229   if (sb_Utils::getSubfieldByMnem(*curfield,"MNTF",cursubfield))
230   {
231     cursubfield->getA( stat_imp.ModuleTypeReferred_);
232   }
233 
234   // MNRF
235   if (sb_Utils::getSubfieldByMnem(*curfield,"MNRF",cursubfield))
236   {
237     cursubfield->getA( stat_imp.ModuleNameReferred_);
238   }
239 
240   // NREC
241   if (sb_Utils::getSubfieldByMnem(*curfield,"NREC",cursubfield))
242   {
243     cursubfield->getI( stat_imp.ModuleRecordCount_);
244   }
245 
246   // NSAD
247   if (sb_Utils::getSubfieldByMnem(*curfield,"NSAD",cursubfield))
248   {
249     cursubfield->getI( stat_imp.SpatialAddressCount_);
250   }
251 
252   return true;
253 
254 
255 } // ingest__record
256 
257 
258 
259 
260 bool
getModuleTypeReferred(string & val) const261 sb_Stat::getModuleTypeReferred( string& val ) const
262 {
263   if ( imp_->ModuleTypeReferred_ == UNVALUED_STRING )
264     return false;
265 
266   val = imp_->ModuleTypeReferred_;
267 
268   return true;
269 } // sb_Stat::getModuleTypeReferred
270 
271 
272 bool
getModuleNameReferred(string & val) const273 sb_Stat::getModuleNameReferred( string& val ) const
274 {
275   if ( imp_->ModuleNameReferred_ == UNVALUED_STRING )
276     return false;
277 
278   val = imp_->ModuleNameReferred_;
279 
280   return true;
281 } // sb_Stat::getModuleNameReferred
282 
283 
284 bool
getModuleRecordCount(long & val) const285 sb_Stat::getModuleRecordCount( long& val ) const
286 {
287   if ( imp_->ModuleRecordCount_ == UNVALUED_LONG )
288     return false;
289 
290   val = imp_->ModuleRecordCount_;
291 
292   return true;
293 } // sb_Stat::getModuleRecordCount
294 
295 
296 bool
getSpatialAddressCount(long & val) const297 sb_Stat::getSpatialAddressCount( long& val ) const
298 {
299   if ( imp_->SpatialAddressCount_ == UNVALUED_LONG )
300     return false;
301 
302   val = imp_->SpatialAddressCount_;
303 
304   return true;
305 } // sb_Stat::getSpatialAddressCount
306 
307 
308 
309 
310 
311 
312 bool
getRecord(sc_Record & record) const313 sb_Stat::getRecord( sc_Record & record ) const
314 {
315   record.clear();               // start with a clean slate
316 
317   // first field, which contains module name and record number
318 
319   record.push_back( sc_Field() );
320 
321   record.back().setMnemonic( "STAT" );
322 
323   record.back().setName( "Transfer Statistics" );
324 
325   string tmp_str;
326   long   tmp_long;
327 
328   getMnemonic( tmp_str );
329   sb_Utils::add_subfield( record.back(), "MODN", tmp_str );
330   sb_Utils::add_subfield( record.back(), "RCID", getID() );
331 
332   if ( getModuleTypeReferred( tmp_str ) )
333   {
334     sb_Utils::add_subfield( record.back(),"MNTF", tmp_str );
335   }
336   else
337   {
338     sb_Utils::add_empty_subfield( record.back(), "MNTF", sc_Subfield::is_A );
339   }
340 
341 
342   if ( getModuleNameReferred( tmp_str ) )
343   {
344     sb_Utils::add_subfield( record.back(),"MNRF", tmp_str );
345   }
346   else
347   {
348     sb_Utils::add_empty_subfield( record.back(), "MNRF", sc_Subfield::is_A );
349   }
350 
351 
352   if ( getModuleRecordCount( tmp_long ) )
353   {
354     sb_Utils::add_subfield( record.back(),"NREC", tmp_long );
355   }
356   else
357   {
358     sb_Utils::add_empty_subfield( record.back(), "NREC", sc_Subfield::is_I );
359   }
360 
361 
362   if ( getSpatialAddressCount( tmp_long ) )
363   {
364     sb_Utils::add_subfield( record.back(),"NSAD", tmp_long );
365   }
366   else
367   {
368     sb_Utils::add_empty_subfield( record.back(), "NSAD", sc_Subfield::is_I );
369   }
370 
371 
372   return true;
373 
374 
375 } // Stat::getRecord
376 
377 
378 
379 
380 bool
setModuleTypeReferred(string const & val)381 sb_Stat::setModuleTypeReferred( string const& val )
382 {
383   imp_->ModuleTypeReferred_ = val;
384 
385   return true;
386 } // sb_Stat::setModuleTypeReferred
387 
388 
389 bool
setModuleNameReferred(string const & val)390 sb_Stat::setModuleNameReferred( string const& val )
391 {
392   imp_->ModuleNameReferred_ = val;
393 
394   return true;
395 } // sb_Stat::setModuleNameReferred
396 
397 
398 bool
setModuleRecordCount(long val)399 sb_Stat::setModuleRecordCount( long val )
400 {
401   imp_->ModuleRecordCount_ = val;
402 
403   return true;
404 } // sb_Stat::setModuleRecordCount
405 
406 
407 bool
setSpatialAddressCount(long val)408 sb_Stat::setSpatialAddressCount( long val )
409 {
410   imp_->SpatialAddressCount_ = val;
411 
412   return true;
413 } // sb_Stat::setSpatialAddressCount
414 
415 
416 bool
setRecord(sc_Record const & record)417 sb_Stat::setRecord( sc_Record const& record )
418 {
419   return ingest_record_( *this, *imp_, record );
420 } // sb_Stat::setRecord
421 
422 
423 
424 
425 void
unDefineModuleTypeReferred()426 sb_Stat::unDefineModuleTypeReferred( )
427 {
428   imp_->ModuleTypeReferred_ = UNVALUED_STRING;
429 } // sb_Stat::unDefineModuleTypeReferred
430 
431 
432 void
unDefineModuleNameReferred()433 sb_Stat::unDefineModuleNameReferred( )
434 {
435   imp_->ModuleNameReferred_ = UNVALUED_STRING;
436 } // sb_Stat::unDefineModuleNameReferred
437 
438 
439 void
unDefineModuleRecordCount()440 sb_Stat::unDefineModuleRecordCount( )
441 {
442   imp_->ModuleRecordCount_ = UNVALUED_LONG;
443 } // sb_Stat::unDefineModuleRecordCount
444 
445 
446 void
unDefineSpatialAddressCount()447 sb_Stat::unDefineSpatialAddressCount( )
448 {
449   imp_->SpatialAddressCount_ = UNVALUED_LONG;
450 } // sb_Stat::unDefineSpatialAddressCount
451 
452 
453