contrib/mul/mbl/mbl_parse_tuple.h
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_parse_tuple.h
00002 #ifndef mbl_parse_tuple_h_
00003 #define mbl_parse_tuple_h_
00004 //:
00005 // \file
00006 // \author Ian Scott
00007 // \date  6-Feb-2008
00008 // \brief Convenience function a tuple of PODs from a config file.
00009 
00010 #include <vcl_istream.h>
00011 #include <vcl_sstream.h>
00012 #include <mbl/mbl_exception.h>
00013 
00014 //: Read a 2-tuple of PODs from a config file.
00015 // This function will read through a stream, and store the text found to a string.
00016 // The function reads 2 elements. If there was an opening brace it will also consume the closing brace.
00017 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00018 //
00019 // \throws mbl_exception_parse_error if unrecoverable parse error.
00020 //
00021 // Example:
00022 // \code
00023 //   vcl_istringstream ss("{ 1.0 4 }");
00024 //   float a;
00025 //   int b;
00026 //   mbl_parse_tuple(ss, a, b)
00027 // \endcode
00028 //
00029 template <class T, class U>
00030 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b)
00031 {
00032   if (!afs) return;
00033   char brace1, brace2;
00034   afs >> vcl_ws >> brace1;
00035   if (afs.eof())
00036     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00037   if ( brace1 == '{')
00038   {
00039     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> brace2;
00040     if (!afs)
00041       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00042     if (brace2 != '}')
00043     {
00044       afs.putback(brace2);
00045       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00046 
00047       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00048     }
00049   }
00050   else
00051   {
00052     afs.putback(brace1);
00053     afs >> vcl_ws >> a >> vcl_ws >> b;
00054     if (!afs)
00055       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00056   }
00057 }
00058 
00059 //: Read a 3-tuple of PODs from a config file.
00060 // This function will read through a stream, and store the text found to a string.
00061 // The function reads 3 elements. If there was an opening brace it will also consume the closing brace.
00062 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00063 //
00064 // \throws mbl_exception_parse_error if unrecoverable parse error.
00065 //
00066 // \verbatim
00067 // Example:
00068 // vcl_istringstream ss("{ 1.0 -5 4 }");
00069 // float a;
00070 // int b;
00071 // unsigned c;
00072 // mbl_parse_tuple(ss, a, b, c)
00073 // \endverbatim
00074 template <class T, class U, class V>
00075 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c)
00076 {
00077   if (!afs) return;
00078   char brace1, brace2;
00079   afs >> vcl_ws >> brace1;
00080   if (afs.eof())
00081     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00082   if ( brace1 == '{')
00083   {
00084     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> brace2;
00085     if (!afs)
00086       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00087     if (brace2 != '}')
00088     {
00089       afs.putback(brace2);
00090       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00091 
00092       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00093     }
00094   }
00095   else
00096   {
00097     afs.putback(brace1);
00098     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c;
00099     if (!afs)
00100       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00101   }
00102 }
00103 
00104 
00105 //: Read a 4-tuple of PODs from a config file.
00106 // This function will read through a stream, and store the text found to a string.
00107 // The function reads 4 elements. If there was an opening brace it will also consume the closing brace.
00108 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00109 //
00110 // \throws mbl_exception_parse_error if unrecoverable parse error.
00111 //
00112 // \verbatim
00113 // Example:
00114 // vcl_istringstream ss("{ 1.0 -5 4 a }");
00115 // float a;
00116 // int b;
00117 // unsigned c;
00118 // char d;
00119 // mbl_parse_tuple(ss, a, b, c, d)
00120 // \endverbatim
00121 template <class T, class U, class V, class W>
00122 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d)
00123 {
00124   if (!afs) return;
00125   char brace1, brace2;
00126   afs >> vcl_ws >> brace1;
00127   if (afs.eof())
00128     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00129   if ( brace1 == '{')
00130   {
00131     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> brace2;
00132     if (!afs)
00133       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00134     if (brace2 != '}')
00135     {
00136       afs.putback(brace2);
00137       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00138 
00139       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00140     }
00141   }
00142   else
00143   {
00144     afs.putback(brace1);
00145     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d;
00146     if (!afs)
00147       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00148   }
00149 }
00150 
00151 
00152 //: Read a 5-tuple of PODs from a config file.
00153 // This function will read through a stream, and store the text found to a string.
00154 // The function reads 5 elements. If there was an opening brace it will also consume the closing brace.
00155 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00156 //
00157 // \throws mbl_exception_parse_error if unrecoverable parse error.
00158 //
00159 // \verbatim
00160 // Example:
00161 // vcl_istringstream ss("{ 1.0 -5 4 k l }");
00162 // float a;
00163 // int b;
00164 // unsigned c;
00165 // char d, e;
00166 // mbl_parse_tuple(ss, a, b, c, d, e)
00167 // \endverbatim
00168 template <class T, class U, class V, class W, class X>
00169 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d, X& e)
00170 {
00171   if (!afs) return;
00172   char brace1, brace2;
00173   afs >> vcl_ws >> brace1;
00174   if (afs.eof())
00175     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00176   if ( brace1 == '{')
00177   {
00178     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> e >> vcl_ws >> brace2;
00179     if (!afs)
00180       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00181     if (brace2 != '}')
00182     {
00183       afs.putback(brace2);
00184       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00185 
00186       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00187     }
00188   }
00189   else
00190   {
00191     afs.putback(brace1);
00192     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> e;
00193     if (!afs)
00194       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00195   }
00196 }
00197 
00198 
00199 //: Read a 6-tuple of PODs from a config file.
00200 // This function will read through a stream, and store the text found to a string.
00201 // The function reads 6 elements. If there was an opening brace it will also consume the closing brace.
00202 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00203 //
00204 // \throws mbl_exception_parse_error if unrecoverable parse error.
00205 //
00206 // \verbatim
00207 // Example:
00208 // vcl_istringstream ss("{ 1.0 -3.4 -5 4 k l }");
00209 // float a;
00210 // double b;
00211 // int c;
00212 // unsigned d;
00213 // char e, f;
00214 // mbl_parse_tuple(ss, a, b, c, d, e, f)
00215 // \endverbatim
00216 template <class T, class U, class V, class W, class X, class Y>
00217 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d, X& e, Y& f)
00218 {
00219   if (!afs) return;
00220   char brace1, brace2;
00221   afs >> vcl_ws >> brace1;
00222   if (afs.eof())
00223     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00224   if ( brace1 == '{')
00225   {
00226     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >>
00227       e >> vcl_ws >> f >> vcl_ws >> brace2;
00228     if (!afs)
00229       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00230     if (brace2 != '}')
00231     {
00232       afs.putback(brace2);
00233       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00234 
00235       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00236     }
00237   }
00238   else
00239   {
00240     afs.putback(brace1);
00241     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >>
00242       vcl_ws >> e >> vcl_ws >> f;
00243     if (!afs)
00244       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00245   }
00246 }
00247 #endif // mbl_parse_tuple_h_