Subversion Repositories SvarDOS

Rev

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

Rev 1338 Rev 1339
Line 25... Line 25...
25
 
25
 
26
#include <dos.h>      /* _dos_open(), _dos_read(), _dos_close(), ... */
26
#include <dos.h>      /* _dos_open(), _dos_read(), _dos_close(), ... */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
30
#include <malloc.h>   /* _fcalloc() */
-
 
31
 
30
 
32
#include "mdr\bios.h"
31
#include "mdr\bios.h"
33
#include "mdr\cout.h"
32
#include "mdr\cout.h"
34
#include "mdr\dos.h"
33
#include "mdr\dos.h"
35
#include "mdr\keyb.h"
34
#include "mdr\keyb.h"
Line 82... Line 81...
82
 
81
 
83
/*****************************************************************************
82
/*****************************************************************************
84
 * functions                                                                 *
83
 * functions                                                                 *
85
 *****************************************************************************/
84
 *****************************************************************************/
86
 
85
 
-
 
86
static struct line far *line_calloc(unsigned short siz) {
-
 
87
  unsigned int seg;
-
 
88
  if (_dos_allocmem((sizeof(struct line) + siz) / 16 + 1, &seg) != 0) return(NULL);
-
 
89
  _fmemset(MK_FP(seg, 0), 0, siz);
-
 
90
  return(MK_FP(seg, 0));
-
 
91
}
-
 
92
 
-
 
93
 
-
 
94
static void line_free(struct line far *ptr) {
-
 
95
  _dos_freemem(FP_SEG(ptr));
-
 
96
}
-
 
97
 
-
 
98
 
-
 
99
static struct line far *line_resize(struct line far *ptr, unsigned short newsiz) {
-
 
100
  unsigned int maxavail;
-
 
101
  struct line far *newptr;
-
 
102
 
-
 
103
  /* try resizing the block (faster) */
-
 
104
  if (_dos_setblock((sizeof(struct line) + newsiz) / 16 + 1, FP_SEG(ptr), &maxavail) == 0) return(ptr);
-
 
105
 
-
 
106
  /* create a new block and copy data over */
-
 
107
  newptr = line_calloc(newsiz);
-
 
108
  if (newptr == NULL) return(NULL);
-
 
109
  _fmemmove(newptr, ptr, sizeof(struct line) + ptr->len);
-
 
110
  return(newptr);
-
 
111
}
-
 
112
 
-
 
113
 
87
/* adds a new line at cursor position into file linked list and advance cursor
114
/* adds a new line at cursor position into file linked list and advance cursor
88
 * returns non-zero on error */
115
 * returns non-zero on error */
89
static int line_add(struct file *db, const char far *line, unsigned short slen) {
116
static int line_add(struct file *db, const char far *line, unsigned short slen) {
90
  struct line far *l;
117
  struct line far *l;
91
 
118
 
92
  l = _fcalloc(1, sizeof(struct line) + slen);
119
  l = line_calloc(slen);
93
  if (l == NULL) return(-1);
120
  if (l == NULL) return(-1);
94
 
121
 
95
  l->prev = db->cursor;
122
  l->prev = db->cursor;
96
  if (db->cursor) {
123
  if (db->cursor) {
97
    l->next = db->cursor->next;
124
    l->next = db->cursor->next;
Line 150... Line 177...
150
 
177
 
151
/* append a nul-terminated string to line at cursor position */
178
/* append a nul-terminated string to line at cursor position */
152
static int line_append(struct file *f, const char far *buf, unsigned short len) {
179
static int line_append(struct file *f, const char far *buf, unsigned short len) {
153
  struct line far *n;
180
  struct line far *n;
154
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
181
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
155
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
182
  n = line_resize(f->cursor, f->cursor->len + len);
156
  if (n == NULL) return(-1);
183
  if (n == NULL) return(-1);
157
  f->cursor = n;
184
  f->cursor = n;
158
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
185
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
159
  f->cursor->len += len;
186
  f->cursor->len += len;
160
 
187
 
Line 426... Line 453...
426
    uidirty.to = db->cursorposy;
453
    uidirty.to = db->cursorposy;
427
    db->modflag = 1;
454
    db->modflag = 1;
428
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
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) */
429
    struct line far *nextline = db->cursor->next;
456
    struct line far *nextline = db->cursor->next;
430
    if (db->cursor->next->len > 0) {
457
    if (db->cursor->next->len > 0) {
431
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
458
      void far *newptr = line_resize(db->cursor, db->cursor->len + db->cursor->next->len + 1);
432
      if (newptr != NULL) {
459
      if (newptr != NULL) {
433
        db->cursor = newptr;
460
        db->cursor = newptr;
434
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
461
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
435
        db->cursor->len += db->cursor->next->len;
462
        db->cursor->len += db->cursor->next->len;
436
      }
463
      }
437
    }
464
    }
438
    db->cursor->next = db->cursor->next->next;
465
    db->cursor->next = db->cursor->next->next;
439
    db->cursor->next->prev = db->cursor;
466
    db->cursor->next->prev = db->cursor;
440
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
467
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
441
    _ffree(nextline);
468
    line_free(nextline);
442
    uidirty.from = db->cursorposy;
469
    uidirty.from = db->cursorposy;
443
    uidirty.to = 0xff;
470
    uidirty.to = 0xff;
444
    db->totlines -= 1;
471
    db->totlines -= 1;
445
    db->modflag = 1;
472
    db->modflag = 1;
446
  }
473
  }
Line 493... Line 520...
493
  char buff[512]; /* read one entire sector at a time (faster) */
520
  char buff[512]; /* read one entire sector at a time (faster) */
494
  char *buffptr;
521
  char *buffptr;
495
  unsigned int len, llen;
522
  unsigned int len, llen;
496
  int fd;
523
  int fd;
497
  unsigned char eolfound;
524
  unsigned char eolfound;
498
  struct file *db;
525
  static struct file db[1];
499
 
526
 
500
  len = strlen(fname) + 1;
-
 
501
  db = calloc(1, sizeof(struct file));
527
  bzero(db, sizeof(db));
502
  if (db == NULL) return(NULL);
-
 
503
  memcpy(db->fname, fname, len);
528
  memcpy(db->fname, fname, strlen(fname));
504
 
529
 
505
  if (*fname == 0) goto SKIPLOADING;
530
  if (*fname == 0) goto SKIPLOADING;
506
 
531
 
507
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
532
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
508
    mdr_coutraw_puts("Failed to open file:");
533
    mdr_coutraw_puts("Failed to open file:");
509
    mdr_coutraw_puts(fname);
534
    mdr_coutraw_puts(fname);
510
    free(db);
-
 
511
    return(NULL);
535
    return(NULL);
512
  }
536
  }
513
 
537
 
514
  db->lfonly = 1;
538
  db->lfonly = 1;
515
 
539
 
Line 550... Line 574...
550
     * llen is the length of line's payload (without its line terminator) */
574
     * llen is the length of line's payload (without its line terminator) */
551
 
575
 
552
    /* append content, if line is non-empty */
576
    /* append content, if line is non-empty */
553
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
577
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
554
      mdr_coutraw_puts("out of memory");
578
      mdr_coutraw_puts("out of memory");
555
      free(db);
-
 
556
      db = NULL;
579
      goto IOERR;
557
      break;
-
 
558
    }
580
    }
559
 
581
 
560
    /* add a new line if necessary */
582
    /* add a new line if necessary */
561
    if (eolfound) {
583
    if (eolfound) {
562
      if (line_add(db, NULL, 0) != 0) {
584
      if (line_add(db, NULL, 0) != 0) {
563
      /* TODO ERROR HANDLING */
585
      /* TODO ERROR HANDLING */
564
        mdr_coutraw_puts("out of memory");
586
        mdr_coutraw_puts("out of memory");
565
        free(db);
-
 
566
        db = NULL;
587
        goto IOERR;
567
        break;
-
 
568
      }
588
      }
569
      eolfound = 0;
589
      eolfound = 0;
570
    }
590
    }
571
 
591
 
572
    /* anything left? process the buffer leftover again */
592
    /* anything left? process the buffer leftover again */
Line 581... Line 601...
581
  _dos_close(fd);
601
  _dos_close(fd);
582
 
602
 
583
  SKIPLOADING:
603
  SKIPLOADING:
584
 
604
 
585
  /* add an empty line at end if not present already, also rewind cursor to top of file */
605
  /* add an empty line at end if not present already, also rewind cursor to top of file */
586
  if (db != NULL) {
-
 
587
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
606
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
588
    db_rewind(db);
607
  db_rewind(db);
589
  }
-
 
590
 
608
 
591
  return(db);
609
  return(db);
-
 
610
 
-
 
611
  IOERR:
-
 
612
  _dos_close(fd);
-
 
613
  return(NULL);
592
}
614
}
593
 
615
 
594
 
616
 
595
static int savefile(const struct file *db, const char *newfname) {
617
static int savefile(const struct file *db, const char *newfname) {
596
  int fd;
618
  int fd;
Line 637... Line 659...
637
}
659
}
638
 
660
 
639
 
661
 
640
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
662
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
641
  struct line far *n;
663
  struct line far *n;
642
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
664
  n = line_resize(db->cursor, db->cursor->len + len);
643
  if (n != NULL) {
665
  if (n != NULL) {
644
    unsigned short off = db->xoffset + db->cursorposx;
666
    unsigned short off = db->xoffset + db->cursorposx;
645
    db->modflag = 1;
667
    db->modflag = 1;
646
    if (n->prev) n->prev->next = n;
668
    if (n->prev) n->prev->next = n;
647
    if (n->next) n->next->prev = n;
669
    if (n->next) n->next->prev = n;