Diff for /np2/common/textfile.c between versions 1.6 and 1.10

version 1.6, 2005/03/19 18:54:58 version 1.10, 2006/12/10 12:45:46
Line 1 Line 1
 #include        "compiler.h"  #include        "compiler.h"
 #include        "dosio.h"  #include        "strres.h"
 #include        "textfile.h"  #include        "textfile.h"
   #if defined(SUPPORT_TEXTCNV)
   #include        "textcnv.h"
   #endif
   #if defined(SUPPORT_ARC)
   #include        "arc.h"
   #else
   #include        "dosio.h"
   #endif
   
   
 typedef struct {  // ---- arc support?
         UINT    mode;  
         FILEH   fh;  
         BOOL    xendian;  
 } _TEXTFH, *TEXTFH;  
   
 typedef struct {  #if defined(SUPPORT_ARC)
         _TEXTFH tf;  #define _FILEH                          ARCFH
   #define _FILEH_INVALID          NULL
   #define _FSEEK_SET                      ARCSEEK_SET
   #define _file_open_rb           arcex_fileopen
   #define _file_create            arcex_filecreate
   #define _file_read                      arc_fileread
   #define _file_write                     arc_filewrite
   #define _file_seek                      arc_fileseek
   #define _file_close                     arc_fileclose
   #else
   #define _FILEH                          FILEH
   #define _FILEH_INVALID          FILEH_INVALID
   #define _FSEEK_SET                      FSEEK_SET
   #define _file_open_rb           file_open_rb
   #define _file_create            file_create
   #define _file_read                      file_read
   #define _file_write                     file_write
   #define _file_seek                      file_seek
   #define _file_close                     file_close
   #endif
   
   enum {
           TFMODE_READ             = 0x01,
           TFMODE_WRITE    = 0x02
   };
   
   struct _textfile;
   typedef struct _textfile                _TEXTFILE;
   typedef struct _textfile                *TEXTFILE;
   
   typedef BRESULT (*READFN)(TEXTFILE tf, void *buffer, UINT size);
   
   struct _textfile {
           UINT8   mode;
           UINT8   width;
   #if defined(SUPPORT_TEXTCNV)
           UINT8   access;
           UINT8   xendian;
   #endif
           _FILEH  fh;
           long    fpos;
         UINT8   *buf;          UINT8   *buf;
         UINT    bufsize;          UINT    bufsize;
         UINT    bufpos;          UINT    bufpos;
         UINT    bufrem;          UINT    bufrem;
 } _TEXTREAD, *TEXTREAD;          READFN  readfn;
   #if defined(SUPPORT_TEXTCNV)
           UINT8           *cnvbuf;
           UINT            cnvbufsize;
           TCTOOEM         tooem;
           TCFROMOEM       fromoem;
   #endif
   };
   
   
 // ---- A  // ---- ReadA
   
 static UINT fillbufferA(TEXTREAD tr) {  static UINT readbufferA(TEXTFILE tf) {
   
         UINT    rsize;          UINT    rsize;
   
         if (tr->bufrem == 0) {          if (tf->bufrem == 0) {
                 rsize = file_read(tr->tf.fh, tr->buf, tr->bufsize);                  rsize = _file_read(tf->fh, tf->buf, tf->bufsize);
                 tr->bufpos = 0;                  rsize = rsize / sizeof(char);
                 tr->bufrem = rsize;                  tf->fpos += rsize * sizeof(char);
                   tf->bufpos = 0;
                   tf->bufrem = rsize;
         }          }
         return(tr->bufrem);          return(tf->bufrem);
 }  }
   
 static BRESULT readlineA(TEXTREAD tr, void *buffer, UINT size) {  static BRESULT readlineA(TEXTFILE tf, void *buffer, UINT size) {
   
         UINT8   *dst;          char    *dst;
         BOOL    crlf;          BOOL    crlf;
         BRESULT ret;          BRESULT ret;
         UINT8   c;          char    c;
 const UINT8     *src;  const char      *src;
         UINT    pos;          UINT    pos;
   
         if (size == 0) {          if (size == 0) {
Line 46  const UINT8 *src; Line 99  const UINT8 *src;
                 size = 0;                  size = 0;
         }          }
         else {          else {
                 dst = (UINT8 *)buffer;                  dst = (char *)buffer;
                 size--;                  size--;
         }          }
   
Line 54  const UINT8 *src; Line 107  const UINT8 *src;
         ret = FAILURE;          ret = FAILURE;
         c = 0;          c = 0;
         do {          do {
                 if (fillbufferA(tr) == 0) {                  if (readbufferA(tf) == 0) {
                         break;                          break;
                 }                  }
                 ret = SUCCESS;                  ret = SUCCESS;
                 src = tr->buf;                  src = (char *)tf->buf;
                 src += tr->bufpos;                  src += tf->bufpos;
                 pos = 0;                  pos = 0;
                 while(pos<tr->bufrem) {                  while(pos<tf->bufrem) {
                         c = src[pos];                          c = src[pos];
                         pos++;                          pos++;
                         if ((c == 0x0d) || (c == 0x0a)) {                          if ((c == 0x0d) || (c == 0x0a)) {
Line 73  const UINT8 *src; Line 126  const UINT8 *src;
                                 *dst++ = c;                                  *dst++ = c;
                         }                          }
                 }                  }
                 tr->bufpos += pos;                  tf->bufpos += pos;
                 tr->bufrem -= pos;                  tf->bufrem -= pos;
         } while(!crlf);          } while(!crlf);
         if ((crlf) && (c == 0x0d)) {          if ((crlf) && (c == 0x0d)) {
                 if (fillbufferA(tr) != 0) {                  if (readbufferA(tf) != 0) {
                         src = tr->buf;                          src = (char *)tf->buf;
                         src += tr->bufpos;                          src += tf->bufpos;
                         if (*src == 0x0a) {                          if (*src == 0x0a) {
                                 tr->bufpos++;                                  tf->bufpos++;
                                 tr->bufrem--;                                  tf->bufrem--;
                         }                          }
                 }                  }
         }          }
Line 93  const UINT8 *src; Line 146  const UINT8 *src;
 }  }
   
   
 // ---- W  // ---- ReadW
   
 static UINT fillbufferW(TEXTREAD tr) {  static UINT readbufferW(TEXTFILE tf) {
   
         UINT    rsize;          UINT    rsize;
         UINT8   *buf;          UINT8   *buf;
         UINT8   tmp;  
   
         if (tr->bufrem == 0) {          if (tf->bufrem == 0) {
                 buf = tr->buf;                  buf = tf->buf;
                 rsize = file_read(tr->tf.fh, buf, tr->bufsize) / 2;                  rsize = _file_read(tf->fh, buf, tf->bufsize);
                 tr->bufpos = 0;                  rsize = rsize / sizeof(UINT16);
                 tr->bufrem = rsize / 2;                  tf->fpos += rsize * sizeof(UINT16);
                 if (tr->tf.xendian) {                  tf->bufpos = 0;
                         while(rsize) {                  tf->bufrem = rsize;
                                 tmp = buf[0];  #if defined(SUPPORT_TEXTCNV)
                                 buf[0] = buf[1];                  if (tf->xendian) {
                                 buf[1] = tmp;                          textcnv_swapendian16(buf, rsize);
                                 buf += 2;  
                                 rsize--;  
                         }  
                 }                  }
   #endif  // defined(SUPPORT_TEXTCNV)
         }          }
         return(tr->bufrem);          return(tf->bufrem);
 }  }
   
 static BRESULT readlineW(TEXTREAD tr, void *buffer, UINT size) {  static BRESULT readlineW(TEXTFILE tf, void *buffer, UINT size) {
   
         UINT16          *dst;          UINT16          *dst;
         BOOL            crlf;          BOOL            crlf;
Line 140  const UINT16 *src; Line 190  const UINT16 *src;
         ret = FAILURE;          ret = FAILURE;
         c = 0;          c = 0;
         do {          do {
                 if (fillbufferW(tr) == 0) {                  if (readbufferW(tf) == 0) {
                         break;                          break;
                 }                  }
                 ret = SUCCESS;                  ret = SUCCESS;
                 src = (UINT16 *)tr->buf;                  src = (UINT16 *)tf->buf;
                 src += tr->bufpos;                  src += tf->bufpos;
                 pos = 0;                  pos = 0;
                 while(pos<tr->bufrem) {                  while(pos<tf->bufrem) {
                         c = src[pos];                          c = src[pos];
                         pos++;                          pos++;
                         if ((c == 0x0d) || (c == 0x0a)) {                          if ((c == 0x0d) || (c == 0x0a)) {
Line 159  const UINT16 *src; Line 209  const UINT16 *src;
                                 *dst++ = c;                                  *dst++ = c;
                         }                          }
                 }                  }
                 tr->bufpos += pos;                  tf->bufpos += pos;
                 tr->bufrem -= pos;                  tf->bufrem -= pos;
         } while(!crlf);          } while(!crlf);
         if ((crlf) && (c == 0x0d)) {          if ((crlf) && (c == 0x0d)) {
                 if (fillbufferW(tr) != 0) {                  if (readbufferW(tf) != 0) {
                         src = (UINT16 *)tr->buf;                          src = (UINT16 *)tf->buf;
                         src += tr->bufpos;                          src += tf->bufpos;
                         if (*src == 0x0a) {                          if (*src == 0x0a) {
                                 tr->bufpos++;                                  tf->bufpos++;
                                 tr->bufrem--;                                  tf->bufrem--;
                         }                          }
                 }                  }
         }          }
Line 179  const UINT16 *src; Line 229  const UINT16 *src;
 }  }
   
   
   // ---- read with convert
   
   #if defined(SUPPORT_TEXTCNV)
   static BRESULT readlineAtoOEM(TEXTFILE tf, void *buffer, UINT size) {
   
           BRESULT ret;
   
           ret = readlineA(tf, tf->cnvbuf, tf->cnvbufsize);
           if (ret == SUCCESS) {
                   (tf->tooem)(buffer, size, tf->cnvbuf, (UINT)-1);
           }
           return(ret);
   }
   
   static BRESULT readlineWtoOEM(TEXTFILE tf, void *buffer, UINT size) {
   
           BRESULT ret;
   
           ret = readlineW(tf, tf->cnvbuf, tf->cnvbufsize / 2);
           if (ret == SUCCESS) {
                   (tf->tooem)(buffer, size, tf->cnvbuf, (UINT)-1);
           }
           return(ret);
   }
   #endif  // defined(SUPPORT_TEXTCNV)
   
   
   // ---- write
   
   static BRESULT flushwritebuffer(TEXTFILE tf) {
   
           UINT    size;
           UINT    wsize;
   
           if (tf->bufpos) {
                   size = tf->bufpos * tf->width;
                   wsize = _file_write(tf->fh, tf->buf, size);
                   tf->fpos += wsize;
                   if (wsize != size) {
                           return(FAILURE);
                   }
           }
           return(SUCCESS);
   }
   
   static BRESULT writebufferA(TEXTFILE tf, const void *buffer, UINT size) {
   
           BRESULT ret;
   const UINT8     *p;
           UINT    wsize;
   
           ret = SUCCESS;
           p = (UINT8 *)buffer;
           while(size) {
                   wsize = min(size, tf->bufrem);
                   if (wsize) {
                           CopyMemory(tf->buf + tf->bufpos, p, wsize);
                           p += wsize;
                           size -= wsize;
                           tf->bufpos += wsize;
                           tf->bufrem -= wsize;
                   }
                   if (tf->bufrem == 0) {
                           ret = flushwritebuffer(tf);
                           tf->bufpos = 0;
                           tf->bufrem = tf->bufsize / sizeof(char);
                   }
           }
           return(ret);
   }
   
   static BRESULT writebufferW(TEXTFILE tf, const void *buffer, UINT size) {
   
           BRESULT ret;
   const UINT8     *p;
           UINT8   *q;
           UINT    wsize;
   
           ret = SUCCESS;
           p = (UINT8 *)buffer;
           while(size) {
                   wsize = min(size, tf->bufrem);
                   if (wsize) {
                           q = tf->buf + (tf->bufpos * sizeof(UINT16));
                           CopyMemory(q, p, wsize * sizeof(UINT16));
   #if defined(SUPPORT_TEXTCNV)
                           if (tf->xendian) {
                                   textcnv_swapendian16(q, wsize);
                           }
   #endif  // defined(SUPPORT_TEXTCNV)
                           p += wsize * sizeof(UINT16);
                           size -= wsize;
                           tf->bufpos += wsize;
                           tf->bufrem -= wsize;
                   }
                   if (tf->bufrem == 0) {
                           ret = flushwritebuffer(tf);
                           tf->bufpos = 0;
                           tf->bufrem = tf->bufsize / sizeof(UINT16);
                   }
           }
           return(ret);
   }
   
   
 // ----  // ----
   
 TEXTFILEH textfile_open(const OEMCHAR *filename, UINT buffersize) {  static TEXTFILEH registfile(_FILEH fh, UINT buffersize,
                                                                                           const UINT8 *hdr, UINT hdrsize) {
   
         FILEH           fh;          UINT            cnvbufsize;
         UINT8           hdr[4];  #if defined(SUPPORT_TEXTCNV)
         UINT            hdrsize;          TCINF           inf;
   #endif
         long            fpos;          long            fpos;
         UINT            srcwidth;          UINT8           width;
         BOOL            xendian;          READFN          readfn;
         TEXTREAD        ret;          TEXTFILE        ret;
   
         buffersize = buffersize & (~3);          buffersize = buffersize & (~3);
         if (buffersize < 256) {          if (buffersize < 256) {
                 buffersize = 256;                  buffersize = 256;
         }          }
         fh = file_open_rb(filename);          cnvbufsize = 0;
         if (fh == FILEH_INVALID) {  #if defined(SUPPORT_TEXTCNV)
                 goto tfo_err1;          if (textcnv_getinfo(&inf, hdr, hdrsize) == 0) {
                   return(NULL);
           }
           fpos = inf.hdrsize;
           width = inf.width;
           if (inf.width == 1) {
                   readfn = (inf.tooem != NULL)?readlineAtoOEM:readlineA;
           }
           else if (inf.width == 2) {
                   buffersize = buffersize * 2;
                   readfn = (inf.tooem != NULL)?readlineWtoOEM:readlineW;
           }
           else {
                   return(NULL);
           }
           if ((inf.tooem != NULL) || (inf.fromoem != NULL)) {
                   cnvbufsize = buffersize;
         }          }
         hdrsize = file_read(fh, hdr, 4);  #else   // defined(SUPPORT_TEXTCNV)
         fpos = 0;          fpos = 0;
         srcwidth = 1;          width = 1;
         xendian = FALSE;  
         if ((hdrsize >= 3) &&          if ((hdrsize >= 3) &&
                 (hdr[0] == 0xef) && (hdr[1] == 0xbb) && (hdr[2] == 0xbf)) {                  (hdr[0] == 0xef) && (hdr[1] == 0xbb) && (hdr[2] == 0xbf)) {
                 // UTF-8                  // UTF-8
Line 211  TEXTFILEH textfile_open(const OEMCHAR *f Line 383  TEXTFILEH textfile_open(const OEMCHAR *f
         else if ((hdrsize >= 2) && (hdr[0] == 0xff) && (hdr[1] == 0xfe)) {          else if ((hdrsize >= 2) && (hdr[0] == 0xff) && (hdr[1] == 0xfe)) {
                 // UCSLE                  // UCSLE
                 fpos = 2;                  fpos = 2;
                 srcwidth = 2;                  width = 2;
 #if defined(BYTESEX_BIG)  #if defined(BYTESEX_BIG)
                 xendian = TRUE;                  return(NULL);
 #endif  #endif  // defined(BYTESEX_BIG)
         }          }
         else if ((hdrsize >= 2) && (hdr[0] == 0xfe) && (hdr[1] == 0xff)) {          else if ((hdrsize >= 2) && (hdr[0] == 0xfe) && (hdr[1] == 0xff)) {
                 // UCS2BE                  // UCS2BE
                 fpos = 2;                  fpos = 2;
                 srcwidth = 2;                  width = 2;
 #if defined(BYTESEX_LITTLE)  #if defined(BYTESEX_LITTLE)
                 xendian = TRUE;                  return(NULL);
 #endif  #endif  // defined(BYTESEX_LITTLE)
         }          }
           if (width != sizeof(OEMCHAR)) {
         if (srcwidth != sizeof(OEMCHAR)) {                  return(NULL);
                 goto tfo_err2;  
         }          }
         buffersize = buffersize * sizeof(OEMCHAR);          buffersize = buffersize * sizeof(OEMCHAR);
           readfn = (width == 2)?readlineW:readlineA;
   #endif  // defined(SUPPORT_TEXTCNV)
   
         if (file_seek(fh, fpos, FSEEK_SET) != fpos) {          ret = (TEXTFILE)_MALLOC(sizeof(_TEXTFILE) + buffersize + cnvbufsize,
                 goto tfo_err2;                                                                                                                                  "TEXTFILE");
         }  
   
         ret = (TEXTREAD)_MALLOC(sizeof(_TEXTREAD) + buffersize, filename);  
         if (ret == NULL) {          if (ret == NULL) {
                 goto tfo_err2;                  return(NULL);
         }          }
         ZeroMemory(ret, sizeof(_TEXTREAD));          ZeroMemory(ret, sizeof(_TEXTFILE));
         ret->tf.mode = 0;  //      ret->mode = 0;
         ret->tf.fh = fh;          ret->width = width;
         ret->tf.xendian = xendian;          ret->fh = fh;
           ret->fpos = fpos;
         ret->buf = (UINT8 *)(ret + 1);          ret->buf = (UINT8 *)(ret + 1);
         ret->bufsize = buffersize;          ret->bufsize = buffersize;
           ret->readfn = readfn;
   #if defined(SUPPORT_TEXTCNV)
           ret->access = inf.caps;
           ret->xendian = inf.xendian;
           ret->cnvbuf = ret->buf + buffersize;
           ret->cnvbufsize = cnvbufsize;
           ret->tooem = inf.tooem;
           ret->fromoem = inf.fromoem;
   #endif  // defined(SUPPORT_TEXTCNV)
         return((TEXTFILEH)ret);          return((TEXTFILEH)ret);
   }
   
   static BRESULT flushfile(TEXTFILE tf) {
   
           BRESULT ret;
           long    fpos;
           UINT    size;
           UINT    wsize;
   
           ret = SUCCESS;
           if (tf->mode & TFMODE_READ) {
                   fpos = tf->fpos - (tf->bufrem * tf->width);
                   tf->fpos = _file_seek(tf->fh, fpos, _FSEEK_SET);
                   if (tf->fpos != fpos) {
                           ret = FAILURE;
                   }
           }
           else if (tf->mode & TFMODE_WRITE) {
                   if (tf->bufpos) {
                           size = tf->bufpos * tf->width;
                           wsize = _file_write(tf->fh, tf->buf, size);
                           if (wsize != size) {
                                   ret = FAILURE;
                           }
                           tf->fpos += wsize;
                   }
           }
           else {
                   fpos = _file_seek(tf->fh, tf->fpos, _FSEEK_SET);
                   if (tf->fpos != fpos) {
                           ret = FAILURE;
                   }
                   tf->fpos = fpos;
           }
           tf->mode = 0;
           tf->bufpos = 0;
           tf->bufrem = 0;
           return(ret);
   }
   
   
   // ----
   
   TEXTFILEH textfile_open(const OEMCHAR *filename, UINT buffersize) {
   
           _FILEH          fh;
           UINT8           hdr[4];
           UINT            hdrsize;
           TEXTFILEH       ret;
   
 tfo_err2:          fh = _file_open_rb(filename);
         file_close(fh);          if (fh == _FILEH_INVALID) {
                   goto tfo_err;
           }
           hdrsize = _file_read(fh, hdr, sizeof(hdr));
           ret = registfile(fh, buffersize, hdr, hdrsize);
           if (ret) {
                   return(ret);
           }
           _file_close(fh);
   
 tfo_err1:  tfo_err:
         return(NULL);          return(NULL);
 }  }
   
 void textfile_close(TEXTFILEH tfh) {  TEXTFILEH textfile_create(const OEMCHAR *filename, UINT buffersize) {
   
         if (tfh) {          _FILEH          fh;
                 file_close(((TEXTFH)tfh)->fh);  const UINT8             *hdr;
                 _MFREE(tfh);          UINT            hdrsize;
           TEXTFILEH       ret;
   
           fh = _file_create(filename);
           if (fh == _FILEH_INVALID) {
                   goto tfc_err1;
           }
   #if defined(OSLANG_UTF8)
           hdr = str_utf8;
           hdrsize = sizeof(str_utf8);
   #elif defined(OSLANG_UCS2) 
           hdr = (UINT8 *)str_ucs2;
           hdrsize = sizeof(str_ucs2);
   #else
           hdr = NULL;
           hdrsize = 0;
   #endif
           if ((hdrsize) && (_file_write(fh, hdr, hdrsize) != hdrsize)) {
                   goto tfc_err2;
         }          }
           ret = registfile(fh, buffersize, hdr, hdrsize);
           if (ret) {
                   return(ret);
           }
   
   tfc_err2:
           _file_close(fh);
   
   tfc_err1:
           return(NULL);
 }  }
   
 BRESULT textfile_read(TEXTFILEH tfh, OEMCHAR *buffer, UINT size) {  BRESULT textfile_read(TEXTFILEH tfh, OEMCHAR *buffer, UINT size) {
   
         TEXTREAD        tr;          TEXTFILE        tf;
   
         tr = (TEXTREAD)tfh;          tf = (TEXTFILE)tfh;
         if (tr) {          if (tf == NULL) {
                 if (sizeof(OEMCHAR) == 1) {                  return(FAILURE);
                         return(readlineA(tr, buffer, size));          }
                 }  #if defined(SUPPORT_TEXTCNV)
                 else if (sizeof(OEMCHAR) == 2) {          if (!(tf->access & TEXTCNV_READ)) {
                         return(readlineW(tr, buffer, size));                  return(FAILURE);
           }
   #endif  // defined(SUPPORT_TEXTCNV)
           if (!(tf->mode & TFMODE_READ)) {
                   flushfile(tf);
                   tf->mode = TFMODE_READ;
           }
           return((tf->readfn)(tf, buffer, size));
   }
   
   BRESULT textfile_write(TEXTFILEH tfh, const OEMCHAR *buffer) {
   
           TEXTFILE        tf;
   const void              *buf;
           UINT            leng;
   
           tf = (TEXTFILE)tfh;
           if (tf == NULL) {
                   return(FAILURE);
           }
   #if defined(SUPPORT_TEXTCNV)
           if (!(tf->access & TEXTCNV_WRITE)) {
                   return(FAILURE);
           }
   #endif  // defined(SUPPORT_TEXTCNV)
           if (!(tf->mode & TFMODE_WRITE)) {
                   flushfile(tf);
                   tf->mode = TFMODE_WRITE;
           }
           leng = OEMSTRLEN(buffer);
   #if defined(SUPPORT_TEXTCNV)
           if (tf->fromoem != NULL) {
                   leng = (tf->fromoem)(tf->cnvbuf, tf->cnvbufsize / tf->width,
                                                                                                                                   buffer, leng);
                   buf = tf->cnvbuf;
           }
           else {
                   buf = buffer;
           }
   #else   // defined(SUPPORT_TEXTCNV)
           buf = buffer;
   #endif  // defined(SUPPORT_TEXTCNV)
           if (tf->width == 1) {
                   return(writebufferA(tf, buf, leng));
           }
           else if (tf->width == 2) {
                   return(writebufferW(tf, buf, leng));
           }
           else {
                   return(FAILURE);
           }
   }
   
   void textfile_close(TEXTFILEH tfh) {
   
           TEXTFILE        tf;
   
           tf = (TEXTFILE)tfh;
           if (tf) {
                   if (tf->mode & TFMODE_WRITE) {
                           flushfile(tf);
                 }                  }
                   _file_close(tf->fh);
                   _MFREE(tf);
         }          }
         return(FAILURE);  
 }  }
   

Removed from v.1.6  
changed lines
  Added in v.1.10


RetroPC.NET-CVS <cvs@retropc.net>