00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "../sexpress/sexpress.hpp"
00017 #include "../sexpress/sstring.hpp"
00018 #include "sstream.hpp"
00019
00020
00022
00023
00024 IntelibTypeId
00025 SExpressionStream::TypeId(&SExpression::TypeId, false);
00026
00027 SString SExpressionStream::TextRepresentation() const
00028 {
00029 char buf[15];
00030 sprintf(buf, "#<STREAM %d>", Fileno());
00031 return SString(buf);
00032 }
00033
00034
00035
00037
00038
00039 IntelibTypeId
00040 SExpressionStreamFile::TypeId(&SExpressionStream::TypeId, false);
00041
00042 SExpressionStreamFile::SExpressionStreamFile(FILE *af)
00043 : SExpressionStream(TypeId)
00044 {
00045 f = af;
00046 }
00047 SExpressionStreamFile::SExpressionStreamFile(
00048 const IntelibTypeId& id, FILE *af)
00049 : SExpressionStream(id)
00050 {
00051 f = af;
00052 }
00053 SExpressionStreamFile::~SExpressionStreamFile()
00054 {
00055 }
00056 int SExpressionStreamFile::Getc()
00057 {
00058 return fgetc(f);
00059 }
00060 int SExpressionStreamFile::Ungetc(int c)
00061 {
00062 return ungetc(c, f);
00063 }
00064 int SExpressionStreamFile::Putc(int c)
00065 {
00066 return fputc(c, f);
00067 }
00068 int SExpressionStreamFile::Puts(const char *s)
00069 {
00070 return fputs(s, f);
00071 }
00072 char* SExpressionStreamFile::Gets(char *buf, int bufsize)
00073 {
00074 return fgets(buf, bufsize, f);
00075 }
00076 int SExpressionStreamFile::Seek(long pos)
00077 {
00078 return fseek(f, pos, SEEK_SET);
00079 }
00080 long SExpressionStreamFile::Tell() const
00081 {
00082 return ftell(f);
00083 }
00084 int SExpressionStreamFile::Flush()
00085 {
00086 return fflush(f);
00087 }
00088 int SExpressionStreamFile::Close()
00089 {
00090 int r = f ? fclose(f) : -1;
00091 f = 0;
00092 return r;
00093 }
00094 int SExpressionStreamFile::Fileno() const
00095 {
00096 return fileno(f);
00097 }
00098
00100
00101
00102 SExpressionStreamCharbuf::
00103 SExpressionStreamCharbuf(const char *initbuf, bool mpass)
00104 {
00105 if(initbuf) buf = initbuf;
00106 multipass = mpass;
00107 pos = 0;
00108 }
00109
00110 SExpressionStreamCharbuf::~SExpressionStreamCharbuf()
00111 {}
00112
00113 void SExpressionStreamCharbuf::AddChar(char c)
00114 {
00115 buf += c;
00116 }
00117
00118 void SExpressionStreamCharbuf::AddString(const char *s)
00119 {
00120 buf += s;
00121 }
00122
00123 void SExpressionStreamCharbuf::Cleanup()
00124 {
00125
00126 buf = buf.c_str() + pos;
00127 pos = 0;
00128 }
00129
00130 int SExpressionStreamCharbuf::Getc()
00131 {
00132 if(buf.length() <= pos) {
00133 return EOF;
00134 } else {
00135 int ret = (int)(unsigned char)(buf[pos]);
00136 pos++;
00137 return ret;
00138 }
00139 }
00140
00141 int SExpressionStreamCharbuf::Ungetc(int c)
00142 {
00143 if(pos) {
00144 pos--;
00145 } else {
00146 char tmp[2]; tmp[0] = c; tmp[1] = 0;
00147 buf = SString(tmp) + buf.c_str();
00148 }
00149 return c;
00150 }
00151
00152 int SExpressionStreamCharbuf::Putc(int c)
00153 {
00154 char tmp[2]; tmp[0] = c; tmp[1] = 0;
00155 buf += tmp;
00156 return c;
00157 }
00158
00159 int SExpressionStreamCharbuf::Puts(const char *s)
00160 {
00161 buf += s;
00162 return 1;
00163 }
00164
00165 char* SExpressionStreamCharbuf::Gets(char *outbuf, int outbuflen)
00166 {
00167 unsigned int l = buf.length();
00168 if(l <= pos) return 0;
00169 if((signed)(l-pos) < outbuflen-1) outbuflen = l-pos+1;
00170 int i;
00171 for(i=0; i<outbuflen-1; i++) {
00172 outbuf[i] = buf[i+pos];
00173 if(outbuf[i] == '\n') {
00174 i++;
00175 break;
00176 }
00177 }
00178 pos += i;
00179 outbuf[i] = 0;
00180 return outbuf;
00181 }
00182
00183 int SExpressionStreamCharbuf::Seek(long npos)
00184 {
00185 if(multipass) {
00186 if(buf.length() < (unsigned long) npos) {
00187 return -1;
00188 } else {
00189 pos = npos;
00190 return 1;
00191 }
00192 } else
00193 return -1;
00194 }
00195
00196 long SExpressionStreamCharbuf::Tell() const
00197 {
00198 return pos;
00199 }
00200
00201 int SExpressionStreamCharbuf::Close()
00202 {
00203 buf = "";
00204 return 0;
00205 }
00206
00207 int SExpressionStreamCharbuf::Flush()
00208 {
00209 return 0;
00210 }
00211
00212 int SExpressionStreamCharbuf::Fileno() const
00213 {
00214 return -1;
00215 }
00216
00218
00219
00220 IntelibTypeId
00221 SExpressionStreamTextInput::TypeId(&SExpressionStreamFile::TypeId, false);
00222
00223 int SExpressionStreamTextInput::Getc()
00224 {
00225 int ret = SExpressionStreamFile::Getc();
00226 if(ret == '\n') line++;
00227 return ret;
00228 }
00229
00230 int SExpressionStreamTextInput::Ungetc(int c)
00231 {
00232 if(c == '\n') line--;
00233 return SExpressionStreamFile::Ungetc(c);
00234 }
00235
00236 char* SExpressionStreamTextInput::Gets(char *buf, int bufsize)
00237 {
00238 char *ret = SExpressionStreamFile::Gets(buf, bufsize);
00239 if(!ret) return 0;
00240 for(int i=0; i<bufsize && buf[i]; i++)
00241 if(buf[i]=='\n') { line++; break; }
00242 return ret;
00243 }
00244
00246
00247
00248 IntelibX_not_a_stream::
00249 IntelibX_not_a_stream(SReference a_param)
00250 : IntelibX("Not a stream or wrong stream type", a_param) {}
00251
00252