Subversion Repositories SvarDOS

Rev

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

Rev 1286 Rev 1287
Line 55... Line 55...
55
  struct line *cursor;
55
  struct line *cursor;
56
  unsigned short xoffset;
56
  unsigned short xoffset;
57
};
57
};
58
 
58
 
59
 
59
 
60
 
-
 
-
 
60
/* returns non-zero on error */
61
void line_add(struct linedb *db, const char *line) {
61
int line_add(struct linedb *db, const char *line) {
62
  unsigned short slen = strlen(line);
62
  unsigned short slen = strlen(line);
63
  struct line *l;
63
  struct line *l;
64
 
64
 
65
  /* trim out CR/LF line endings */
65
  /* trim out CR/LF line endings */
66
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
66
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
67
    slen -= 2;
67
    slen -= 2;
68
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
68
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
69
    slen--;
69
    slen--;
70
  }
70
  }
71
 
71
 
72
  l =  calloc(1, sizeof(struct line) + slen + 1);
72
  l = calloc(1, sizeof(struct line) + slen + 1);
-
 
73
  if (l == NULL) return(-1);
-
 
74
 
73
  l->prev = db->cursor;
75
  l->prev = db->cursor;
74
  if (db->cursor) {
76
  if (db->cursor) {
75
    l->next = db->cursor->next;
77
    l->next = db->cursor->next;
76
    db->cursor->next = l;
78
    db->cursor->next = l;
77
    l->next->prev = l;
79
    l->next->prev = l;
78
  }
80
  }
79
  db->cursor = l;
81
  db->cursor = l;
80
  memmove(l->payload, line, slen);
82
  memmove(l->payload, line, slen);
81
  l->len = slen;
83
  l->len = slen;
-
 
84
 
-
 
85
  return(0);
82
}
86
}
83
 
87
 
84
 
88
 
85
void db_rewind(struct linedb *db) {
89
void db_rewind(struct linedb *db) {
86
  if (db->cursor == NULL) return;
90
  if (db->cursor == NULL) return;
Line 225... Line 229...
225
static char *parseargv(void) {
229
static char *parseargv(void) {
226
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
230
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
227
  unsigned char count = 0;
231
  unsigned char count = 0;
228
  char *argv[4];
232
  char *argv[4];
229
 
233
 
230
  mdr_coutraw_puts(tail);
-
 
231
 
-
 
232
  while (count < 4) {
234
  while (count < 4) {
233
    /* jump to nearest arg */
235
    /* jump to nearest arg */
234
    while (*tail == ' ') {
236
    while (*tail == ' ') {
235
      *tail = 0;
237
      *tail = 0;
236
      tail++;
238
      tail++;
Line 252... Line 254...
252
 
254
 
253
  return(argv[0]);
255
  return(argv[0]);
254
}
256
}
255
 
257
 
256
 
258
 
-
 
259
static int loadfile(struct linedb *db, const char *fname) {
257
int main(void) {
260
  char buff[1024];
-
 
261
  unsigned int prevlen = 0, len, llen;
258
  int fd;
262
  int fd;
-
 
263
  int r = 0;
-
 
264
 
-
 
265
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
-
 
266
    mdr_coutraw_puts("Failed to open file:");
-
 
267
    mdr_coutraw_puts(fname);
-
 
268
    return(-1);
-
 
269
  }
-
 
270
 
-
 
271
  do {
-
 
272
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
-
 
273
      len += prevlen;
-
 
274
    } else {
-
 
275
      len = prevlen;
-
 
276
    }
-
 
277
 
-
 
278
    /* look for nearest \n and replace with 0*/
-
 
279
    for (llen = 0; buff[llen] != '\n'; llen++) {
-
 
280
      if (llen == sizeof(buff)) break;
-
 
281
    }
-
 
282
    buff[llen] = 0;
-
 
283
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
-
 
284
    if (line_add(db, buff) != 0) {
-
 
285
      mdr_coutraw_puts("out of memory");
-
 
286
      r = -1;
-
 
287
      break;
-
 
288
    }
-
 
289
 
-
 
290
    len -= llen + 1;
-
 
291
    memmove(buff, buff + llen + 1, len);
-
 
292
    prevlen = len;
-
 
293
  } while (len > 0);
-
 
294
 
-
 
295
  _dos_close(fd);
-
 
296
 
-
 
297
  return(r);
-
 
298
}
-
 
299
 
-
 
300
 
-
 
301
int main(void) {
259
  const char *fname;
302
  const char *fname;
260
  char buff[1024];
-
 
261
  struct linedb db;
303
  struct linedb db;
262
  unsigned char screenw = 0, screenh = 0;
304
  unsigned char screenw = 0, screenh = 0;
263
  unsigned char cursorposx = 0, cursorposy = 0;
305
  unsigned char cursorposx = 0, cursorposy = 0;
264
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
306
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
265
 
307
 
Line 272... Line 314...
272
  if (fname == NULL) {
314
  if (fname == NULL) {
273
    mdr_coutraw_puts("usage: sved file.txt");
315
    mdr_coutraw_puts("usage: sved file.txt");
274
    return(0);
316
    return(0);
275
  }
317
  }
276
 
318
 
277
  mdr_coutraw_puts("");
-
 
278
 
-
 
279
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
-
 
280
    mdr_coutraw_puts("Failed to open file:");
-
 
281
    mdr_coutraw_puts(fname);
-
 
282
    return(1);
-
 
283
  }
-
 
284
 
-
 
285
  /* load file */
319
  /* load file */
286
  {
-
 
287
    unsigned int prevlen = 0, len, llen;
320
  if (loadfile(&db, fname) != 0) return(1);
288
 
-
 
289
    do {
-
 
290
      if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) != 0) break;
-
 
291
      len += prevlen;
-
 
292
 
-
 
293
      /* look for nearest \n and replace with 0*/
-
 
294
      for (llen = 0; buff[llen] != '\n'; llen++) {
-
 
295
        if (llen == sizeof(buff)) break;
-
 
296
      }
-
 
297
      buff[llen] = 0;
-
 
298
      if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
-
 
299
      line_add(&db, buff);
-
 
300
 
-
 
301
      len -= llen + 1;
-
 
302
      memmove(buff, buff + llen + 1, len);
-
 
303
      prevlen = len;
-
 
304
    } while (len > 0);
-
 
305
 
-
 
306
  }
-
 
307
 
321
 
308
  /* add an empty line at end if not present already */
322
  /* add an empty line at end if not present already */
309
  if (db.cursor->len != 0) line_add(&db, "");
323
  if (db.cursor->len != 0) line_add(&db, "");
310
 
324
 
311
  _dos_close(fd);
-
 
312
 
-
 
313
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
325
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
314
  ui_basic(screenw, screenh, fname);
326
  ui_basic(screenw, screenh, fname);
315
 
327
 
316
  db_rewind(&db);
328
  db_rewind(&db);
317
 
329