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