contrib/mul/mbl/mbl_parse_string_list.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Parse list of strings
00004 // \author Tim Cootes
00005 
00006 #include <mbl/mbl_parse_string_list.h>
00007 
00008 #include <mbl/mbl_exception.h>
00009 #include <mbl/mbl_parse_block.h>
00010 #include <vcl_sstream.h>
00011 
00012 //: Parse list of strings
00013 // Expects format of data:
00014 // \verbatim
00015 // {
00016 //   string1 string2 string3 ...
00017 // }
00018 // \endverbatim
00019 // Throws a mbl_exception_parse_error if it fails.
00020 void mbl_parse_string_list(vcl_istream& is,
00021                           vcl_vector<vcl_string>& items,
00022                           const vcl_string& comment_str)
00023 {
00024   vcl_string s = mbl_parse_block(is);
00025   vcl_istringstream ss(s);
00026   char c;
00027   ss>>c;  // Remove opening brace
00028   if (c!='{')
00029   {
00030     throw mbl_exception_parse_error("Expected '{' in mbl_parse_string_list");
00031   }
00032 
00033   unsigned comment_len = comment_str.size();
00034   
00035   items.resize(0);
00036   vcl_string label;
00037   while (!ss.eof())
00038   {
00039     ss >> label;
00040     if (comment_len>0 && 
00041         label.length()>=comment_len && 
00042         label.substr(0,comment_len)==comment_str)
00043     {
00044       // label begins with comment_str 
00045       // - treat as comment and discard rest of line
00046       vcl_string dummy;
00047       vcl_getline(ss,dummy);
00048       continue;
00049     }
00050     if (label == "}") continue;
00051     items.push_back(label);
00052   }
00053 
00054   if (label!="}")
00055   {
00056     throw mbl_exception_parse_error("Expected closing '}' in mbl_parse_string_list");
00057   }
00058 }
00059 
00060 //: Parse list of strings
00061 // Expects format of data:
00062 // \verbatim
00063 // {
00064 //   string1 string2 string3 ...
00065 // }
00066 // \endverbatim
00067 // Throws a mbl_exception_parse_error if it fails.
00068 void mbl_parse_string_list(const vcl_string& data,
00069                           vcl_vector<vcl_string>& items,
00070                           const vcl_string& comment_str)
00071 {
00072   vcl_istringstream data_stream(data);
00073   mbl_parse_string_list(data_stream,items,comment_str);
00074 }
00075 
00076