| [0b990d] | 1 | // | 
|---|
|  | 2 | // statein.cc | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // Copyright (C) 1998 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 <ctype.h> | 
|---|
|  | 33 | #include <util/misc/formio.h> | 
|---|
|  | 34 | #include <util/state/translate.h> | 
|---|
|  | 35 | #include <util/state/statein.h> | 
|---|
|  | 36 |  | 
|---|
|  | 37 | using namespace std; | 
|---|
|  | 38 | using namespace sc; | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #define DEBUG 0 | 
|---|
|  | 41 |  | 
|---|
|  | 42 | static ClassDesc StateIn_cd( | 
|---|
|  | 43 | typeid(StateIn),"StateIn",1,"public DescribedClass"); | 
|---|
|  | 44 |  | 
|---|
|  | 45 | StateIn::StateIn(const StateIn&) | 
|---|
|  | 46 | { | 
|---|
|  | 47 | ExEnv::errn() << "StateIn: private copy ctor called???" << endl; | 
|---|
|  | 48 | abort(); | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | void | 
|---|
|  | 52 | StateIn::operator=(const StateIn&) | 
|---|
|  | 53 | { | 
|---|
|  | 54 | ExEnv::errn() << "StateIn: private assignment called???" << endl; | 
|---|
|  | 55 | abort(); | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | StateIn::StateIn() : | 
|---|
|  | 59 | have_cd_(0), | 
|---|
|  | 60 | translate_(new TranslateDataIn(this, new TranslateDataBigEndian)), | 
|---|
|  | 61 | expected_object_num_(0), | 
|---|
|  | 62 | nextclassid_(0), | 
|---|
|  | 63 | node_to_node_(0) | 
|---|
|  | 64 | { | 
|---|
|  | 65 | key_[0] = '\0'; | 
|---|
|  | 66 | keylength_ = 0; | 
|---|
|  | 67 | } | 
|---|
|  | 68 |  | 
|---|
|  | 69 | StateIn::~StateIn() | 
|---|
|  | 70 | { | 
|---|
|  | 71 | delete translate_; | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | int | 
|---|
|  | 75 | StateIn::push_key(const char *keyword) | 
|---|
|  | 76 | { | 
|---|
|  | 77 | if (!keyword || override_.null()) return keylength_; | 
|---|
|  | 78 |  | 
|---|
|  | 79 | int length = strlen(keyword); | 
|---|
|  | 80 | if (keylength_ + length + 1 >= KeyVal::MaxKeywordLength) { | 
|---|
|  | 81 | ExEnv::errn() << "StateIn: KeyVal::MaxKeywordLength exceeded" << endl; | 
|---|
|  | 82 | abort(); | 
|---|
|  | 83 | } | 
|---|
|  | 84 | int old_keylength = keylength_; | 
|---|
|  | 85 | if (keylength_) key_[keylength_++] = ':'; | 
|---|
|  | 86 | char *tmp = &key_[keylength_]; | 
|---|
|  | 87 | for (int i=0; i<length; i++) tmp[i] = keyword[i]; | 
|---|
|  | 88 | keylength_ += length; | 
|---|
|  | 89 | key_[keylength_] = '\0'; | 
|---|
|  | 90 |  | 
|---|
|  | 91 | return old_keylength; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | int | 
|---|
|  | 95 | StateIn::tell() | 
|---|
|  | 96 | { | 
|---|
|  | 97 | return 0; | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | void | 
|---|
|  | 101 | StateIn::seek(int loc) | 
|---|
|  | 102 | { | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | int | 
|---|
|  | 106 | StateIn::seekable() | 
|---|
|  | 107 | { | 
|---|
|  | 108 | return 0; | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | int | 
|---|
|  | 112 | StateIn::use_directory() | 
|---|
|  | 113 | { | 
|---|
|  | 114 | return 0; | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | int | 
|---|
|  | 118 | StateIn::get_array_char(char*p,int size) | 
|---|
|  | 119 | { | 
|---|
|  | 120 | return translate_->get(p,size); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | int | 
|---|
|  | 124 | StateIn::get_array_uint(unsigned int*p,int size) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | return translate_->get(p,size); | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
|  | 129 | int | 
|---|
|  | 130 | StateIn::get_array_int(int*p,int size) | 
|---|
|  | 131 | { | 
|---|
|  | 132 | return translate_->get(p,size); | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | int | 
|---|
|  | 136 | StateIn::get_array_float(float*p,int size) | 
|---|
|  | 137 | { | 
|---|
|  | 138 | return translate_->get(p,size); | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 | int | 
|---|
|  | 142 | StateIn::get_array_double(double*p,int size) | 
|---|
|  | 143 | { | 
|---|
|  | 144 | return translate_->get(p,size); | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | int | 
|---|
|  | 148 | StateIn::get(char&r, const char *keyword) | 
|---|
|  | 149 | { | 
|---|
|  | 150 | int n = get_array_char(&r,1); | 
|---|
|  | 151 | if (keyword && override().nonnull()) { | 
|---|
|  | 152 | int p = push_key(keyword); | 
|---|
|  | 153 | char roverride = override()->charvalue(key()); | 
|---|
|  | 154 | if (override()->error() == KeyVal::OK) { | 
|---|
|  | 155 | ExEnv::out0() << indent << "overriding \"" << key() | 
|---|
|  | 156 | << "\": " << r << " -> " << roverride << endl; | 
|---|
|  | 157 | r = roverride; | 
|---|
|  | 158 | } | 
|---|
|  | 159 | pop_key(p); | 
|---|
|  | 160 | } | 
|---|
|  | 161 | return n; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | int | 
|---|
|  | 165 | StateIn::get(unsigned int&r, const char *keyword) | 
|---|
|  | 166 | { | 
|---|
|  | 167 | int n = get_array_uint(&r,1); | 
|---|
|  | 168 | if (keyword && override().nonnull()) { | 
|---|
|  | 169 | int p = push_key(keyword); | 
|---|
|  | 170 | int roverride = override()->intvalue(key()); | 
|---|
|  | 171 | if (override()->error() == KeyVal::OK) { | 
|---|
|  | 172 | ExEnv::out0() << indent << "overriding \"" << key() | 
|---|
|  | 173 | << "\": " << r << " -> " << roverride << endl; | 
|---|
|  | 174 | r = roverride; | 
|---|
|  | 175 | } | 
|---|
|  | 176 | pop_key(p); | 
|---|
|  | 177 | } | 
|---|
|  | 178 | return n; | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|
|  | 181 | int | 
|---|
|  | 182 | StateIn::get(int&r, const char *keyword) | 
|---|
|  | 183 | { | 
|---|
|  | 184 | int n = get_array_int(&r,1); | 
|---|
|  | 185 | if (keyword && override().nonnull()) { | 
|---|
|  | 186 | int p = push_key(keyword); | 
|---|
|  | 187 | int roverride = override()->intvalue(key()); | 
|---|
|  | 188 | if (override()->error() == KeyVal::OK) { | 
|---|
|  | 189 | ExEnv::out0() << indent << "overriding \"" << key() | 
|---|
|  | 190 | << "\": " << r << " -> " << roverride << endl; | 
|---|
|  | 191 | r = roverride; | 
|---|
|  | 192 | } | 
|---|
|  | 193 | pop_key(p); | 
|---|
|  | 194 | } | 
|---|
|  | 195 | return n; | 
|---|
|  | 196 | } | 
|---|
|  | 197 |  | 
|---|
|  | 198 | int | 
|---|
|  | 199 | StateIn::get(bool&r, const char *keyword) | 
|---|
|  | 200 | { | 
|---|
|  | 201 | int b; | 
|---|
|  | 202 | int n = get(b,keyword); | 
|---|
|  | 203 | r = b; | 
|---|
|  | 204 | return n; | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | int | 
|---|
|  | 208 | StateIn::get(float&r, const char *keyword) | 
|---|
|  | 209 | { | 
|---|
|  | 210 | int n = get_array_float(&r,1); | 
|---|
|  | 211 | if (keyword && override().nonnull()) { | 
|---|
|  | 212 | int p = push_key(keyword); | 
|---|
|  | 213 | float roverride = override()->floatvalue(key()); | 
|---|
|  | 214 | if (override()->error() == KeyVal::OK) { | 
|---|
|  | 215 | ExEnv::out0() << indent << "overriding \"" << key() | 
|---|
|  | 216 | << "\": " << r << " -> " << roverride << endl; | 
|---|
|  | 217 | r = roverride; | 
|---|
|  | 218 | } | 
|---|
|  | 219 | pop_key(p); | 
|---|
|  | 220 | } | 
|---|
|  | 221 | return n; | 
|---|
|  | 222 | } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | int | 
|---|
|  | 225 | StateIn::get(double&r, const char *keyword) | 
|---|
|  | 226 | { | 
|---|
|  | 227 | int n = get_array_double(&r,1); | 
|---|
|  | 228 | if (keyword && override().nonnull()) { | 
|---|
|  | 229 | int p = push_key(keyword); | 
|---|
|  | 230 | double roverride = override()->doublevalue(key()); | 
|---|
|  | 231 | if (override()->error() == KeyVal::OK) { | 
|---|
|  | 232 | ExEnv::out0() << indent << "overriding \"" << key() | 
|---|
|  | 233 | << "\": " << r << " -> " << roverride << endl; | 
|---|
|  | 234 | r = roverride; | 
|---|
|  | 235 | } | 
|---|
|  | 236 | pop_key(p); | 
|---|
|  | 237 | } | 
|---|
|  | 238 | return n; | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 | int | 
|---|
|  | 242 | StateIn::get_array_void(void*p,int s) | 
|---|
|  | 243 | { | 
|---|
|  | 244 | ExEnv::errn() << "StateIn::get_array_void(void*p,int s) " | 
|---|
|  | 245 | << "is a derived class responsiblility" << endl | 
|---|
|  | 246 | << "  exact type is \"" << class_name() << "\"" << endl; | 
|---|
|  | 247 | abort(); | 
|---|
|  | 248 | return -1; | 
|---|
|  | 249 | } | 
|---|
|  | 250 |  | 
|---|
|  | 251 | void | 
|---|
|  | 252 | StateIn::get_directory() | 
|---|
|  | 253 | { | 
|---|
|  | 254 | int i, length; | 
|---|
|  | 255 |  | 
|---|
|  | 256 | // read the type information | 
|---|
|  | 257 | #if DEBUG | 
|---|
|  | 258 | ExEnv::outn() << "Directory length location = " << tell() << endl; | 
|---|
|  | 259 | #endif | 
|---|
|  | 260 | get(length); | 
|---|
|  | 261 | #if DEBUG | 
|---|
|  | 262 | ExEnv::outn() << "Directory length = " << length << endl; | 
|---|
|  | 263 | ExEnv::outn() << "Directory entries location = " << tell() << endl; | 
|---|
|  | 264 | #endif | 
|---|
|  | 265 | for (i=0; i<length; i++) { | 
|---|
|  | 266 | char *name; | 
|---|
|  | 267 | int version, classid; | 
|---|
|  | 268 | getstring(name); | 
|---|
|  | 269 | get(version); | 
|---|
|  | 270 | get(classid); | 
|---|
|  | 271 | #if DEBUG | 
|---|
|  | 272 | ExEnv::outn() << "GET CLASS:" | 
|---|
|  | 273 | << " NAME = " << name | 
|---|
|  | 274 | << " VERSION = " << version | 
|---|
|  | 275 | << " ID = " << classid << endl; | 
|---|
|  | 276 | #endif | 
|---|
|  | 277 | ClassDesc* tmp = ClassDesc::name_to_class_desc(name); | 
|---|
|  | 278 |  | 
|---|
|  | 279 | classidmap_[tmp] = classid; | 
|---|
|  | 280 | StateClassData classdat(version,tmp,name); | 
|---|
|  | 281 | classdatamap_[classid] = classdat; | 
|---|
|  | 282 | } | 
|---|
|  | 283 |  | 
|---|
|  | 284 | // read the object information | 
|---|
|  | 285 | get(length); | 
|---|
|  | 286 | for (i=0; i<length; i++) { | 
|---|
|  | 287 | int n; | 
|---|
|  | 288 | get(n); | 
|---|
|  | 289 | StateInData num; | 
|---|
|  | 290 | get(num.type); | 
|---|
|  | 291 | get(num.offset); | 
|---|
|  | 292 | get(num.size); | 
|---|
|  | 293 | #if DEBUG | 
|---|
|  | 294 | ExEnv::outn() << "GET OBJECT:" | 
|---|
|  | 295 | << " NUM=" << setw(2) << n | 
|---|
|  | 296 | << " OFF=" << setw(5) << num.offset | 
|---|
|  | 297 | << " SZ=" << setw(4) << num.size | 
|---|
|  | 298 | << " ID=" << setw(2) << num.type | 
|---|
|  | 299 | << " (" << classdatamap_[num.type].name << ")" | 
|---|
|  | 300 | << endl; | 
|---|
|  | 301 | #endif | 
|---|
|  | 302 | ps_[n] = num; | 
|---|
|  | 303 | classdatamap_[num.type].ninstance++; | 
|---|
|  | 304 | } | 
|---|
|  | 305 | } | 
|---|
|  | 306 |  | 
|---|
|  | 307 | void | 
|---|
|  | 308 | StateIn::find_and_get_directory() | 
|---|
|  | 309 | { | 
|---|
|  | 310 | if (directory_location() && seekable()) { | 
|---|
|  | 311 | int original_loc = tell(); | 
|---|
|  | 312 | seek(directory_location()); | 
|---|
|  | 313 | #if DEBUG | 
|---|
|  | 314 | ExEnv::outn() << "Getting directory from " << tell() << endl; | 
|---|
|  | 315 | #endif | 
|---|
|  | 316 | get_directory(); | 
|---|
|  | 317 | seek(original_loc); | 
|---|
|  | 318 | } | 
|---|
|  | 319 | } | 
|---|
|  | 320 |  | 
|---|
|  | 321 | int | 
|---|
|  | 322 | StateIn::getstring(char*&s) | 
|---|
|  | 323 | { | 
|---|
|  | 324 | int r=0; | 
|---|
|  | 325 | int size; | 
|---|
|  | 326 | #if DEBUG | 
|---|
|  | 327 | ExEnv::outn() << "String length location = " << tell() << endl; | 
|---|
|  | 328 | #endif | 
|---|
|  | 329 | r += get(size); | 
|---|
|  | 330 | #if DEBUG | 
|---|
|  | 331 | ExEnv::outn() << "String length = " << size << endl; | 
|---|
|  | 332 | #endif | 
|---|
|  | 333 | if (size) { | 
|---|
|  | 334 | #if DEBUG | 
|---|
|  | 335 | ExEnv::outn() << "String location = " << tell() << endl; | 
|---|
|  | 336 | #endif | 
|---|
|  | 337 | s = new char[size]; | 
|---|
|  | 338 | r += get_array_char(s,size-1); | 
|---|
|  | 339 | s[size-1] = '\0'; | 
|---|
|  | 340 | } | 
|---|
|  | 341 | else { | 
|---|
|  | 342 | s = 0; | 
|---|
|  | 343 | } | 
|---|
|  | 344 | return r; | 
|---|
|  | 345 | } | 
|---|
|  | 346 |  | 
|---|
|  | 347 | int | 
|---|
|  | 348 | StateIn::get(std::string&s) | 
|---|
|  | 349 | { | 
|---|
|  | 350 | char *cstr; | 
|---|
|  | 351 | int r = getstring(cstr); | 
|---|
|  | 352 | if (cstr) s = cstr; | 
|---|
|  | 353 | else s = ""; | 
|---|
|  | 354 | delete[] cstr; | 
|---|
|  | 355 | return r; | 
|---|
|  | 356 | } | 
|---|
|  | 357 |  | 
|---|
|  | 358 | int | 
|---|
|  | 359 | StateIn::get(char*&s) | 
|---|
|  | 360 | { | 
|---|
|  | 361 | int r=0; | 
|---|
|  | 362 | int size; | 
|---|
|  | 363 | r += get(size); | 
|---|
|  | 364 | if (size) { | 
|---|
|  | 365 | s = new char[size]; | 
|---|
|  | 366 | r += get_array_char(s,size); | 
|---|
|  | 367 | } | 
|---|
|  | 368 | else { | 
|---|
|  | 369 | s = 0; | 
|---|
|  | 370 | } | 
|---|
|  | 371 | return r; | 
|---|
|  | 372 | } | 
|---|
|  | 373 |  | 
|---|
|  | 374 | int | 
|---|
|  | 375 | StateIn::get(unsigned int*&s) | 
|---|
|  | 376 | { | 
|---|
|  | 377 | int r=0; | 
|---|
|  | 378 | int size; | 
|---|
|  | 379 | r += get(size); | 
|---|
|  | 380 | if (size) { | 
|---|
|  | 381 | s = new unsigned int[size]; | 
|---|
|  | 382 | r += get_array_uint(s,size); | 
|---|
|  | 383 | } | 
|---|
|  | 384 | else { | 
|---|
|  | 385 | s = 0; | 
|---|
|  | 386 | } | 
|---|
|  | 387 | return r; | 
|---|
|  | 388 | } | 
|---|
|  | 389 |  | 
|---|
|  | 390 | int | 
|---|
|  | 391 | StateIn::get(int*&s) | 
|---|
|  | 392 | { | 
|---|
|  | 393 | int r=0; | 
|---|
|  | 394 | int size; | 
|---|
|  | 395 | r += get(size); | 
|---|
|  | 396 | if (size) { | 
|---|
|  | 397 | s = new int[size]; | 
|---|
|  | 398 | r += get_array_int(s,size); | 
|---|
|  | 399 | } | 
|---|
|  | 400 | else { | 
|---|
|  | 401 | s = 0; | 
|---|
|  | 402 | } | 
|---|
|  | 403 | return r; | 
|---|
|  | 404 | } | 
|---|
|  | 405 |  | 
|---|
|  | 406 | int | 
|---|
|  | 407 | StateIn::get(float*&s) | 
|---|
|  | 408 | { | 
|---|
|  | 409 | int r=0; | 
|---|
|  | 410 | int size; | 
|---|
|  | 411 | r += get(size); | 
|---|
|  | 412 | if (size) { | 
|---|
|  | 413 | s = new float[size]; | 
|---|
|  | 414 | r += get_array_float(s,size); | 
|---|
|  | 415 | } | 
|---|
|  | 416 | else { | 
|---|
|  | 417 | s = 0; | 
|---|
|  | 418 | } | 
|---|
|  | 419 | return r; | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
|  | 422 | int | 
|---|
|  | 423 | StateIn::get(double*&s) | 
|---|
|  | 424 | { | 
|---|
|  | 425 | int r=0; | 
|---|
|  | 426 | int size; | 
|---|
|  | 427 | r += get(size); | 
|---|
|  | 428 | if (size) { | 
|---|
|  | 429 | s = new double[size]; | 
|---|
|  | 430 | r += get_array_double(s,size); | 
|---|
|  | 431 | } | 
|---|
|  | 432 | else { | 
|---|
|  | 433 | s = 0; | 
|---|
|  | 434 | } | 
|---|
|  | 435 | return r; | 
|---|
|  | 436 | } | 
|---|
|  | 437 |  | 
|---|
|  | 438 | int | 
|---|
|  | 439 | StateIn::version(const ClassDesc* cd) | 
|---|
|  | 440 | { | 
|---|
|  | 441 | int classid = classidmap_[(ClassDesc*)cd]; | 
|---|
|  | 442 | return classdatamap_[classid].version; | 
|---|
|  | 443 | } | 
|---|
|  | 444 |  | 
|---|
|  | 445 | int | 
|---|
|  | 446 | StateIn::get(const ClassDesc**cd) | 
|---|
|  | 447 | { | 
|---|
|  | 448 | int r=0; | 
|---|
|  | 449 |  | 
|---|
|  | 450 | // if a list of class descriptors exist then read it in | 
|---|
|  | 451 | if (!use_directory()) { | 
|---|
|  | 452 | int size; | 
|---|
|  | 453 | r += get(size); | 
|---|
|  | 454 | while (size) { | 
|---|
|  | 455 | char* name = new char[size+1]; | 
|---|
|  | 456 | r += get_array_char(name,size); | 
|---|
|  | 457 | name[size] = '\0'; | 
|---|
|  | 458 | int version; | 
|---|
|  | 459 | r += get(version); | 
|---|
|  | 460 | ClassDesc* tmp = ClassDesc::name_to_class_desc(name); | 
|---|
|  | 461 | // save the class descriptor and the version | 
|---|
|  | 462 | int classid = nextclassid_++; | 
|---|
|  | 463 | classidmap_[tmp] = classid; | 
|---|
|  | 464 | StateClassData classdat(version,tmp,name); | 
|---|
|  | 465 | classdatamap_[classid] = classdat; | 
|---|
|  | 466 | r += get(size); | 
|---|
|  | 467 | } | 
|---|
|  | 468 | } | 
|---|
|  | 469 |  | 
|---|
|  | 470 | // get the class id for the object | 
|---|
|  | 471 | int classid; | 
|---|
|  | 472 | r += get(classid); | 
|---|
|  | 473 |  | 
|---|
|  | 474 | if (classdatamap_.find(classid) == classdatamap_.end()) { | 
|---|
|  | 475 | ExEnv::errn() << "ERROR: StateIn: couldn't find class descriptor for classid " | 
|---|
|  | 476 | << classid << endl; | 
|---|
|  | 477 | abort(); | 
|---|
|  | 478 | } | 
|---|
|  | 479 |  | 
|---|
|  | 480 | // convert the class id into the class descriptor | 
|---|
|  | 481 | *cd = classdatamap_[classid].classdesc; | 
|---|
|  | 482 |  | 
|---|
|  | 483 | return r; | 
|---|
|  | 484 | } | 
|---|
|  | 485 |  | 
|---|
|  | 486 | void | 
|---|
|  | 487 | StateIn::list_objects(ostream &o) | 
|---|
|  | 488 | { | 
|---|
|  | 489 | if (SCFormIO::getverbose(o)) { | 
|---|
|  | 490 | int ii = 1; | 
|---|
|  | 491 | for (std::map<int,StateInData>::iterator i=ps_.begin(); i!=ps_.end(); | 
|---|
|  | 492 | i++,ii++) { | 
|---|
|  | 493 | const StateInData &num(i->second); | 
|---|
|  | 494 | const char *classname = classdatamap_[num.type].name; | 
|---|
|  | 495 | o << indent | 
|---|
|  | 496 | << "object " << ii | 
|---|
|  | 497 | << " at offset " << num.offset | 
|---|
|  | 498 | << " is of type " << classname | 
|---|
|  | 499 | << endl; | 
|---|
|  | 500 | } | 
|---|
|  | 501 | } | 
|---|
|  | 502 | else { | 
|---|
|  | 503 | int ntot = 0; | 
|---|
|  | 504 | for (std::map<int,StateClassData>::iterator i=classdatamap_.begin(); | 
|---|
|  | 505 | i!=classdatamap_.end(); i++) { | 
|---|
|  | 506 | StateClassData &dat = i->second; | 
|---|
|  | 507 | if (dat.ninstance > 0) { | 
|---|
|  | 508 | o << indent << dat.ninstance | 
|---|
|  | 509 | << " " | 
|---|
|  | 510 | << dat.name | 
|---|
|  | 511 | << endl; | 
|---|
|  | 512 | ntot += dat.ninstance; | 
|---|
|  | 513 | } | 
|---|
|  | 514 | } | 
|---|
|  | 515 | o << indent << "total of " << ntot << endl; | 
|---|
|  | 516 | } | 
|---|
|  | 517 | } | 
|---|
|  | 518 |  | 
|---|
|  | 519 | int | 
|---|
|  | 520 | StateIn::dir_getobject(Ref<SavableState> &p, const char *name) | 
|---|
|  | 521 | { | 
|---|
|  | 522 | int r=0; | 
|---|
|  | 523 |  | 
|---|
|  | 524 | p = 0; | 
|---|
|  | 525 |  | 
|---|
|  | 526 | if (!has_directory()) { | 
|---|
|  | 527 | ExEnv::errn() << "ERROR: StateIn: no directory to get object from" << endl; | 
|---|
|  | 528 | abort(); | 
|---|
|  | 529 | } | 
|---|
|  | 530 |  | 
|---|
|  | 531 | if (!seekable()) { | 
|---|
|  | 532 | ExEnv::errn() << "ERROR: StateIn: cannot get object because cannot seek" << endl; | 
|---|
|  | 533 | abort(); | 
|---|
|  | 534 | } | 
|---|
|  | 535 |  | 
|---|
|  | 536 | // find the class name and/or object number | 
|---|
|  | 537 | const char *colon = ::strrchr(name,':'); | 
|---|
|  | 538 | int number = 1; | 
|---|
|  | 539 | char *classname = 0; | 
|---|
|  | 540 | if (colon == 0) { | 
|---|
|  | 541 | if (isdigit(*name)) number = atoi(name); | 
|---|
|  | 542 | else classname = strcpy(new char[strlen(name)+1], name); | 
|---|
|  | 543 | } | 
|---|
|  | 544 | else { | 
|---|
|  | 545 | number = atoi(&colon[1]); | 
|---|
|  | 546 | classname = strcpy(new char[strlen(name)+1], name); | 
|---|
|  | 547 | *strrchr(classname,':') = '\0'; | 
|---|
|  | 548 | } | 
|---|
|  | 549 |  | 
|---|
|  | 550 | const ClassDesc *cd = 0; | 
|---|
|  | 551 | if (classname) { | 
|---|
|  | 552 | cd = ClassDesc::name_to_class_desc(classname); | 
|---|
|  | 553 | if (!cd) { | 
|---|
|  | 554 | ExEnv::errn() << "ERROR: StateIn: class " << classname << " unknown" << endl; | 
|---|
|  | 555 | abort(); | 
|---|
|  | 556 | } | 
|---|
|  | 557 | delete[] classname; | 
|---|
|  | 558 | } | 
|---|
|  | 559 |  | 
|---|
|  | 560 | int classid; | 
|---|
|  | 561 | if (cd) classid = classidmap_[(ClassDesc*)cd]; | 
|---|
|  | 562 | else classid = -1; | 
|---|
|  | 563 |  | 
|---|
|  | 564 | std::map<int,StateInData>::iterator i; | 
|---|
|  | 565 | int nfound = 0; | 
|---|
|  | 566 | for (i=ps_.begin(); i!=ps_.end(); i++) { | 
|---|
|  | 567 | if (classid == -1 || i->second.type == classid) nfound++; | 
|---|
|  | 568 | if (nfound == number) { | 
|---|
|  | 569 | if (i->second.ptr.nonnull()) { | 
|---|
|  | 570 | p = i->second.ptr; | 
|---|
|  | 571 | } | 
|---|
|  | 572 | else { | 
|---|
|  | 573 | seek(i->second.offset); | 
|---|
|  | 574 | r += getobject(p); | 
|---|
|  | 575 | } | 
|---|
|  | 576 | return r; | 
|---|
|  | 577 | } | 
|---|
|  | 578 | } | 
|---|
|  | 579 |  | 
|---|
|  | 580 | return r; | 
|---|
|  | 581 | } | 
|---|
|  | 582 |  | 
|---|
|  | 583 | int | 
|---|
|  | 584 | StateIn::getobject(Ref<SavableState> &p) | 
|---|
|  | 585 | { | 
|---|
|  | 586 | int use_dir = use_directory(); | 
|---|
|  | 587 | int r=0; | 
|---|
|  | 588 | int refnum; | 
|---|
|  | 589 | int original_loc=0; | 
|---|
|  | 590 | if (use_dir) original_loc = tell(); | 
|---|
|  | 591 | int size_refnum; | 
|---|
|  | 592 | r += (size_refnum = get(refnum)); | 
|---|
|  | 593 | if (refnum == 0) { | 
|---|
|  | 594 | // reference to null | 
|---|
|  | 595 | #if DEBUG | 
|---|
|  | 596 | ExEnv::outn() << indent << "getting null object" << endl; | 
|---|
|  | 597 | #endif | 
|---|
|  | 598 | p = 0; | 
|---|
|  | 599 | } | 
|---|
|  | 600 | else { | 
|---|
|  | 601 | #if DEBUG | 
|---|
|  | 602 | ExEnv::outn() << indent << "getting object number " << setw(2) | 
|---|
|  | 603 | << refnum << endl; | 
|---|
|  | 604 | ExEnv::outn() << incindent; | 
|---|
|  | 605 | #endif | 
|---|
|  | 606 | std::map<int,StateInData>::iterator ind = ps_.find(refnum); | 
|---|
|  | 607 | if (ind == ps_.end() && use_dir) { | 
|---|
|  | 608 | ExEnv::errn() << "ERROR: StateIn: directory missing object number " | 
|---|
|  | 609 | << refnum << endl; | 
|---|
|  | 610 | abort(); | 
|---|
|  | 611 | } | 
|---|
|  | 612 | if (ind == ps_.end() || ind->second.ptr.null()) { | 
|---|
|  | 613 | #if DEBUG | 
|---|
|  | 614 | ExEnv::outn() << indent << "reading object" << endl; | 
|---|
|  | 615 | #endif | 
|---|
|  | 616 | // object has not yet been read in | 
|---|
|  | 617 | int need_seek = 0; | 
|---|
|  | 618 | if (use_dir) { | 
|---|
|  | 619 | if (original_loc != ind->second.offset) { | 
|---|
|  | 620 | need_seek = 1; | 
|---|
|  | 621 | original_loc = tell(); | 
|---|
|  | 622 | #if DEBUG | 
|---|
|  | 623 | ExEnv::outn() << indent << "seeking to" | 
|---|
|  | 624 | << setw(5) << ind->second.offset << endl; | 
|---|
|  | 625 | #endif | 
|---|
|  | 626 | seek(ind->second.offset); | 
|---|
|  | 627 | int trefnum; | 
|---|
|  | 628 | get(trefnum); | 
|---|
|  | 629 | if (trefnum != refnum) { | 
|---|
|  | 630 | ExEnv::errn() << "StateIn: didn't find expected reference"<<endl; | 
|---|
|  | 631 | abort(); | 
|---|
|  | 632 | } | 
|---|
|  | 633 | } | 
|---|
|  | 634 | } | 
|---|
|  | 635 | const ClassDesc *cd; | 
|---|
|  | 636 | r += get(&cd); | 
|---|
|  | 637 | have_classdesc(); | 
|---|
|  | 638 | nextobject(refnum); | 
|---|
|  | 639 | DescribedClass *dc = cd->create(*this); | 
|---|
|  | 640 | p = dynamic_cast<SavableState*>(dc); | 
|---|
|  | 641 | if (use_dir) { | 
|---|
|  | 642 | ind->second.ptr = p; | 
|---|
|  | 643 | if (need_seek) seek(original_loc); | 
|---|
|  | 644 | } | 
|---|
|  | 645 | #if DEBUG | 
|---|
|  | 646 | ExEnv::outn() << indent << "got object with type = " | 
|---|
|  | 647 | << p->class_name() << endl; | 
|---|
|  | 648 | #endif | 
|---|
|  | 649 | } | 
|---|
|  | 650 | else { | 
|---|
|  | 651 | // object already exists | 
|---|
|  | 652 | p = ind->second.ptr; | 
|---|
|  | 653 | #if DEBUG | 
|---|
|  | 654 | ExEnv::outn() << indent << "object already existed, type = " | 
|---|
|  | 655 | << p->class_name() << endl; | 
|---|
|  | 656 | ExEnv::outn() << indent | 
|---|
|  | 657 | << "  use_dir = " << use_dir | 
|---|
|  | 658 | << " tell() = " << setw(5) << tell() | 
|---|
|  | 659 | << " offset = " << setw(5) << ind->second.offset | 
|---|
|  | 660 | << " size_refnum = " << setw(1) << size_refnum | 
|---|
|  | 661 | << endl; | 
|---|
|  | 662 | #endif | 
|---|
|  | 663 | if (use_dir && tell() - size_refnum == ind->second.offset) { | 
|---|
|  | 664 | seek(tell() - size_refnum + ind->second.size); | 
|---|
|  | 665 | #if DEBUG | 
|---|
|  | 666 | ExEnv::outn() << indent << "  seeking to " | 
|---|
|  | 667 | << tell() - size_refnum + ind->second.offset | 
|---|
|  | 668 | << endl; | 
|---|
|  | 669 | #endif | 
|---|
|  | 670 | } | 
|---|
|  | 671 | } | 
|---|
|  | 672 | #if DEBUG | 
|---|
|  | 673 | ExEnv::outn() << decindent; | 
|---|
|  | 674 | #endif | 
|---|
|  | 675 | } | 
|---|
|  | 676 | return r; | 
|---|
|  | 677 | } | 
|---|
|  | 678 |  | 
|---|
|  | 679 | void | 
|---|
|  | 680 | StateIn::nextobject(int objnum) | 
|---|
|  | 681 | { | 
|---|
|  | 682 | expected_object_num_ = objnum; | 
|---|
|  | 683 | } | 
|---|
|  | 684 |  | 
|---|
|  | 685 | void | 
|---|
|  | 686 | StateIn::haveobject(const Ref<SavableState> &p) | 
|---|
|  | 687 | { | 
|---|
|  | 688 | if (expected_object_num_) { | 
|---|
|  | 689 | haveobject(expected_object_num_,p); | 
|---|
|  | 690 | expected_object_num_ = 0; | 
|---|
|  | 691 | } | 
|---|
|  | 692 | } | 
|---|
|  | 693 |  | 
|---|
|  | 694 | void | 
|---|
|  | 695 | StateIn::haveobject(int objnum,const Ref<SavableState> &p) | 
|---|
|  | 696 | { | 
|---|
|  | 697 | std::map<int,StateInData>::iterator ind = ps_.find(objnum); | 
|---|
|  | 698 | if (ind == ps_.end()) { | 
|---|
|  | 699 | ps_[objnum].ptr = p; | 
|---|
|  | 700 | #if DEBUG | 
|---|
|  | 701 | ExEnv::outn() << indent << "have object adding number " << objnum << endl; | 
|---|
|  | 702 | #endif | 
|---|
|  | 703 | } | 
|---|
|  | 704 | else { | 
|---|
|  | 705 | ind->second.ptr = p; | 
|---|
|  | 706 | #if DEBUG | 
|---|
|  | 707 | ExEnv::outn() << indent << "have object updating number " << objnum | 
|---|
|  | 708 | << endl; | 
|---|
|  | 709 | #endif | 
|---|
|  | 710 | } | 
|---|
|  | 711 | } | 
|---|
|  | 712 |  | 
|---|
|  | 713 | void | 
|---|
|  | 714 | StateIn::get_header() | 
|---|
|  | 715 | { | 
|---|
|  | 716 | const char *magic = "\001SCSO\002"; | 
|---|
|  | 717 | char tmp[7]; | 
|---|
|  | 718 | get_array_char(tmp,6); | 
|---|
|  | 719 | tmp[6] = '\0'; | 
|---|
|  | 720 | if (strcmp(tmp,magic)) { | 
|---|
|  | 721 | ExEnv::errn() << "StateIn: bad magic number" << endl; | 
|---|
|  | 722 | abort(); | 
|---|
|  | 723 | } | 
|---|
|  | 724 |  | 
|---|
|  | 725 | get_array_char(&format_,1); | 
|---|
|  | 726 | // switch to the new format | 
|---|
|  | 727 | if (translate_->translator()->format_code() != format_) { | 
|---|
|  | 728 | delete translate_; | 
|---|
|  | 729 | translate_ = new TranslateDataIn(this,TranslateData::vctor(format_)); | 
|---|
|  | 730 | } | 
|---|
|  | 731 |  | 
|---|
|  | 732 | get_array_int(&version_,1); | 
|---|
|  | 733 |  | 
|---|
|  | 734 | get_array_char(userid_,9); | 
|---|
|  | 735 |  | 
|---|
|  | 736 | get_array_int(&date_,1); | 
|---|
|  | 737 |  | 
|---|
|  | 738 | // get the directory location | 
|---|
|  | 739 | get_array_int(&dir_loc_,1); | 
|---|
|  | 740 | if (dir_loc_ == -1) { | 
|---|
|  | 741 | ExEnv::errn() << "ERROR: StateIn: directory corrupted" << endl; | 
|---|
|  | 742 | abort(); | 
|---|
|  | 743 | } | 
|---|
|  | 744 | } | 
|---|
|  | 745 |  | 
|---|
|  | 746 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 747 |  | 
|---|
|  | 748 | StateClassData::~StateClassData() | 
|---|
|  | 749 | { | 
|---|
|  | 750 | delete[] name; | 
|---|
|  | 751 | } | 
|---|
|  | 752 |  | 
|---|
|  | 753 | StateClassData & | 
|---|
|  | 754 | StateClassData::operator=(const StateClassData &d) | 
|---|
|  | 755 | { | 
|---|
|  | 756 | version = d.version; | 
|---|
|  | 757 | classdesc = d.classdesc; | 
|---|
|  | 758 | ninstance = d.ninstance; | 
|---|
|  | 759 | if (d.name) name = strcpy(new char[strlen(d.name)+1], d.name); | 
|---|
|  | 760 | else name = 0; | 
|---|
|  | 761 | return *this; | 
|---|
|  | 762 | } | 
|---|
|  | 763 |  | 
|---|
|  | 764 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 765 |  | 
|---|
|  | 766 | // Local Variables: | 
|---|
|  | 767 | // mode: c++ | 
|---|
|  | 768 | // c-file-style: "CLJ" | 
|---|
|  | 769 | // End: | 
|---|