00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "magic/mobject.h"
00026 #include "magic/mstream.h"
00027 #include "magic/mpararr.h"
00028
00029 #ifdef SunOS
00030 #include <sys/varargs.h>
00031 #else
00032 #include <stdarg.h>
00033 #endif
00034
00035
00036
00037 OStream sout (stdout);
00038 OStream serr (stderr);
00039
00041
00042
00043
00044
00045
00046
00047
00049
00050 void Stream::close () {
00051 if (stream)
00052 fclose (stream);
00053 }
00054
00056
00057
00058
00059
00060
00061
00062
00064
00065 OStream::OStream (FILE* strm=stdout) : Stream (strm) {
00066 aflush=0;
00067 mUserFlags = 0;
00068 }
00069
00070 OStream::OStream (OStream& o) : Stream (o.stream) {
00071 aflush = o.aflush;
00072 mUserFlags = 0;
00073 }
00074
00075
00076 OStream& OStream::print (const char c) {
00077 if (!stream)
00078 return *this;
00079
00080 ::fputc (c, stream);
00081
00082 if (aflush)
00083 fflush (stream);
00084
00085 return *this;
00086 }
00087
00088 OStream& OStream::print (const char* str) {
00089 if (!stream)
00090 return *this;
00091
00092 ::fputs (str, stream);
00093
00094 if (aflush)
00095 fflush (stream);
00096 return *this;
00097 }
00098
00099 OStream& OStream::printf (const char* sformat, ...) {
00100 if (!stream)
00101 return *this;
00102
00103 va_list args;
00104 va_start (args, sformat);
00105 vfprintf (stream, sformat, args);
00106 va_end (args);
00107
00108 if (aflush)
00109 fflush (stream);
00110 return *this;
00111 }
00112
00113 OStream& OStream::operator<< (const char* str) {
00114 return print (str);
00115 }
00116
00117 OStream& OStream::operator<< (int i) {
00118 return operator<< ((long) i);
00119 }
00120
00121 OStream& OStream::operator<< (long i) {
00122 printf ("%d", i);
00123 return *this;
00124 }
00125
00126
00127 void OStream::operator= (const OStream& other) {
00128 stream = other.stream;
00129 aflush = other.aflush;
00130 }
00131
00132 void OStream::setFlag (int flag, bool value) {
00133 ASSERT (flag>=0 && flag<32);
00134 mUserFlags &= ~(long(1)<<flag);
00135 mUserFlags |= (long(value)<<flag);
00136 }
00137
00138 bool OStream::flag (int flag) const{
00139 ASSERT (flag>=0 && flag<32);
00140 return mUserFlags & (long(1)<<flag);
00141 }
00142
00143
00144
00146
00147
00148
00149
00150
00151
00152
00154
00155
00156
00157
00158 bool IStream::openload (const char* filename) {
00159 stream = fopen (filename, "rb");
00160 return stream;
00161 }
00162
00163 IStream& IStream::operator>> (int& i) {
00164 long i2=i;
00165 operator>> (i2);
00166 i=i2;
00167 return *this;
00168 }
00169
00170 IStream& IStream::operator>> (long& i) {
00171 if (stream)
00172 ASSERT (fread ((char*) &i, sizeof(i), 1, stream) == 1);
00173 return *this;
00174 }
00175
00176 IStream& IStream::operator>> (char& i) {
00177 if (stream)
00178 ASSERT (fread ((char*) &i, sizeof(i), 1, stream) == 1);
00179 return *this;
00180 }
00181
00182 IStream& IStream::operator>> (float& i) {
00183 double i2=i;
00184 operator>> (i2);
00185 i=i2;
00186 return *this;
00187 }
00188
00189 IStream& IStream::operator>> (double& i) {
00190 if (stream)
00191 ASSERT (fread ((char*) &i, sizeof(i), 1, stream) == 1);
00192 return *this;
00193 }
00194
00195 IStream& IStream::read (char* p, int n) {
00196 if (stream) {
00197 int bytesread = fread (p, 1, n, stream);
00198 ASSERTWITH (bytesread == n,
00199 format ("%d bytes read instead of %d that were asked",
00200 bytesread, n));
00201 }
00202 return *this;
00203 }
00204
00205 IStream& operator<< (IStream& out, const char* str) {
00206
00207
00208 return out;
00209 }
00210
00211
00212
00214
00215
00216
00217
00218
00219
00220
00222
00223 DumpContext::DumpContext (FILE* strm=stdout) : OStream (strm) {
00224 names = false;
00225 classnames = false;
00226 depth=0;
00227 prevdepth=-1;
00228
00229 mode = DUMP_TEXT;
00230 attribs = NULL;
00231 }
00232
00233 DumpContext::DumpContext (OStream& o) : OStream (o) {
00234 names = false;
00235 classnames = false;
00236 depth=0;
00237 prevdepth=-1;
00238
00239 mode = DUMP_TEXT;
00240 attribs = NULL;
00241 }
00242
00243 DumpContext::~DumpContext () {
00244 delete attribs;
00245 }
00246
00247 DumpContext& DumpContext::printf (const char* sformat, ...) {
00248 if (!stream)
00249 return *this;
00250
00251 va_list args;
00252 va_start (args, sformat);
00253 vfprintf (stream, sformat, args);
00254 va_end (args);
00255
00256 if (aflush)
00257 fflush (stream);
00258
00259 return *this;
00260 }
00261
00262 DumpContext& DumpContext::printName (){
00263 if (mode&DUMP_TEXT && !nextname.isEmpty()) {
00264 print (nextname);
00265 print ("=");
00266 nextname="";
00267 }
00268 return *this;
00269 }
00270
00271 void DumpContext::printIndent () {
00272 if ((mode & DUMP_TEXT) && false)
00273 for (int j=0; j<depth; j++)
00274 print (" ");
00275 }
00276
00277 void DumpContext::printComma () {
00278 if (depth<=prevdepth)
00279 print (", ");
00280 prevdepth = depth;
00281 }
00282
00283 #define CHECKRECORD(rtype,var) \
00284 if (mode&DUMP_RECORD) {\
00285 if (nextname.isEmpty())\
00286 nextname = "?";\
00287 attribs->add (new String (strformat ("%s %s", #rtype, (CONSTR)nextname)));\
00288 return *this;\
00289 }
00290
00291 DumpContext& DumpContext::operator<< (int i) {
00292 CHECKRECORD (int, i);
00293
00294 operator<< ((long) i);
00295 return *this;
00296 }
00297
00298 DumpContext& DumpContext::operator<< (long i) {
00299 CHECKRECORD (long, int (i));
00300
00301 if (mode&DUMP_TEXT) {
00302 printComma ();
00303 printName ();
00304 printf ("%d", i);
00305 }
00306
00307 return *this;
00308 }
00309
00310 DumpContext& DumpContext::operator<< (char i) {
00311 CHECKRECORD (char, i);
00312
00313
00314 return *this;
00315 }
00316
00317 DumpContext& DumpContext::operator<< (float i) {
00318 CHECKRECORD (float, i);
00319 operator<< ((double) i);
00320 return *this;
00321 }
00322
00323 DumpContext& DumpContext::operator<< (double i) {
00324 CHECKRECORD (double, float(i));
00325 if (mode&DUMP_TEXT) {
00326 printComma ();
00327 printName ();
00328 printf ("%f", i);
00329 }
00330
00331 return *this;
00332 }
00333
00334 DumpContext& DumpContext::operator<< (CONSTR str) {
00335 if (mode&DUMP_TEXT) {
00336 printComma ();
00337 printName ();
00338 print (str);
00339 }
00340 return *this;
00341 }
00342
00343 DumpContext& DumpContext::write (const char* p, int n) {
00344 CHECKRECORD (char*, p);
00345 if (mode&DUMP_TEXT) {
00346 printIndent ();
00347 printComma ();
00348 printName ();
00349 String str, str2 (p, n);
00350 if (str2.isNull())
00351 print ("NULL");
00352 else {
00353
00354 printf ("\x22%s\x22", (CONSTR) str2);
00355 }
00356 }
00357
00358 return *this;
00359 }