home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / MULTIPAD.PAK / FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.2 KB  |  307 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: file.c
  9. //
  10. //  PURPOSE: File manipulation functions.
  11. //
  12. //  FUNCTIONS:
  13. //    CmdFileSave   - Save the current document.
  14. //    Open          - Open a file based on a file name and readonly attribute.
  15. //    QuerySaveFile - Save the current document if necessary & the user 
  16. //                      agrees.
  17. //    SetNewBuffer  - Create a new edit buffer.
  18. //    SaveFile      - Save the text from the editor control to the current 
  19. //                      file.
  20. //    SaveAs        - Save the edit text to the specified file.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29.  
  30. void SetNewBuffer(HANDLE);
  31. BOOL SaveFile(HWND);
  32.  
  33. char szFName[256];
  34.  
  35. //
  36. //  FUNCTION: CmdFileSave(HWND, WORD, WORD, HWND)
  37. //
  38. //  PURPOSE: Save the current document.
  39. //
  40. //  PARAMETERS:
  41. //    hwnd     - The window.
  42. //    wCommand - IDM_FILESAVE (unused)
  43. //    wNotify  - Notification number (unused)
  44. //    hwndCtrl - NULL (unused)
  45. //
  46. //  RETURN VALUE:
  47. //    Always returns 0 - command handled.
  48. //
  49. //  COMMENTS:
  50. //
  51. //
  52.  
  53. #pragma argsused
  54. LRESULT CmdFileSave(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  55. {
  56.     // If there is no filename, use the saveas command to get one.
  57.     //   Otherwise, save the file using the current filename.
  58.  
  59.     if (!GetProp(GetActiveMDIChild(), "FilenameSet"))
  60.     {
  61.         CmdFileSaveAs(hwnd, wCommand, 0, hwndCtrl);
  62.     }
  63.     else
  64.     {
  65.         // If the document has not been modified, don't bother saving it.
  66.         if (SendMessage(GetEditWindow(NULL), EM_GETMODIFY, FALSE, 0L))
  67.             SaveFile(hwnd);
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
  73.  
  74. //
  75. //  FUNCTION: Open(LPSTR, BOOL, HWND)
  76. //
  77. //  PURPOSE: Open a file based on a file name and readonly attribute.
  78. //
  79. //  PARAMETERS:
  80. //    lsz - The full path name of the file to be opened.
  81. //    fReadOnly - Read only attribute of the file.
  82. //    hwnd - The window into which the file is being opened.
  83. //
  84. //  RETURN VALUE:
  85. //    NONE
  86. //
  87. //  COMMENTS:
  88. //
  89. //
  90.  
  91. VOID Open(LPSTR lsz, BOOL fReadOnly, HWND hwnd)
  92. {
  93.     HFILE   hfile;      // Hande to the file being opened.
  94.     HCURSOR hcursSave;  // The cursor replace by the hourglass during save.
  95.     LPVOID lpvEdit;     // The edit buffer
  96.     long   cchSize;     // The size of the edit buffer
  97.     UINT   cchRead;     // The number of bytes read from the file
  98.     HANDLE hszT;        // Handle to the edit buffer
  99.  
  100.     // Open the file and get its handle
  101.     hfile = _lopen(lsz, OF_READ | OF_SHARE_DENY_WRITE);
  102.     if (hfile == HFILE_ERROR)
  103.         return;
  104.  
  105.     // Allocate edit buffer to the size of the file + 1
  106.     cchSize = _llseek(hfile, 0L, 2);
  107.     _llseek(hfile,0L,0);
  108.     hszT = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, (UINT)cchSize + 1);
  109.  
  110.     if (!hszT)
  111.     {
  112.         MessageBox(hwnd, 
  113.                    "Not enough memory.", 
  114.                    SZAPPNAME, 
  115.                    MB_OK | MB_ICONHAND);
  116.         return;
  117.     }
  118.     hcursSave = SetCursor(hcursHourGlass);
  119.     lpvEdit = LocalLock(hszT);
  120.  
  121.     cchRead = _lread(hfile, lpvEdit, (UINT)cchSize);
  122.     _lclose(hfile);
  123.  
  124.     // # bytes read must equal file size
  125.     if (cchRead == HFILE_ERROR ||  cchRead != (UINT)cchSize)
  126.     {
  127.         char rgchT[256];
  128.  
  129.         wsprintf(rgchT, "Error reading %s.", lsz);
  130.         SetCursor(hcursSave);      // Remove the hourglass
  131.         MessageBox(hwnd, rgchT, SZAPPNAME, MB_OK | MB_ICONEXCLAMATION);
  132.     }
  133.     LocalUnlock(hszT);
  134.  
  135.     // Set up a new buffer and window title
  136.     {
  137.         SetNewBuffer(hszT);
  138.     }
  139.  
  140.     SendMessage(GetEditWindow(NULL), EM_SETREADONLY, fReadOnly, 0L);
  141.     SetCursor(hcursSave);
  142.     lstrcpy(GetFName(), lsz);
  143.     SetProp(GetActiveMDIChild(), "FilenameSet", (HANDLE)1);
  144. }
  145.  
  146. //
  147. //  FUNCTION: QuerySaveFile(HWND)
  148. //
  149. //  PURPOSE: Save the current document if necessary & the user agrees.
  150. //
  151. //  PARAMETERS:
  152. //    hwnd - The document window.
  153. //
  154. //  RETURN VALUE:
  155. //    TRUE  - The file has been saved, or any changes can be thrown out.
  156. //    FALSE - The use has changed his/her mind, don't continue the
  157. //      operation that would throw away this document.
  158. //
  159. //  COMMENTS:
  160. //
  161. //
  162.  
  163. BOOL QuerySaveFile(HWND hwnd)
  164. {
  165.     int  idResponse;
  166.     char rgch[256];
  167.  
  168.     if (SendMessage(GetEditWindow(NULL), EM_GETMODIFY, 0, 0L))
  169.     {
  170.         wsprintf(rgch, "Save current changes? %s", GetFName());
  171.         idResponse = MessageBox(hwnd, 
  172.                                 rgch, 
  173.                                 SZAPPNAME,  
  174.                                 MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  175.  
  176.         if (idResponse == IDYES)            // Save changes
  177.         {
  178.             // Make sure there is a filename to save to
  179.             if (!GetProp(GetActiveMDIChild(), "FilenameSet"))
  180.                 return (BOOL)CmdFileSaveAs(hwnd, IDM_FILESAVEAS, 0, NULL);
  181.             else
  182.                 SaveFile(hwnd);
  183.         }
  184.         else if (idResponse == IDCANCEL)    // Cancel entire operation
  185.             return FALSE;
  186.     }
  187.  
  188.     return TRUE;                            // Proceed
  189. }
  190.  
  191.  
  192. //
  193. //  FUNCTION: SetNewBuffer(HWND, HANDLE, LPSTR)
  194. //
  195. //  PURPOSE: Create a new edit buffer.
  196. //
  197. //  PARAMETERS:
  198. //    hwnd    - Handle to the main window.
  199. //    hsz     - Local handle to the text (may be NULL).
  200. //    szTitle - Title to display for the user.
  201. //
  202. //  RETURN VALUE:
  203. //    None.
  204. //
  205. //  COMMENTS:
  206. //
  207.  
  208. void SetNewBuffer(HANDLE hsz)
  209. {
  210.     if (!hsz)                    // Allocates a buffer if none exists
  211.         hsz = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, 1);
  212.  
  213.     // Updates the buffer and displays new buffer
  214.     SetEditText(GetEditWindow(NULL), hsz);
  215.  
  216.     SendMessage(GetEditWindow(NULL), EM_SETMODIFY, FALSE, 0L);
  217. }
  218.  
  219.  
  220. //
  221. //  FUNCTION: SaveFile(HWND)
  222. //
  223. //  PURPOSE: Save the text from the editor control to the current file.
  224. //
  225. //  PARAMETERS:
  226. //    hwnd - The main application window.
  227. //
  228. //  RETURN VALUE:
  229. //    TRUE - Sucessfully saved the file.
  230. //    FALSE - Unable to save the file.
  231. //
  232. //  COMMENTS:
  233. //
  234.  
  235. BOOL SaveFile(HWND hwnd)
  236. {
  237.     BOOL  fSuccess;
  238.     int   IOStatus;  // result of a file write
  239.     char  rgch[256];
  240.     HFILE hfile;
  241.     OFSTRUCT ofs;
  242.     HCURSOR hcursSave;
  243.     LPVOID lpvEdit;
  244.  
  245.     hfile = OpenFile(GetFName(),  &ofs, OF_PROMPT | OF_CANCEL | OF_CREATE);
  246.     if (hfile == HFILE_ERROR)
  247.     {
  248.         // If the file can't be saved
  249.  
  250.         wsprintf(rgch, "Cannot write to %s.", GetFName());
  251.         MessageBox(hwnd, rgch, SZAPPNAME, MB_OK | MB_ICONEXCLAMATION);
  252.         return FALSE;
  253.     }
  254.  
  255.     lpvEdit = LockEditText(GetEditWindow(NULL));
  256.  
  257.     // Set the cursor to an hourglass during the file transfer
  258.  
  259.     hcursSave = SetCursor(hcursHourGlass);
  260.     IOStatus = _lwrite(hfile, lpvEdit, lstrlen(lpvEdit));
  261.     _lclose(hfile);
  262.     SetCursor(hcursSave);
  263.  
  264.     if (IOStatus != (int)lstrlen(lpvEdit))
  265.     {
  266.         wsprintf(rgch, "Error writing to %s.", GetFName());
  267.         MessageBox(hwnd, rgch, SZAPPNAME, MB_OK | MB_ICONHAND);
  268.         fSuccess = FALSE;
  269.     }
  270.     else
  271.     {
  272.         fSuccess = TRUE;    // Indicates the file was saved
  273.         SendMessage(GetEditWindow(NULL), EM_SETMODIFY, FALSE, 0L);
  274.     }
  275.  
  276.     UnlockEditText(GetEditWindow(NULL));
  277.     return fSuccess;
  278. }
  279.  
  280.  
  281. //
  282. //  FUNCTION: SaveAs(LPSTR, LPSTR, HWND)
  283. //
  284. //  PURPOSE: Save the edit text to the specified file.
  285. //
  286. //  PARAMETERS:
  287. //    lsz             - The fully qualified path of the file to save.
  288. //    lszCurrentName  - The current name of the file; used to restore 
  289. //                      the caption if saving fails
  290. //    hwnd            - The main application window.
  291. //
  292. //  RETURN VALUE:
  293. //    NONE
  294. //
  295. //  COMMENTS:
  296. //
  297. //
  298.  
  299. #pragma argsused
  300. VOID SaveAs(LPSTR lsz, LPSTR lszCurrentName, HWND hwnd)
  301. {
  302.      if (!SaveFile(hwnd))
  303.           SetWindowText(hwnd, lszCurrentName);
  304.      else
  305.           SetProp(GetActiveMDIChild(), "FilenameSet", (HANDLE)1);
  306. }
  307.