home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap13 / PopPad / PopFile.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  6.2 KB  |  201 lines

  1. /*------------------------------------------
  2.    POPFILE.C -- Popup Editor File Functions
  3.   ------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7.  
  8. static OPENFILENAME ofn ;
  9.  
  10. void PopFileInitialize (HWND hwnd)
  11. {
  12.      static TCHAR szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0")  \
  13.                                TEXT ("ASCII Files (*.ASC)\0*.asc\0") \
  14.                                TEXT ("All Files (*.*)\0*.*\0\0") ;
  15.      
  16.      ofn.lStructSize       = sizeof (OPENFILENAME) ;
  17.      ofn.hwndOwner         = hwnd ;
  18.      ofn.hInstance         = NULL ;
  19.      ofn.lpstrFilter       = szFilter ;
  20.      ofn.lpstrCustomFilter = NULL ;
  21.      ofn.nMaxCustFilter    = 0 ;
  22.      ofn.nFilterIndex      = 0 ;
  23.      ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
  24.      ofn.nMaxFile          = MAX_PATH ;
  25.      ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
  26.      ofn.nMaxFileTitle     = MAX_PATH ;
  27.      ofn.lpstrInitialDir   = NULL ;
  28.      ofn.lpstrTitle        = NULL ;
  29.      ofn.Flags             = 0 ;             // Set in Open and Close functions
  30.      ofn.nFileOffset       = 0 ;
  31.      ofn.nFileExtension    = 0 ;
  32.      ofn.lpstrDefExt       = TEXT ("txt") ;
  33.      ofn.lCustData         = 0L ;
  34.      ofn.lpfnHook          = NULL ;
  35.      ofn.lpTemplateName    = NULL ;
  36. }
  37.  
  38. BOOL PopFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  39. {
  40.      ofn.hwndOwner         = hwnd ;
  41.      ofn.lpstrFile         = pstrFileName ;
  42.      ofn.lpstrFileTitle    = pstrTitleName ;
  43.      ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
  44.      
  45.      return GetOpenFileName (&ofn) ;
  46. }
  47.  
  48. BOOL PopFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  49. {
  50.      ofn.hwndOwner         = hwnd ;
  51.      ofn.lpstrFile         = pstrFileName ;
  52.      ofn.lpstrFileTitle    = pstrTitleName ;
  53.      ofn.Flags             = OFN_OVERWRITEPROMPT ;
  54.      
  55.      return GetSaveFileName (&ofn) ;
  56. }
  57.  
  58. BOOL PopFileRead (HWND hwndEdit, PTSTR pstrFileName)
  59. {
  60.      BYTE   bySwap ;
  61.      DWORD  dwBytesRead ;
  62.      HANDLE hFile ;
  63.      int    i, iFileLength, iUniTest ;
  64.      PBYTE  pBuffer, pText, pConv ;
  65.  
  66.           // Open the file.
  67.  
  68.      if (INVALID_HANDLE_VALUE == 
  69.                (hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ,
  70.                                     NULL, OPEN_EXISTING, 0, NULL)))
  71.           return FALSE ;
  72.  
  73.           // Get file size in bytes and allocate memory for read.
  74.           // Add an extra two bytes for zero termination.
  75.                     
  76.      iFileLength = GetFileSize (hFile, NULL) ; 
  77.      pBuffer = malloc (iFileLength + 2) ;
  78.  
  79.           // Read file and put terminating zeros at end.
  80.      
  81.      ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
  82.      CloseHandle (hFile) ;
  83.      pBuffer[iFileLength] = '\0' ;
  84.      pBuffer[iFileLength + 1] = '\0' ;
  85.  
  86.           // Test to see if the text is unicode
  87.  
  88.      iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ;
  89.      
  90.      if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
  91.      {
  92.           pText = pBuffer + 2 ;
  93.           iFileLength -= 2 ;
  94.  
  95.           if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
  96.           {
  97.                for (i = 0 ; i < iFileLength / 2 ; i++)
  98.                {
  99.                     bySwap = ((BYTE *) pText) [2 * i] ;
  100.                     ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
  101.                     ((BYTE *) pText) [2 * i + 1] = bySwap ;
  102.                }
  103.           }
  104.  
  105.                // Allocate memory for possibly converted string
  106.  
  107.           pConv = malloc (iFileLength + 2) ;
  108.  
  109.                // If the edit control is not Unicode, convert Unicode text to 
  110.                // non-Unicode (ie, in general, wide character).
  111.  
  112. #ifndef UNICODE
  113.           WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv, 
  114.                                iFileLength + 2, NULL, NULL) ;
  115.  
  116.                // If the edit control is Unicode, just copy the string
  117. #else
  118.           lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
  119. #endif
  120.  
  121.      }
  122.      else      // the file is not Unicode
  123.      {
  124.           pText = pBuffer ;
  125.  
  126.                // Allocate memory for possibly converted string.
  127.  
  128.           pConv = malloc (2 * iFileLength + 2) ;
  129.  
  130.                // If the edit control is Unicode, convert ASCII text.
  131.  
  132. #ifdef UNICODE
  133.           MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv, 
  134.                                iFileLength + 1) ;
  135.  
  136.                // If not, just copy buffer
  137. #else
  138.           lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
  139. #endif
  140.      }
  141.      
  142.      SetWindowText (hwndEdit, (PTSTR) pConv) ;
  143.      free (pBuffer) ;
  144.      free (pConv) ;
  145.    
  146.      return TRUE ;
  147. }
  148.  
  149. BOOL PopFileWrite (HWND hwndEdit, PTSTR pstrFileName)
  150. {
  151.      DWORD  dwBytesWritten ;
  152.      HANDLE hFile ;
  153.      int    iLength ;
  154.      PTSTR  pstrBuffer ;
  155.      WORD   wByteOrderMark = 0xFEFF ;
  156.  
  157.           // Open the file, creating it if necessary
  158.      
  159.      if (INVALID_HANDLE_VALUE == 
  160.                (hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, 
  161.                                     NULL, CREATE_ALWAYS, 0, NULL)))
  162.           return FALSE ;
  163.  
  164.           // Get the number of characters in the edit control and allocate
  165.           // memory for them.
  166.      
  167.      iLength = GetWindowTextLength (hwndEdit) ;
  168.      pstrBuffer = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR)) ;
  169.      
  170.      if (!pstrBuffer)
  171.      {
  172.           CloseHandle (hFile) ;
  173.           return FALSE ;
  174.      }
  175.  
  176.           // If the edit control will return Unicode text, write the
  177.           // byte order mark to the file.
  178.  
  179. #ifdef UNICODE
  180.      WriteFile (hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL) ;
  181. #endif
  182.  
  183.           // Get the edit buffer and write that out to the file.
  184.      
  185.      GetWindowText (hwndEdit, pstrBuffer, iLength + 1) ;
  186.      WriteFile (hFile, pstrBuffer, iLength * sizeof (TCHAR), 
  187.                 &dwBytesWritten, NULL) ;
  188.      
  189.      if ((iLength * sizeof (TCHAR)) != (int) dwBytesWritten)
  190.      {
  191.           CloseHandle (hFile) ;
  192.           free (pstrBuffer) ;
  193.           return FALSE ;
  194.      }
  195.      
  196.      CloseHandle (hFile) ;
  197.      free (pstrBuffer) ;
  198.      
  199.      return TRUE ;
  200. }
  201.