...
|
...
|
@@ -169,7 +169,7 @@ redata_getused(redata_t *redata)
|
169
|
169
|
}
|
170
|
170
|
|
171
|
171
|
int
|
172
|
|
-redata_getposptr(redata_t *redata, long pos, int *numchunk, long *offset)
|
|
172
|
+redata_getposptr(redata_t *redata, long pos, int *numchunk, int *offset)
|
173
|
173
|
{
|
174
|
174
|
long used;
|
175
|
175
|
int i;
|
...
|
...
|
@@ -624,8 +624,8 @@ redata_undo_new(redata_t *redata, undostack_t *stack, char type)
|
624
|
624
|
undo_t *
|
625
|
625
|
redata_undo_newfromchunks(redata_t *redata, undostack_t *stack, char type, int pos1, int len)
|
626
|
626
|
{
|
627
|
|
- int startpos,endpos;
|
628
|
|
- long startoff,endoff;
|
|
627
|
+ int startchunkno,endchunkno;
|
|
628
|
+ int startoff,endoff;
|
629
|
629
|
undo_t *undo;
|
630
|
630
|
int k;
|
631
|
631
|
long used;
|
...
|
...
|
@@ -633,8 +633,8 @@ redata_undo_newfromchunks(redata_t *redata, undostack_t *stack, char type, int p
|
633
|
633
|
if(redata==NULL || len<=0
|
634
|
634
|
|| (stack!=&(redata->undostack) && stack!=&(redata->redostack)))
|
635
|
635
|
return(NULL); /* sanity check failed */
|
636
|
|
- if(redata_getposptr(redata,pos1,&startpos,&startoff)==-1 ||
|
637
|
|
- redata_getposptr(redata,pos1+len,&endpos,&endoff)==-1) {
|
|
636
|
+ if(redata_getposptr(redata,pos1,&startchunkno,&startoff)==-1 ||
|
|
637
|
+ redata_getposptr(redata,pos1+len,&endchunkno,&endoff)==-1) {
|
638
|
638
|
return(NULL); /* chunk data out of bounds */
|
639
|
639
|
}
|
640
|
640
|
if(redata_undobuf_reserve(redata,stack,len)!=0)
|
...
|
...
|
@@ -643,14 +643,14 @@ redata_undo_newfromchunks(redata_t *redata, undostack_t *stack, char type, int p
|
643
|
643
|
return(NULL); /* insuf. mem. */
|
644
|
644
|
undo->off=stack->usedbuf;
|
645
|
645
|
/* copy contents */
|
646
|
|
- for(k=startpos,used=0;k<=endpos;k++) {
|
647
|
|
- if(k==startpos && k==endpos) {
|
|
646
|
+ for(k=startchunkno,used=0;k<=endchunkno;k++) {
|
|
647
|
+ if(k==startchunkno && k==endchunkno) {
|
648
|
648
|
copyfrom=startoff;
|
649
|
649
|
copysize=endoff-startoff;
|
650
|
|
- } else if(k==startpos) {
|
|
650
|
+ } else if(k==startchunkno) {
|
651
|
651
|
copyfrom=startoff;
|
652
|
652
|
copysize=redata->chunks[k]->useddata-startoff;
|
653
|
|
- } else if(k==endpos) {
|
|
653
|
+ } else if(k==endchunkno) {
|
654
|
654
|
copyfrom=0;
|
655
|
655
|
copysize=endoff;
|
656
|
656
|
} else {
|
...
|
...
|
@@ -763,7 +763,7 @@ int
|
763
|
763
|
redata_op_add(redata_t *redata, long insertpos, char *buf, long buflen, undostack_t *fromhere)
|
764
|
764
|
{
|
765
|
765
|
int chunkno;
|
766
|
|
- long pos;
|
|
766
|
+ int offset;
|
767
|
767
|
undo_t *undo;
|
768
|
768
|
rechunk_t *chunk,*nextchunk;
|
769
|
769
|
long avail,nextavail;
|
...
|
...
|
@@ -774,7 +774,7 @@ redata_op_add(redata_t *redata, long insertpos, char *buf, long buflen, undostac
|
774
|
774
|
|| insertpos>redata_getused(redata)
|
775
|
775
|
|| (fromhere!=NULL && (fromhere!=&(redata->undostack) && fromhere!=&(redata->redostack))))
|
776
|
776
|
return(-1); /* sanity check failed */
|
777
|
|
- if(redata_getposptr(redata,insertpos,&chunkno,&pos)==-1)
|
|
777
|
+ if(redata_getposptr(redata,insertpos,&chunkno,&offset)==-1)
|
778
|
778
|
return(-1); /* invalid pos */
|
779
|
779
|
if(fromhere!=&(redata->undostack)) {
|
780
|
780
|
if((undo=redata_undo_newfrombuf(redata,&(redata->undostack),'A',buf,buflen))==NULL)
|
...
|
...
|
@@ -789,24 +789,24 @@ redata_op_add(redata_t *redata, long insertpos, char *buf, long buflen, undostac
|
789
|
789
|
nextavail=(nextchunk==NULL)?0:redata->chunkdatasize-nextchunk->useddata;
|
790
|
790
|
if(avail>=buflen) {
|
791
|
791
|
/* fits in current chunk */
|
792
|
|
- redata_chunk_insertdata(redata,chunkno,pos,buf,buflen);
|
|
792
|
+ redata_chunk_insertdata(redata,chunkno,offset,buf,buflen);
|
793
|
793
|
redata_fix_nl(redata,chunkno);
|
794
|
794
|
nextchunk=chunk;
|
795
|
795
|
} else if((avail+nextavail)>=buflen) {
|
796
|
796
|
/* part fits in current chunk, part in next chunk */
|
797
|
797
|
int bothering;
|
798
|
|
- bothering=chunk->useddata-pos;
|
|
798
|
+ bothering=chunk->useddata-offset;
|
799
|
799
|
bothering=(bothering>nextavail)?nextavail:bothering;
|
800
|
800
|
redata_chunk_movedata(redata,chunkno,chunk->useddata-bothering,chunkno+1,0,bothering);
|
801
|
801
|
avail=redata->chunkdatasize-chunk->useddata;
|
802
|
802
|
avail=(avail>buflen)?buflen:avail;
|
803
|
|
- redata_chunk_insertdata(redata,chunkno,pos,buf,avail);
|
|
803
|
+ redata_chunk_insertdata(redata,chunkno,offset,buf,avail);
|
804
|
804
|
redata_chunk_insertdata(redata,chunkno+1,0,buf+avail,buflen-avail);
|
805
|
805
|
} else {
|
806
|
806
|
/* will need to add more chunks */
|
807
|
807
|
needed=(buflen+redata->chunkdatasize-1)/redata->chunkdatasize;
|
808
|
808
|
needed*=redata->chunkdatasize;
|
809
|
|
- needed+=(chunk->useddata-pos);
|
|
809
|
+ needed+=(chunk->useddata-offset);
|
810
|
810
|
if(redata_preallocate(redata,redata_getsize(redata)+needed)!=0) {
|
811
|
811
|
if(undo!=NULL) {
|
812
|
812
|
redata->undostack.usedundo--;
|
...
|
...
|
@@ -816,7 +816,7 @@ redata_op_add(redata_t *redata, long insertpos, char *buf, long buflen, undostac
|
816
|
816
|
return(-1); /* insuf. mem. */
|
817
|
817
|
}
|
818
|
818
|
redata_chunk_insertnew(redata,chunkno);
|
819
|
|
- redata_chunk_movedata(redata,chunkno,pos,chunkno+1,0,chunk->useddata-pos);
|
|
819
|
+ redata_chunk_movedata(redata,chunkno,offset,chunkno+1,0,chunk->useddata-offset);
|
820
|
820
|
nextchunk=redata->chunks[chunkno+1];
|
821
|
821
|
avail=redata->chunkdatasize-chunk->useddata;
|
822
|
822
|
avail=(avail>buflen)?buflen:avail;
|
...
|
...
|
@@ -863,7 +863,7 @@ int
|
863
|
863
|
redata_op_del(redata_t *redata, long delpos, long size, undostack_t *fromhere)
|
864
|
864
|
{
|
865
|
865
|
int chunkno,curchunk;
|
866
|
|
- long pos;
|
|
866
|
+ int offset;
|
867
|
867
|
undo_t *undo;
|
868
|
868
|
rechunk_t *chunk;
|
869
|
869
|
long ndel;
|
...
|
...
|
@@ -874,7 +874,7 @@ redata_op_del(redata_t *redata, long delpos, long size, undostack_t *fromhere)
|
874
|
874
|
|| (delpos+size)>redata_getused(redata)
|
875
|
875
|
|| (fromhere!=NULL && (fromhere!=&(redata->undostack) && fromhere!=&(redata->redostack))))
|
876
|
876
|
return(-1); /* sanity check failed */
|
877
|
|
- if(redata_getposptr(redata,delpos,&chunkno,&pos)==-1)
|
|
877
|
+ if(redata_getposptr(redata,delpos,&chunkno,&offset)==-1)
|
878
|
878
|
return(-1); /* invalid pos */
|
879
|
879
|
if(fromhere!=&(redata->undostack)) {
|
880
|
880
|
if((undo=redata_undo_newfromchunks(redata,&(redata->undostack),'D',delpos,size))==NULL)
|
...
|
...
|
@@ -884,7 +884,7 @@ redata_op_del(redata_t *redata, long delpos, long size, undostack_t *fromhere)
|
884
|
884
|
undo=NULL;
|
885
|
885
|
}
|
886
|
886
|
for(curchunk=chunkno,ndel=0;ndel<size;curchunk++) {
|
887
|
|
- curpos=(curchunk==chunkno)?pos:0;
|
|
887
|
+ curpos=(curchunk==chunkno)?offset:0;
|
888
|
888
|
curdel=redata->chunks[curchunk]->useddata-curpos;
|
889
|
889
|
curdel=(curdel>(size-ndel))?(size-ndel):curdel;
|
890
|
890
|
redata_chunk_deletedata(redata,curchunk,curpos,curdel);
|
...
|
...
|
@@ -927,7 +927,7 @@ int
|
927
|
927
|
redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostack_t *fromhere)
|
928
|
928
|
{
|
929
|
929
|
int chunkno,dchunkno,curchunk;
|
930
|
|
- long pos,dpos;
|
|
930
|
+ int offset,doffset;
|
931
|
931
|
undo_t *undo;
|
932
|
932
|
rechunk_t *chunk;
|
933
|
933
|
int i;
|
...
|
...
|
@@ -938,9 +938,9 @@ redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostac
|
938
|
938
|
|| (posdest>=posorig && posdest<(posorig+size))
|
939
|
939
|
|| (fromhere!=NULL && (fromhere!=&(redata->undostack) && fromhere!=&(redata->redostack))))
|
940
|
940
|
return(-1); /* sanity check failed */
|
941
|
|
- if(redata_getposptr(redata,posorig,&chunkno,&pos)==-1)
|
|
941
|
+ if(redata_getposptr(redata,posorig,&chunkno,&offset)==-1)
|
942
|
942
|
return(-1); /* invalid pos */
|
943
|
|
- if(redata_getposptr(redata,posdest,&dchunkno,&dpos)==-1)
|
|
943
|
+ if(redata_getposptr(redata,posdest,&dchunkno,&doffset)==-1)
|
944
|
944
|
return(-1); /* invalid pos */
|
945
|
945
|
if(fromhere!=&(redata->undostack)) {
|
946
|
946
|
if((undo=redata_undo_newfromchunks(redata,&(redata->undostack),'M',posorig,size))==NULL)
|
...
|
...
|
@@ -951,48 +951,48 @@ redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostac
|
951
|
951
|
} else {
|
952
|
952
|
undo=NULL;
|
953
|
953
|
}
|
954
|
|
- if((pos+size)<=redata->chunks[chunkno]->useddata
|
|
954
|
+ if((offset+size)<=redata->chunks[chunkno]->useddata
|
955
|
955
|
&& (chunkno==dchunkno || (redata->chunkdatasize-redata->chunks[dchunkno]->useddata)>=size)) {
|
956
|
956
|
/* trivial case: (all the data is in the same chunk) AND (it is intra-chunk move or destination chunk has enough avail. space) */
|
957
|
|
- redata_chunk_movedata(redata, chunkno, pos, dchunkno, dpos, size);
|
|
957
|
+ redata_chunk_movedata(redata, chunkno, offset, dchunkno, doffset, size);
|
958
|
958
|
} else {
|
959
|
959
|
/* data spans several chunks, no space on dest, etc: do it the hard way */
|
960
|
960
|
/* separate the selected data into its own chunk(s) */
|
961
|
961
|
/* lower positions have to make the boundary first (or risk undoing it with next create boundary) */
|
962
|
962
|
if(posdest<posorig) {
|
963
|
963
|
/* make a chunk boundary in posdest */
|
964
|
|
- if(redata_getposptr(redata,posdest,&chunkno,&pos)==-1
|
965
|
|
- || redata_chunk_splithere(redata,chunkno,pos)!=0)
|
|
964
|
+ if(redata_getposptr(redata,posdest,&chunkno,&offset)==-1
|
|
965
|
+ || redata_chunk_splithere(redata,chunkno,offset)!=0)
|
966
|
966
|
return(-1); /* invalid pos or insuf. mem */
|
967
|
967
|
}
|
968
|
968
|
/* make a chunk boundary in posorig and in posorig+size */
|
969
|
|
- if(redata_getposptr(redata,posorig,&chunkno,&pos)==-1
|
970
|
|
- || redata_chunk_splithere(redata,chunkno,pos)!=0)
|
|
969
|
+ if(redata_getposptr(redata,posorig,&chunkno,&offset)==-1
|
|
970
|
+ || redata_chunk_splithere(redata,chunkno,offset)!=0)
|
971
|
971
|
return(-1); /* invalid pos or insuf. mem */
|
972
|
|
- if(redata_getposptr(redata,posorig+size,&chunkno,&pos)==-1
|
973
|
|
- || redata_chunk_splithere(redata,chunkno,pos)!=0)
|
|
972
|
+ if(redata_getposptr(redata,posorig+size,&chunkno,&offset)==-1
|
|
973
|
+ || redata_chunk_splithere(redata,chunkno,offset)!=0)
|
974
|
974
|
return(-1); /* invalid pos or insuf. mem */
|
975
|
975
|
if(posdest>posorig) {
|
976
|
976
|
/* make a chunk boundary in posdest */
|
977
|
|
- if(redata_getposptr(redata,posdest,&chunkno,&pos)==-1
|
978
|
|
- || redata_chunk_splithere(redata,chunkno,pos)!=0)
|
|
977
|
+ if(redata_getposptr(redata,posdest,&chunkno,&offset)==-1
|
|
978
|
+ || redata_chunk_splithere(redata,chunkno,offset)!=0)
|
979
|
979
|
return(-1); /* invalid pos or insuf. mem */
|
980
|
980
|
}
|
981
|
981
|
/* reorder the chunks */
|
982
|
982
|
{
|
983
|
983
|
int schunkno;
|
984
|
|
- long spos;
|
985
|
|
- if(redata_getposptr(redata,posorig,&chunkno,&pos)==-1)
|
|
984
|
+ int soffset;
|
|
985
|
+ if(redata_getposptr(redata,posorig,&chunkno,&offset)==-1)
|
986
|
986
|
return(-1); /* invalid pos or insuf. mem */
|
987
|
|
- if(redata_getposptr(redata,posorig+size,&schunkno,&spos)==-1)
|
|
987
|
+ if(redata_getposptr(redata,posorig+size,&schunkno,&soffset)==-1)
|
988
|
988
|
return(-1); /* invalid pos or insuf. mem */
|
989
|
|
- if(redata_getposptr(redata,posdest,&dchunkno,&dpos)==-1)
|
|
989
|
+ if(redata_getposptr(redata,posdest,&dchunkno,&doffset)==-1)
|
990
|
990
|
return(-1); /* invalid pos or insuf. mem */
|
991
|
|
- if(pos!=0)
|
|
991
|
+ if(offset!=0)
|
992
|
992
|
chunkno++;
|
993
|
|
- if(spos!=0)
|
|
993
|
+ if(soffset!=0)
|
994
|
994
|
schunkno++;
|
995
|
|
- if(dpos!=0)
|
|
995
|
+ if(doffset!=0)
|
996
|
996
|
dchunkno++;
|
997
|
997
|
if(chunkno<0 || schunkno<0 || dchunkno<0
|
998
|
998
|
|| chunkno>=redata->sizechunks || schunkno>=redata->sizechunks || dchunkno>=redata->sizechunks)
|
...
|
...
|
@@ -1011,14 +1011,14 @@ redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostac
|
1011
|
1011
|
}
|
1012
|
1012
|
/* fix nl and delete unused chunks */
|
1013
|
1013
|
if(posorig<posdest) {
|
1014
|
|
- if(redata_getposptr(redata,posorig,&chunkno,&pos)==-1)
|
|
1014
|
+ if(redata_getposptr(redata,posorig,&chunkno,&offset)==-1)
|
1015
|
1015
|
return(-1); /* invalid pos */
|
1016
|
|
- if(redata_getposptr(redata,posdest,&dchunkno,&dpos)==-1)
|
|
1016
|
+ if(redata_getposptr(redata,posdest,&dchunkno,&doffset)==-1)
|
1017
|
1017
|
return(-1); /* invalid pos */
|
1018
|
1018
|
} else { /* posorig>posdest */
|
1019
|
|
- if(redata_getposptr(redata,posdest,&chunkno,&pos)==-1)
|
|
1019
|
+ if(redata_getposptr(redata,posdest,&chunkno,&offset)==-1)
|
1020
|
1020
|
return(-1); /* invalid pos */
|
1021
|
|
- if(redata_getposptr(redata,posorig+size,&dchunkno,&dpos)==-1)
|
|
1021
|
+ if(redata_getposptr(redata,posorig+size,&dchunkno,&doffset)==-1)
|
1022
|
1022
|
return(-1); /* invalid pos */
|
1023
|
1023
|
}
|
1024
|
1024
|
for(curchunk=dchunkno;curchunk>=chunkno;curchunk--) {
|
...
|
...
|
@@ -1105,7 +1105,7 @@ int
|
1105
|
1105
|
redata_data_compare(redata_t *redata, long cmppos, char *buf, long buflen)
|
1106
|
1106
|
{
|
1107
|
1107
|
int chunkno;
|
1108
|
|
- long pos;
|
|
1108
|
+ int offset;
|
1109
|
1109
|
long compared;
|
1110
|
1110
|
long n;
|
1111
|
1111
|
int res;
|
...
|
...
|
@@ -1113,12 +1113,12 @@ redata_data_compare(redata_t *redata, long cmppos, char *buf, long buflen)
|
1113
|
1113
|
|| cmppos>redata_getused(redata)
|
1114
|
1114
|
|| (cmppos+buflen)>redata_getused(redata))
|
1115
|
1115
|
return(-1); /* sanity check failed */
|
1116
|
|
- if(redata_getposptr(redata,cmppos,&chunkno,&pos)==-1)
|
|
1116
|
+ if(redata_getposptr(redata,cmppos,&chunkno,&offset)==-1)
|
1117
|
1117
|
return(-1); /* invalid pos */
|
1118
|
|
- for(compared=0,n=0;compared<buflen && chunkno<redata->sizechunks;chunkno++,pos=0,compared+=n) {
|
1119
|
|
- n=redata->chunks[chunkno]->useddata-pos;
|
|
1118
|
+ for(compared=0,n=0;compared<buflen && chunkno<redata->sizechunks;chunkno++,offset=0,compared+=n) {
|
|
1119
|
+ n=redata->chunks[chunkno]->useddata-offset;
|
1120
|
1120
|
n=(n<0)?0:((compared+n)>buflen)?(buflen-compared):n;
|
1121
|
|
- if((res=memcmp(redata->chunks[chunkno]->data+pos,buf+compared,n))!=0)
|
|
1121
|
+ if((res=memcmp(redata->chunks[chunkno]->data+offset,buf+compared,n))!=0)
|
1122
|
1122
|
return(res);
|
1123
|
1123
|
}
|
1124
|
1124
|
if(compared<buflen)
|