contrib/mul/mbl/mbl_parse_colon_pairs_list.cxx
Go to the documentation of this file.
00001 #include "mbl_parse_colon_pairs_list.h"
00002 //:
00003 // \file
00004 // \brief Parse list of string pairs separated by colons
00005 // \author Tim Cootes
00006 
00007 #include <mbl/mbl_exception.h>
00008 #include <vsl/vsl_indent.h>
00009 #include <vcl_sstream.h>
00010 #include <vcl_cassert.h>
00011 
00012 //: Parse list of string pairs separated by colons
00013 // Expects format of data string to contain pairs of
00014 // strings, with colons between items, eg
00015 // \verbatim
00016 // {
00017 //   item1_0 : item2_0
00018 //   item1_1 : item2_1
00019 //   item1_2 : item2_2
00020 // }
00021 // \endverbatim
00022 // Throws a mbl_exception_parse_error if it fails.
00023 void mbl_parse_colon_pairs_list(const vcl_string& data,
00024                                 vcl_vector<vcl_string>& item1,
00025                                 vcl_vector<vcl_string>& item2)
00026 {
00027   vcl_istringstream data_stream(data);
00028   mbl_parse_colon_pairs_list(data_stream,item1,item2);
00029 }
00030 
00031 //: Parse list of string pairs separated by colons
00032 // Expects format of data string to contain pairs of
00033 // strings, with colons between items, eg
00034 // \verbatim
00035 // {
00036 //   item1_0 : item2_0
00037 //   item1_1 : item2_1
00038 //   item1_2 : item2_2
00039 // }
00040 // \endverbatim
00041 // Throws a mbl_exception_parse_error if it fails.
00042 void mbl_parse_colon_pairs_list(vcl_istream& is,
00043                                 vcl_vector<vcl_string>& item1,
00044                                 vcl_vector<vcl_string>& item2)
00045 {
00046   char c;
00047   is >> vcl_ws>>c;
00048   if (c!='{')
00049   {
00050     is.clear(vcl_ios::failbit);  // Set error flag
00051     vcl_string error_msg("Expecting '{' got: '");
00052     error_msg+=c;
00053     error_msg+="'";
00054     throw (mbl_exception_parse_error(error_msg));
00055   }
00056 
00057   // Empty lists
00058   item1.resize(0);
00059   item2.resize(0);
00060 
00061   vcl_string string1,string2;
00062   while (!is.eof())
00063   {
00064     // Read first item
00065     is >> string1;
00066     if (string1=="}") continue;
00067     item1.push_back(string1);
00068 
00069     // Check there is a colon
00070     is >> vcl_ws>>c;
00071     if (c!=':')
00072     {
00073       is.clear(vcl_ios::failbit);  // Set error flag
00074       vcl_string error_msg("Expecting ':' after ");
00075       error_msg+=string1;
00076       error_msg+=" Got `";
00077       error_msg+=c;
00078       error_msg+="'";
00079       throw (mbl_exception_parse_error(error_msg));
00080     }
00081 
00082     // Read second item
00083     is >> string2;
00084     if (string2=="}")
00085     {
00086       is.clear(vcl_ios::failbit);  // Set error flag
00087       vcl_string error_msg("Expecting a string after ");
00088       error_msg+=string1;
00089       error_msg+=" : Got `}`";
00090       throw (mbl_exception_parse_error(error_msg));
00091     }
00092     item2.push_back(string2);
00093   }
00094 
00095   if (string1!="}")
00096   {
00097     vcl_string error_msg("Expected list to end with a }, not ");
00098     error_msg+=string2;
00099     is.clear(vcl_ios::failbit);  // Set error flag
00100     throw (mbl_exception_parse_error(error_msg));
00101   }
00102 }
00103 
00104 //: Writes pairs to a stream, separated by colons
00105 // Format of output
00106 // \verbatim
00107 // {
00108 //   item1[0] : item2[0]
00109 //   item1[1] : item2[1]
00110 // }
00111 // \endverbatim
00112 void mbl_write_colon_pairs_list(vcl_ostream& os,
00113                                 const vcl_vector<vcl_string>& item1,
00114                                 const vcl_vector<vcl_string>& item2)
00115 {
00116   assert(item1.size()==item2.size());
00117   os<<vsl_indent()<<'{'<<vcl_endl;
00118   vsl_indent_inc(os);
00119   for (unsigned i=0;i<item1.size();++i)
00120   {
00121     os<<vsl_indent()<<item1[i]<<" : "<<item2[i]<<vcl_endl;
00122   }
00123   vsl_indent_dec(os);
00124   os<<'}'<<vcl_endl;
00125 }
00126