Go to the documentation of this file.00001 #include "mbl_parse_colon_pairs_list.h"
00002
00003
00004
00005
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
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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);
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
00058 item1.resize(0);
00059 item2.resize(0);
00060
00061 vcl_string string1,string2;
00062 while (!is.eof())
00063 {
00064
00065 is >> string1;
00066 if (string1=="}") continue;
00067 item1.push_back(string1);
00068
00069
00070 is >> vcl_ws>>c;
00071 if (c!=':')
00072 {
00073 is.clear(vcl_ios::failbit);
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
00083 is >> string2;
00084 if (string2=="}")
00085 {
00086 is.clear(vcl_ios::failbit);
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);
00100 throw (mbl_exception_parse_error(error_msg));
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
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