Subversion Repositories SvarDOS

Rev

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

Rev 1323 Rev 1324
Line 41... Line 41...
41
#define COL_STATUSBAR2 2
41
#define COL_STATUSBAR2 2
42
#define COL_SCROLLBAR  3
42
#define COL_SCROLLBAR  3
43
#define COL_MSG        4
43
#define COL_MSG        4
44
#define COL_ERR        5
44
#define COL_ERR        5
45
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
45
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
46
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70, 0x70, 0x70};
46
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70, 0x70, 0xf0};
47
 
47
 
48
#define SCROLL_CURSOR 0xB1
48
#define SCROLL_CURSOR 0xB1
49
 
49
 
50
 
50
 
51
struct line {
51
struct line {
Line 64... Line 64...
64
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
64
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
65
  char fname[1]; /* dynamically sized */
65
  char fname[1]; /* dynamically sized */
66
};
66
};
67
 
67
 
68
 
68
 
-
 
69
/* adds a new line at cursor position into file linked list and dvance cursor
69
/* returns non-zero on error */
70
 * returns non-zero on error */
70
static int line_add(struct file *db, const char far *line) {
71
static int line_add(struct file *db, const char far *line) {
71
  unsigned short slen;
72
  unsigned short slen;
72
  struct line far *l;
73
  struct line far *l;
73
 
74
 
74
  /* slen = strlen(line) (but for far pointer) */
75
  /* slen = strlen(line) (but for far pointer) */
Line 96... Line 97...
96
 
97
 
97
  return(0);
98
  return(0);
98
}
99
}
99
 
100
 
100
 
101
 
-
 
102
/* append a nul-terminated string to line at cursor position */
-
 
103
static int line_append(struct file *f, const char far *buf, unsigned short len) {
-
 
104
  struct line far *n;
-
 
105
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
-
 
106
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
-
 
107
  if (n == NULL) return(-1);
-
 
108
  f->cursor = n;
-
 
109
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
-
 
110
  f->cursor->len += len;
-
 
111
 
-
 
112
  /* rewire the linked list */
-
 
113
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
-
 
114
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
-
 
115
 
-
 
116
  return(0);
-
 
117
}
-
 
118
 
-
 
119
 
101
static void db_rewind(struct file *db) {
120
static void db_rewind(struct file *db) {
102
  if (db->cursor == NULL) return;
121
  if (db->cursor == NULL) return;
103
  while (db->cursor->prev) db->cursor = db->cursor->prev;
122
  while (db->cursor->prev) db->cursor = db->cursor->prev;
104
}
123
}
105
 
124
 
Line 107... Line 126...
107
static void load_colorscheme(void) {
126
static void load_colorscheme(void) {
108
  scheme[COL_TXT] = 0x17;
127
  scheme[COL_TXT] = 0x17;
109
  scheme[COL_STATUSBAR1] = 0x70;
128
  scheme[COL_STATUSBAR1] = 0x70;
110
  scheme[COL_STATUSBAR2] = 0x78;
129
  scheme[COL_STATUSBAR2] = 0x78;
111
  scheme[COL_SCROLLBAR] = 0x70;
130
  scheme[COL_SCROLLBAR] = 0x70;
112
  scheme[COL_MSG] = 0x2f;
131
  scheme[COL_MSG] = 0xf0;
113
  scheme[COL_ERR] = 0x4f;
132
  scheme[COL_ERR] = 0x4f;
114
}
133
}
115
 
134
 
116
 
135
 
117
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
136
static void ui_basic(unsigned char screenw, unsigned char screenh, const struct file *db) {
Line 396... Line 415...
396
  return(argv[0]);
415
  return(argv[0]);
397
}
416
}
398
 
417
 
399
 
418
 
400
static struct file *loadfile(const char *fname) {
419
static struct file *loadfile(const char *fname) {
-
 
420
  char buff[512]; /* read one entire sector at a time (faster) */
401
  char buff[1024];
421
  char *buffptr;
402
  unsigned int prevlen = 0, len, llen;
422
  unsigned int len, llen;
403
  int fd;
423
  int fd;
-
 
424
  unsigned char eolfound;
404
  struct file *db;
425
  struct file *db;
405
 
426
 
406
  len = strlen(fname) + 1;
427
  len = strlen(fname) + 1;
407
  db = calloc(1, sizeof(struct file) + len);
428
  db = calloc(1, sizeof(struct file) + len);
408
  if (db == NULL) return(NULL);
429
  if (db == NULL) return(NULL);
Line 417... Line 438...
417
    return(NULL);
438
    return(NULL);
418
  }
439
  }
419
 
440
 
420
  db->lfonly = 1;
441
  db->lfonly = 1;
421
 
442
 
422
  do {
443
  /* start by adding an empty line */
423
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
444
  if (line_add(db, "") != 0) {
424
      len += prevlen;
445
    /* TODO ERROR HANDLING */
-
 
446
  }
-
 
447
 
425
    } else {
448
  for (eolfound = 0;;) {
426
      len = prevlen;
449
    unsigned short consumedbytes;
-
 
450
 
-
 
451
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
427
    }
452
    buffptr = buff;
428
 
453
 
-
 
454
    FINDLINE:
-
 
455
 
429
    /* look for nearest \n and replace with 0, also expand tabs */
456
    /* look for nearest \n */
430
    for (llen = 0; buff[llen] != '\n'; llen++) {
457
    for (consumedbytes = 0;; consumedbytes++) {
431
      if (buff[llen] == '\t') {
458
      if (consumedbytes == len) {
432
        unsigned char c;
459
        llen = consumedbytes;
433
        for (c = 0; c < 8; c++) buff[llen++] = ' ';
460
        break;
434
      }
461
      }
-
 
462
      if (buffptr[consumedbytes] == '\r') {
-
 
463
        llen = consumedbytes;
-
 
464
        consumedbytes++;
-
 
465
        db->lfonly = 0;
-
 
466
        break;
-
 
467
      }
435
      if (llen == sizeof(buff)) { /* TODO line too long: handle it in some classy way */
468
      if (buffptr[consumedbytes] == '\n') {
-
 
469
        eolfound = 1;
-
 
470
        llen = consumedbytes;
-
 
471
        consumedbytes++;
436
        break;
472
        break;
437
      }
473
      }
438
    }
474
    }
439
    buff[llen] = 0;
-
 
-
 
475
 
440
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
476
    /* consumedbytes is the amount of bytes processed from buffptr,
441
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
477
     * llen is the length of line's payload (without its line terminator) */
-
 
478
 
442
      db->lfonly = 0;
479
    /* append content, if line is non-empty */
443
    }
-
 
444
    if (line_add(db, buff) != 0) {
480
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
445
      mdr_coutraw_puts("out of memory");
481
      mdr_coutraw_puts("out of memory");
446
      free(db);
482
      free(db);
447
      db = NULL;
483
      db = NULL;
448
      break;
484
      break;
449
    }
485
    }
450
 
486
 
-
 
487
    /* add a new line if necessary */
451
    len -= llen + 1;
488
    if (eolfound) {
-
 
489
      if (line_add(db, "") != 0) {
-
 
490
      /* TODO ERROR HANDLING */
452
    memmove(buff, buff + llen + 1, len);
491
        mdr_coutraw_puts("out of memory");
-
 
492
        free(db);
453
    prevlen = len;
493
        db = NULL;
-
 
494
        break;
-
 
495
      }
454
  } while (len > 0);
496
      eolfound = 0;
-
 
497
    }
-
 
498
 
-
 
499
    /* anything left? process the buffer leftover again */
-
 
500
    if (consumedbytes < len) {
-
 
501
      len -= consumedbytes;
-
 
502
      buffptr += consumedbytes;
-
 
503
      goto FINDLINE;
-
 
504
    }
-
 
505
 
-
 
506
  }
455
 
507
 
456
  _dos_close(fd);
508
  _dos_close(fd);
457
 
509
 
458
  SKIPLOADING:
510
  SKIPLOADING:
459
 
511
 
Line 490... Line 542...
490
    eolbuf[1] = '\n';
542
    eolbuf[1] = '\n';
491
    eollen = 2;
543
    eollen = 2;
492
  }
544
  }
493
 
545
 
494
  while (l) {
546
  while (l) {
-
 
547
    /* do not write the last empty line, it is only useful for edition */
-
 
548
    if (l->len != 0) {
495
    _dos_write(fd, l->payload, l->len, &bytes);
549
      _dos_write(fd, l->payload, l->len, &bytes);
-
 
550
    } else if (l->next == NULL) {
-
 
551
      break;
-
 
552
    }
496
    _dos_write(fd, eolbuf, eollen, &bytes);
553
    _dos_write(fd, eolbuf, eollen, &bytes);
497
    l = l->next;
554
    l = l->next;
498
  }
555
  }
499
 
556
 
500
  _dos_close(fd);
557
  _dos_close(fd);