| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef VMDATASRC_H |
| 19 |
#define VMDATASRC_H |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
#include "t3std.h" |
| 23 |
#include "osifcnet.h" |
| 24 |
#include "vmerr.h" |
| 25 |
#include "vmfile.h" |
| 26 |
#include "vmerrnum.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class CVmDataSource |
| 35 |
{ |
| 36 |
public: |
| 37 |
virtual ~CVmDataSource() { } |
| 38 |
|
| 39 |
|
| 40 |
virtual int read(void *buf, size_t len) = 0; |
| 41 |
|
| 42 |
|
| 43 |
virtual int readc(void *buf, size_t len) = 0; |
| 44 |
|
| 45 |
|
| 46 |
virtual int write(const void *buf, size_t len) = 0; |
| 47 |
|
| 48 |
|
| 49 |
virtual int writez(const char *str) { return write(str, strlen(str)); } |
| 50 |
|
| 51 |
|
| 52 |
virtual long get_size() = 0; |
| 53 |
|
| 54 |
|
| 55 |
virtual long get_pos() = 0; |
| 56 |
|
| 57 |
|
| 58 |
virtual int seek(long ofs, int mode) = 0; |
| 59 |
|
| 60 |
|
| 61 |
virtual int flush() = 0; |
| 62 |
|
| 63 |
|
| 64 |
virtual void close() = 0; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
virtual char *read_line(char *buf, size_t len) |
| 73 |
{ |
| 74 |
|
| 75 |
if (get_pos() >= get_size()) |
| 76 |
return 0; |
| 77 |
|
| 78 |
|
| 79 |
char *dst = buf; |
| 80 |
while (len > 0 && get_pos() < get_size()) |
| 81 |
{ |
| 82 |
|
| 83 |
char ch; |
| 84 |
if (read(&ch, 1)) |
| 85 |
return 0; |
| 86 |
|
| 87 |
|
| 88 |
if (ch == 10 || ch == 13) |
| 89 |
{ |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
*dst++ = '\n'; |
| 96 |
--len; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
char ch2; |
| 103 |
if (ch == 13 |
| 104 |
&& get_pos() < get_size() |
| 105 |
&& !read(&ch2, 1) |
| 106 |
&& ch2 != 10) |
| 107 |
{ |
| 108 |
|
| 109 |
seek(get_pos() - 1, OSFSK_SET); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
*dst++ = ch; |
| 118 |
|
| 119 |
|
| 120 |
--len; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
if (len != 0) |
| 125 |
*dst = '\0'; |
| 126 |
|
| 127 |
|
| 128 |
return buf; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
char *read_line_alo() |
| 133 |
{ |
| 134 |
char *ret = 0; |
| 135 |
size_t retlen = 0; |
| 136 |
|
| 137 |
|
| 138 |
for (;;) |
| 139 |
{ |
| 140 |
|
| 141 |
char buf[1024]; |
| 142 |
if (read_line(buf, sizeof(buf)) == 0) |
| 143 |
break; |
| 144 |
|
| 145 |
|
| 146 |
size_t len = strlen(buf); |
| 147 |
|
| 148 |
|
| 149 |
if (ret == 0) |
| 150 |
ret = (char *)t3malloc(len + 1); |
| 151 |
else |
| 152 |
ret = (char *)t3realloc(ret, retlen + len + 1); |
| 153 |
|
| 154 |
|
| 155 |
if (ret == 0) |
| 156 |
return 0; |
| 157 |
|
| 158 |
|
| 159 |
memcpy(ret + retlen, buf, len + 1); |
| 160 |
retlen += len; |
| 161 |
|
| 162 |
|
| 163 |
if (retlen != 0 && ret[retlen-1] == '\n') |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
virtual CVmDataSource *clone(VMG_ const char *mode) = 0; |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
class CVmFileSource: public CVmDataSource |
| 190 |
{ |
| 191 |
public: |
| 192 |
CVmFileSource(osfildef *fp) { this->fp = fp; } |
| 193 |
~CVmFileSource() |
| 194 |
{ |
| 195 |
if (fp != 0) |
| 196 |
close(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
virtual int read(void *buf, size_t len) { return osfrb(fp, buf, len); } |
| 201 |
|
| 202 |
|
| 203 |
virtual int readc(void *buf, size_t len) { return osfrbc(fp, buf, len); } |
| 204 |
|
| 205 |
|
| 206 |
virtual char *read_line(char *buf, size_t len) |
| 207 |
{ return osfgets(buf, len, fp); } |
| 208 |
|
| 209 |
|
| 210 |
virtual int write(const void *buf, size_t len) |
| 211 |
{ return osfwb(fp, buf, len); } |
| 212 |
|
| 213 |
|
| 214 |
virtual long get_size() |
| 215 |
{ |
| 216 |
|
| 217 |
long oldpos = osfpos(fp); |
| 218 |
|
| 219 |
|
| 220 |
osfseek(fp, 0, OSFSK_END); |
| 221 |
|
| 222 |
|
| 223 |
long siz = osfpos(fp); |
| 224 |
|
| 225 |
|
| 226 |
osfseek(fp, oldpos, OSFSK_SET); |
| 227 |
|
| 228 |
|
| 229 |
return siz; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
virtual long get_pos() { return osfpos(fp); } |
| 234 |
|
| 235 |
|
| 236 |
virtual int seek(long ofs, int mode) { return osfseek(fp, ofs, mode); } |
| 237 |
|
| 238 |
|
| 239 |
virtual int flush() { return osfflush(fp); } |
| 240 |
|
| 241 |
|
| 242 |
virtual void close() |
| 243 |
{ |
| 244 |
osfcls(fp); |
| 245 |
fp = 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
virtual CVmDataSource *clone(VMG_ const char *mode) |
| 250 |
{ |
| 251 |
|
| 252 |
osfildef *fpdup = osfdup(fp, mode); |
| 253 |
if (fpdup == 0) |
| 254 |
return 0; |
| 255 |
|
| 256 |
|
| 257 |
return new CVmFileSource(fpdup); |
| 258 |
} |
| 259 |
|
| 260 |
protected: |
| 261 |
|
| 262 |
osfildef *fp; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
class CVmResFileSource: public CVmDataSource |
| 272 |
{ |
| 273 |
public: |
| 274 |
CVmResFileSource(osfildef *fp, unsigned long start, long end) |
| 275 |
{ |
| 276 |
this->fp = fp; |
| 277 |
this->start = start; |
| 278 |
this->end = end; |
| 279 |
} |
| 280 |
~CVmResFileSource() |
| 281 |
{ |
| 282 |
if (fp != 0) |
| 283 |
close(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
virtual CVmDataSource *clone(VMG_ const char *mode) |
| 288 |
{ |
| 289 |
|
| 290 |
osfildef *fpdup = osfdup(fp, mode); |
| 291 |
if (fpdup == 0) |
| 292 |
return 0; |
| 293 |
|
| 294 |
|
| 295 |
return new CVmResFileSource(fpdup, start, end); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
virtual int read(void *buf, size_t len) |
| 300 |
{ |
| 301 |
|
| 302 |
unsigned long pos = osfpos(fp); |
| 303 |
if (pos + len > end) |
| 304 |
return 1; |
| 305 |
|
| 306 |
|
| 307 |
return osfrb(fp, buf, len); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
virtual char *read_line(char *buf, size_t len) |
| 312 |
{ |
| 313 |
|
| 314 |
unsigned long pos = osfpos(fp); |
| 315 |
if (pos >= end) |
| 316 |
return 0; |
| 317 |
|
| 318 |
|
| 319 |
char *ret = osfgets(buf, len, fp); |
| 320 |
if (ret == 0) |
| 321 |
return 0; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if ((unsigned long)osfpos(fp) > end) |
| 328 |
{ |
| 329 |
|
| 330 |
osfseek(fp, pos, OSFSK_SET); |
| 331 |
|
| 332 |
|
| 333 |
size_t r = end - pos > len ? (long)len : (size_t)(end - pos); |
| 334 |
if (osfrb(fp, buf, r)) |
| 335 |
return 0; |
| 336 |
|
| 337 |
|
| 338 |
if (r < len) |
| 339 |
buf[r] = '\0'; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
return buf; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
virtual int readc(void *buf, size_t len) |
| 348 |
{ |
| 349 |
|
| 350 |
unsigned long pos = osfpos(fp); |
| 351 |
if (pos >= end) |
| 352 |
return 0; |
| 353 |
|
| 354 |
|
| 355 |
unsigned long limit = end - pos; |
| 356 |
if (len > limit) |
| 357 |
len = (size_t)limit; |
| 358 |
|
| 359 |
|
| 360 |
return osfrbc(fp, buf, len); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
virtual int write(const void *buf, size_t len) { return 1; } |
| 365 |
|
| 366 |
|
| 367 |
virtual long get_size() { return end - start; } |
| 368 |
|
| 369 |
|
| 370 |
virtual long get_pos() { return osfpos(fp) - start; } |
| 371 |
|
| 372 |
|
| 373 |
virtual int seek(long ofs, int mode) |
| 374 |
{ |
| 375 |
switch (mode) |
| 376 |
{ |
| 377 |
case OSFSK_SET: |
| 378 |
do_set: |
| 379 |
|
| 380 |
if (ofs < 0 || (unsigned long)ofs > end - start) |
| 381 |
return 1; |
| 382 |
|
| 383 |
|
| 384 |
return osfseek(fp, start + ofs, OSFSK_SET); |
| 385 |
|
| 386 |
case OSFSK_CUR: |
| 387 |
|
| 388 |
ofs += get_pos(); |
| 389 |
goto do_set; |
| 390 |
|
| 391 |
case OSFSK_END: |
| 392 |
|
| 393 |
ofs += (end - start); |
| 394 |
goto do_set; |
| 395 |
|
| 396 |
default: |
| 397 |
|
| 398 |
return 1; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
virtual int flush() { return 0; } |
| 404 |
|
| 405 |
|
| 406 |
virtual void close() |
| 407 |
{ |
| 408 |
osfcls(fp); |
| 409 |
fp = 0; |
| 410 |
} |
| 411 |
|
| 412 |
protected: |
| 413 |
|
| 414 |
osfildef *fp; |
| 415 |
|
| 416 |
|
| 417 |
unsigned long start, end; |
| 418 |
}; |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
class CVmStringSource: public CVmDataSource |
| 427 |
{ |
| 428 |
public: |
| 429 |
CVmStringSource(const char *mem, long len) |
| 430 |
{ |
| 431 |
this->mem = mem; |
| 432 |
this->memlen = len; |
| 433 |
this->pos = 0; |
| 434 |
} |
| 435 |
|
| 436 |
virtual ~CVmStringSource() { } |
| 437 |
|
| 438 |
virtual CVmDataSource *clone(VMG_ const char * ) |
| 439 |
{ return new CVmStringSource(mem, memlen); } |
| 440 |
|
| 441 |
|
| 442 |
virtual int read(void *buf, size_t len) |
| 443 |
{ |
| 444 |
|
| 445 |
if (pos + (long)len > memlen) |
| 446 |
return 1; |
| 447 |
|
| 448 |
|
| 449 |
memcpy(buf, mem + pos, len); |
| 450 |
|
| 451 |
|
| 452 |
pos += len; |
| 453 |
|
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
virtual int readc(void *buf, size_t len) |
| 460 |
{ |
| 461 |
|
| 462 |
long rem = memlen - pos; |
| 463 |
if ((long)len > rem) |
| 464 |
len = rem; |
| 465 |
|
| 466 |
|
| 467 |
memcpy(buf, mem + pos, len); |
| 468 |
|
| 469 |
|
| 470 |
pos += len; |
| 471 |
|
| 472 |
|
| 473 |
return (int)len; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
virtual char *read_line(char *buf, size_t len) |
| 478 |
{ |
| 479 |
|
| 480 |
if (pos == memlen) |
| 481 |
return 0; |
| 482 |
|
| 483 |
|
| 484 |
char *dst = buf; |
| 485 |
while (pos < memlen && len > 0) |
| 486 |
{ |
| 487 |
|
| 488 |
if (mem[pos] == 10 || mem[pos] == 13) |
| 489 |
{ |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
*dst++ = '\n'; |
| 496 |
--len; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
++pos; |
| 503 |
if (pos < memlen && mem[pos-1] == 13 && mem[pos] == 10) |
| 504 |
++pos; |
| 505 |
|
| 506 |
|
| 507 |
break; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
*dst++ = mem[pos++]; |
| 512 |
|
| 513 |
|
| 514 |
--len; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
if (len != 0) |
| 519 |
*dst = '\0'; |
| 520 |
|
| 521 |
|
| 522 |
return buf; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
virtual int write(const void *buf, size_t len) { return 1; } |
| 527 |
|
| 528 |
|
| 529 |
virtual long get_size() { return memlen; } |
| 530 |
|
| 531 |
|
| 532 |
virtual long get_pos() { return pos; } |
| 533 |
|
| 534 |
|
| 535 |
virtual int seek(long ofs, int mode) |
| 536 |
{ |
| 537 |
switch (mode) |
| 538 |
{ |
| 539 |
case OSFSK_SET: |
| 540 |
do_set: |
| 541 |
|
| 542 |
if (ofs < 0 || ofs > memlen) |
| 543 |
return 1; |
| 544 |
|
| 545 |
|
| 546 |
pos = ofs; |
| 547 |
|
| 548 |
|
| 549 |
return 0; |
| 550 |
|
| 551 |
case OSFSK_CUR: |
| 552 |
|
| 553 |
ofs += pos; |
| 554 |
goto do_set; |
| 555 |
|
| 556 |
case OSFSK_END: |
| 557 |
|
| 558 |
ofs += memlen; |
| 559 |
goto do_set; |
| 560 |
|
| 561 |
default: |
| 562 |
|
| 563 |
return 1; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
virtual int flush() { return 0; } |
| 569 |
|
| 570 |
|
| 571 |
virtual void close() { } |
| 572 |
|
| 573 |
protected: |
| 574 |
|
| 575 |
const char *mem; |
| 576 |
long memlen; |
| 577 |
|
| 578 |
|
| 579 |
long pos; |
| 580 |
}; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
class CVmPrivateStringSource: public CVmStringSource |
| 588 |
{ |
| 589 |
public: |
| 590 |
CVmPrivateStringSource(const char *mem, long len) |
| 591 |
: CVmStringSource(0, len) |
| 592 |
{ |
| 593 |
this->bufobj = new CVmRefCntBuf(mem, len); |
| 594 |
this->mem = bufobj->buf_; |
| 595 |
} |
| 596 |
|
| 597 |
CVmPrivateStringSource(CVmRefCntBuf *bufobj, long len) |
| 598 |
: CVmStringSource(bufobj->buf_, len) |
| 599 |
{ |
| 600 |
this->bufobj = bufobj; |
| 601 |
} |
| 602 |
|
| 603 |
CVmDataSource *clone(VMG_ const char * ) |
| 604 |
{ return new CVmPrivateStringSource(bufobj, memlen); } |
| 605 |
|
| 606 |
~CVmPrivateStringSource() |
| 607 |
{ |
| 608 |
bufobj->release_ref(); |
| 609 |
} |
| 610 |
|
| 611 |
protected: |
| 612 |
CVmRefCntBuf *bufobj; |
| 613 |
}; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
struct CVmMemorySourceBlock |
| 624 |
{ |
| 625 |
CVmMemorySourceBlock(long ofs) |
| 626 |
{ |
| 627 |
this->ofs = ofs; |
| 628 |
nxt = 0; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
static const int BlockLen = 4096; |
| 633 |
|
| 634 |
|
| 635 |
CVmMemorySourceBlock *nxt; |
| 636 |
|
| 637 |
|
| 638 |
long ofs; |
| 639 |
|
| 640 |
|
| 641 |
char buf[BlockLen]; |
| 642 |
}; |
| 643 |
|
| 644 |
|
| 645 |
class CVmMemorySourceList: public CVmRefCntObj |
| 646 |
{ |
| 647 |
public: |
| 648 |
static const int BlockLen = CVmMemorySourceBlock::BlockLen; |
| 649 |
|
| 650 |
CVmMemorySourceList(long init_len) |
| 651 |
{ |
| 652 |
|
| 653 |
first_block_ = last_block_ = 0; |
| 654 |
|
| 655 |
|
| 656 |
add_block(); |
| 657 |
init_len -= BlockLen; |
| 658 |
|
| 659 |
|
| 660 |
cur_block_ = first_block_; |
| 661 |
cur_block_ofs_ = 0; |
| 662 |
|
| 663 |
|
| 664 |
for ( ; init_len > 0 ; init_len -= BlockLen) |
| 665 |
add_block(); |
| 666 |
|
| 667 |
|
| 668 |
len_ = 0; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
int read(void *buf, size_t len) |
| 673 |
{ |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
if (cur_block_->ofs + cur_block_ofs_ + (long)len > len_) |
| 679 |
return 1; |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
return (size_t)readc(buf, len) != len; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
int readc(void *buf0, size_t len) |
| 690 |
{ |
| 691 |
|
| 692 |
char *buf = (char *)buf0; |
| 693 |
|
| 694 |
|
| 695 |
size_t bytes_read = 0; |
| 696 |
|
| 697 |
|
| 698 |
long rem = len_ - get_pos(); |
| 699 |
if ((long)len > rem) |
| 700 |
len = (size_t)rem; |
| 701 |
|
| 702 |
|
| 703 |
while (len != 0) |
| 704 |
{ |
| 705 |
|
| 706 |
size_t rem = BlockLen - cur_block_ofs_; |
| 707 |
size_t cur = (len < rem ? len : rem); |
| 708 |
|
| 709 |
|
| 710 |
if (cur != 0) |
| 711 |
{ |
| 712 |
|
| 713 |
memcpy(buf, cur_block_->buf + cur_block_ofs_, cur); |
| 714 |
|
| 715 |
|
| 716 |
cur_block_ofs_ += cur; |
| 717 |
|
| 718 |
|
| 719 |
len -= cur; |
| 720 |
buf += cur; |
| 721 |
|
| 722 |
|
| 723 |
bytes_read += cur; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
if (len == 0) |
| 728 |
break; |
| 729 |
|
| 730 |
|
| 731 |
if (cur_block_->nxt == 0) |
| 732 |
return bytes_read; |
| 733 |
|
| 734 |
|
| 735 |
cur_block_ = cur_block_->nxt; |
| 736 |
cur_block_ofs_ = 0; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
return bytes_read; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
int write(const void *buf0, size_t len) |
| 745 |
{ |
| 746 |
|
| 747 |
const char *buf = (const char *)buf0; |
| 748 |
|
| 749 |
|
| 750 |
for (;;) |
| 751 |
{ |
| 752 |
|
| 753 |
size_t rem = BlockLen - cur_block_ofs_; |
| 754 |
size_t cur = (len < rem ? len : rem); |
| 755 |
|
| 756 |
|
| 757 |
if (cur != 0) |
| 758 |
{ |
| 759 |
memcpy(cur_block_->buf + cur_block_ofs_, buf, cur); |
| 760 |
cur_block_ofs_ += cur; |
| 761 |
len -= cur; |
| 762 |
buf += cur; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
if (len == 0) |
| 767 |
break; |
| 768 |
|
| 769 |
|
| 770 |
if (cur_block_->nxt == 0) |
| 771 |
{ |
| 772 |
|
| 773 |
add_block(); |
| 774 |
|
| 775 |
|
| 776 |
if (cur_block_->nxt == 0) |
| 777 |
return 1; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
cur_block_ = cur_block_->nxt; |
| 782 |
cur_block_ofs_ = 0; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
if (cur_block_->ofs + cur_block_ofs_ > len_) |
| 791 |
len_ = cur_block_->ofs + cur_block_ofs_; |
| 792 |
|
| 793 |
|
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
long get_size() { return len_; } |
| 799 |
|
| 800 |
|
| 801 |
long get_pos() |
| 802 |
{ |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
return cur_block_->ofs + cur_block_ofs_; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
int seek(long pos, int mode) |
| 812 |
{ |
| 813 |
|
| 814 |
switch (mode) |
| 815 |
{ |
| 816 |
case OSFSK_SET: |
| 817 |
|
| 818 |
break; |
| 819 |
|
| 820 |
case OSFSK_CUR: |
| 821 |
|
| 822 |
pos += get_pos(); |
| 823 |
break; |
| 824 |
|
| 825 |
case OSFSK_END: |
| 826 |
|
| 827 |
pos += get_size(); |
| 828 |
break; |
| 829 |
|
| 830 |
default: |
| 831 |
|
| 832 |
return 1; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
if (pos < 0) |
| 837 |
{ |
| 838 |
|
| 839 |
pos = 0; |
| 840 |
} |
| 841 |
else if (pos >= len_) |
| 842 |
{ |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
pos = len_; |
| 850 |
cur_block_ = last_block_; |
| 851 |
cur_block_ofs_ = pos - last_block_->ofs; |
| 852 |
|
| 853 |
|
| 854 |
return 0; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
CVmMemorySourceBlock *b; |
| 859 |
for (b = first_block_ ; b != 0 ; b = b->nxt) |
| 860 |
{ |
| 861 |
|
| 862 |
if (pos >= b->ofs && pos < b->ofs + BlockLen) |
| 863 |
{ |
| 864 |
|
| 865 |
cur_block_ = b; |
| 866 |
cur_block_ofs_ = pos - b->ofs; |
| 867 |
|
| 868 |
|
| 869 |
break; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
return 0; |
| 875 |
} |
| 876 |
|
| 877 |
protected: |
| 878 |
~CVmMemorySourceList() |
| 879 |
{ |
| 880 |
|
| 881 |
CVmMemorySourceBlock *cur, *nxt; |
| 882 |
for (cur = first_block_ ; cur != 0 ; cur = nxt) |
| 883 |
{ |
| 884 |
nxt = cur->nxt; |
| 885 |
delete cur; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
void add_block() |
| 891 |
{ |
| 892 |
long ofs = last_block_ == 0 ? 0 : last_block_->ofs + BlockLen; |
| 893 |
CVmMemorySourceBlock *b = new CVmMemorySourceBlock(ofs); |
| 894 |
|
| 895 |
if (last_block_ != 0) |
| 896 |
last_block_->nxt = b; |
| 897 |
else |
| 898 |
first_block_ = b; |
| 899 |
last_block_ = b; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
CVmMemorySourceBlock *cur_block_; |
| 904 |
long cur_block_ofs_; |
| 905 |
|
| 906 |
|
| 907 |
long len_; |
| 908 |
|
| 909 |
|
| 910 |
CVmMemorySourceBlock *first_block_; |
| 911 |
CVmMemorySourceBlock *last_block_; |
| 912 |
}; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
class CVmMemorySource: public CVmDataSource |
| 918 |
{ |
| 919 |
public: |
| 920 |
CVmMemorySource(long init_len) |
| 921 |
{ |
| 922 |
|
| 923 |
pl = new CVmMemorySourceList(init_len); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
CVmDataSource *clone(VMG_ const char * ) |
| 928 |
{ |
| 929 |
|
| 930 |
return new CVmMemorySource(pl); |
| 931 |
} |
| 932 |
|
| 933 |
~CVmMemorySource() |
| 934 |
{ |
| 935 |
|
| 936 |
pl->release_ref(); |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
virtual int read(void *buf, size_t len) { return pl->read(buf, len); } |
| 941 |
|
| 942 |
|
| 943 |
virtual int readc(void *buf, size_t len) { return pl->readc(buf, len); } |
| 944 |
|
| 945 |
|
| 946 |
virtual int write(const void *buf, size_t len) |
| 947 |
{ return pl->write(buf, len); } |
| 948 |
|
| 949 |
|
| 950 |
virtual long get_size() { return pl->get_size(); } |
| 951 |
|
| 952 |
|
| 953 |
virtual long get_pos() { return pl->get_pos(); } |
| 954 |
|
| 955 |
|
| 956 |
virtual int seek(long pos, int mode) { return pl->seek(pos, mode); } |
| 957 |
|
| 958 |
|
| 959 |
virtual int flush() { return 0; } |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
virtual void close() { } |
| 966 |
|
| 967 |
protected: |
| 968 |
CVmMemorySource(CVmMemorySourceList *pl) |
| 969 |
{ |
| 970 |
pl->add_ref(); |
| 971 |
this->pl = pl; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
CVmMemorySourceList *pl; |
| 976 |
}; |
| 977 |
|
| 978 |
|
| 979 |
#endif |