home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / w16gui / popfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.6 KB  |  149 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*------------------------------------------
  20.    POPFILE.C -- Popup Editor File Functions
  21.    (c) Charles Petzold, 1992
  22.   ------------------------------------------*/
  23.  
  24. #include <windows.h>
  25. #include <commdlg.h>
  26. #include <stdlib.h>
  27.  
  28. static OPENFILENAME ofn ;
  29.  
  30. void PopFileInitialize (HWND hwnd)
  31.      {
  32.      static char *szFilter[] = { "Text Files (*.TXT)",  "*.txt",
  33.                                  "ASCII Files (*.ASC)", "*.asc",
  34.                                  "All Files (*.*)",     "*.*",
  35.                                  "" } ;
  36.  
  37.      ofn.lStructSize       = sizeof (OPENFILENAME) ;
  38.      ofn.hwndOwner         = hwnd ;
  39.      ofn.hInstance         = NULL ;
  40.      ofn.lpstrFilter       = szFilter [0] ;
  41.      ofn.lpstrCustomFilter = NULL ;
  42.      ofn.nMaxCustFilter    = 0 ;
  43.      ofn.nFilterIndex      = 0 ;
  44.      ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
  45.      ofn.nMaxFile          = _MAX_PATH ;
  46.      ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
  47.      ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT ;
  48.      ofn.lpstrInitialDir   = NULL ;
  49.      ofn.lpstrTitle        = NULL ;
  50.      ofn.Flags             = 0 ;             // Set in Open and Close functions
  51.      ofn.nFileOffset       = 0 ;
  52.      ofn.nFileExtension    = 0 ;
  53.      ofn.lpstrDefExt       = "txt" ;
  54.      ofn.lCustData         = 0L ;
  55.      ofn.lpfnHook          = NULL ;
  56.      ofn.lpTemplateName    = NULL ;
  57.      }
  58.  
  59. BOOL PopFileOpenDlg (HWND hwnd, LPSTR lpstrFileName, LPSTR lpstrTitleName)
  60.      {
  61.      ofn.hwndOwner         = hwnd ;
  62.      ofn.lpstrFile         = lpstrFileName ;
  63.      ofn.lpstrFileTitle    = lpstrTitleName ;
  64.      ofn.Flags             = OFN_CREATEPROMPT ;
  65.  
  66.      return GetOpenFileName (&ofn) ;
  67.      }
  68.  
  69. BOOL PopFileSaveDlg (HWND hwnd, LPSTR lpstrFileName, LPSTR lpstrTitleName)
  70.      {
  71.      ofn.hwndOwner         = hwnd ;
  72.      ofn.lpstrFile         = lpstrFileName ;
  73.      ofn.lpstrFileTitle    = lpstrTitleName ;
  74.      ofn.Flags             = OFN_OVERWRITEPROMPT ;
  75.  
  76.      return GetSaveFileName (&ofn) ;
  77.      }
  78.  
  79. static long PopFileLength (int hFile)
  80.      {
  81.      long lCurrentPos = _llseek (hFile, 0L, 1) ;
  82.      long lFileLength = _llseek (hFile, 0L, 2) ;
  83.      
  84.      _llseek (hFile, lCurrentPos, 0) ;
  85.  
  86.      return lFileLength ;
  87.      }
  88.  
  89. BOOL PopFileRead (HWND hwndEdit, LPSTR lpstrFileName)
  90.      {
  91.      long   lLength ;
  92.      HANDLE hBuffer ;
  93.      int    hFile ;
  94.      LPSTR  lpstrBuffer ;
  95.  
  96.      if (-1 == (hFile = _lopen (lpstrFileName, OF_READ | OF_SHARE_DENY_WRITE)))
  97.           return FALSE ;
  98.  
  99.      if ((lLength = PopFileLength (hFile)) >= 32000)
  100.           {
  101.           _lclose (hFile) ;
  102.           return FALSE ;
  103.           }
  104.  
  105.      if (NULL == (hBuffer = GlobalAlloc (GHND, lLength + 1)))
  106.           {
  107.           _lclose (hFile) ;
  108.           return FALSE ;
  109.           }
  110.  
  111.      lpstrBuffer = GlobalLock (hBuffer) ;
  112.      _lread (hFile, lpstrBuffer, (WORD) lLength) ;
  113.      _lclose (hFile) ;
  114.      lpstrBuffer [(WORD) lLength] = '\0' ;
  115.  
  116.      SetWindowText (hwndEdit, lpstrBuffer) ;
  117.      GlobalUnlock (hBuffer) ;
  118.      GlobalFree (hBuffer) ;
  119.  
  120.      return TRUE ;
  121.      }
  122.  
  123. BOOL PopFileWrite (HWND hwndEdit, LPSTR lpstrFileName)
  124.      {
  125.      HANDLE hBuffer ;
  126.      int    hFile ;
  127.      LPSTR  lpstrBuffer ;
  128.      WORD   wLength ;
  129.  
  130.      if (-1 == (hFile = _lopen (lpstrFileName, OF_WRITE | OF_SHARE_EXCLUSIVE)))
  131.           if (-1 == (hFile = _lcreat (lpstrFileName, 0)))
  132.                return FALSE ;
  133.  
  134.      wLength = GetWindowTextLength (hwndEdit) ;
  135.      hBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L) ;
  136.      lpstrBuffer = (LPSTR) LocalLock (hBuffer) ;
  137.  
  138.      if (wLength != _lwrite (hFile, lpstrBuffer, wLength))
  139.           {
  140.           _lclose (hFile) ;
  141.           return FALSE ;
  142.           }
  143.  
  144.      _lclose (hFile) ;
  145.      LocalUnlock (hBuffer) ;
  146.  
  147.      return TRUE ;
  148.      }
  149.