1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
|
/*
* HT Editor
* textfile.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "except.h"
#include "htdebug.h"
#include "textfile.h"
#include "tools.h"
#define TEXTFILE_READSIZE 256
#define TEXTFILE_MAX_LINELEN 512
#define TEXTFILE_MAX_LINEENDLEN 2
//#define TEXTFILE_MAX_UPDATE_PARSE_STEPS 64
/*
* CLASS ht_textfile
*/
ht_textfile::ht_textfile(File *file, bool own_file)
: FileLayer(file, own_file)
{
}
/*
* CLASS ht_layer_textfile
*/
ht_layer_textfile::ht_layer_textfile(ht_textfile *textfile, bool own_textfile)
: ht_textfile(textfile, own_textfile)
{
}
bool ht_layer_textfile::convert_ofs2line(FileOfs o, uint *line, uint *pofs) const
{
return ((ht_textfile*)mFile)->convert_ofs2line(o, line, pofs);
}
bool ht_layer_textfile::convert_line2ofs(uint line, uint pofs, FileOfs *o) const
{
return ((ht_textfile*)mFile)->convert_line2ofs(line, pofs, o);
}
void ht_layer_textfile::delete_lines(uint line, uint count)
{
((ht_textfile*)mFile)->delete_lines(line, count);
}
void ht_layer_textfile::delete_chars(uint line, uint ofs, uint count)
{
((ht_textfile*)mFile)->delete_chars(line, ofs, count);
}
bool ht_layer_textfile::get_char(uint line, char *ch, uint pos)
{
return ((ht_textfile*)mFile)->get_char(line, ch, pos);
}
bool ht_layer_textfile::getline(uint line, uint pofs, void *buf, uint buflen, uint *retlen, lexer_state *state)
{
return ((ht_textfile*)mFile)->getline(line, pofs, buf, buflen, retlen, state);
}
uint ht_layer_textfile::getlinelength(uint line) const
{
return ((ht_textfile*)mFile)->getlinelength(line);
}
void ht_layer_textfile::insert_lines(uint before, uint count, void **line_ends, int *line_end_lens)
{
((ht_textfile*)mFile)->insert_lines(before, count, line_ends, line_end_lens);
}
void ht_layer_textfile::insert_chars(uint line, uint ofs, void *chars, uint len)
{
((ht_textfile*)mFile)->insert_chars(line, ofs, chars, len);
}
bool ht_layer_textfile::has_line(uint line)
{
return ((ht_textfile*)mFile)->has_line(line);
}
uint ht_layer_textfile::linecount() const
{
return ((ht_textfile*)mFile)->linecount();
}
void ht_layer_textfile::set_layered_assume(File *s, bool ownNewLayered, bool changes_applied)
{
/* File *q = streamfile->get_layered();
if (q)*/ ((ht_textfile*)mFile)->set_layered_assume(s, ownNewLayered, changes_applied);
/* else
streamfile = s;*/
}
void ht_layer_textfile::set_lexer(ht_syntax_lexer *lexer)
{
((ht_textfile*)mFile)->set_lexer(lexer);
}
/*
* CLASS ht_ltextfile_line
*/
ht_ltextfile_line::~ht_ltextfile_line()
{
if (is_in_memory) free(in_memory.data);
}
/*
* CLASS ht_ltextfile
*/
ht_ltextfile::ht_ltextfile(File *streamfile, bool own_streamfile, ht_syntax_lexer *l)
: ht_textfile(streamfile, own_streamfile)
{
lexer = l;
lines = new Array(true);
orig_lines = new Array(true);
reread();
first_parse_dirty_line = linecount();
first_nofs_dirty_line = linecount();
dirty = false;
ofs = 0;
}
ht_ltextfile::~ht_ltextfile()
{
delete lines;
delete orig_lines;
}
void ht_ltextfile::cache_invd()
{
lines->delAll();
foreach(ht_ltextfile_line, l, *orig_lines, {
lines->insert(new ht_ltextfile_line(*l));
});
dirty_parse(0);
/* reread();*/
dirty = false;
}
void ht_ltextfile::cache_flush()
{
}
bool ht_ltextfile::convert_line2ofs(uint line, uint pofs, FileOfs *o) const
{
ht_ltextfile_line *x;
x=fetch_line_nofs_ok(line);
if (x) {
uint m=getlinelength_i(x);
if (pofs<m) {
*o=x->nofs+pofs;
} else {
*o=x->nofs+m;
}
return true;
}
return false;
}
bool ht_ltextfile::convert_ofs2line(FileOfs o, uint *line, uint *pofs) const
{
uint l=0, r=linecount();
if (!r) return 0; else r--;
uint m=0;
ht_ltextfile_line *x, *y;
while (l<=r) {
m=(l+r) / 2;
x=fetch_line_nofs_ok(m);
y=fetch_line_nofs_ok(m+1);
if ((x->nofs < o) && ((y && (y->nofs <= o)) || !y)) l=m+1; else
if (x->nofs > o) r=m-1; else break;
}
/* FIXME: debug */
x=fetch_line_nofs_ok(m);
if (x) assert(o>=x->nofs);
x=fetch_line_nofs_ok(m+1);
if (x) assert(o<x->nofs);
/**/
x=fetch_line_nofs_ok(m);
*line=m;
*pofs=o - x->nofs;
return true;
}
FileOfs ht_ltextfile::copyAllTo(Stream *stream)
{
/* uint c=linecount();
char buf[TEXTFILE_MAX_LINELEN+1];
for (uint i=0; i<c; i++) {
ht_ltextfile_line *e=fetch_line(i);
if (e) {
uint s;
getline(i, 0, buf, TEXTFILE_MAX_LINELEN+1, &s, NULL);
stream->write(buf, s);
stream->write(e->lineend, e->lineendlen);
}
}*/
#define STREAM_COPYBUF_SIZE (64*1024)
const uint bufsize=STREAM_COPYBUF_SIZE;
byte *buf = ht_malloc(bufsize);
uint r;
FileOfs res = 0;
do {
r = read(buf, bufsize);
stream->writex(buf, r);
res += r;
} while (r);
free(buf);
return res;
}
void ht_ltextfile::delete_chars(uint line, uint ofs, uint count)
{
ht_ltextfile_line *e=fetch_line_into_memory(line);
if (e) {
char *ostr = e->in_memory.data;
uint olen = e->in_memory.len;
if (ofs < olen) {
if (ofs+count>olen) count=olen-ofs;
char *nstr = ht_malloc(olen-count);
memcpy(nstr, ostr, ofs);
memcpy(nstr+ofs, ostr+ofs+count, olen-ofs-count);
free(ostr);
e->in_memory.data=nstr;
e->in_memory.len=olen-count;
dirty_parse(line+1);
dirty_nofs(line+1);
dirty=true;
}
}
}
void ht_ltextfile::delete_lines(uint line, uint count)
{
if (count) {
lines->delRange(line, line+count-1);
dirty_parse(line);
dirty_nofs(line);
dirty=true;
}
}
void ht_ltextfile::dirty_parse(uint line)
{
if (line<first_parse_dirty_line) first_parse_dirty_line=line;
}
void ht_ltextfile::dirty_nofs(uint line)
{
if (line<first_nofs_dirty_line) first_nofs_dirty_line=line;
}
void ht_ltextfile::extend(FileOfs newsize)
{
/* FIXME: nyi */
throw IOException(ENOSYS);
}
ht_ltextfile_line *ht_ltextfile::fetch_line(uint line) const
{
return (ht_ltextfile_line*)(*lines)[line];
}
ht_ltextfile_line *ht_ltextfile::fetch_line_nofs_ok(uint line) const
{
if (is_dirty_nofs(line)) update_nofs(line);
return fetch_line(line);
}
uint ht_ltextfile::find_linelen_forwd(byte *buf, uint maxbuflen, FileOfs ofs, int *le_len)
{
uint readlen=(maxbuflen>TEXTFILE_READSIZE) ? TEXTFILE_READSIZE : maxbuflen;
byte *bufp;
uint s;
uint len = 0;
if (le_len) *le_len = 0;
do {
mFile->seek(ofs);
s = mFile->read(buf, readlen);
int l;
bufp = match_lineend_forwd(buf, s, &l);
if (bufp) {
len += bufp-buf+l;
if (le_len) *le_len = l;
break;
}
if (s != readlen) {
len += s;
break;
}
/* make sure current and next read overlap
to guarantee proper lineend-matching */
if (s > (TEXTFILE_MAX_LINEENDLEN-1)) {
len += s-(TEXTFILE_MAX_LINEENDLEN-1);
}
ofs += s-(TEXTFILE_MAX_LINEENDLEN-1);
} while (s == readlen);
if (len > TEXTFILE_MAX_LINELEN) {
len = TEXTFILE_MAX_LINELEN;
if (le_len) *le_len = 0;
}
return len;
}
ht_ltextfile_line *ht_ltextfile::fetch_line_into_memory(uint line)
{
ht_ltextfile_line *e=fetch_line(line);
if (e) {
if (!e->is_in_memory) {
char *data = ht_malloc(e->on_disk.len);
mFile->seek(e->on_disk.ofs);
uint x=mFile->read(data, e->on_disk.len);
e->is_in_memory=true;
e->in_memory.data=data;
e->in_memory.len=x/*e->data.on_disk.len*/;
}
}
return e;
}
FileOfs ht_ltextfile::getSize() const
{
int line = linecount()-1;
FileOfs o = 0;
convert_line2ofs(line, getlinelength(line), &o);
return o;
}
bool ht_ltextfile::get_char(uint line, char *ch, uint pos)
{
ht_ltextfile_line *e=fetch_line(line);
if (e) {
if (e->is_in_memory) {
if (pos<e->in_memory.len) {
*ch=e->in_memory.data[pos];
return true;
}
} else {
if (pos<e->on_disk.len) {
mFile->seek(e->on_disk.ofs);
mFile->readx(ch, 1);
return true;
}
}
}
return false;
}
bool ht_ltextfile::getline(uint line, uint pofs, void *buf, uint buflen, uint *retlen, lexer_state *state)
{
/* <debug> */
/* bool debug=false;
if (line & 0x80000000) {
debug=true;
line&=0x7fffffff;
}*/
/* </debug> */
ht_ltextfile_line *e=fetch_line(line);
if (e) {
if (is_dirty_parse(line)) update_parse(line);
if (is_dirty_nofs(line)) update_nofs(line);
/* <debug> */
/* if (debug) {
buf+=sprintf(buf, "%5d:", e->nofs);
maxbuflen-=9;
}*/
/* </debug> */
uint ret;
if (e->is_in_memory) {
uint l=e->in_memory.len;
if (l>buflen-1) l=buflen-1;
if (pofs>l) l=0; else l-=pofs;
memcpy(buf, e->in_memory.data+pofs, l);
ret=l;
} else {
uint l=e->on_disk.len;
if (l>buflen-1) l=buflen-1;
if (pofs>l) l=0; else l-=pofs;
mFile->seek(e->on_disk.ofs+pofs);
mFile->read(buf, l);
ret=l;
}
if (state) *state=e->instate;
*retlen=ret;
return true;
}
return false;
}
uint ht_ltextfile::getlinelength(uint line) const
{
ht_ltextfile_line *e=fetch_line(line);
return getlinelength_i(e);
}
uint ht_ltextfile::getlinelength_i(ht_ltextfile_line *e) const
{
if (e) {
if (e->is_in_memory) {
return e->in_memory.len;
} else {
return e->on_disk.len;
}
}
return 0;
}
void ht_ltextfile::insert_lines(uint before, uint count, void **line_ends, int *line_end_lens)
{
ht_ltextfile_line *e = fetch_line(before);
int instate = e->instate;
while (count--) {
e = new ht_ltextfile_line();
e->is_in_memory = true;
e->instate = instate;
e->on_disk.ofs = 0xffffffff;
e->on_disk.len = 0;
e->in_memory.data = ht_malloc(1);
e->in_memory.len = 0;
e->nofs = 0;
if (line_ends && line_end_lens) {
e->lineendlen = *line_end_lens++;
memcpy(e->lineend, *line_ends++, e->lineendlen);
} else {
e->lineendlen = 1;
e->lineend[0] = '\n';
}
lines->insertBefore(before, e);
}
dirty_parse(before);
dirty_nofs(before);
dirty = true;
}
void ht_ltextfile::insert_chars(uint line, uint ofs, void *chars, uint len)
{
ht_ltextfile_line *e=fetch_line(line);
if (e) {
uint olen=e->is_in_memory ? e->in_memory.len : e->on_disk.len;
if (ofs<=olen) {
e=fetch_line_into_memory(line);
char *ostr=e->in_memory.data;
char *nstr;
uint nlen=olen;
uint clen=len;
if (ofs>nlen) nlen=ofs;
nlen+=clen;
nstr = ht_malloc(nlen);
memcpy(nstr, ostr, ofs);
memcpy(nstr+ofs, chars, clen);
memcpy(nstr+ofs+clen, ostr+ofs, olen-ofs);
e->in_memory.data=nstr;
e->in_memory.len=nlen;
dirty_parse(line+1);
dirty_nofs(line+1);
dirty=true;
free(ostr);
}
}
}
bool ht_ltextfile::has_line(uint line)
{
return (fetch_line(line)!=NULL);
}
bool ht_ltextfile::is_dirty_nofs(uint line) const
{
return (line >= first_nofs_dirty_line);
}
bool ht_ltextfile::is_dirty_parse(uint line) const
{
return (line >= first_parse_dirty_line);
}
uint ht_ltextfile::linecount() const
{
return lines->count();
}
byte *ht_ltextfile::match_lineend_forwd(byte *buf, uint buflen, int *le_len)
{
byte *result=NULL;
byte *n=(byte*)memchr(buf, '\n', buflen);
if (n) {
if ((n>buf) && (n[-1] == '\r')) {
*le_len=2;
result=n-1;
} else {
*le_len=1;
result=n;
}
}
return result;
}
lexer_state ht_ltextfile::next_instate(uint line)
{
byte buf[TEXTFILE_MAX_LINELEN+1];
lexer_state state = 0;
uint buflen;
if (!getline(line, 0, buf, TEXTFILE_MAX_LINELEN, &buflen, &state)) return state;
buf[buflen] = 0;
if (lexer) {
text_pos p;
char *bufp = (char*)buf;
uint toklen;
bool start_of_line = true;
p.line = line;
p.pofs = 0;
int bufplen = buflen;
int prev_bufplen = -1;
while ((lexer->gettoken(bufp, bufplen, p, start_of_line, &state, &toklen))) {
bufp += toklen;
p.pofs += toklen;
bufplen -= toklen;
start_of_line = false;
if (!bufplen && !prev_bufplen) break;
prev_bufplen = bufplen;
}
}
return state;
}
FileOfs ht_ltextfile::next_nofs(ht_ltextfile_line *l) const
{
if (l) {
if (l->is_in_memory) return l->nofs+l->in_memory.len+l->lineendlen; else
return l->nofs+l->on_disk.len+l->lineendlen;
}
return 0;
}
void ht_ltextfile::pstat(pstat_t &s) const
{
mFile->pstat(s);
s.size = getSize();
}
uint ht_ltextfile::read(void *buf, uint size)
{
FileOfs o=tell();
uint line;
uint pofs;
uint c=0;
byte *b=(byte*)buf;
if (convert_ofs2line(o, &line, &pofs)) while (size) {
ht_ltextfile_line *l=fetch_line(line);
if (!l)
break;
uint q;
if (l->is_in_memory) {
q=l->in_memory.len;
uint s=q;
if (s>pofs) {
s-=pofs;
s=MIN(size, s);
memcpy(b+c, l->in_memory.data+pofs, s);
size-=s;
c+=s;
pofs+=s;
}
} else {
q=l->on_disk.len;
uint r;
uint s=q;
if (s>pofs) {
s-=pofs;
s=MIN(size, s);
mFile->seek(l->on_disk.ofs+pofs);
r = mFile->read(b+c, s);
if (r!=s) break;
size-=s;
c+=s;
pofs+=s;
}
}
uint s=l->lineendlen;
if ((q+s>pofs) && (pofs >= q)) {
s-=pofs-q;
s=MIN(size, s);
memcpy(b+c, l->lineend+(pofs-q), s);
size-=s;
c+=s;
}
line++;
pofs=0;
}
ofs += c;
return c;
}
void ht_ltextfile::reread()
{
lines->delAll();
orig_lines->delAll();
dirty = false;
FileOfs ofs=0;
int ll, pll=-1, ln=0;
bool firstline = true;
byte buf[TEXTFILE_MAX_LINELEN+1];
lexer_state state=0;
ht_ltextfile_line *e, *ce;
if (lexer) state=lexer->getinitstate();
uint l=0;
while ((l=find_linelen_forwd(buf, sizeof buf, ofs, &ll))) {
mFile->seek(ofs);
uint x = mFile->read(buf, l-ll);
buf[x]=0;
e=new ht_ltextfile_line();
e->is_in_memory=false;
e->instate=state;
e->on_disk.ofs=ofs;
e->on_disk.len=l-ll;
e->nofs=ofs;
e->lineendlen=ll;
assert( (uint)ll <= sizeof e->lineend);
mFile->read(e->lineend, ll);
lines->insert(e);
ce=new ht_ltextfile_line();
*ce = *e;
orig_lines->insert(ce);
if (lexer) {
text_pos p;
char *bufp=(char*)buf;
uint toklen;
bool start_of_line=true;
p.line=ln;
p.pofs=0;
while (lexer->gettoken(bufp, l-ll-(bufp-(char*)buf), p, start_of_line, &state, &toklen)) {
bufp+=toklen;
p.pofs+=toklen;
start_of_line=false;
}
}
ofs+=l;
pll=ll;
firstline=false;
ln++;
}
if (pll || firstline) {
ll=0;
e=new ht_ltextfile_line();
e->is_in_memory=false;
e->instate=state;
e->on_disk.ofs=ofs;
e->on_disk.len=l-ll;
e->nofs=ofs;
e->lineendlen=ll;
lines->insert(e);
ce=new ht_ltextfile_line();
*ce = *e;
orig_lines->insert(ce);
}
}
void ht_ltextfile::split_line(uint a, uint pos, void *line_end, int line_end_len)
{
if (pos == 0) {
insert_lines(a, 1, &line_end, &line_end_len);
} else {
uint l=getlinelength(a);
if (pos > l) pos = l;
char *aline = ht_malloc(pos+1);
uint alinelen;
getline(a, 0, aline, pos+1, &alinelen, NULL);
insert_lines(a, 1, &line_end, &line_end_len);
insert_chars(a, 0, aline, alinelen);
delete_chars(a+1, 0, pos);
free(aline);
}
}
void ht_ltextfile::seek(FileOfs offset)
{
ofs = offset;
}
void ht_ltextfile::setLayered(File *streamfile, bool ownNewLayered)
{
FileLayer::setLayered(streamfile, ownNewLayered);
reread();
}
void ht_ltextfile::set_layered_assume(File *streamfile, bool ownNewLayered, bool changes_applied)
{
if (changes_applied) {
orig_lines->delAll();
uint c = lines->count();
for (uint i=0; i<c; i++) {
ht_ltextfile_line *l = fetch_line_nofs_ok(i);
ht_ltextfile_line *m = new ht_ltextfile_line();
l->on_disk.ofs = l->nofs;
if (l->is_in_memory) {
l->on_disk.len = l->in_memory.len;
free(l->in_memory.data);
l->is_in_memory = false;
}
*m = *l;
orig_lines->insert(m);
}
cache_invd();
}
FileLayer::setLayered(streamfile, ownNewLayered);
}
void ht_ltextfile::set_lexer(ht_syntax_lexer *l)
{
if (lexer != l) {
lexer = l;
dirty_parse(0);
}
}
FileOfs ht_ltextfile::tell() const
{
return ofs;
}
void ht_ltextfile::truncate(FileOfs newsize)
{
/* FIXME: nyi */
throw IOException(ENOSYS);
}
void ht_ltextfile::update_parse(uint target)
{
ht_ltextfile_line *e;
uint line = first_parse_dirty_line;
lexer_state instate=0;
if (line) {
instate = next_instate(line-1);
// FIXME: function help works with this already...
/* e = fetch_line(line);
instate = e->instate;*/
} else {
if (lexer) instate = lexer->getinitstate();
}
while (line<=target) {
e = fetch_line(line);
if (!e) break;
// if (e->instate==instate) break; /* FIXME: valid simplification ?!... */
e->instate = instate;
first_parse_dirty_line = line+1;
instate = next_instate(line);
line++;
}
e = fetch_line(line);
if (e) e->instate = instate;
}
void ht_ltextfile::update_nofs(uint target) const
{
ht_ltextfile_line *e;
uint line=first_nofs_dirty_line;
FileOfs nofs;
if (line) {
e=fetch_line(line-1);
nofs=next_nofs(e);
} else {
nofs=0;
}
while (line<=target) {
e=fetch_line(line);
if (!e) break;
e->nofs=nofs;
first_nofs_dirty_line=line+1;
nofs=next_nofs(e);
line++;
}
}
int ht_ltextfile::vcntl(uint cmd, va_list vargs)
{
switch (cmd) {
case FCNTL_MODS_INVD:
cache_invd();
return 0;
case FCNTL_MODS_FLUSH:
cache_flush();
return 0;
case FCNTL_MODS_IS_DIRTY: {
FileOfs o = va_arg(vargs, FileOfs);
FileOfs s = va_arg(vargs, FileOfs);
bool *b = va_arg(vargs, bool*);
*b = dirty;
o = 0; // gcc warns otherwise
s = 0; // gcc warns otherwise
return 0;
}
case FCNTL_MODS_CLEAR_DIRTY_RANGE: {
FileOfs o = va_arg(vargs, FileOfs);
FileOfs s = va_arg(vargs, FileOfs);
dirty = false;
o = s = 0;
return 0;
}
}
return ht_textfile::vcntl(cmd, vargs);
}
uint ht_ltextfile::write(const void *buf, uint size)
{
FileOfs o = tell();
uint line;
uint pofs;
byte *b = (byte*)buf;
uint r = 0;
if (convert_ofs2line(o, &line, &pofs)) {
r = size;
while (size) {
int lelen;
uint s;
byte *c = match_lineend_forwd(b, size, &lelen);
if (c) {
s = c-b;
split_line(line, pofs, c, lelen);
} else {
s = size;
lelen = 0;
}
insert_chars(line, pofs, b, s);
s += lelen;
b += s;
size -= s;
line++;
pofs = 0;
}
}
ofs += r;
return r;
}
|