Go to the documentation of this file.00001
00002 #include "mbl_parse_block.h"
00003
00004
00005
00006
00007
00008
00009 #include <vcl_cctype.h>
00010 #include <vcl_cstring.h>
00011 #include <vcl_iostream.h>
00012 #include <mbl/mbl_exception.h>
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 vcl_string mbl_parse_block(vcl_istream &afs, bool open_already , const char * comment )
00029 {
00030 if (!afs) return "{}";
00031
00032 char c;
00033
00034
00035 const unsigned comment_length = vcl_strlen(comment);
00036
00037 if (!open_already)
00038 {
00039 if (afs.eof())
00040 return "{}";
00041 afs >> c;
00042
00043 if (!afs) return "{}";
00044 while (comment && comment_length && c == *comment)
00045 {
00046 for (unsigned comment_pos=1; comment_pos < comment_length; ++comment_pos)
00047 {
00048 if (afs.eof())
00049 return "{}";
00050 afs >> c;
00051 if (c != comment[comment_pos])
00052 {
00053 afs.putback(c);
00054 return "{}";
00055 }
00056 }
00057 vcl_string dummy;
00058 vcl_getline(afs, dummy);
00059 if (afs.eof())
00060 return "{}";
00061 afs >> vcl_ws >> c;
00062 }
00063 if (c != '{')
00064 {
00065 afs.putback(c);
00066 return "{}";
00067 }
00068 }
00069
00070 vcl_string s="{";
00071
00072
00073 vcl_string s2="";
00074
00075 unsigned level=1;
00076
00077 unsigned comment_position=0;
00078
00079 bool newline=true;
00080
00081 while (!(!afs))
00082 {
00083
00084 while (!(!afs))
00085 {
00086 afs.get(c);
00087 s2 += c;
00088 if (c=='\n')
00089 {
00090 s+=s2;
00091 s2="";
00092 newline = true;
00093 comment_position = 0;
00094 }
00095 else if (vcl_isspace(c))
00096 comment_position = 0;
00097 else if (newline && comment_position < comment_length
00098 && c==comment[comment_position])
00099 {
00100 if (++comment_position == 2)
00101 {
00102 vcl_string dummy;
00103 vcl_getline(afs, dummy);
00104 s2 = "";
00105 newline = true;
00106 comment_position = 0;
00107 }
00108 }
00109 else
00110 {
00111 newline = false;
00112 comment_position = 0;
00113 if (c=='{') ++level;
00114 else if (c=='}')
00115 if (--level==0)
00116 {
00117 s+=s2;
00118 for (unsigned i = 1; i+1 < s.size(); ++i)
00119 if (!vcl_isspace(s[i])) return s;
00120
00121 return "{}";
00122 }
00123 }
00124 }
00125 }
00126 mbl_exception_warning(mbl_exception_parse_block_parse_error(
00127 "Read problem (possibly end-of-file) before closing '}'\n",s));
00128 afs.clear(vcl_ios::badbit);
00129 return "{}";
00130 }