/* * * Copyright (c) 1996 * Knowledge Science Institute, University of Calgary * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. The Knowledge Science Institute makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */ #include #include #include char Lexer::Delim=','; void Lexer::setDelim(char delim) {Delim = delim;} char Lexer::getDelim() {return Delim;} int Lexer::writeDelim(ostream& s, char delim) { s << delim; return !s.good(); } int Lexer::readDelim(istream& s, char delim) { int ret = -1; char c; while (isspace(c=s.get()) && !s.eof()); //scan past any white space if (c == delim) ret = 0; else s.putback(c); return ret; } static char hexDigit(char x) { if (x<10) return x+'0'; if (x<16) return (x-10)+'a'; return '?'; } int Lexer::writeQuotedString(ostream& s, string& x) { int ret = 0; const int bufsize=128; const int buflim=bufsize-4; char buf[bufsize] = {'\"'}; int n = 1; char c; long i=0; while (i buflim) { s.write(buf,n); n=0; } if (c == '\\' || c == '"') { buf[n++] = '\\'; buf[n++] = c; } else if (isprint(c)) buf[n++]=c; else { buf[n++]='\\'; if (c=='\n') buf[n++]='n'; else { buf[n++]=hexDigit((c&0xf0)>>4); buf[n++]=hexDigit(c&0x0f); } } } buf[n++]='\"'; s.write(buf,n); return ret; } inline char hexVal(char c) { return (c&0xF) + (c>'9'? 9 : 0); } int Lexer::readQuotedString(istream& s, string& x) { int ret = 0; if (readDelim(s,'\"')) ret = -1; else { x=""; int c; for (c=s.get(); c!=EOF && c!='"'; c=s.get()) { if (isprint(c)) { if (c == '\\') { if (isxdigit(c=s.get())) c = ((hexVal(c) << 4) | hexVal(s.get())); else if (c == 'n') c = '\n'; } x.append(string((char)c)); } } if (c!='\"') ret = 1; } return ret; } int Lexer::writeString(ostream& s, string x) { int ret = 0; s << x; return ret; } int Lexer::readString(istream& s, string& x) { int ret = 0; x.read_to_delim(s,Delim); return ret; } int Lexer::writeLong(ostream& s, long x) { int ret = 0; s << setbase(10) << x; return ret; } int Lexer::readLong(istream& s, long& x) { int ret = 0; s >> setbase(10) >> x; if (!s.good()) { s.clear(); if (!readDelim(s)) {ret=1; s.putback(Delim);} else ret=-1; } return ret; } int Lexer::writeUnsigned(ostream& s, unsigned long x) { int ret = 0; s << setbase(16) << x; return ret; } int Lexer::readUnsigned(istream& s, unsigned long& x) { int ret = 0; s >> setbase(16) >> x; if (!s.good()) { s.clear(); if (!readDelim(s)) {ret=1; s.putback(Delim);} else ret=-1; } return ret; } int Lexer::writeRectangle(ostream& s, RECTANGLE& x) { int ret = 0; s << setbase(10) << x.left << ' ' << x.top << ' ' << x.right << ' ' << x.bottom; return ret; } int Lexer::readRectangle(istream& s, RECTANGLE& x) { int ret = 0; s >> setbase(10) >> x.left >> x.top >> x.right >> x.bottom; if (!s.good()) { s.clear(); ret=-1; } return ret; } int Lexer::writePoint(ostream& s, POINT& x) { int ret = 0; s << setbase(10) << x.x << ' ' << x.y; return ret; } int Lexer::readPoint(istream& s, POINT& x) { int ret = 0; s >> setbase(10) >> x.x >> x.y; if (!s.good()) { s.clear(); if (!readDelim(s)) {ret=1; s.putback(Delim);} else ret=-1; } return ret; } int Lexer::scanPastDelim(istream& s, char delim) { char c; for (c=s.get(); c!=EOF && c!=delim; c=s.get()) { if (c=='"') { s.putback(c); string str; readQuotedString(s,str); } } if (c==delim) return 0; else return -1; }