00001
00002 #ifndef SPEC_DMPLIB_H_
00003 #define SPEC_DMPLIB_H_
00004
00005 #include <string>
00006 #include <iostream>
00007
00008
00009
00010
00011 class AsciiDumpException {};
00012
00013 class AsciiNoStartChar : public AsciiDumpException {};
00014 class AsciiNoEndChar : public AsciiDumpException {};
00015
00016 class AsciiWrongTag : public AsciiDumpException {
00017 public:
00018 AsciiWrongTag(const std::string& expected, const std::string& got)
00019 : m_expected(expected), m_got(got) {};
00020 public:
00021 std::string expected() const { return m_expected; }
00022 std::string got() const { return m_got; };
00023 private:
00024 std::string m_expected;
00025 std::string m_got;
00026 };
00027
00028 class AsciiWrongStartTag : public AsciiWrongTag {
00029 public:
00030 AsciiWrongStartTag(const std::string& expected, const std::string& got)
00031 : AsciiWrongTag(expected, got)
00032 {}
00033 };
00034
00035 class AsciiWrongEndTag : public AsciiWrongTag {
00036 public:
00037 AsciiWrongEndTag(const std::string& expected, const std::string& got)
00038 : AsciiWrongTag(expected, got)
00039 {}
00040 };
00041
00042
00043
00044
00045 class Tagged {
00046 public:
00047 Tagged()
00048 : m_initialized(false)
00049 {};
00050 public:
00051 bool initialized() const;
00052 void set_initialized();
00053 void unset_initalized();
00054 protected:
00055 void check_start_tag(std::istream& is, const char *tag);
00056 void check_end_tag(std::istream& is, const char *tag);
00057 private:
00058 bool m_initialized;
00059 static std::string s_saved_tag;
00060 };
00061
00062 inline
00063 bool Tagged::initialized() const
00064 {
00065 return m_initialized;
00066 }
00067
00068 inline
00069 void Tagged::set_initialized()
00070 {
00071 m_initialized = true;
00072 }
00073
00074 inline
00075 void Tagged::unset_initalized()
00076 {
00077 m_initialized = false;
00078 }
00079
00080 inline
00081 void Tagged::check_start_tag(std::istream& is, const char *tag)
00082 {
00083
00084 char c;
00085 if(!(is >> c) || (c != '{')) {
00086 throw AsciiNoStartChar();
00087 }
00088
00089
00090 std::string in_tag;
00091 is >> in_tag;
00092 if(in_tag != tag)
00093 throw AsciiWrongStartTag(tag, in_tag);
00094
00095
00096 is >> c;
00097 if(c == '}') {
00098 is >> in_tag;
00099 if(in_tag != tag)
00100 throw AsciiWrongEndTag(tag, in_tag);
00101 } else {
00102 is.putback(c);
00103 set_initialized();
00104 }
00105 }
00106
00107 inline
00108 void Tagged::check_end_tag(std::istream& is, const char *tag)
00109 {
00110 char c;
00111 is >> c;
00112 if(c != '}')
00113 throw AsciiNoEndChar();
00114
00115 std::string in_tag;
00116 is >> in_tag;
00117 if(in_tag != tag)
00118 throw AsciiWrongEndTag(tag, in_tag);
00119 }
00120
00121
00122 #endif