00001
00002
00003
00004
00005
00006
00007
00008 #include "ioblock.H"
00009
00010 str_ptr
00011 get_word(istream &is, str_list &list)
00012 {
00013 if (list.num()) return list.pop();
00014 char buf[1024];
00015
00016
00017 return (is >> buf) ? str_ptr(buf) : NULL_STR;
00018 }
00019
00020
00021
00022
00023
00024
00025
00026 int
00027 IOBlock::consume(istream &is, const IOBlockList &list, str_list &leftover)
00028 {
00029 leftover.clear();
00030 if (is.bad()) return 0;
00031 if (is.eof()) return 1;
00032
00033 static Cstr_ptr endtoken ("#END");
00034 static Cstr_ptr begintoken("#BEGIN");
00035 str_ptr str1;
00036 str_ptr str2;
00037 str_ptr starttype;
00038 int consumed = 0;
00039
00040 str1 = get_word(is, leftover);
00041 str2 = get_word(is, leftover);
00042 if (is.eof())
00043 return 1;
00044 while(str1 == begintoken && !is.eof()) {
00045
00046 starttype = str2;
00047 consumed = 0;
00048 for (int i = 0; !consumed && i < list.num(); i++) {
00049 if (starttype == list[i]->name()) {
00050 if (leftover.num()) {
00051 cerr << "IOBlock::consume - "
00052 << "warning: leftover data before consume"
00053 << ", at byte " << is.tellg() << endl;
00054 }
00055
00056 int ret = list[i]->consume_block(is, leftover);
00057 if (!ret) return 0;
00058 consumed = 1;
00059 if (is.bad()) {
00060 cerr << "IOBlock::consume - "
00061 << "stream is bad after reading block "
00062 << list[i]->name()
00063 << ", at byte " << is.tellg() << endl;
00064 return 0;
00065 }
00066 }
00067 }
00068 if (!consumed) {
00069 cerr << "IOBlock::consume - skipping unknown block '"
00070 << starttype << "'"
00071 << ", at byte " << is.tellg() << endl;
00072 }
00073
00074 if (is.eof()) return 1;
00075
00076 str1 = get_word(is, leftover);
00077 if (str1 == NULL_STR) {
00078 cerr << "IOBlock::consume - empty string at beginning of block, "
00079 << "after block " << starttype
00080 << ", at byte " << is.tellg() << endl;
00081 return 0;
00082 }
00083 str2 = get_word(is, leftover);
00084 do {
00085
00086 while (!is.eof() && !is.fail() && str1 != endtoken) {
00087 str1 = get_word(is, leftover);
00088 str2 = NULL_STR;
00089 }
00090
00091
00092 if (str2 == NULL_STR) {
00093 str2 = get_word(is, leftover);
00094 if (str2 != starttype) {
00095 cerr << "IOBlock::consume - found " << str1 << " " << str2
00096 << " before " << endtoken << " " << starttype
00097 << ", at byte " << is.tellg() << endl;
00098 }
00099 }
00100 } while (!is.eof() && str2 != starttype);
00101
00102 if (str1 == endtoken) {
00103 str1 = get_word(is, leftover);
00104 str2 = get_word(is, leftover);
00105 }
00106 if (is.eof()) break;
00107 }
00108 if (!is.eof()) {
00109
00110 leftover += str_ptr(str2);
00111 leftover += str_ptr(str1);
00112 }
00113 return 1;
00114 }