Polaris: FormatDB.cc Source File

FormatDB.cc

Go to the documentation of this file.
00001 ///
00002 #ifdef POLARIS_GNU_PRAGMAS
00003 #pragma implementation
00004 #endif
00005 ///
00006 #include <stdlib.h>
00007 #include <ctype.h>
00008 
00009 #include "BinRep.h"
00010 #include "Collection/Iterator.h"
00011 #include "Collection/KeyIterator.h"
00012 #include "FormatDB.h"
00013 #include "VDL.h"
00014 #include "debug.h"
00015 #include "split_line.h"
00016 #include "wide_output.h"
00017 
00018 #include "p-assert.h"
00019 
00020 FormatDB::~FormatDB()
00021 {
00022     #ifdef CLASS_INSTANCE_REGISTRY
00023     unregister_instance(FORMATDB, this);
00024     #endif
00025 }
00026 
00027 void
00028 FormatDB::print (ostream & o) const
00029 {
00030     o << "{\n";
00031 
00032     KeyIterator<int,Format> iter = *this;
00033 
00034     while (iter.valid()) {
00035       o << iter.current_data() << "\n";
00036       ++iter;
00037     }
00038 
00039     o << "}\n";
00040 
00041 }
00042 
00043 void 
00044 FormatDB::write(ostream & o, int space_for_label, int max_line_len) const
00045 {
00046   for (KeyIterator<int,Format> iter = *this; iter.valid(); ++iter) {
00047 
00048     int    num_label = iter.current_data().value();
00049 
00050     String line("FORMAT(");
00051 
00052     line += iter.current_data().format_ref();
00053     line += ")";
00054 
00055     if (space_for_label == 0)
00056       blank_wide_output( o );
00057     split_line(o, space_for_label, "", num_label, line, max_line_len);
00058 
00059   }
00060 }
00061 
00062 FormatDB &
00063 FormatDB::operator = (const FormatDB & fdb) 
00064 {
00065   Database<int,Format>::operator = ( fdb );
00066   return (*this);
00067 }
00068 
00069 FormatDB     *
00070 FormatDB::clone() const
00071 {
00072     return new FormatDB(*this);
00073 }
00074 
00075 void 
00076 FormatDB::ins(Format *elem, int replace_if_already_exists GIV(0))
00077 {
00078     p_assert( elem, "FormatDB::ins: must insert a non-NULL element" );
00079     p_assert( _pgm, "FormatDB::ins: error _pgm is NULL" );
00080 
00081     int value = elem->value();
00082 
00083     p_assert( replace_if_already_exists ||
00084           (! (int) find_ref(value)),
00085           "FormatDB::ins: found duplicate format statement" );
00086 
00087     p_assert( !_pgm->stmts().labels().member(value),
00088           "FormatDB::ins: found duplicate label statement");
00089 
00090     Database<int,Format>::ins(value, elem);
00091 }
00092 
00093 void
00094 FormatDB::relabel(int value, int new_value)
00095 {
00096   Format * grabbed = grab( value );
00097   p_assert( grabbed,
00098         "FormatDB::relabel: element to relabel not found" );
00099   
00100 /// WARNING: this is a private member access!
00101 
00102   grabbed->_value = new_value;
00103 
00104 /// Reinsert format with new value
00105 
00106   ins(grabbed);
00107 
00108 }
00109 
00110 int 
00111 FormatDB::structures_OK() const
00112 {
00113   if (!Database<int,Format>::structures_OK()) {
00114     cerr << "In context of a FormatDB.\n";
00115     return 0;
00116   }
00117 
00118   return 1;
00119 }
00120 
00121 FormatDB::FormatDB(const ProgramUnit &pgm, const BinRep &binstr)
00122 {
00123     #ifdef CLASS_INSTANCE_REGISTRY
00124     register_instance(FORMATDB, sizeof(FormatDB), this);
00125     #endif
00126 
00127   _pgm = CASTAWAY(ProgramUnit *) &pgm;
00128 
00129   for (Iterator<BinRep> iter = binstr.to_set(); iter.valid(); ++iter) {
00130     String format_name;
00131 
00132     iter.current()[0].to_string(format_name);
00133 
00134     p_assert(format_name[0] == 'F',
00135          "Format from SETL did not start with an 'F'");
00136 
00137     /// ...  Skip past the 'F' at the beginning, taking only the
00138     /// ...  (ascii) integer
00139     const char *format_id = (const char *)format_name + 1;
00140 
00141     int len = strlen(format_id);
00142 
00143     int value = 0;
00144 
00145     for (int i = 0; i < len; ++i) {
00146       p_assert( isdigit(format_id[i]),
00147         "Format from SETL was not in the format 'Fxxx' "
00148         "with xxx being an integer");
00149       value = (value * 10) + (format_id[i] - '0');
00150     }
00151 
00152     if (dbx_format_debug_level >= 1) {
00153       cout << "FormatDB: Initializing " << format_name << 
00154     "( => \"" << format_id << "\" )\n";
00155     }
00156 
00157     Database<int,Format>::ins(value, new Format(value, iter.current()[1]));
00158   }
00159 }
00160 
00161 void
00162 FormatDB::exchange_convert( VDL &vdl )
00163 {
00164     BinRep     *b = CASTAWAY(BinRep *) vdl.data_ref();
00165  
00166     if ( this->entries() > 0 ) {
00167         List<BinRep> *L  = new List<BinRep>;
00168  
00169         L->ins_last(new BinRep( "formats" ));
00170         L->ins_last(new BinRep( new Set<BinRep> ));
00171  
00172         b->to_set().ins( new BinRep( L ) );
00173 
00174         Set<BinRep> &S  = b->find_ref( "formats" )->to_set();
00175  
00176         for (KeyIterator<int,Format> iter = (*this); iter.valid(); ++iter) {
00177             Format      &ft = iter.current_data();
00178             BinRep      *br = new BinRep( new List<BinRep> );
00179 
00180         ostrstream *str_ref = new ostrstream;
00181 
00182         (*str_ref) << 'F' << ft.value() << '\000';
00183 
00184         char *fdata = str_ref->str();
00185 
00186             br->to_tuple().ins_last( new BinRep( fdata ));
00187             br->to_tuple().ins_last( new BinRep( ft.format_ref() ));
00188  
00189             S.ins( br );
00190 
00191         delete [] fdata;
00192         delete str_ref;
00193         }
00194     }
00195 }
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:52 2005