/* This class defines input stream operators using an object of class StringClass for data input or output. This class defines the stream operators to read out numeric or string values out of a StringClass object used as an input string. */ #ifndef _istrstream_header_ #define _istrstream_header_ #include "StringClass.h" class istrstream { public: istrstream (StringClass *input) { data = input; position = input->begin(); } ~istrstream() {} private: StringClass *data; StringClass::iterator position; public: #ifdef USE_MAC_BOOL Boolean good() { return position < data->end(); } #else bool good() { return position < data->end(); } #endif istrstream& operator>>(char *c) { *c = *position; ++position; return *this; } istrstream& operator>>(unsigned char *c) { *c = *position; ++position; return *this; } istrstream& operator>>(char& c) { c = *position; ++position; return *this; } istrstream& operator>>(unsigned char& c) { c = *position; ++position; return *this; } istrstream& operator>>(StringClass *stval); istrstream& operator>>(short *sval); istrstream& operator>>(unsigned short *sval); istrstream& operator>>(int *ival); istrstream& operator>>(unsigned int *ival); istrstream& operator>>(long int *lval); istrstream& operator>>(float& fval); istrstream& operator>>(double& dval); istrstream& operator>>(long double& ldval) { ldval = 0; return *this; } istrstream& operator>>(streambuf *val) { return *this; } }; istrstream& istrstream::operator>>(StringClass *stval) { char buffer[100]; int idx=0; #ifdef DEBUG if (0 == stval->size()) cout << "Istrstream >> with non allocated param" << endl; #endif while((*position==' ' || *position=='\t') && positionend()) ++position; while (*position!=' ' && *position!='\t' && positionend()) { buffer[idx++] = *position; ++position; } buffer[idx] = 0; stval->assign(buffer); return *this; } istrstream& istrstream::operator>>(short *sval) { int sign = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='-') { sign = -1;++position; } if (*position=='+') { ++position; } *sval = *position-'0'; ++position; while (*position>='0' && *position<='9' && positionend()) { *sval = *sval*10 + *position-'0'; ++position; } *sval *= sign; return *this; } istrstream& istrstream::operator>>(unsigned short *sval) { while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='+') { ++position; } *sval = *position-'0'; ++position; while (*position>='0' && *position<'9' && positionend()) { *sval = *sval*10 + *position-'0'; ++position; } return *this; } istrstream& istrstream::operator>>(int *ival) { char buf[10]; int idx = 1; int sign = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='-') { sign = -1;++position; } else if (*position=='+') ++position; buf[0] = *position; ++position; while (*position>='0' && *position<='9' && positionend()) { buf[idx++] = *position; ++position; } *ival = 0; for(int lp=0; lp>(unsigned int *ival) { char buf[10]; int idx = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='+') { ++position; } buf[0] = *position; ++position; while (*position>='0' && *position<='9' && positionend()) { buf[idx++] = *position; ++position; } *ival = 0; for(int lp=0; lp>(long int *lval) { char buf[20]; int idx = 1; int sign = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='-') { sign = -1;++position; } else if (*position=='+') ++position; buf[0] = *position; ++position; while (*position>='0' && *position<='9' && positionend()) { buf[idx++] = *position; ++position; } *lval = 0; for(int lp=0; lp>(float& fval) { char buf[10]; int idx=0; float flpart, sign = 1; short exp, sgexp = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='-') { sign = -1;++position; } else if (*position=='+') ++position; // Read integer part fval = *position-'0'; ++position; while (*position>='0' && *position<='9' && positionend()) { fval = fval*10 + *position-'0'; ++position; } cout << "dbg ip " << fval; // Read float part idx=0; flpart=0; if (*position=='.' || *position==',') { ++position; while (*position>='0' && *position<='9' && positionend()) { buf[idx++] = *position; ++position; } for(int lp=idx-1; lp>=0; --lp){ flpart = flpart/10 + buf[lp]-'0'; } fval += flpart/10; } cout << " fp " << flpart; // Read exponent exp = 0; if(*position=='E' || *position=='e') { ++position; if(*position=='-') { sgexp = -1; ++position; } else if (*position=='+'){ sgexp = +1; ++position; } while (*position>='0' && *position<='9' && positionend()) { exp = exp*10 + *position-'0'; ++position; } exp *= sgexp; cout << " exp " << exp << endl; } fval *= sign*pow(10,exp); return *this; } istrstream& istrstream::operator>>(double& dval) { char buf[15]; int idx=0; double flpart, sign = 1; short exp, sgexp = 1; while((*position==' ' || *position=='\t') && positionend()) ++position; if (*position=='-') { sign = -1;++position; } else if (*position=='+') ++position; // Read integer part dval = *position-'0'; ++position; while (*position>='0' && *position<='9' && positionend()) { dval = dval*10 + *position-'0'; ++position; } cout << "dbg ip " << dval; // Read float part idx=0; flpart=0; if (*position=='.' || *position==',') { ++position; while (*position>='0' && *position<='9' && positionend()) { buf[idx++] = *position; ++position; } for(int lp=idx-1; lp>=0; --lp){ flpart = flpart/10 + buf[lp]-'0'; } dval += flpart/10; } cout << " fp " << flpart; // Read exponent exp = 0; if(*position=='E' || *position=='e') { ++position; if(*position=='-') { sgexp = -1; ++position; } else if (*position=='+'){ sgexp = +1; ++position; } while (*position>='0' && *position<='9' && positionend()) { exp = exp*10 + *position-'0'; ++position; } exp *= sgexp; cout << " exp " << exp << endl; } dval *= sign*pow(10,exp); return *this; } #endif