1 //
2 // convert.cpp
3 //
4 
5 #include "convert.h"
6 
7 #include <iostream>
8 #include <fstream>
9 #include <strstream>
10 #include <iomanip>
11 
12 #include <string>
13 #include <algorithm>
14 #include <functional>
15 
16 using namespace std;
17 
18 
19 #include <sdts++/container/sc_Record.h>
20 #include <sdts++/io/sio_Reader.h>
21 
22 
23 
24 static const char * const ident_ =
25     "$Id: convert.cpp,v 1.3 2001/05/18 23:08:36 mcoletti Exp $";
26 
27 
28 
29 static
30 void
emitSubfieldSchema_(sc_Subfield const & subfield)31 emitSubfieldSchema_( sc_Subfield const & subfield )
32 {
33    cout << subfield.mnemonic() << " : ";
34 
35    switch( subfield.getSubfieldType() )
36    {
37       case sc_Subfield::is_A :
38          cout << "A";
39          break;
40       case sc_Subfield::is_I :
41          cout << "I";
42          break;
43       case sc_Subfield::is_R :
44          cout << "R";
45          break;
46       case sc_Subfield::is_S :
47          cout << "S";
48          break;
49       case sc_Subfield::is_C :
50          cout << "C";
51          break;
52       case sc_Subfield::is_B :
53          cout << "B";
54          break;
55       case sc_Subfield::is_BI8 :
56          cout << "BI8";
57          break;
58       case sc_Subfield::is_BI16 :
59          cout << "BI16";
60          break;
61       case sc_Subfield::is_BI24 :
62          cout << "BI24";
63          break;
64       case sc_Subfield::is_BI32 :
65          cout << "BI32";
66          break;
67       case sc_Subfield::is_BUI :
68          cout << "BUI";
69          break;
70       case sc_Subfield::is_BUI8 :
71          cout << "BUI8";
72          break;
73       case sc_Subfield::is_BUI16 :
74          cout << "BUI16";
75          break;
76       case sc_Subfield::is_BUI24 :
77          cout << "BUI24";
78          break;
79       case sc_Subfield::is_BUI32 :
80          cout << "BUI32";
81          break;
82       case sc_Subfield::is_BFP32 :
83          cout << "BFP32";
84          break;
85       case sc_Subfield::is_BFP64 :
86          cout << "BFP64";
87          break;
88    }
89 } // emitSubfieldSchema
90 
91 
92 
93 
94 static
95 void
emitFieldSchema_(sc_Field const & field)96 emitFieldSchema_( sc_Field const & field )
97 {
98    string buffer;
99 
100    buffer = field.name( );
101 
102    cout << "field \"" << buffer << "\" ";
103 
104    buffer = field.mnemonic( );
105 
106    cout << buffer << "\n";
107    cout << "{\n   ";
108 
109    sc_Field::const_iterator current_subfield = field.begin();
110 
111    do
112    {
113       // these two subfields are automatically handled by mksdts
114       // so we don't need to emit them
115       if ( "MODN" == current_subfield->mnemonic() )
116       {
117          current_subfield++;
118          continue;
119       }
120       else if ( "RCID" == current_subfield->mnemonic() )
121       {
122          current_subfield++;
123          continue;
124       }
125 
126       emitSubfieldSchema_( *current_subfield );
127 
128       current_subfield ++;
129 
130       if ( field.end() != current_subfield )
131       {
132          cout << ", ";
133       }
134    } while ( current_subfield != field.end() );
135 
136    cout << "\n}\n\n";
137 } // emitFieldSchema_
138 
139 
140 
141 
142 static
143 void
emitSchema_(sc_Record const & record)144 emitSchema_( sc_Record const & record )
145 {
146    // the first field should have information about the module, like
147    // it's mnemonic and name
148 
149    string buffer = record.front().getMnemonic( ) ;
150 
151    cout << "module " << buffer << "\n\n";
152 
153                                 // This assumes, of course, that the
154                                 // module is homogenous; i.e., it's
155                                 // possible that there exist fields
156                                 // defined in the DDR that won't
157                                 // necessarily show up in the first
158                                 // record; though this CAN happen,
159                                 // I've never seen that to be the case.
160 
161    for ( sc_Record::const_iterator i = record.begin() ;
162          i != record.end();
163          i++ )
164    {
165       emitFieldSchema_( *i );
166    }
167 
168 } // emitSchema_
169 
170 
171 
172 
173 static
174 void
emitField_(sc_Field const & field)175 emitField_( sc_Field const & field )
176 {
177    cout << "\t" << field.mnemonic() << "\n";
178    cout << "\t" << "{\n\t   ";
179 
180    sc_Field::const_iterator i = field.begin();
181 
182    do
183    {
184       // these two subfields are automatically handled by mksdts
185       // so we don't need to emit them
186       if ( "MODN" == i->mnemonic() )
187       {
188          i++;
189          continue;
190       }
191       else if ( "RCID" == i->mnemonic() )
192       {
193          i++;
194          continue;
195       }
196 
197       if ( sc_Subfield::is_A == i->getSubfieldType() )
198       {
199          string value;
200          i->getA( value );
201 
202          cout << "\""
203               << value
204               << "\"";
205       }
206       else
207       {
208          float f_val;
209          int   i_val;
210 
211          if ( i->getFloat( f_val ) )
212          {
213             cout << f_val;
214          }
215          else if ( i->getInt( i_val ) )
216          {
217             cout << i_val;
218          }
219          else
220          {
221             cerr << "unknown or unhandled subfield type\n";
222             exit( 7 );
223          }
224       }
225 
226       i++;
227 
228       if ( i != field.end() )
229       {
230          cout << ", ";
231       }
232 
233    } while ( i != field.end() );
234 
235    cout << "\n\t}\n";
236 
237 } // emitField
238 
239 
240 
241 
242 static
243 void
emitRecord_(sc_Record const & record)244 emitRecord_( sc_Record const & record )
245 {
246    cout << "record\n";
247    cout << "{\n";
248 
249    for ( sc_Record::const_iterator current_field = record.begin();
250          current_field != record.end();
251          current_field++ )
252    {
253       emitField_( *current_field );
254    }
255 
256    cout << "\n}\n\n";
257 
258 } // emitRecord_
259 
260 
261 
262 void
convertToSDL(ifstream & module_stream)263 convertToSDL( ifstream & module_stream )
264 {
265    sio_8211Reader reader( module_stream ); // XXX we need to specify
266                                 //  binary converters
267 
268    sio_8211ForwardIterator i( reader );
269 
270    sc_Record record;
271 
272    if ( ! i.get( record ) )
273    {
274       cerr << "unable to read from module\n";
275       exit( 5 );
276    }
277 
278    // first, emit the schema section
279    emitSchema_( record );
280 
281 
282    // then blat out the records
283 
284    do
285    {
286       emitRecord_( record );
287 
288       ++i;
289 
290       if ( i ) { i.get( record ) ; }
291 
292    } while( i );
293 
294 } // convertToSDL( ifstream module_stream )
295 
296