Subversion Repositories SvarDOS

Rev

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

Rev 1345 Rev 1346
Line 23... Line 23...
23
 * IN THE SOFTWARE.
23
 * IN THE SOFTWARE.
24
 */
24
 */
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>
-
 
29
#include <string.h>
28
#include <string.h>
30
 
29
 
31
#include "mdr\bios.h"
30
#include "mdr\bios.h"
32
#include "mdr\cout.h"
31
#include "mdr\cout.h"
33
#include "mdr\dos.h"
32
#include "mdr\dos.h"
Line 531... Line 530...
531
 
530
 
532
  return(argv[0]);
531
  return(argv[0]);
533
}
532
}
534
 
533
 
535
 
534
 
-
 
535
/* returns 0 on success, 1 on file not found, 2 on other error */
536
static struct file *loadfile(const char *fname) {
536
static unsigned char loadfile(struct file *db, const char *fname) {
537
  char buff[512]; /* read one entire sector at a time (faster) */
537
  char buff[512]; /* read one entire sector at a time (faster) */
538
  char *buffptr;
538
  char *buffptr;
539
  unsigned int len, llen;
539
  unsigned int len, llen;
540
  int fd;
540
  int fd;
541
  unsigned char eolfound;
541
  unsigned char eolfound;
542
  static struct file db[1];
-
 
543
 
542
 
544
  bzero(db, sizeof(db));
543
  bzero(db, sizeof(db));
545
  memcpy(db->fname, fname, strlen(fname));
544
  memcpy(db->fname, fname, strlen(fname));
546
 
545
 
547
  if (*fname == 0) goto SKIPLOADING;
546
  if (*fname == 0) goto SKIPLOADING;
548
 
547
 
549
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
548
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
550
    mdr_coutraw_puts("Failed to open file:");
-
 
551
    mdr_coutraw_puts(fname);
-
 
552
    return(NULL);
549
    return(1);
553
  }
550
  }
554
 
551
 
555
  db->lfonly = 1;
552
  db->lfonly = 1;
556
 
553
 
557
  /* start by adding an empty line */
554
  /* start by adding an empty line */
Line 590... Line 587...
590
    /* consumedbytes is the amount of bytes processed from buffptr,
587
    /* consumedbytes is the amount of bytes processed from buffptr,
591
     * llen is the length of line's payload (without its line terminator) */
588
     * llen is the length of line's payload (without its line terminator) */
592
 
589
 
593
    /* append content, if line is non-empty */
590
    /* append content, if line is non-empty */
594
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
591
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
595
      mdr_coutraw_puts("out of memory");
-
 
596
      goto IOERR;
592
      goto IOERR;
597
    }
593
    }
598
 
594
 
599
    /* add a new line if necessary */
595
    /* add a new line if necessary */
600
    if (eolfound) {
596
    if (eolfound) {
601
      if (line_add(db, NULL, 0) != 0) {
597
      if (line_add(db, NULL, 0) != 0) {
602
      /* TODO ERROR HANDLING */
-
 
603
        mdr_coutraw_puts("out of memory");
-
 
604
        goto IOERR;
598
        goto IOERR;
605
      }
599
      }
606
      eolfound = 0;
600
      eolfound = 0;
607
    }
601
    }
608
 
602
 
Line 621... Line 615...
621
 
615
 
622
  /* add an empty line at end if not present already, also rewind cursor to top of file */
616
  /* add an empty line at end if not present already, also rewind cursor to top of file */
623
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
617
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
624
  db_rewind(db);
618
  db_rewind(db);
625
 
619
 
626
  return(db);
620
  return(0);
627
 
621
 
628
  IOERR:
622
  IOERR:
629
  _dos_close(fd);
623
  _dos_close(fd);
630
  return(NULL);
624
  return(2);
631
}
625
}
632
 
626
 
633
 
627
 
634
static int savefile(const struct file *db, const char *newfname) {
628
static int savefile(const struct file *db, const char *newfname) {
635
  int fd;
629
  int fd;
Line 724... Line 718...
724
  }
718
  }
725
}
719
}
726
 
720
 
727
 
721
 
728
int main(void) {
722
int main(void) {
-
 
723
  static struct file dbarr[1];
729
  const char *fname;
724
  const char *fname;
730
  struct file *db;
725
  struct file *db = dbarr;
731
 
726
 
732
  {
727
  {
733
    char nlspath[128], lang[8];
728
    char nlspath[128], lang[8];
734
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
729
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
735
  }
730
  }
Line 740... Line 735...
740
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
735
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
741
    return(0);
736
    return(0);
742
  }
737
  }
743
 
738
 
744
  /* load file */
739
  /* load file */
-
 
740
  {
745
  db = loadfile(fname);
741
    unsigned char err = loadfile(db, fname);
-
 
742
    if (err == 1) {
-
 
743
      mdr_coutraw_puts(svarlang_str(0,11)); /* file not found */
-
 
744
      return(1);
-
 
745
    } else if (err != 0) {
-
 
746
      mdr_coutraw_puts(svarlang_str(0,10)); /* ERROR */
746
  if (db == NULL) return(1);
747
      return(1);
-
 
748
    }
-
 
749
  }
747
 
750
 
748
  if (mdr_cout_init(&screenw, &screenh)) {
751
  if (mdr_cout_init(&screenw, &screenh)) {
749
    /* load color scheme if mdr_cout_init returns a color flag */
752
    /* load color scheme if mdr_cout_init returns a color flag */
750
    SCHEME_TEXT = 0x17;
753
    SCHEME_TEXT = 0x17;
751
    SCHEME_STBAR1 = 0x70;
754
    SCHEME_STBAR1 = 0x70;
Line 895... Line 898...
895
      if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
898
      if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
896
 
899
 
897
      /* ask for filename */
900
      /* ask for filename */
898
      ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
901
      ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
899
      if (fname[0] != 0) {
902
      if (fname[0] != 0) {
-
 
903
        unsigned char err;
900
        clear_file(db);
904
        clear_file(db);
901
        db = loadfile(fname);
905
        err = loadfile(db, fname);
-
 
906
        if (err != 0) {
-
 
907
          if (err == 1) {
-
 
908
            ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
-
 
909
          } else {
-
 
910
            ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
-
 
911
          }
-
 
912
          mdr_bios_tickswait(44); /* 3s */
-
 
913
          clear_file(db);
-
 
914
        }
902
      }
915
      }
903
      uidirty.from = 0;
916
      uidirty.from = 0;
904
      uidirty.to = 0xff;
917
      uidirty.to = 0xff;
905
      uidirty.statusbar = 1;
918
      uidirty.statusbar = 1;
906
 
919