contrib/mul/mbl/mbl_parse_keyword_list.cxx
Go to the documentation of this file.
00001 #include "mbl_parse_keyword_list.h"
00002 //:
00003 // \file
00004 // \brief Parse list of strings
00005 // \author Tim Cootes
00006 
00007 #include <mbl/mbl_exception.h>
00008 #include <mbl/mbl_parse_block.h>
00009 #include <vcl_sstream.h>
00010 
00011 
00012 //: Read in data from a stream, assumed to be a list of items
00013 // Assumes list of objects separated by a keyword.
00014 //  keyword is always the same word, defined in the input variable.
00015 // Expects format of data:
00016 // \verbatim
00017 // {
00018 //   keyword: string1
00019 //   keyword: { stuff in braces }
00020 //   keyword: string3
00021 // }
00022 // \endverbatim
00023 void mbl_parse_keyword_list(vcl_istream& is, const vcl_string& keyword,
00024                             vcl_vector<vcl_string>& items,
00025                             bool discard_comments /* = false */)
00026 {
00027   vcl_string s = mbl_parse_block(is);
00028   vcl_istringstream ss(s);
00029   char c;
00030   ss>>c;  // Remove opening brace
00031   if (c!='{')
00032   {
00033     throw mbl_exception_parse_error("Expected '{' in mbl_parse_keyword_list");
00034   }
00035 
00036   items.resize(0);
00037   vcl_string label;
00038   while (!ss.eof())
00039   {
00040     ss >> label;         // Next follows the parameters
00041 
00042     if (label == "}") 
00043       continue;
00044 
00045     else if ( discard_comments && (label.substr(0,2) == "//") )
00046     {
00047       // Comment line, so read to end
00048       vcl_string comment_string;
00049       vcl_getline(ss, comment_string);
00050       continue;
00051     }
00052 
00053     else if (label!=keyword)
00054     {
00055       vcl_string error_msg = "Expected keyword: '";
00056       error_msg+=keyword;
00057       error_msg+="' Got '";
00058       error_msg+=label;
00059       error_msg+="'";
00060       throw mbl_exception_parse_error(error_msg);
00061     }
00062 
00063     items.push_back(mbl_parse_block(ss));
00064   }
00065 }
00066 
00067 //: Read in data from a stream, assumed to be a list of items
00068 // Assumes list of objects separated by a keyword.
00069 //  keyword is always the same word, defined in the input variable.
00070 // Expects format of data:
00071 // \verbatim
00072 // {
00073 //   keyword: string1 { stuff in braces }
00074 // }
00075 // \endverbatim
00076 void mbl_parse_keyword_list2(vcl_istream& is, const vcl_string& keyword,
00077                              vcl_vector<vcl_string>& items,
00078                              bool discard_comments /* = false */)
00079 {
00080   vcl_string s = mbl_parse_block(is);
00081   vcl_istringstream ss(s);
00082   char c;
00083   ss>>c;  // Remove opening brace
00084   if (c!='{')
00085   {
00086     throw mbl_exception_parse_error("Expected '{' in mbl_parse_keyword_list");
00087   }
00088 
00089   items.resize(0);
00090   vcl_string label,string1,string2;
00091   while (!ss.eof())
00092   {
00093     ss >> label;         // Next follows the parameters
00094 
00095     if (label == "}") 
00096       continue;
00097 
00098     else if ( discard_comments && (label.substr(0,2) == "//") )
00099     {
00100       // Comment line, so read to end
00101       vcl_string comment_string;
00102       vcl_getline(ss, comment_string);
00103       continue;
00104     }
00105 
00106     if (label!=keyword)
00107     {
00108       vcl_string error_msg = "Expected keyword: '";
00109       error_msg+=keyword;
00110       error_msg+="' Got '";
00111       error_msg+=label;
00112       error_msg+="'";
00113       throw mbl_exception_parse_error(error_msg);
00114     }
00115 
00116     ss>>string1;
00117     string2=string1 + " " + mbl_parse_block(ss);
00118 
00119     items.push_back(string2);
00120   }
00121 }
00122 
00123