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 
13 #include <sdts++/builder/sb_ForeignID.h>
14 
15 
16 #ifndef INCLUDED_SIO_CONVERTERFACTORY_H
17 #include "io/sio_ConverterFactory.h"
18 #endif
19 
20 #ifndef INCLUDED_SIO_8211CONVERTER_H
21 #include "io/sio_8211Converter.h"
22 #endif
23 
24 #ifndef INCLUDED_SC_FIELD_H
25 #include "container/sc_Field.h"
26 #endif
27 
28 #ifndef INCLUDED_SB_UTILS_H
29 #include "sb_Utils.h"
30 #endif
31 
32 
33 #include <strstream>
34 
35 
36 using namespace std;
37 
38 
39 static const char* ident_ =
40   "$Id: sb_ForeignID.cpp,v 1.14 2002/11/24 22:07:42 mcoletti Exp $";
41 
42 
43 
44 
45 static
46 char usage_type_codes_[9] =
47 { 0x0, 'S', 'E', 'L', 'R', 'F', 'B', 'I', 'X' };
48 
49 
50 /**
51     maps a usage modifer to its character representation according to
52     the SDTS specification
53 */
54 static
55 char
usageModifierToChar_(sb_ForeignID::usage_t ut)56 usageModifierToChar_( sb_ForeignID::usage_t ut )
57 {
58    return usage_type_codes_[ ut ];
59 } // usageModifierToChar_
60 
61 
62 
63 
64 bool
packedIdentifierString(string & fid) const65 sb_ForeignID::packedIdentifierString( string& fid ) const
66 {
67   if ( moduleName_.empty() || recordID_ < 0 )
68     {
69       return false;
70     }
71 
72   strstream tmp_ss;
73 
74                                 // XXX Should add sanity check to see
75                                 // XXX that usage modifier is in the
76                                 // XXX proper domain if it actually has a
77                                 // XXX value.
78 
79   tmp_ss << moduleName_ << "#" << recordID_;
80 
81   if ( sb_ForeignID::none != usageModifier_ )
82   {
83      tmp_ss << usageModifierToChar_( usageModifier_ );
84   }
85 
86   getline( tmp_ss, fid );
87 
88   return true;
89 } // sb_ForeignID::get( string& fid )
90 
91 
92 
93 /// These are the strings used by default for sb_ForeignID, but can be
94 /// over-ridden by the user.
95 static std::string const foreign_id_long_str = "FOREIGN ID";
96 static std::string const foreign_id_mnemonic = "FRID";
97 
98 
sb_ForeignID()99 sb_ForeignID::sb_ForeignID()
100    : recordID_( 1 ),      // most module records start at one, not zero
101      usageModifier_( sb_ForeignID::none ),
102      name_( foreign_id_long_str ),
103      mnemonic_( foreign_id_mnemonic )
104 {}
105 
106 
sb_ForeignID(std::string const & name,std::string const & mnemonic)107 sb_ForeignID::sb_ForeignID( std::string const& name,
108                             std::string const& mnemonic)
109    : recordID_( 1 ),            // most module records start at one, not zero
110      usageModifier_( sb_ForeignID::none ),
111      name_( name ),
112      mnemonic_( mnemonic )
113 {}
114 
115 
sb_ForeignID(std::string const & mn,long id,sb_ForeignID::usage_t um)116 sb_ForeignID::sb_ForeignID( std::string const& mn,
117                             long id,
118                             sb_ForeignID::usage_t um )
119    : moduleName_( mn ),
120      recordID_( id ),
121      usageModifier_( um ),
122      name_( foreign_id_long_str ),
123      mnemonic_( foreign_id_mnemonic )
124 {}
125 
126 
127 
128 
129 void
addFieldToSchema(sio_8211Schema & schema,std::string const & name,std::string const & mnemonic,bool isRepeating) const130 sb_ForeignID::addFieldToSchema( sio_8211Schema& schema,
131                                 std::string const & name,
132                                 std::string const & mnemonic,
133                                 bool isRepeating ) const
134 {
135    schema.push_back( sio_8211FieldFormat() );
136 
137    sio_8211FieldFormat& field_format = schema.back();
138 
139    field_format.setDataStructCode( sio_8211FieldFormat::array );
140    field_format.setDataTypeCode( sio_8211FieldFormat::mixed_data_type );
141 
142    // XXX probably a better way to do this; suggestive of API flaw
143    if ( "" == name )
144    {
145       field_format.setName( sb_ForeignID::name() );
146    }
147    else
148    {
149       field_format.setName( name );
150    }
151 
152    if ( "" == mnemonic )
153    {
154       field_format.setTag( sb_ForeignID::mnemonic() );
155    }
156    else
157    {
158       field_format.setTag( mnemonic );
159    }
160 
161    field_format.push_back( sio_8211SubfieldFormat() );
162 
163    field_format.back().setLabel( "MODN" );
164    field_format.back().setType( sio_8211SubfieldFormat::A );
165    field_format.back().setFormat( sio_8211SubfieldFormat::variable );
166    field_format.back().setConverter( sio_ConverterFactory::instance()->get( "A" ) );
167 
168    field_format.back().setLabel( "RCID" );
169    field_format.back().setType( sio_8211SubfieldFormat::I );
170    field_format.back().setFormat( sio_8211SubfieldFormat::variable );
171    field_format.back().setConverter( sio_ConverterFactory::instance()->get( "I" ) );
172 
173    // XXX add usage modifier field?
174 
175    if ( isRepeating )
176    {
177       field_format.setIsRepeating( true );
178    }
179 
180 } // sb_ForeignID::addFieldToSchema( sio_8211Schema& schema )
181 
182 
183 
184 bool
assign(sc_Field const & field)185 sb_ForeignID::assign( sc_Field const & field )
186 {
187    mnemonic( field.getMnemonic() );
188    name( field.getName() );
189 
190    // now pull in the module and record values
191 
192    sc_SubfieldCntr::const_iterator cursubfield;
193 
194    string tmp_str;
195 
196    if ( sb_Utils::getSubfieldByMnem( field, "MODN", cursubfield ) )
197    {
198       cursubfield->getA( tmp_str );
199       moduleName( tmp_str );
200    }
201    else                         // maybe a packed id?
202    {
203       return false;
204    }
205 
206 
207    if ( sb_Utils::getSubfieldByMnem( field, "RCID", cursubfield ) )
208    {
209       long tmp_long;
210       cursubfield->getI( tmp_long );
211       recordID( tmp_long );
212    }
213    else                         // maybe a packed id?
214    {
215       return false;
216    }
217 
218    return true;
219 
220 } // sb_ForeignID::assign
221 
222 
223 
224 
225 static std::string const attribute_id_long_str = "ATTRIBUTE ID";
226 static std::string const attribute_id_mnemonic = "ATID";
227 
228 
sb_AttributeID()229 sb_AttributeID::sb_AttributeID()
230    : sb_ForeignID( attribute_id_long_str, attribute_id_mnemonic )
231 {}
232 
233 
234 
sb_AttributeID(std::string const & mn,long id,sb_ForeignID::usage_t um)235 sb_AttributeID::sb_AttributeID( std::string const& mn,
236                                 long id,
237                                 sb_ForeignID::usage_t um )
238    : sb_ForeignID( mn, id, um )
239 {}
240 
241 
242 
243