home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / winsock / wvnsc926 / wvfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  10.2 KB  |  379 lines

  1.  
  2. /*
  3.  *
  4.  * $Id: wvfile.c 1.8 1994/09/16 00:59:38 jcooper Exp $
  5.  * $Log: wvfile.c $
  6.  * Revision 1.8  1994/09/16  00:59:38  jcooper
  7.  * cleanup for 92.6
  8.  *
  9.  * Revision 1.7  1994/05/27  01:29:29  rushing
  10.  * unnecessary winundoc.h
  11.  *
  12.  * Revision 1.6  1994/01/16  12:03:44  jcoop
  13.  * Mod to MRRWriteLine to allow unsigned size (65536 byte) writes
  14.  *
  15.  * Revision 1.5  1994/01/12  19:27:32  mrr
  16.  * mrr mods 4
  17.  * 
  18.  * Revision 1.4  1993/12/08  01:27:21  rushing
  19.  * new version box and cr lf consistency
  20.  *
  21.  * Revision 1.3  1993/08/25  18:54:36  mbretherton
  22.  * MRB merge, mail & post logging
  23.  *
  24.  * Revision 1.x  1993/07/21 MBretherton
  25.  *                remove vRef from MRROpenFile (Mac Stuff)
  26.  * Revision 1.2  1993/07/06  21:09:09  cnolan
  27.  * win32 support
  28.  *
  29.  * Revision 1.1  1993/02/16  20:53:50  rushing
  30.  * Initial revision
  31.  *
  32.  *
  33.  */
  34.  
  35. /*-- WVFILE.C -- File containing routines to do I/O under Windows.
  36.  */
  37.  
  38. #include <windows.h>
  39. #include <windowsx.h>
  40. #include "wvglob.h"
  41. #include "winvn.h"
  42. #pragma hdrstop
  43. #include <io.h>
  44.  
  45.  
  46. /*--- function MRROpenFile --------------------------------------------
  47.  *
  48.  *  Perform the same function as Windows' OpenFile, but also
  49.  *  create an instance of a structure that keeps track of file-related
  50.  *  information (most importantly, an file buffer so I don't have to
  51.  *  do 1-byte system reads).
  52.  *
  53.  *    Entry    FileName    is the file name of the file to open.
  54.  *             Mode        is the mode under which to open the file.
  55.  *
  56.  *    Exit     MRRFile     points to a dynamically-allocated structure
  57.  *                         containing info about the file.
  58.  *             Returns a handle to the file; 0 if failure.
  59.  */
  60. HFILE
  61. MRROpenFile (FileName, Mode, MRRFile)
  62.      char *FileName;
  63.      int Mode;
  64.      TypMRRFile **MRRFile;
  65. {
  66.   HFILE hMyFile;
  67.   HANDLE hMRR;
  68.   TypMRRFile *MyMRRFile;
  69.   int retcode;
  70.  
  71.   hMRR = LocalAlloc (LMEM_FIXED, sizeof (TypMRRFile));
  72.   if (!hMRR)
  73.     {
  74.       return (0);
  75.     }
  76.   else
  77.     {
  78.       MyMRRFile = (TypMRRFile *) LocalLock (hMRR);
  79.       MyMRRFile->hthis = hMRR;
  80.       MyMRRFile->bufidx = 0;
  81.       MyMRRFile->bytesread = 0;
  82.       MyMRRFile->eofflag = FALSE;
  83.       MyMRRFile->mode = Mode;
  84.  
  85.       if (Mode == OF_WRITE)
  86.    {
  87.      hMyFile = OpenFile (FileName, &(MyMRRFile->of), OF_EXIST);
  88.      if (hMyFile == -1)
  89.        Mode = OF_CREATE;
  90.    }
  91.       hMyFile = retcode = OpenFile ((char far *) FileName, &(MyMRRFile->of), Mode);
  92.       if (retcode == (-1))
  93.    {
  94.      LocalUnlock (hMRR);
  95.      LocalFree (hMRR);
  96.      return (0);
  97.    }
  98.       else
  99.    {
  100.      MyMRRFile->hFile = hMyFile;
  101.    }
  102.     }
  103.   *MRRFile = MyMRRFile;
  104.   return (hMyFile);
  105. }
  106.  
  107. /*--- function MRRCloseFile --------------------------------------------
  108.  *
  109.  *  Perform the same function as the close function, but also
  110.  *  deallocate the structure allocated by MRROpenFile.
  111.  *
  112.  *    Entry    MRRFile  points to a structure describing the file.
  113.  */
  114. void
  115. MRRCloseFile (MRRFile)
  116.      TypMRRFile *MRRFile;
  117. {
  118.   HANDLE hMyMRRFile;
  119.  
  120.   if (MRRFile->mode == OF_WRITE || MRRFile->mode == OF_CREATE)
  121.     {
  122.       _lwrite (MRRFile->hFile, MRRFile->buf, MRRFile->bufidx);
  123.     }
  124.   _lclose (MRRFile->hFile);
  125.  
  126.   hMyMRRFile = MRRFile->hthis;
  127.   LocalUnlock (hMyMRRFile);
  128.   LocalFree (hMyMRRFile);
  129. }
  130.  
  131. /*--- function MRRReadLine ---------------------------------------------
  132.  *
  133.  *  Read in a line from a file, very much like "fgets".
  134.  *  Lines are assumed to be terminated by CR/LF (except that this
  135.  *  is optional for the last line in a file).
  136.  *
  137.  *  No CR, LF, or zero byte is placed in the user's buffer or
  138.  *  counted as a data byte in the returned count.
  139.  *
  140.  *    Entry    MRRFile  points to a structure describing the file.
  141.  *             Linebuf  is the place to put the line.
  142.  *             Len      is the length of Linebuf.
  143.  *
  144.  *    Exit     Linebuf  contains a line.
  145.  *             Returns number of characters read.  0 means an empty line;
  146.  *              -1 means EOF.
  147.  */
  148. int
  149. MRRReadLine (MRRFile, Linebuf, Len)
  150.      TypMRRFile *MRRFile;
  151.      char *Linebuf;
  152.      int Len;
  153. {
  154.   int BytesReturned = 0;
  155.   char ch;
  156.  
  157.   /* If we hit the EOF while reading last time, we might not have   */
  158.   /* had to return an EOF indication then--but we certainly do now. */
  159.  
  160.   if (MRRFile->eofflag)
  161.     return (-1);
  162.  
  163.   /* Read bytes until we exhaust the user's buffer,                 */
  164.   /* empty our own internal buffer,                                 */
  165.   /* or hit a CR (which hopefully belongs to a CR/LF pair).         */
  166.  
  167. readlp:;
  168.   while (Len && MRRFile->bufidx < MRRFile->bytesread &&
  169.     (ch = MRRFile->buf[MRRFile->bufidx]) != '\r' && ch != '\n')
  170.     {
  171.       *(Linebuf++) = ch;
  172.       BytesReturned++;
  173.       (MRRFile->bufidx)++;
  174.       Len--;
  175.     }
  176.  
  177.   /* If we emptied our own internal buffer, fill 'er up again       */
  178.   /* from the file.  If the read hits EOF, return the user's        */
  179.   /* data now (indicating EOF if we never got any data bytes        */
  180.   /* else go back up and continue taking from the buffer.           */
  181.  
  182.   if (MRRFile->bufidx >= MRRFile->bytesread)
  183.     {
  184.       MRRFile->bufidx = 0;
  185.       MRRFile->bytesread = _lread (MRRFile->hFile, MRRFile->buf, BUFSIZE);
  186.       if (MRRFile->bytesread > 0)
  187.    {
  188.      goto readlp;
  189.    }
  190.       else
  191.    {
  192.      MRRFile->eofflag = TRUE;
  193.      if (!BytesReturned)
  194.        BytesReturned--;
  195.      goto endit;
  196.    }
  197.     }
  198.  
  199.   /* If we reach here, we either filled the user's buffer or        */
  200.   /* hit a CR.  No EOF was encountered.                             */
  201.   /* Either way, we must now skip to the beginning of the next      */
  202.   /* line.  This means skipping to the next LF.  Since in most      */
  203.   /* cases the user does specify a big enough buffer, in most       */
  204.   /* cases all we are doing here is reading up the next character   */
  205.   /* (assuming it's a LF).                                          */
  206.   /* All data that should go in the user's buffer is there by now.  */
  207.  
  208. skipLF:;
  209.  
  210.   while (MRRFile->bufidx < MRRFile->bytesread &&
  211.     MRRFile->buf[MRRFile->bufidx] != '\n')
  212.     {
  213.       (MRRFile->bufidx)++;
  214.     }
  215.  
  216.   /* We either found the LineFeed we were looking for, or hit       */
  217.   /* the end of our internal buffer.  If the latter, fill 'er       */
  218.   /* up and try again.                                              */
  219.  
  220.   if (MRRFile->bufidx >= MRRFile->bytesread)
  221.     {
  222.       MRRFile->bufidx = 0;
  223.       MRRFile->bytesread = _lread (MRRFile->hFile, MRRFile->buf, BUFSIZE);
  224.       if (MRRFile->bytesread > 0)
  225.    {
  226.      goto skipLF;
  227.    }
  228.       else
  229.    {
  230.      MRRFile->eofflag = TRUE;
  231.      goto endit;
  232.    }
  233.     }
  234.  
  235.   /* The buffer pointer is now pointing at the LF.  Advance         */
  236.   /* it by one so we'll get the first character of the next         */
  237.   /* line next time.                                                */
  238.   /* If this takes us past the end of the buffer, no problem.       */
  239.  
  240.  
  241.   if (MRRFile->buf[MRRFile->bufidx] == '\n')
  242.     (MRRFile->bufidx)++;
  243.  
  244. endit:;
  245.   return (BytesReturned);
  246. }
  247.  
  248. /*--- function MRRWriteLine ---------------------------------------------
  249.  *
  250.  *  Write out a line of text, followed by a CR and LF.
  251.  *
  252.  *    Entry    MRRFile  points to a structure describing the file.
  253.  *             LineBuf  points to line buffer to write out.
  254.  *             Len      is the number of bytes to write.
  255.  */
  256. BOOL
  257. MRRWriteLine (MRRFile, LineBuf, Len)
  258.      TypMRRFile *MRRFile;
  259.      char far *LineBuf;
  260.      unsigned int Len;
  261. {
  262.   unsigned int BytesToCopy;
  263.   static NotFirst = 0;
  264.  
  265.   do
  266.     {
  267.       BytesToCopy = Len < (BUFSIZE - (unsigned int)MRRFile->bufidx) ?
  268.     Len : BUFSIZE - (unsigned int)MRRFile->bufidx;
  269.  
  270.       MoveBytes (LineBuf, (char far *) (MRRFile->buf + MRRFile->bufidx), BytesToCopy);
  271.       MRRFile->bufidx += BytesToCopy;
  272.       LineBuf += BytesToCopy;
  273.       Len -= BytesToCopy;
  274.       if (MRRFile->bufidx >= BUFSIZE)
  275.    {
  276.      _lwrite (MRRFile->hFile, MRRFile->buf, BUFSIZE);
  277.      MRRFile->bufidx = 0;
  278.    }
  279.     }
  280.   while (Len > 0);
  281.  
  282.   if (!(NotFirst++))
  283.     {
  284.       MRRWriteLine (MRRFile, "\r\n", 2);
  285.     }
  286.   NotFirst--;
  287.   return (1);
  288. }
  289.  
  290. /*-- function MRRWriteDocument -----------------------------------------
  291.  *
  292.  *  Write out an entire document to disk.
  293.  *
  294.  *  Entry   Document    points to a document.
  295.  *          Offset      is the number of bytes to skip at the beginning
  296.  *                      of the line (between the end of the structure
  297.  *                      described in TypLine and the beginning of text).
  298.  *                      In most cases this will be zero.
  299.  *          szFileName  points to the file name to save to.
  300.  *          Append      is TRUE iff we should append to the file.
  301.  *
  302.  *    Returns TRUE iff we wrote the file OK.
  303.  */
  304. BOOL
  305. MRRWriteDocument (Document, Offset, szFileName, Append)
  306.      TypDoc *Document;
  307.      int Offset;
  308.      char *szFileName;
  309.      BOOL Append;
  310. {
  311.   TypMRRFile *MRRFile;
  312.   HFILE hFile;
  313.   TypBlock far *BlockPtr;
  314.   TypLine far *LinePtr;
  315.   int mode;
  316.  
  317.   if (Append)
  318.     {
  319.       mode = OF_WRITE;
  320.     }
  321.   else
  322.     {
  323.       mode = OF_CREATE;
  324.     }
  325.   hFile = MRROpenFile (szFileName, mode, &MRRFile);
  326.   if (Append)
  327.     {
  328.       _llseek (hFile, 0L, 2);
  329.     }
  330.  
  331.   if (hFile)
  332.     {
  333.       LockLine (Document->hFirstBlock, sizeof (TypBlock), (TypLineID) 0L, &BlockPtr, &LinePtr);
  334.       while (LinePtr->length != END_OF_BLOCK)
  335.    {
  336.      MRRWriteLine (MRRFile, ((char far *) LinePtr) + Offset + sizeof (TypLine),
  337.  
  338.          lstrlen (((char far *) LinePtr) + sizeof (TypLine) + Offset));
  339.      NextLine (&BlockPtr, &LinePtr);
  340.    }
  341.       GlobalUnlock (BlockPtr->hCurBlock);
  342.       MRRCloseFile (MRRFile);
  343.     }
  344.   else
  345.     {
  346.       return (0);
  347.     }
  348.   return (TRUE);
  349. }
  350.  
  351.  
  352. /*
  353.  *  Append the edit text to the end of the appropriate log file 
  354.  *   hWnd  - handle to window for warning messages
  355.  *   fName - name of the log file
  356.  *           (will be created if does not exist) 
  357.  *   MBuf  - pointer to string to be written to file
  358.  */
  359. BOOL
  360. WriteEditLog(HWND hWnd, char *szFileName, char *mBuf, unsigned int size)
  361. {
  362.   TypMRRFile *MRRFile;
  363.   HFILE hFile;
  364.     
  365.   hFile = MRROpenFile (szFileName, OF_WRITE, &MRRFile);
  366.   
  367.   if (hFile)
  368.   {
  369.      _llseek (hFile, 0L, 2);
  370.      MRRWriteLine (MRRFile, mBuf, size);
  371.      MRRCloseFile (MRRFile);
  372.   }
  373.   else
  374.   {
  375.      return (0);
  376.   }
  377.   return (TRUE);
  378. }
  379.