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_Dddf.cpp,v 1.4 2002/11/24 22:07:42 mcoletti Exp $
13 //
14
15 #include "builder/sb_Dddf.h"
16
17
18 #include <iostream>
19 #include <strstream>
20 #include <string>
21
22 using namespace std;
23
24 #include <limits.h>
25 #include <float.h>
26
27 #ifndef INCLUDED_SB_UTILS_H
28 #include "builder/sb_Utils.h"
29 #endif
30
31 #ifndef INCLUDED_SB_FOREIGNID_H
32 #include "builder/sb_ForeignID.h"
33 #endif
34
35 #ifndef INCLUDED_SC_RECORD_H
36 #include "container/sc_Record.h"
37 #endif
38
39 #ifndef INCLUDED_SC_FIELD_H
40 #include "container/sc_Field.h"
41 #endif
42
43 #ifndef INCLUDED_SC_SUBFIELD_H
44 #include "container/sc_Subfield.h"
45 #endif
46
47 #ifndef INCLUDED_SIO_8211CONVERTER_H
48 #include "io/sio_8211Converter.h"
49 #endif
50
51
52 using namespace std;
53
54
55 static const char* _ident = "$Id: sb_Dddf.cpp,v 1.4 2002/11/24 22:07:42 mcoletti Exp $";
56
57 // Strings and integers are initialized with these values; they are used
58 // to indicate whether a given module value has been assigned a value or not.
59
60 // (XXX I arbitrarily chose 0x4 as the sentinal value. I hate ad hoc crap.)
61
62 static const string UNVALUED_STRING(1, static_cast<string::value_type>(0x4) );
63
64 static const long UNVALUED_LONG = INT_MIN;
65
66 static const double UNVALUED_DOUBLE = DBL_MAX;
67
68
69 struct sb_Dddf_Imp
70 {
71 string _EntityOrAttribute;
72 string _Label;
73 string _Source;
74 string _Definition;
75 string _AttributeAuthority;
76 string _AttributeAuthorityDescription;
77
78
sb_Dddf_Impsb_Dddf_Imp79 sb_Dddf_Imp()
80 : _EntityOrAttribute( UNVALUED_STRING ),
81 _Label( UNVALUED_STRING ),
82 _Source( UNVALUED_STRING ),
83 _Definition( UNVALUED_STRING ),
84 _AttributeAuthority( UNVALUED_STRING ),
85 _AttributeAuthorityDescription( UNVALUED_STRING )
86 {}
87
resetsb_Dddf_Imp88 void reset()
89 {
90 _EntityOrAttribute = UNVALUED_STRING;
91 _Label = UNVALUED_STRING;
92 _Source = UNVALUED_STRING;
93 _Definition = UNVALUED_STRING;
94 _AttributeAuthority = UNVALUED_STRING;
95 _AttributeAuthorityDescription = UNVALUED_STRING;
96
97 }
98
99 };// struct sb_Dddf_Imp
100
101
sb_Dddf()102 sb_Dddf::sb_Dddf()
103 : _imp( new sb_Dddf_Imp() )
104 {
105 setMnemonic("DDDF");
106 setID( 1 );
107
108
109 // insert static initializers
110
111 } // Dddf ctor
112
113
~sb_Dddf()114 sb_Dddf::~sb_Dddf()
115 {
116 delete _imp;
117 } // Dddf dtor
118
119
120
121
122 static sio_8211Converter_I converter_I; // XXX should define these in
123 static sio_8211Converter_A converter_A; // XXX sio_8211Converter.h
124 static sio_8211Converter_R converter_R;
125 static sio_8211Converter_C converter_C;
126
127 static sio_8211Schema _schema; // module specific schema
128
129 static
130 void
_build_schema(sio_8211Schema & schema)131 _build_schema( sio_8211Schema& schema )
132 {
133 schema.clear(); // make sure we are starting with clean schema
134
135 schema.push_back( sio_8211FieldFormat() );
136
137 sio_8211FieldFormat& field_format = schema.back();
138
139 field_format.setDataStructCode( sio_8211FieldFormat::vector );
140 field_format.setDataTypeCode( sio_8211FieldFormat::mixed_data_type );
141 field_format.setName( "Dddf" );
142 field_format.setTag( "DDDF" );
143
144
145 field_format.push_back( sio_8211SubfieldFormat() );
146
147 field_format.back().setLabel( "MODN" );
148 field_format.back().setType( sio_8211SubfieldFormat::A );
149 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
150 field_format.back().setConverter( &converter_A );
151
152 field_format.push_back( sio_8211SubfieldFormat() );
153
154 field_format.back().setLabel( "RCID" );
155 field_format.back().setType( sio_8211SubfieldFormat::I );
156 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
157 field_format.back().setConverter( &converter_I );
158
159
160 field_format.push_back( sio_8211SubfieldFormat() );
161
162 field_format.back().setLabel( "EORA" );
163 field_format.back().setType( sio_8211SubfieldFormat::A );
164 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
165 field_format.back().setConverter( &converter_A );
166
167
168 field_format.push_back( sio_8211SubfieldFormat() );
169
170 field_format.back().setLabel( "EALB" );
171 field_format.back().setType( sio_8211SubfieldFormat::A );
172 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
173 field_format.back().setConverter( &converter_A );
174
175
176 field_format.push_back( sio_8211SubfieldFormat() );
177
178 field_format.back().setLabel( "SRCE" );
179 field_format.back().setType( sio_8211SubfieldFormat::A );
180 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
181 field_format.back().setConverter( &converter_A );
182
183
184 field_format.push_back( sio_8211SubfieldFormat() );
185
186 field_format.back().setLabel( "DFIN" );
187 field_format.back().setType( sio_8211SubfieldFormat::A );
188 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
189 field_format.back().setConverter( &converter_A );
190
191
192 field_format.push_back( sio_8211SubfieldFormat() );
193
194 field_format.back().setLabel( "AUTH" );
195 field_format.back().setType( sio_8211SubfieldFormat::A );
196 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
197 field_format.back().setConverter( &converter_A );
198
199
200 field_format.push_back( sio_8211SubfieldFormat() );
201
202 field_format.back().setLabel( "ADSC" );
203 field_format.back().setType( sio_8211SubfieldFormat::A );
204 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
205 field_format.back().setConverter( &converter_A );
206
207
208
209 } // _build_schema
210
211
212 static
213 bool
_ingest_record(sb_Dddf & dddf,sb_Dddf_Imp & dddf_imp,sc_Record const & record)214 _ingest_record( sb_Dddf& dddf, sb_Dddf_Imp &dddf_imp, sc_Record const& record )
215 {
216
217 // Make sure we have a record from an
218 // External Spatial Reference module.
219
220 sc_FieldCntr::const_iterator curfield;
221
222 if ( ! sb_Utils::getFieldByMnem( record,"DDDF",curfield) )
223 {
224 #ifdef SDTSXX_DEBUG
225 cerr << "sb_Dddf::sb_Dddf(sc_Record const&): "
226 << "Not an data dictionary/definition record.";
227 cerr << endl;
228 #endif
229 return false;
230 }
231
232
233 // We have a primary field from a module. Start// picking it apart.
234
235 sc_SubfieldCntr::const_iterator cursubfield;
236
237 string tmp_str;
238 long tmp_int;
239
240
241 // MODN
242 if (sb_Utils::getSubfieldByMnem(*curfield,"MODN",cursubfield))
243 {
244 cursubfield->getA( tmp_str );
245 dddf.setMnemonic( tmp_str );
246 }
247
248
249 // RCID
250 if (sb_Utils::getSubfieldByMnem(*curfield,"RCID",cursubfield))
251 {
252 cursubfield->getI( tmp_int );
253 dddf.setID( tmp_int );
254 }
255
256
257 // EORA
258 if (sb_Utils::getSubfieldByMnem(*curfield,"EORA",cursubfield))
259 {
260 cursubfield->getA( dddf_imp._EntityOrAttribute);
261 }
262 else
263 {
264 // return false;
265 }
266
267
268 // EALB
269 if (sb_Utils::getSubfieldByMnem(*curfield,"EALB",cursubfield))
270 {
271 cursubfield->getA( dddf_imp._Label);
272 }
273 else
274 {
275 // return false;
276 }
277
278
279 // SRCE
280 if (sb_Utils::getSubfieldByMnem(*curfield,"SRCE",cursubfield))
281 {
282 cursubfield->getA( dddf_imp._Source);
283 }
284 else
285 {
286 // return false;
287 }
288
289
290 // DFIN
291 if (sb_Utils::getSubfieldByMnem(*curfield,"DFIN",cursubfield))
292 {
293 cursubfield->getA( dddf_imp._Definition);
294 }
295 else
296 {
297 // return false;
298 }
299
300
301 // AUTH
302 if (sb_Utils::getSubfieldByMnem(*curfield,"AUTH",cursubfield))
303 {
304 cursubfield->getA( dddf_imp._AttributeAuthority);
305 }
306 else
307 {
308 // return false;
309 }
310
311
312 // ADSC
313 if (sb_Utils::getSubfieldByMnem(*curfield,"ADSC",cursubfield))
314 {
315 cursubfield->getA( dddf_imp._AttributeAuthorityDescription);
316 }
317 else
318 {
319 // return false;
320 }
321
322
323 return true;
324
325
326 } // _ingest_record
327
328
329
330
331 bool
getEntityOrAttribute(string & val) const332 sb_Dddf::getEntityOrAttribute( string & val ) const
333 {
334 if ( _imp->_EntityOrAttribute == UNVALUED_STRING )
335 return false;
336
337 val = _imp->_EntityOrAttribute;
338
339 return true;
340
341 } // sb_Dddf::getEntityOrAttribute
342
343
344 bool
getLabel(string & val) const345 sb_Dddf::getLabel( string & val ) const
346 {
347 if ( _imp->_Label == UNVALUED_STRING )
348 return false;
349
350 val = _imp->_Label;
351
352 return true;
353
354 } // sb_Dddf::getLabel
355
356
357 bool
getSource(string & val) const358 sb_Dddf::getSource( string & val ) const
359 {
360 if ( _imp->_Source == UNVALUED_STRING )
361 return false;
362
363 val = _imp->_Source;
364
365 return true;
366
367 } // sb_Dddf::getSource
368
369
370 bool
getDefinition(string & val) const371 sb_Dddf::getDefinition( string & val ) const
372 {
373 if ( _imp->_Definition == UNVALUED_STRING )
374 return false;
375
376 val = _imp->_Definition;
377
378 return true;
379
380 } // sb_Dddf::getDefinition
381
382
383 bool
getAttributeAuthority(string & val) const384 sb_Dddf::getAttributeAuthority( string & val ) const
385 {
386 if ( _imp->_AttributeAuthority == UNVALUED_STRING )
387 return false;
388
389 val = _imp->_AttributeAuthority;
390
391 return true;
392
393 } // sb_Dddf::getAttributeAuthority
394
395
396 bool
getAttributeAuthorityDescription(string & val) const397 sb_Dddf::getAttributeAuthorityDescription( string & val ) const
398 {
399 if ( _imp->_AttributeAuthorityDescription == UNVALUED_STRING )
400 return false;
401
402 val = _imp->_AttributeAuthorityDescription;
403
404 return true;
405
406 } // sb_Dddf::getAttributeAuthorityDescription
407
408
409 bool
getSchema(sio_8211Schema & schema) const410 sb_Dddf::getSchema( sio_8211Schema& schema ) const
411 {
412 // If the schema hasn't been
413 // initialized, please do so.
414
415 if ( _schema.empty() )
416 {
417 _build_schema( _schema );
418 }
419
420 if ( _schema.empty() ) // oops ... something screwed up
421 {
422 return false;
423 }
424
425 schema = _schema;
426
427 return true;
428
429 } // sb_Dddf::getSchema
430
431
432
433
434 bool
getRecord(sc_Record & record) const435 sb_Dddf::getRecord( sc_Record & record ) const
436 {
437 record.clear(); // start with a clean slate
438
439 // first field, which contains module name and record number
440
441 sb_ForeignID tmp_foreign_id;
442
443 record.push_back( sc_Field() );
444
445 record.back().setMnemonic( "DDDF" );
446
447 record.back().setName( "Dddf" );
448
449 string tmp_str;
450
451 getMnemonic( tmp_str );
452 sb_Utils::add_subfield( record.back(), "MODN", tmp_str );
453 sb_Utils::add_subfield( record.back(), "RCID", getID() );
454
455 if ( getEntityOrAttribute( tmp_str ) )
456 {
457 sb_Utils::add_subfield( record.back(),"EORA", tmp_str );
458 }
459 else
460 {
461 sb_Utils::add_empty_subfield( record.back(), "EORA", sc_Subfield::is_A );
462 }
463
464
465 if ( getLabel( tmp_str ) )
466 {
467 sb_Utils::add_subfield( record.back(),"EALB", tmp_str );
468 }
469 else
470 {
471 sb_Utils::add_empty_subfield( record.back(), "EALB", sc_Subfield::is_A );
472 }
473
474
475 if ( getSource( tmp_str ) )
476 {
477 sb_Utils::add_subfield( record.back(),"SRCE", tmp_str );
478 }
479 else
480 {
481 sb_Utils::add_empty_subfield( record.back(), "SRCE", sc_Subfield::is_A );
482 }
483
484
485 if ( getDefinition( tmp_str ) )
486 {
487 sb_Utils::add_subfield( record.back(),"DFIN", tmp_str );
488 }
489 else
490 {
491 sb_Utils::add_empty_subfield( record.back(), "DFIN", sc_Subfield::is_A );
492 }
493
494
495 if ( getAttributeAuthority( tmp_str ) )
496 {
497 sb_Utils::add_subfield( record.back(),"AUTH", tmp_str );
498 }
499 else
500 {
501 sb_Utils::add_empty_subfield( record.back(), "AUTH", sc_Subfield::is_A );
502 }
503
504
505 if ( getAttributeAuthorityDescription( tmp_str ) )
506 {
507 sb_Utils::add_subfield( record.back(),"ADSC", tmp_str );
508 }
509 else
510 {
511 sb_Utils::add_empty_subfield( record.back(), "ADSC", sc_Subfield::is_A );
512 }
513
514
515 return true;
516
517
518 } // Dddf::getRecord
519
520
521
522
523 bool
setEntityOrAttribute(string const & val)524 sb_Dddf::setEntityOrAttribute( string const& val )
525 {
526 _imp->_EntityOrAttribute = val;
527
528 return true;
529 } // sb_Dddf::setEntityOrAttribute
530
531
532 bool
setLabel(string const & val)533 sb_Dddf::setLabel( string const& val )
534 {
535 _imp->_Label = val;
536
537 return true;
538 } // sb_Dddf::setLabel
539
540
541 bool
setSource(string const & val)542 sb_Dddf::setSource( string const& val )
543 {
544 _imp->_Source = val;
545
546 return true;
547 } // sb_Dddf::setSource
548
549
550 bool
setDefinition(string const & val)551 sb_Dddf::setDefinition( string const& val )
552 {
553 _imp->_Definition = val;
554
555 return true;
556 } // sb_Dddf::setDefinition
557
558
559 bool
setAttributeAuthority(string const & val)560 sb_Dddf::setAttributeAuthority( string const& val )
561 {
562 _imp->_AttributeAuthority = val;
563
564 return true;
565 } // sb_Dddf::setAttributeAuthority
566
567
568 bool
setAttributeAuthorityDescription(string const & val)569 sb_Dddf::setAttributeAuthorityDescription( string const& val )
570 {
571 _imp->_AttributeAuthorityDescription = val;
572
573 return true;
574 } // sb_Dddf::setAttributeAuthorityDescription
575
576
577 bool
setRecord(sc_Record const & record)578 sb_Dddf::setRecord( sc_Record const& record )
579 {
580 _imp->reset(); // reset to new state; i.e., clear out foreign
581 // identifiers, etc.
582 return _ingest_record( *this, *_imp, record );
583 } // sb_Dddf::setRecord
584
585
586
587
588 void
unDefineEntityOrAttribute()589 sb_Dddf::unDefineEntityOrAttribute( )
590 {
591 _imp->_EntityOrAttribute = UNVALUED_STRING;
592 } // sb_Dddf::unDefineEntityOrAttribute
593
594
595 void
unDefineLabel()596 sb_Dddf::unDefineLabel( )
597 {
598 _imp->_Label = UNVALUED_STRING;
599 } // sb_Dddf::unDefineLabel
600
601
602 void
unDefineSource()603 sb_Dddf::unDefineSource( )
604 {
605 _imp->_Source = UNVALUED_STRING;
606 } // sb_Dddf::unDefineSource
607
608
609 void
unDefineDefinition()610 sb_Dddf::unDefineDefinition( )
611 {
612 _imp->_Definition = UNVALUED_STRING;
613 } // sb_Dddf::unDefineDefinition
614
615
616 void
unDefineAttributeAuthority()617 sb_Dddf::unDefineAttributeAuthority( )
618 {
619 _imp->_AttributeAuthority = UNVALUED_STRING;
620 } // sb_Dddf::unDefineAttributeAuthority
621
622
623 void
unDefineAttributeAuthorityDescription()624 sb_Dddf::unDefineAttributeAuthorityDescription( )
625 {
626 _imp->_AttributeAuthorityDescription = UNVALUED_STRING;
627 } // sb_Dddf::unDefineAttributeAuthorityDescription
628
629
630 sio_8211Schema&
schema_()631 sb_Dddf::schema_()
632 {
633 if ( _schema.empty() )
634 {
635 buildSpecificSchema_();
636 }
637
638 return _schema;
639 } // sb_Dddf::schema_()
640
641
642
643 void
buildSpecificSchema_()644 sb_Dddf::buildSpecificSchema_()
645 {_build_schema( _schema );
646 } // sb_Dddf::buildSpecificSchema_()
647