Subversion Repositories SvarDOS

Rev

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

Rev 1312 Rev 1313
Line 54... Line 54...
54
};
54
};
55
 
55
 
56
struct linedb {
56
struct linedb {
57
  struct line far *cursor;
57
  struct line far *cursor;
58
  unsigned short xoffset;
58
  unsigned short xoffset;
-
 
59
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
59
};
60
};
60
 
61
 
61
 
62
 
62
/* returns non-zero on error */
63
/* returns non-zero on error */
63
static int line_add(struct linedb *db, const char far *line) {
64
static int line_add(struct linedb *db, const char far *line) {
Line 103... Line 104...
103
  scheme[COL_STATUSBAR2] = 0x78;
104
  scheme[COL_STATUSBAR2] = 0x78;
104
  scheme[COL_SCROLLBAR] = 0x70;
105
  scheme[COL_SCROLLBAR] = 0x70;
105
}
106
}
106
 
107
 
107
 
108
 
108
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct linedb *db) {
109
  unsigned char i;
110
  unsigned char i;
110
  const char *s = svarlang_strid(0); /* HELP */
111
  const char *s = svarlang_strid(0); /* HELP */
111
  unsigned char helpcol = screenw - (strlen(s) + 4);
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
112
 
113
 
113
  /* clear screen */
114
  /* clear screen */
114
  mdr_cout_cls(scheme[COL_TXT]);
115
  mdr_cout_cls(scheme[COL_TXT]);
115
 
116
 
116
  /* status bar */
117
  /* fill status bar with background */
-
 
118
  mdr_cout_char_rep(screenh - 1, 0, ' ', scheme[COL_STATUSBAR1], screenw);
-
 
119
 
117
  for (i = 0; i < helpcol; i++) {
120
  /* filename */
118
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
121
  mdr_cout_str(screenh - 1, 0, fname, scheme[COL_STATUSBAR1], screenw);
-
 
122
 
-
 
123
  /* eol type */
119
    if (*fname != 0) fname++;
124
  if (db->lfonly) {
-
 
125
    mdr_cout_str(screenh - 1, helpcol - 3, "LF", scheme[COL_STATUSBAR1], 5);
-
 
126
  } else {
-
 
127
    mdr_cout_str(screenh - 1, helpcol - 5, "CRLF", scheme[COL_STATUSBAR1], 5);
120
  }
128
  }
-
 
129
 
121
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
130
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
122
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
131
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
123
 
132
 
124
  /* scroll bar */
133
  /* scroll bar */
125
  for (i = 0; i < (screenh - 1); i++) {
134
  for (i = 0; i < (screenh - 1); i++) {
Line 375... Line 384...
375
    mdr_coutraw_puts("Failed to open file:");
384
    mdr_coutraw_puts("Failed to open file:");
376
    mdr_coutraw_puts(fname);
385
    mdr_coutraw_puts(fname);
377
    return(-1);
386
    return(-1);
378
  }
387
  }
379
 
388
 
-
 
389
  db->lfonly = 1;
-
 
390
 
380
  do {
391
  do {
381
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
392
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
382
      len += prevlen;
393
      len += prevlen;
383
    } else {
394
    } else {
384
      len = prevlen;
395
      len = prevlen;
Line 387... Line 398...
387
    /* look for nearest \n and replace with 0*/
398
    /* look for nearest \n and replace with 0*/
388
    for (llen = 0; buff[llen] != '\n'; llen++) {
399
    for (llen = 0; buff[llen] != '\n'; llen++) {
389
      if (llen == sizeof(buff)) break;
400
      if (llen == sizeof(buff)) break;
390
    }
401
    }
391
    buff[llen] = 0;
402
    buff[llen] = 0;
-
 
403
    if ((llen > 0) && (buff[llen - 1] == '\r')) {
392
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
404
      buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
-
 
405
      db->lfonly = 0;
-
 
406
    }
393
    if (line_add(db, buff) != 0) {
407
    if (line_add(db, buff) != 0) {
394
      mdr_coutraw_puts("out of memory");
408
      mdr_coutraw_puts("out of memory");
395
      r = -1;
409
      r = -1;
396
      break;
410
      break;
397
    }
411
    }
Line 409... Line 423...
409
 
423
 
410
static int savefile(const struct linedb *db, const char *fname) {
424
static int savefile(const struct linedb *db, const char *fname) {
411
  int fd;
425
  int fd;
412
  const struct line far *l;
426
  const struct line far *l;
413
  unsigned bytes;
427
  unsigned bytes;
-
 
428
  unsigned char eollen;
-
 
429
  unsigned char eolbuf[2];
-
 
430
 
414
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
431
  if (_dos_open(fname, O_WRONLY, &fd) != 0) {
415
    return(-1);
432
    return(-1);
416
  }
433
  }
417
 
434
 
418
  l = db->cursor;
435
  l = db->cursor;
419
  while (l->prev) l = l->prev;
436
  while (l->prev) l = l->prev;
420
 
437
 
-
 
438
  /* preset line terminators */
-
 
439
  if (db->lfonly) {
-
 
440
    eolbuf[0] = '\n';
-
 
441
    eollen = 1;
-
 
442
  } else {
-
 
443
    eolbuf[0] = '\r';
-
 
444
    eolbuf[1] = '\n';
-
 
445
    eollen = 2;
-
 
446
  }
-
 
447
 
421
  while (l) {
448
  while (l) {
422
    _dos_write(fd, l->payload, l->len, &bytes);
449
    _dos_write(fd, l->payload, l->len, &bytes);
423
    _dos_write(fd, "\r\n", 2, &bytes);
450
    _dos_write(fd, eolbuf, eollen, &bytes);
424
    l = l->next;
451
    l = l->next;
425
  }
452
  }
426
 
453
 
427
  _dos_close(fd);
454
  _dos_close(fd);
428
  return(0);
455
  return(0);
Line 455... Line 482...
455
 
482
 
456
  /* add an empty line at end if not present already */
483
  /* add an empty line at end if not present already */
457
  if (db.cursor->len != 0) line_add(&db, "");
484
  if (db.cursor->len != 0) line_add(&db, "");
458
 
485
 
459
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
486
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
460
  ui_basic(screenw, screenh, fname);
487
  ui_basic(screenw, screenh, fname, &db);
461
 
488
 
462
  db_rewind(&db);
489
  db_rewind(&db);
463
 
490
 
464
  for (;;) {
491
  for (;;) {
465
    int k;
492
    int k;