Subversion Repositories SvarDOS

Rev

Rev 1339 | Rev 1341 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1339 Rev 1340
Line 94... Line 94...
94
static void line_free(struct line far *ptr) {
94
static void line_free(struct line far *ptr) {
95
  _dos_freemem(FP_SEG(ptr));
95
  _dos_freemem(FP_SEG(ptr));
96
}
96
}
97
 
97
 
98
 
98
 
99
static struct line far *line_resize(struct line far *ptr, unsigned short newsiz) {
99
static signed char curline_resize(struct file far *db, unsigned short newsiz) {
100
  unsigned int maxavail;
100
  unsigned int maxavail;
101
  struct line far *newptr;
101
  struct line far *newptr;
102
 
102
 
103
  /* try resizing the block (faster) */
103
  /* try resizing the block (faster) */
104
  if (_dos_setblock((sizeof(struct line) + newsiz) / 16 + 1, FP_SEG(ptr), &maxavail) == 0) return(ptr);
104
  if (_dos_setblock((sizeof(struct line) + newsiz) / 16 + 1, FP_SEG(db->cursor), &maxavail) == 0) return(0);
105
 
105
 
106
  /* create a new block and copy data over */
106
  /* create a new block and copy data over */
107
  newptr = line_calloc(newsiz);
107
  newptr = line_calloc(newsiz);
108
  if (newptr == NULL) return(NULL);
108
  if (newptr == NULL) return(-1);
109
  _fmemmove(newptr, ptr, sizeof(struct line) + ptr->len);
109
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
-
 
110
 
-
 
111
  /* rewire the linked list */
-
 
112
  db->cursor = newptr;
-
 
113
  if (newptr->next) newptr->next->prev = newptr;
-
 
114
  if (newptr->prev) newptr->prev->next = newptr;
-
 
115
 
110
  return(newptr);
116
  return(0);
111
}
117
}
112
 
118
 
113
 
119
 
114
/* adds a new line at cursor position into file linked list and advance cursor
120
/* adds a new line at cursor position into file linked list and advance cursor
115
 * returns non-zero on error */
121
 * returns non-zero on error */
Line 175... Line 181...
175
}
181
}
176
 
182
 
177
 
183
 
178
/* append a nul-terminated string to line at cursor position */
184
/* append a nul-terminated string to line at cursor position */
179
static int line_append(struct file *f, const char far *buf, unsigned short len) {
185
static int line_append(struct file *f, const char far *buf, unsigned short len) {
180
  struct line far *n;
-
 
181
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
186
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
182
  n = line_resize(f->cursor, f->cursor->len + len);
187
  if (curline_resize(f, f->cursor->len + len) != 0) return(-1);
183
  if (n == NULL) return(-1);
-
 
184
  f->cursor = n;
-
 
185
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
188
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
186
  f->cursor->len += len;
189
  f->cursor->len += len;
187
 
190
 
188
  /* rewire the linked list */
-
 
189
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
-
 
190
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
-
 
191
 
-
 
192
  return(0);
191
  return(0);
193
}
192
}
194
 
193
 
195
 
194
 
196
static void db_rewind(struct file *db) {
195
static void db_rewind(struct file *db) {
Line 453... Line 452...
453
    uidirty.to = db->cursorposy;
452
    uidirty.to = db->cursorposy;
454
    db->modflag = 1;
453
    db->modflag = 1;
455
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
454
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
456
    struct line far *nextline = db->cursor->next;
455
    struct line far *nextline = db->cursor->next;
457
    if (db->cursor->next->len > 0) {
456
    if (db->cursor->next->len > 0) {
458
      void far *newptr = line_resize(db->cursor, db->cursor->len + db->cursor->next->len + 1);
457
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
459
      if (newptr != NULL) {
-
 
460
        db->cursor = newptr;
-
 
461
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
458
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
462
        db->cursor->len += db->cursor->next->len;
459
        db->cursor->len += db->cursor->next->len;
463
      }
460
      }
464
    }
461
    }
-
 
462
 
465
    db->cursor->next = db->cursor->next->next;
463
    db->cursor->next = db->cursor->next->next;
466
    db->cursor->next->prev = db->cursor;
464
    db->cursor->next->prev = db->cursor;
467
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
-
 
-
 
465
 
468
    line_free(nextline);
466
    line_free(nextline);
469
    uidirty.from = db->cursorposy;
467
    uidirty.from = db->cursorposy;
470
    uidirty.to = 0xff;
468
    uidirty.to = 0xff;
471
    db->totlines -= 1;
469
    db->totlines -= 1;
472
    db->modflag = 1;
470
    db->modflag = 1;
Line 658... Line 656...
658
  return(errflag);
656
  return(errflag);
659
}
657
}
660
 
658
 
661
 
659
 
662
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
660
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
663
  struct line far *n;
-
 
664
  n = line_resize(db->cursor, db->cursor->len + len);
661
  if (curline_resize(db, db->cursor->len + len) == 0) {
665
  if (n != NULL) {
-
 
666
    unsigned short off = db->xoffset + db->cursorposx;
662
    unsigned short off = db->xoffset + db->cursorposx;
667
    db->modflag = 1;
663
    db->modflag = 1;
668
    if (n->prev) n->prev->next = n;
-
 
669
    if (n->next) n->next->prev = n;
-
 
670
    db->cursor = n;
-
 
671
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
664
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
672
    db->cursor->len += len;
665
    db->cursor->len += len;
673
    uidirty.from = db->cursorposy;
666
    uidirty.from = db->cursorposy;
674
    uidirty.to = db->cursorposy;
667
    uidirty.to = db->cursorposy;
675
    while (len--) {
668
    while (len--) {