Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

io_tools.h

Go to the documentation of this file.
00001 
00082 #ifndef __IO_TOOLS__
00083 #define __IO_TOOLS__
00084 
00085 
00086 #include <iostream>
00087 
00088 #include "msg_stream.h"
00089 
00090 
00091 namespace IO_tools{
00092 
00094   template<typename T> inline void write_4_bytes(const T& v,
00095                                                  std::ostream& out);
00096 
00098   template<typename T> inline void write_2_bytes(const T& v,
00099                                                  std::ostream& out);
00100 
00102   template<typename T> inline void read_4_bytes(T* result,
00103                                                 std::istream& in);
00104 
00106   template<typename T> inline void read_2_bytes(T* result,
00107                                                 std::istream& in);
00108 
00110   template<typename T>
00111   inline void convert_bit_order(const T& var,
00112                                 T* result);
00113 
00115   inline void go_to_next(const char c,
00116                          std::istream& in);
00117 
00119   inline void go_to_next_token(const std::string token,
00120                                std::istream& in);
00121 
00123   inline char go_to_next_of_list(const std::string token_list,
00124                                  std::istream& in);
00125  
00126 
00127 
00128 /*
00129   ###########################################################
00130   ###########################################################
00131   ###########################################################
00132   ##############                               ##############
00133   ##############  I M P L E M E N T A T I O N  ##############
00134   ##############                               ##############
00135   ###########################################################
00136   ###########################################################
00137   ###########################################################
00138 */
00139 
00140 
00141 
00142   template<typename T>
00143   inline void convert_bit_order(const T& var,
00144                                 T* result){
00145     
00146 #ifdef REVERSE_BIT_ORDER
00147     int i;
00148     char* in  = reinterpret_cast<char*>(&const_cast<T&>(var));
00149     char* out = reinterpret_cast<char*>(result);
00150 #endif
00151 
00152     int size = sizeof(T);
00153     switch (size){
00154     case 1:
00155       *result = var;
00156       return;
00157     case 2:
00158     case 4:
00159     
00160 #ifdef REVERSE_BIT_ORDER
00161       for(i=0;i<size;i++){
00162         out[i] = in[size-1-i];
00163       }
00164 #else
00165       *result = var;
00166 #endif
00167     
00168       return;
00169       
00170     default:
00171       Message::error<<"convert_bit_order: variable size("
00172                     <<size<<") not handled"<<Message::done;
00173       break;
00174     }
00175   }
00176 
00177   template<typename T> inline void write_4_bytes(const T& v,
00178                                                  std::ostream& out){
00179 #ifdef DEBUG
00180     if (sizeof(T)!=4) {
00181       Message::error<<"write_4_bytes: the variable is not coded with 4 bytes"
00182                     <<Message::done;
00183     }
00184 #endif
00185 
00186     T buf;
00187     convert_bit_order(v,&buf);
00188     char* byte = (char*)(&buf);
00189     for(int i=0;i<4;i++){
00190       out.put(byte[i]);
00191     }
00192   }
00193 
00194 
00195   template<typename T> inline void write_2_bytes(const T& v,
00196                                                  std::ostream& out){
00197 #ifdef DEBUG
00198     if (sizeof(T)!=2) {
00199       Message::error<<"write_2_bytes: the variable is not coded with 2 bytes"
00200                     <<Message::done;
00201     }
00202 #endif
00203 
00204     T buf;
00205     convert_bit_order(v,&buf);
00206     char* byte = (char*)(&buf);
00207     for(int i=0;i<2;i++){
00208       out.put(byte[i]);
00209     }
00210   }
00211 
00212   template<typename T> inline void read_4_bytes(T* result,
00213                                                 std::istream& in){
00214 #ifdef DEBUG
00215     if (sizeof(T)!=4) {
00216       Message::error<<"read_4_bytes: the variable is not coded with 4 bytes"
00217                     <<Message::done;
00218     }
00219 #endif
00220 
00221     T buf;
00222     char* byte = (char*)(&buf);
00223 
00224     for(int i=0;i<4;i++){
00225       in.get(byte[i]);
00226     }
00227 
00228     convert_bit_order(buf,result);
00229   }
00230 
00231 
00232 
00233   
00234   template<typename T> inline void read_2_bytes(T* result,
00235                                                 std::istream& in){
00236 #ifdef DEBUG
00237     if (sizeof(T)!=2) {
00238       Message::error<<"read_2_bytes: the variable is not coded with 2 bytes"
00239                     <<Message::done;
00240     }
00241 #endif
00242 
00243     T buf;
00244     char* byte = (char*)(&buf);
00245 
00246     for(int i=0;i<2;i++){
00247       in.get(byte[i]);
00248     }
00249 
00250     convert_bit_order(buf,result);
00251   }
00252 
00253 
00254 
00255   
00256   void go_to_next(const char c,
00257                   std::istream& in){
00258     char ch;
00259     
00260     do{ 
00261       in.get(ch);
00262       if (!in.good()) {
00263         Message::error<<"go_to_next: error in input stream\nwas looking for '"
00264                       <<c<<"'"
00265                       <<Message::done;
00266       }
00267     }
00268     while (ch!=c);
00269   }
00270 
00274   void go_to_next_token(const std::string token,
00275                         std::istream& in){
00276 
00277     const unsigned int token_size = token.size();
00278     
00279     if (token_size<1) {
00280       Message::error<<"go_to_next_token: token size too small ("
00281                     <<token_size<<")"<<Message::done;
00282     }
00283 
00284     char ch;
00285     unsigned int size_found = 0;
00286     
00287     while (size_found<token_size) {   
00288       in.get(ch);
00289     
00290       if (!in.good()) {
00291         Message::error<<"go_to_next_token: error in input stream"
00292                       <<Message::done;
00293       }
00294     
00295       if (ch==token[size_found]) {
00296         size_found++;
00297       }
00298       else {
00299         size_found=0;
00300       }
00301     }
00302   }
00303 
00304   
00305   char go_to_next_of_list(const std::string token_list,
00306                           std::istream& in){
00307     char ch;
00308     bool found;
00309 
00310     const unsigned int list_size = token_list.size();
00311     
00312     do{ 
00313       in.get(ch);
00314       if (!in.good()) {
00315         Message::error<<"go_to_next: error in input stream"
00316                       <<Message::done;
00317       }
00318 
00319       found = false;
00320       for(unsigned int i=0;i<list_size;i++){
00321         found = found||(ch==token_list[i]);
00322       }
00323     }
00324     while (!found);
00325      
00326     return ch;
00327   }
00328 
00329 
00330 
00331 }
00332 
00333 #endif

Generated on Fri Aug 20 15:03:52 2004 by doxygen1.2.18