| 1 | //
 | 
|---|
| 2 | // state_text.cc
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
| 7 | // Maintainer: LPS
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // This file is part of the SC Toolkit.
 | 
|---|
| 10 | //
 | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
| 14 | // any later version.
 | 
|---|
| 15 | //
 | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 | // GNU Library General Public License for more details.
 | 
|---|
| 20 | //
 | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
| 26 | //
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifdef __GNUC__
 | 
|---|
| 29 | #pragma implementation
 | 
|---|
| 30 | #endif
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include <stdarg.h>
 | 
|---|
| 33 | #include <stdio.h>
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <util/state/state_text.h>
 | 
|---|
| 36 | 
 | 
|---|
| 37 | using namespace std;
 | 
|---|
| 38 | using namespace sc;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | static ClassDesc StateOutText_cd(
 | 
|---|
| 41 |     typeid(StateOutText),"StateOutText",1,"public StateOutFile");
 | 
|---|
| 42 | 
 | 
|---|
| 43 | StateOutText::StateOutText() :
 | 
|---|
| 44 |   StateOutFile(),
 | 
|---|
| 45 |   no_newline_(0),
 | 
|---|
| 46 |   no_array_(0)
 | 
|---|
| 47 | {
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | StateOutText::StateOutText(ostream&s) :
 | 
|---|
| 51 |   StateOutFile(s),
 | 
|---|
| 52 |   no_newline_(0),
 | 
|---|
| 53 |   no_array_(0)
 | 
|---|
| 54 | {
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | StateOutText::StateOutText(const char *path) :
 | 
|---|
| 58 |   StateOutFile(path),
 | 
|---|
| 59 |   no_newline_(0),
 | 
|---|
| 60 |   no_array_(0)
 | 
|---|
| 61 | {
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | StateOutText::~StateOutText()
 | 
|---|
| 65 | {
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | static ClassDesc StateInText_cd(typeid(StateInText),
 | 
|---|
| 69 |                                 "StateInText",1,"public StateInFile",
 | 
|---|
| 70 |                                 0, create<StateInText>);
 | 
|---|
| 71 | 
 | 
|---|
| 72 | StateInText::StateInText() :
 | 
|---|
| 73 |   StateInFile(),
 | 
|---|
| 74 |   newlines_(0),
 | 
|---|
| 75 |   no_newline_(0),
 | 
|---|
| 76 |   no_array_(0)
 | 
|---|
| 77 | {
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | StateInText::StateInText(istream& s) :
 | 
|---|
| 81 |   StateInFile(s),
 | 
|---|
| 82 |   newlines_(0),
 | 
|---|
| 83 |   no_newline_(0),
 | 
|---|
| 84 |   no_array_(0)
 | 
|---|
| 85 | {
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | StateInText::StateInText(const char *path) :
 | 
|---|
| 89 |   StateInFile(path),
 | 
|---|
| 90 |   newlines_(0),
 | 
|---|
| 91 |   no_newline_(0),
 | 
|---|
| 92 |   no_array_(0)
 | 
|---|
| 93 | {
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | StateInText::StateInText(const Ref<KeyVal> &keyval):
 | 
|---|
| 97 |   newlines_(0),
 | 
|---|
| 98 |   no_newline_(0),
 | 
|---|
| 99 |   no_array_(0)
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   char *path = keyval->pcharvalue("file");
 | 
|---|
| 102 |   if (!path) {
 | 
|---|
| 103 |       ExEnv::errn() << "StateInText(const Ref<KeyVal>&): no path given" << endl;
 | 
|---|
| 104 |     }
 | 
|---|
| 105 |   open(path);
 | 
|---|
| 106 |   delete[] path;
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | StateInText::~StateInText()
 | 
|---|
| 110 | {
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 114 | 
 | 
|---|
| 115 | // no_newline() and its associated no_newline_ variable
 | 
|---|
| 116 | // are used to omit the next newline generated by newline() in
 | 
|---|
| 117 | // the input/output stream
 | 
|---|
| 118 | void
 | 
|---|
| 119 | StateOutText::no_newline()
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   no_newline_ = 1;
 | 
|---|
| 122 | }
 | 
|---|
| 123 | void
 | 
|---|
| 124 | StateOutText::no_array()
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   no_array_ = 1;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | void
 | 
|---|
| 129 | StateInText::no_newline()
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   no_newline_ = 1;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | void
 | 
|---|
| 134 | StateInText::no_array()
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   no_array_ = 1;
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 140 | 
 | 
|---|
| 141 | int
 | 
|---|
| 142 | StateInText::read(char*s)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   istream in(buf_);
 | 
|---|
| 145 |   in >> s;
 | 
|---|
| 146 |   if (in.fail()) {
 | 
|---|
| 147 |       ExEnv::errn() << "StateInText::read(char*): failed" << endl;
 | 
|---|
| 148 |       abort();
 | 
|---|
| 149 |     }
 | 
|---|
| 150 |   return strlen(s)+1;
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | int
 | 
|---|
| 154 | StateInText::read(unsigned int&i)
 | 
|---|
| 155 | {
 | 
|---|
| 156 |   istream in(buf_);
 | 
|---|
| 157 |   in >> i;
 | 
|---|
| 158 |   if (in.fail()) {
 | 
|---|
| 159 |       ExEnv::errn() << "StateInText::read(unsigned int&): failed\n" << endl;
 | 
|---|
| 160 |       abort();
 | 
|---|
| 161 |     }
 | 
|---|
| 162 |   return (sizeof(int));
 | 
|---|
| 163 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | int
 | 
|---|
| 166 | StateInText::read(int&i)
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   istream in(buf_);
 | 
|---|
| 169 |   in >> i;
 | 
|---|
| 170 |   if (in.fail()) {
 | 
|---|
| 171 |       ExEnv::errn() << "StateInText::read(int&): failed\n" << endl;
 | 
|---|
| 172 |       abort();
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |   return (sizeof(int));
 | 
|---|
| 175 | }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | int
 | 
|---|
| 178 | StateInText::read(float&f)
 | 
|---|
| 179 | {
 | 
|---|
| 180 |   istream in(buf_);
 | 
|---|
| 181 |   in >> f;
 | 
|---|
| 182 |   if (in.fail()) {
 | 
|---|
| 183 |       ExEnv::errn() << "StateInText::read(float&): failed" << endl;
 | 
|---|
| 184 |       abort();
 | 
|---|
| 185 |     }
 | 
|---|
| 186 |   return sizeof(float);
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | int
 | 
|---|
| 190 | StateInText::read(double&d)
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   istream in(buf_);
 | 
|---|
| 193 |   in >> d;
 | 
|---|
| 194 |   if (in.fail()) {
 | 
|---|
| 195 |       ExEnv::errn() << "StateInText::read(double&): failed" << endl;
 | 
|---|
| 196 |       abort();
 | 
|---|
| 197 |     }
 | 
|---|
| 198 |   return sizeof(double);
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | void
 | 
|---|
| 202 | StateInText::abort()
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   ExEnv::errn() << "StateInText aborting at line " << newlines_+1 << " in the input"
 | 
|---|
| 205 |        << endl;
 | 
|---|
| 206 |   ::abort();
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 209 | 
 | 
|---|
| 210 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 211 | 
 | 
|---|
| 212 | int StateOutText::put(const ClassDesc*cd)
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   ostream out(buf_);
 | 
|---|
| 215 |   //
 | 
|---|
| 216 |   // write out parent info
 | 
|---|
| 217 |   if (classidmap_.find((ClassDesc*)cd) == classidmap_.end()) {
 | 
|---|
| 218 |       putparents(cd);
 | 
|---|
| 219 |       out << " version of class " << cd->name()
 | 
|---|
| 220 |           << " is " << cd->version() << endl;
 | 
|---|
| 221 |       out.flush();
 | 
|---|
| 222 |       classidmap_[(ClassDesc*)cd] = nextclassid_++;
 | 
|---|
| 223 |     }
 | 
|---|
| 224 |   out << "object of class " << cd->name() << " being written" << endl;
 | 
|---|
| 225 |   out.flush();
 | 
|---|
| 226 |   return 0;
 | 
|---|
| 227 |   }
 | 
|---|
| 228 | int
 | 
|---|
| 229 | StateOutText::putparents(const ClassDesc*cd)
 | 
|---|
| 230 | {
 | 
|---|
| 231 |   ostream out(buf_);
 | 
|---|
| 232 |   const ParentClasses& parents = cd->parents();
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   for (int i=0; i<parents.n(); i++) {
 | 
|---|
| 235 |       // the cast is needed to de-const-ify the class descriptor
 | 
|---|
| 236 |       ClassDesc*tmp = (ClassDesc*) parents[i].classdesc();
 | 
|---|
| 237 |       if (classidmap_.find(tmp) == classidmap_.end()) {
 | 
|---|
| 238 |           putparents(tmp);
 | 
|---|
| 239 |           out << " version of class " << tmp->name()
 | 
|---|
| 240 |               << " is " << tmp->version() << endl;
 | 
|---|
| 241 |           out.flush();
 | 
|---|
| 242 |           classidmap_[tmp] = nextclassid_++;
 | 
|---|
| 243 |         }
 | 
|---|
| 244 |     }
 | 
|---|
| 245 |   return 0;
 | 
|---|
| 246 | }
 | 
|---|
| 247 | int StateInText::get(const ClassDesc**cd)
 | 
|---|
| 248 | {
 | 
|---|
| 249 |   istream in(buf_);
 | 
|---|
| 250 |   const int line_length = 512;
 | 
|---|
| 251 |   char line[line_length];
 | 
|---|
| 252 | 
 | 
|---|
| 253 |   // if a list of class descriptors exists then read it in
 | 
|---|
| 254 |   
 | 
|---|
| 255 |   in.getline(line,line_length); newlines_++;
 | 
|---|
| 256 |   while (strncmp(line,"object",6)) {
 | 
|---|
| 257 |       char name[line_length];
 | 
|---|
| 258 |       int version;
 | 
|---|
| 259 |       sscanf(line," version of class %s is %d\n",
 | 
|---|
| 260 |              name,
 | 
|---|
| 261 |              &version);
 | 
|---|
| 262 |       ClassDesc* tmp = ClassDesc::name_to_class_desc(name);
 | 
|---|
| 263 |       // save the class descriptor and the version
 | 
|---|
| 264 |       int classid = nextclassid_++;
 | 
|---|
| 265 |       classidmap_[tmp] = classid;
 | 
|---|
| 266 |       StateClassData classdat(version,tmp);
 | 
|---|
| 267 |       classdatamap_[classid] = classdat;
 | 
|---|
| 268 |       in.getline(line,line_length); newlines_++;
 | 
|---|
| 269 |     }
 | 
|---|
| 270 | 
 | 
|---|
| 271 |   // get the class name for the object
 | 
|---|
| 272 |   char classname[line_length];
 | 
|---|
| 273 |   sscanf(line,"object of class %s being written\n", classname);
 | 
|---|
| 274 | 
 | 
|---|
| 275 |   // convert the class id into the class descriptor
 | 
|---|
| 276 |   *cd = ClassDesc::name_to_class_desc(classname);
 | 
|---|
| 277 |   
 | 
|---|
| 278 |   return 0;
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | int StateOutText::put(char r)
 | 
|---|
| 282 | {
 | 
|---|
| 283 |   no_array();
 | 
|---|
| 284 |   return StateOut::put(r);
 | 
|---|
| 285 | }
 | 
|---|
| 286 | int StateInText::get(char&r, const char *key)
 | 
|---|
| 287 | {
 | 
|---|
| 288 |   no_array();
 | 
|---|
| 289 |   return StateIn::get(r,key);
 | 
|---|
| 290 | }
 | 
|---|
| 291 | 
 | 
|---|
| 292 | int StateOutText::put(unsigned int r)
 | 
|---|
| 293 | {
 | 
|---|
| 294 |   no_array();
 | 
|---|
| 295 |   return StateOut::put(r);
 | 
|---|
| 296 | }
 | 
|---|
| 297 | int StateInText::get(unsigned int&r, const char *key)
 | 
|---|
| 298 | {
 | 
|---|
| 299 |   no_array();
 | 
|---|
| 300 |   return StateIn::get(r,key);
 | 
|---|
| 301 | }
 | 
|---|
| 302 | 
 | 
|---|
| 303 | int StateOutText::put(int r)
 | 
|---|
| 304 | {
 | 
|---|
| 305 |   no_array();
 | 
|---|
| 306 |   return StateOut::put(r);
 | 
|---|
| 307 | }
 | 
|---|
| 308 | int StateInText::get(int&r, const char *key)
 | 
|---|
| 309 | {
 | 
|---|
| 310 |   no_array();
 | 
|---|
| 311 |   return StateIn::get(r,key);
 | 
|---|
| 312 | }
 | 
|---|
| 313 | 
 | 
|---|
| 314 | int StateOutText::put(float r)
 | 
|---|
| 315 | {
 | 
|---|
| 316 |   no_array();
 | 
|---|
| 317 |   return StateOut::put(r);
 | 
|---|
| 318 | }
 | 
|---|
| 319 | int StateInText::get(float&r, const char *key)
 | 
|---|
| 320 | {
 | 
|---|
| 321 |   no_array();
 | 
|---|
| 322 |   return StateIn::get(r,key);
 | 
|---|
| 323 | }
 | 
|---|
| 324 | 
 | 
|---|
| 325 | int StateOutText::put(double r)
 | 
|---|
| 326 | {
 | 
|---|
| 327 |   no_array();
 | 
|---|
| 328 |   return StateOut::put(r);
 | 
|---|
| 329 | }
 | 
|---|
| 330 | int StateInText::get(double&r, const char *key)
 | 
|---|
| 331 | {
 | 
|---|
| 332 |   no_array();
 | 
|---|
| 333 |   return StateIn::get(r,key);
 | 
|---|
| 334 | }
 | 
|---|
| 335 | 
 | 
|---|
| 336 | int StateOutText::put(const char*d,int n)
 | 
|---|
| 337 | {
 | 
|---|
| 338 |   return StateOut::put(d,n);
 | 
|---|
| 339 | }
 | 
|---|
| 340 | int StateInText::get(char*&r)
 | 
|---|
| 341 | {
 | 
|---|
| 342 |   return StateIn::get(r);
 | 
|---|
| 343 | }
 | 
|---|
| 344 | int StateOutText::put(const unsigned int*d,int n)
 | 
|---|
| 345 | {
 | 
|---|
| 346 |   return StateOut::put(d,n);
 | 
|---|
| 347 | }
 | 
|---|
| 348 | int StateInText::get(unsigned int*&r)
 | 
|---|
| 349 | {
 | 
|---|
| 350 |   return StateIn::get(r);
 | 
|---|
| 351 | }
 | 
|---|
| 352 | int StateOutText::put(const int*d,int n)
 | 
|---|
| 353 | {
 | 
|---|
| 354 |   return StateOut::put(d,n);
 | 
|---|
| 355 | }
 | 
|---|
| 356 | int StateInText::get(int*&r)
 | 
|---|
| 357 | {
 | 
|---|
| 358 |   return StateIn::get(r);
 | 
|---|
| 359 | }
 | 
|---|
| 360 | int StateOutText::put(const float*d,int n)
 | 
|---|
| 361 | {
 | 
|---|
| 362 |   return StateOut::put(d,n);
 | 
|---|
| 363 | }
 | 
|---|
| 364 | int StateInText::get(float*&r)
 | 
|---|
| 365 | {
 | 
|---|
| 366 |   return StateIn::get(r);
 | 
|---|
| 367 | }
 | 
|---|
| 368 | int StateOutText::put(const double*d,int n)
 | 
|---|
| 369 | {
 | 
|---|
| 370 |   return StateOut::put(d,n);
 | 
|---|
| 371 | }
 | 
|---|
| 372 | int StateInText::get(double*&r)
 | 
|---|
| 373 | {
 | 
|---|
| 374 |   return StateIn::get(r);
 | 
|---|
| 375 | }
 | 
|---|
| 376 | 
 | 
|---|
| 377 | int StateOutText::putobject(const Ref<SavableState> &p)
 | 
|---|
| 378 | {
 | 
|---|
| 379 |   ostream out(buf_);
 | 
|---|
| 380 |   int r=0;
 | 
|---|
| 381 |   if (p.null()) {
 | 
|---|
| 382 |       out << "reference to null" << endl;
 | 
|---|
| 383 |       out.flush();
 | 
|---|
| 384 |     }
 | 
|---|
| 385 |   else {
 | 
|---|
| 386 |       std::map<Ref<SavableState>,StateOutData>::iterator ind = ps_.find(p);
 | 
|---|
| 387 |       if (ind == ps_.end() || copy_references_) {
 | 
|---|
| 388 |           // object has not been written yet
 | 
|---|
| 389 |           StateOutData dp;
 | 
|---|
| 390 |           dp.num = next_object_number_++;
 | 
|---|
| 391 |           out << "writing object " << dp.num << endl;
 | 
|---|
| 392 |           out.flush();
 | 
|---|
| 393 |           const ClassDesc *cd = p->class_desc();
 | 
|---|
| 394 |           put(cd);
 | 
|---|
| 395 |           out.flush();
 | 
|---|
| 396 |           dp.type = classidmap_[(ClassDesc*)cd];
 | 
|---|
| 397 |           if (!copy_references_) ps_[p] = dp;
 | 
|---|
| 398 |           have_classdesc();
 | 
|---|
| 399 |           p->save_vbase_state(*this);
 | 
|---|
| 400 |           p->save_data_state(*this);
 | 
|---|
| 401 |         }
 | 
|---|
| 402 |       else {
 | 
|---|
| 403 |           out << "reference to object " << ind->second.num << endl;
 | 
|---|
| 404 |           out.flush();
 | 
|---|
| 405 |         }
 | 
|---|
| 406 |     }
 | 
|---|
| 407 |   return r;
 | 
|---|
| 408 | }
 | 
|---|
| 409 | int StateInText::getobject(Ref<SavableState> &p)
 | 
|---|
| 410 | {
 | 
|---|
| 411 |   istream in(buf_);
 | 
|---|
| 412 |   const int line_length = 512;
 | 
|---|
| 413 |   char line[line_length];
 | 
|---|
| 414 | 
 | 
|---|
| 415 |   in.getline(line,line_length);
 | 
|---|
| 416 |   newlines_++;
 | 
|---|
| 417 | 
 | 
|---|
| 418 |   if (!strcmp("reference to null",line)) {
 | 
|---|
| 419 |       p = 0;
 | 
|---|
| 420 |     }
 | 
|---|
| 421 |   else if (!strncmp("writing",line,7)) {
 | 
|---|
| 422 |       int refnum;
 | 
|---|
| 423 |       sscanf(line,"writing object %d",&refnum);
 | 
|---|
| 424 |       const ClassDesc *cd;
 | 
|---|
| 425 |       get(&cd);
 | 
|---|
| 426 |       have_classdesc();
 | 
|---|
| 427 |       nextobject(refnum);
 | 
|---|
| 428 |       DescribedClass *dc = cd->create(*this);
 | 
|---|
| 429 |       p = dynamic_cast<SavableState*>(dc);
 | 
|---|
| 430 |     }
 | 
|---|
| 431 |   else if (!strncmp("reference",line,9)) {
 | 
|---|
| 432 |       int refnum;
 | 
|---|
| 433 |       sscanf(line,"reference to object %d",&refnum);
 | 
|---|
| 434 |       p = ps_[refnum].ptr;
 | 
|---|
| 435 |     }
 | 
|---|
| 436 |   else {
 | 
|---|
| 437 |       ExEnv::errn() << "StateInText: couldn't find a reference object" << endl;
 | 
|---|
| 438 |       abort();
 | 
|---|
| 439 |     }
 | 
|---|
| 440 | 
 | 
|---|
| 441 |   return 0;
 | 
|---|
| 442 | }
 | 
|---|
| 443 | 
 | 
|---|
| 444 | void
 | 
|---|
| 445 | StateOutText::start_array()
 | 
|---|
| 446 | {
 | 
|---|
| 447 |   ostream out(buf_);
 | 
|---|
| 448 |   if (!no_array_) { out.put(' '); out.put('<'); }
 | 
|---|
| 449 | }
 | 
|---|
| 450 | void
 | 
|---|
| 451 | StateInText::start_array()
 | 
|---|
| 452 | {
 | 
|---|
| 453 |   istream in(buf_);
 | 
|---|
| 454 |   if (!no_array_) {
 | 
|---|
| 455 |       if (in.get() != ' ' || in.get() != '<') {
 | 
|---|
| 456 |           ExEnv::errn() << "StateInText: expected a \" <\"" << endl;
 | 
|---|
| 457 |           abort();
 | 
|---|
| 458 |         }
 | 
|---|
| 459 |     }
 | 
|---|
| 460 | }
 | 
|---|
| 461 | 
 | 
|---|
| 462 | void
 | 
|---|
| 463 | StateOutText::end_array()
 | 
|---|
| 464 | {
 | 
|---|
| 465 |   ostream out(buf_);
 | 
|---|
| 466 |   if (!no_array_) {
 | 
|---|
| 467 |       out.put(' '); out.put('>');
 | 
|---|
| 468 |     }
 | 
|---|
| 469 |   else {
 | 
|---|
| 470 |       no_array_ = 0;
 | 
|---|
| 471 |     }
 | 
|---|
| 472 | }
 | 
|---|
| 473 | void
 | 
|---|
| 474 | StateInText::end_array()
 | 
|---|
| 475 | {
 | 
|---|
| 476 |   istream in(buf_);
 | 
|---|
| 477 |   if (!no_array_) {
 | 
|---|
| 478 |       if (in.get() != ' ' || in.get() != '>') {
 | 
|---|
| 479 |           ExEnv::errn() << "StateInText: expected a \"> \"" << endl;
 | 
|---|
| 480 |           abort();
 | 
|---|
| 481 |         }
 | 
|---|
| 482 |     }
 | 
|---|
| 483 |   else {
 | 
|---|
| 484 |       no_array_ = 0;
 | 
|---|
| 485 |     }
 | 
|---|
| 486 | }
 | 
|---|
| 487 | 
 | 
|---|
| 488 | void
 | 
|---|
| 489 | StateOutText::newline()
 | 
|---|
| 490 | {
 | 
|---|
| 491 |   ostream out(buf_);
 | 
|---|
| 492 |   if (no_newline_) {
 | 
|---|
| 493 |       no_newline_ = 0;
 | 
|---|
| 494 |       return;
 | 
|---|
| 495 |     }
 | 
|---|
| 496 |   out << endl;
 | 
|---|
| 497 |   out.flush();
 | 
|---|
| 498 | }
 | 
|---|
| 499 | void
 | 
|---|
| 500 | StateInText::newline()
 | 
|---|
| 501 | {
 | 
|---|
| 502 |   istream in(buf_);
 | 
|---|
| 503 |   if (no_newline_) {
 | 
|---|
| 504 |       no_newline_ = 0;
 | 
|---|
| 505 |       return;
 | 
|---|
| 506 |     }
 | 
|---|
| 507 |   if (in.get() != '\n') {
 | 
|---|
| 508 |       ExEnv::errn() << "StateInText: expected newline" << endl;
 | 
|---|
| 509 |       abort();
 | 
|---|
| 510 |     }
 | 
|---|
| 511 |   newlines_++;
 | 
|---|
| 512 | }
 | 
|---|
| 513 | 
 | 
|---|
| 514 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 515 | 
 | 
|---|
| 516 | int StateOutText::putstring(const char*s)
 | 
|---|
| 517 | {
 | 
|---|
| 518 |   int r = 0;
 | 
|---|
| 519 |   if (s) {
 | 
|---|
| 520 |       int size = strlen(s);
 | 
|---|
| 521 |       no_newline(); r += put(size);
 | 
|---|
| 522 |       if (size) {
 | 
|---|
| 523 |           r += put_array_char(s,size);
 | 
|---|
| 524 |         }
 | 
|---|
| 525 |     }
 | 
|---|
| 526 |   else {
 | 
|---|
| 527 |       r += put((int)0);
 | 
|---|
| 528 |     }
 | 
|---|
| 529 |   return r;
 | 
|---|
| 530 | }
 | 
|---|
| 531 | int StateInText::getstring(char*&s)
 | 
|---|
| 532 | {
 | 
|---|
| 533 |   int r = 0;
 | 
|---|
| 534 |   int size;
 | 
|---|
| 535 |   no_newline(); r += get(size);
 | 
|---|
| 536 |   if (size) {
 | 
|---|
| 537 |       s = new char[size+1];
 | 
|---|
| 538 |       s[size] = '\0';
 | 
|---|
| 539 |       if (size) {
 | 
|---|
| 540 |           r += get_array_char(s,size);
 | 
|---|
| 541 |         }
 | 
|---|
| 542 |     }
 | 
|---|
| 543 |   else {
 | 
|---|
| 544 |       s = 0;
 | 
|---|
| 545 |     }
 | 
|---|
| 546 |   return r;
 | 
|---|
| 547 | }
 | 
|---|
| 548 | 
 | 
|---|
| 549 | ///////////////////////////////////////////////////////////////////////
 | 
|---|
| 550 | 
 | 
|---|
| 551 | int StateOutText::put_array_char(const char*d,int size)
 | 
|---|
| 552 | {
 | 
|---|
| 553 |   ostream out(buf_);
 | 
|---|
| 554 |   start_array();
 | 
|---|
| 555 |   int nwrit=size+1;
 | 
|---|
| 556 |   for (int i=0; i<size; i++) { out.put(d[i]); }
 | 
|---|
| 557 |   end_array();
 | 
|---|
| 558 |   newline();
 | 
|---|
| 559 |   return nwrit;
 | 
|---|
| 560 | }
 | 
|---|
| 561 | int StateInText::get_array_char(char*d,int size)
 | 
|---|
| 562 | {
 | 
|---|
| 563 |   istream in(buf_);
 | 
|---|
| 564 |   start_array();
 | 
|---|
| 565 |   int ch;
 | 
|---|
| 566 |   for (int i=0; i<size; i++) {
 | 
|---|
| 567 |       ch = in.get();
 | 
|---|
| 568 |       if (ch == EOF) {
 | 
|---|
| 569 |           ExEnv::errn() << "StateInText::get_array_char: EOF while reading array"
 | 
|---|
| 570 |                << endl;
 | 
|---|
| 571 |           abort();
 | 
|---|
| 572 |         }
 | 
|---|
| 573 |       d[i] = ch;
 | 
|---|
| 574 |     }
 | 
|---|
| 575 |   end_array();
 | 
|---|
| 576 |   newline();
 | 
|---|
| 577 |   return size+1;
 | 
|---|
| 578 | }
 | 
|---|
| 579 | 
 | 
|---|
| 580 | int StateOutText::put_array_uint(const unsigned int*d,int size)
 | 
|---|
| 581 | {
 | 
|---|
| 582 |   ostream out(buf_);
 | 
|---|
| 583 |   start_array();
 | 
|---|
| 584 |   int nwrit=0;
 | 
|---|
| 585 |   for (int i=0; i<size; i++) { out << ' ' << d[i]; nwrit++; }
 | 
|---|
| 586 |   out.flush();
 | 
|---|
| 587 |   end_array();
 | 
|---|
| 588 |   newline();
 | 
|---|
| 589 |   return nwrit;
 | 
|---|
| 590 | }
 | 
|---|
| 591 | int StateInText::get_array_uint(unsigned int*d,int size)
 | 
|---|
| 592 | {
 | 
|---|
| 593 |   start_array();
 | 
|---|
| 594 |   int nread,tnread=0;
 | 
|---|
| 595 |   for (int i=0; i<size; i++) {
 | 
|---|
| 596 |       nread=read(d[i]);
 | 
|---|
| 597 |       tnread += nread;
 | 
|---|
| 598 |     }
 | 
|---|
| 599 |   end_array();
 | 
|---|
| 600 |   newline();
 | 
|---|
| 601 |   return tnread;
 | 
|---|
| 602 | }
 | 
|---|
| 603 | 
 | 
|---|
| 604 | int StateOutText::put_array_int(const int*d,int size)
 | 
|---|
| 605 | {
 | 
|---|
| 606 |   ostream out(buf_);
 | 
|---|
| 607 |   start_array();
 | 
|---|
| 608 |   int nwrit=0;
 | 
|---|
| 609 |   for (int i=0; i<size; i++) { out << ' ' << d[i]; nwrit++; }
 | 
|---|
| 610 |   out.flush();
 | 
|---|
| 611 |   end_array();
 | 
|---|
| 612 |   newline();
 | 
|---|
| 613 |   return nwrit;
 | 
|---|
| 614 | }
 | 
|---|
| 615 | int StateInText::get_array_int(int*d,int size)
 | 
|---|
| 616 | {
 | 
|---|
| 617 |   start_array();
 | 
|---|
| 618 |   int nread,tnread=0;
 | 
|---|
| 619 |   for (int i=0; i<size; i++) {
 | 
|---|
| 620 |       nread=read(d[i]);
 | 
|---|
| 621 |       tnread += nread;
 | 
|---|
| 622 |     }
 | 
|---|
| 623 |   end_array();
 | 
|---|
| 624 |   newline();
 | 
|---|
| 625 |   return tnread;
 | 
|---|
| 626 | }
 | 
|---|
| 627 | 
 | 
|---|
| 628 | int StateOutText::put_array_float(const float*d,int size)
 | 
|---|
| 629 | {
 | 
|---|
| 630 |   ostream out(buf_);
 | 
|---|
| 631 |   start_array();
 | 
|---|
| 632 |   int nwrit=0;
 | 
|---|
| 633 |   for (int i=0; i<size; i++) {
 | 
|---|
| 634 |       out.setf(ios::scientific);
 | 
|---|
| 635 |       out.width(20);
 | 
|---|
| 636 |       out.precision(15);
 | 
|---|
| 637 |       out << ' ' << d[i];
 | 
|---|
| 638 |       nwrit++;
 | 
|---|
| 639 |     }
 | 
|---|
| 640 |   out.flush();
 | 
|---|
| 641 |   end_array();
 | 
|---|
| 642 |   newline();
 | 
|---|
| 643 |   return nwrit;
 | 
|---|
| 644 | }
 | 
|---|
| 645 | int StateInText::get_array_float(float*d,int size)
 | 
|---|
| 646 | {
 | 
|---|
| 647 |   start_array();
 | 
|---|
| 648 |   int nread,tnread=0;
 | 
|---|
| 649 |   for (int i=0; i<size; i++) {
 | 
|---|
| 650 |       nread=read(d[i]);
 | 
|---|
| 651 |       tnread += nread;
 | 
|---|
| 652 |     }
 | 
|---|
| 653 |   end_array();
 | 
|---|
| 654 |   newline();
 | 
|---|
| 655 |   return tnread;
 | 
|---|
| 656 | }
 | 
|---|
| 657 | 
 | 
|---|
| 658 | int StateOutText::put_array_double(const double*d,int size)
 | 
|---|
| 659 | {
 | 
|---|
| 660 |   ostream out(buf_);
 | 
|---|
| 661 |   start_array();
 | 
|---|
| 662 |   int nwrit=0;
 | 
|---|
| 663 |   for (int i=0; i<size; i++) {
 | 
|---|
| 664 |       out.setf(ios::scientific);
 | 
|---|
| 665 |       out.width(20);
 | 
|---|
| 666 |       out.precision(15);
 | 
|---|
| 667 |       out << ' ' << d[i];
 | 
|---|
| 668 |       nwrit++;
 | 
|---|
| 669 |     }
 | 
|---|
| 670 |   out.flush();
 | 
|---|
| 671 |   end_array();
 | 
|---|
| 672 |   newline();
 | 
|---|
| 673 |   return nwrit;
 | 
|---|
| 674 | }
 | 
|---|
| 675 | int StateInText::get_array_double(double*d,int size)
 | 
|---|
| 676 | {
 | 
|---|
| 677 |   start_array();
 | 
|---|
| 678 |   int nread,tnread=0;
 | 
|---|
| 679 |   for (int i=0; i<size; i++) {
 | 
|---|
| 680 |       nread=read(d[i]);
 | 
|---|
| 681 |       tnread += nread;
 | 
|---|
| 682 |     }
 | 
|---|
| 683 |   end_array();
 | 
|---|
| 684 |   newline();
 | 
|---|
| 685 |   return tnread;
 | 
|---|
| 686 | }
 | 
|---|
| 687 | 
 | 
|---|
| 688 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 689 | 
 | 
|---|
| 690 | // Local Variables:
 | 
|---|
| 691 | // mode: c++
 | 
|---|
| 692 | // c-file-style: "CLJ"
 | 
|---|
| 693 | // End:
 | 
|---|