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 <mbl/mbl_exception.h>
00012 
00013 //: Read a 2-tuple of PODs from a config file.
00014 // This function will read through a stream, and store the text found to a string.
00015 // The function reads 2 elements. If there was an openning brace it will also consume the closing brace.
00016 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00017 //
00018 // \throws mbl_exception_parse_error if unrecoverable parse error.
00019 //
00020 // Example:
00021 // \code
00022 //   vcl_istringstream ss("{ 1.0 4 }");
00023 //   float a;
00024 //   int b;
00025 //   mbl_parse_tuple(ss, a, b)
00026 // \endcode
00027 //
00028 template <class T, class U>
00029 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b)
00030 {
00031   if (!afs) return;
00032   char brace1, brace2;
00033   afs >> vcl_ws >> brace1;
00034   if (afs.eof())
00035     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00036   if ( brace1 == '{')
00037   {
00038     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> brace2;
00039     if (!afs)
00040       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00041     if (brace2 != '}')
00042     {
00043       afs.putback(brace2);
00044       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00045 
00046       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00047     }
00048   }
00049   else
00050   {
00051     afs.putback(brace1);
00052     afs >> vcl_ws >> a >> vcl_ws >> b;
00053     if (!afs)
00054       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00055   }
00056 }
00057 
00058 //: Read a 3-tuple of PODs from a config file.
00059 // This function will read through a stream, and store the text found to a string.
00060 // The function reads 3 elements. If there was an openning brace it will also consume the closing brace.
00061 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00062 //
00063 // \throws mbl_exception_parse_error if unrecoverable parse error.
00064 //
00065 // \verbatim
00066 // Example:
00067 // vcl_istringstream ss("{ 1.0 -5 4 }");
00068 // float a;
00069 // int b;
00070 // unsigned c;
00071 // mbl_parse_tuple(ss, a, b, c)
00072 // \endverbatim
00073 template <class T, class U, class V>
00074 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c)
00075 {
00076   if (!afs) return;
00077   char brace1, brace2;
00078   afs >> vcl_ws >> brace1;
00079   if (afs.eof())
00080     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00081   if ( brace1 == '{')
00082   {
00083     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> brace2;
00084     if (!afs)
00085       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00086     if (brace2 != '}')
00087     {
00088       afs.putback(brace2);
00089       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00090 
00091       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00092     }
00093   }
00094   else
00095   {
00096     afs.putback(brace1);
00097     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c;
00098     if (!afs)
00099       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00100   }
00101 }
00102 
00103 
00104 //: Read a 4-tuple of PODs from a config file.
00105 // This function will read through a stream, and store the text found to a string.
00106 // The function reads 4 elements. If there was an openning brace it will also consume the closing brace.
00107 // Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
00108 //
00109 // \throws mbl_exception_parse_error if unrecoverable parse error.
00110 //
00111 // \verbatim
00112 // Example:
00113 // vcl_istringstream ss("{ 1.0 -5 4 a }");
00114 // float a;
00115 // int b;
00116 // unsigned c;
00117 // char d;
00118 // mbl_parse_tuple(ss, a, b, c)
00119 // \endverbatim
00120 template <class T, class U, class V, class W>
00121 inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d)
00122 {
00123   if (!afs) return;
00124   char brace1, brace2;
00125   afs >> vcl_ws >> brace1;
00126   if (afs.eof())
00127     throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
00128   if ( brace1 == '{')
00129   {
00130     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> brace2;
00131     if (!afs)
00132       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00133     if (brace2 != '}')
00134     {
00135       afs.putback(brace2);
00136       afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream
00137 
00138       throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
00139     }
00140   }
00141   else
00142   {
00143     afs.putback(brace1);
00144     afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d;
00145     if (!afs)
00146       throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
00147   }
00148 }
00149 
00150 #endif // mbl_parse_tuple_h_