home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap10 / poppadf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.8 KB  |  126 lines

  1. /*-----------------------------------
  2.    POPPADF -- Popup Notepad File I/O
  3.   -----------------------------------*/
  4.  
  5. #include <windows.h>
  6.                                         // in FILEDLG.C
  7.  
  8. int DoFileOpenDlg (HANDLE, WORD, char *, char *, WORD,   char *, POFSTRUCT) ;
  9. int DoFileSaveDlg (HANDLE, WORD, char *, char *, WORD *, char *, POFSTRUCT) ;
  10.  
  11. extern char szAppName  [] ;             // in POPPAD.C
  12. extern char szFileSpec [] ;
  13.  
  14. long FileLength (HANDLE hFile)
  15.      {
  16.      long   lCurrentPos = _llseek (hFile, 0L, 1) ;
  17.      long   lFileLength = _llseek (hFile, 0L, 2) ;
  18.      
  19.      _llseek (hFile, lCurrentPos, 0) ;
  20.  
  21.      return lFileLength ;
  22.      }
  23.  
  24. void OkMessageBox (HWND hwnd, char *szString, char *szFileName)
  25.      {
  26.      char szBuffer [40] ;
  27.  
  28.      wsprintf (szBuffer, szString, (LPSTR) szFileName) ;
  29.  
  30.      MessageBox (hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION) ;
  31.      }
  32.  
  33. BOOL ReadFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
  34.            char *szFileName, BOOL bAskName)
  35.      {
  36.      DWORD  dwLength ;
  37.      HANDLE hFile, hTextBuffer ;
  38.      LPSTR  lpTextBuffer ;
  39.  
  40.      if (bAskName)
  41.           {
  42.           if (!DoFileOpenDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
  43.                                         0x4010, szFileName, pof))
  44.                return FALSE ;
  45.           }
  46.  
  47.      if (-1 == (hFile = OpenFile (szFileName, pof, OF_READ | OF_REOPEN)))
  48.           {
  49.           OkMessageBox (hwnd, "Cannot open file %s", szFileName) ;
  50.           return FALSE ;
  51.           }
  52.  
  53.      if ((dwLength = FileLength (hFile)) >= 32000)
  54.           {
  55.           _lclose (hFile) ;
  56.           OkMessageBox (hwnd, "File %s too large", szFileName) ;
  57.           return FALSE ;
  58.           }
  59.  
  60.      if (NULL == (hTextBuffer = GlobalAlloc (GHND, (DWORD) dwLength + 1)))
  61.           {
  62.           _lclose (hFile) ;
  63.           OkMessageBox (hwnd, "Cannot allocate memory for %s", szFileName) ;
  64.           return FALSE ;
  65.           }
  66.  
  67.      lpTextBuffer = GlobalLock (hTextBuffer) ;
  68.      _lread (hFile, lpTextBuffer, (WORD) dwLength) ;
  69.      _lclose (hFile) ;
  70.      lpTextBuffer [(WORD) dwLength] = '\0' ;
  71.  
  72.      SetWindowText (hwndEdit, lpTextBuffer) ;
  73.      GlobalUnlock (hTextBuffer) ;
  74.      GlobalFree (hTextBuffer) ;
  75.  
  76.      return TRUE ;
  77.      }
  78.  
  79. BOOL WriteFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
  80.         char *szFileName, BOOL bAskName)
  81.      {
  82.      char      szBuffer [40] ;
  83.      HANDLE    hFile, hTextBuffer ;
  84.      NPSTR     npTextBuffer ;
  85.      WORD      wStatus, wLength ;
  86.  
  87.      if (bAskName)
  88.           {
  89.           if (!DoFileSaveDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
  90.                                    &wStatus, szFileName, pof))
  91.                return FALSE ;
  92.  
  93.           if (wStatus == 1)
  94.                {
  95.                wsprintf (szBuffer, "Replace existing %s", (LPSTR) szFileName) ;
  96.                if (IDNO == MessageBox (hwnd, szBuffer, szAppName, 
  97.                                              MB_YESNO | MB_ICONQUESTION))
  98.                     return FALSE ;
  99.                }
  100.           }
  101.      else
  102.           OpenFile (szFileName, pof, OF_PARSE) ;
  103.  
  104.      if (-1 == (hFile = OpenFile (szFileName, pof, OF_CREATE | OF_REOPEN)))
  105.           {
  106.           OkMessageBox (hwnd, "Cannot create file %s", szFileName) ;
  107.           return FALSE ;
  108.           }
  109.  
  110.      wLength = GetWindowTextLength (hwndEdit) ;
  111.      hTextBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L) ;
  112.      npTextBuffer = LocalLock (hTextBuffer) ;
  113.  
  114.      if (wLength != _lwrite (hFile, npTextBuffer, wLength))
  115.           {
  116.           _lclose (hFile) ;
  117.           OkMessageBox (hwnd, "Cannot write file %s to disk", szFileName) ;
  118.           return FALSE ;
  119.           }
  120.  
  121.      _lclose (hFile) ;
  122.      LocalUnlock (hTextBuffer) ;
  123.  
  124.      return TRUE ;
  125.      }
  126.