Back to index

lightning-sunbird  0.9+nobinonly
popfile.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*------------------------------------------
00039    POPFILE.C -- Popup Editor File Functions
00040    (c) Charles Petzold, 1992
00041   ------------------------------------------*/
00042 
00043 #include <windows.h>
00044 #include <commdlg.h>
00045 #include <stdlib.h>
00046 
00047 static OPENFILENAME ofn ;
00048 
00049 void PopFileInitialize (HWND hwnd)
00050      {
00051      static char *szFilter[] = { "Text Files (*.TXT)",  "*.txt",
00052                                  "ASCII Files (*.ASC)", "*.asc",
00053                                  "All Files (*.*)",     "*.*",
00054                                  "" } ;
00055 
00056      ofn.lStructSize       = sizeof (OPENFILENAME) ;
00057      ofn.hwndOwner         = hwnd ;
00058      ofn.hInstance         = NULL ;
00059      ofn.lpstrFilter       = szFilter [0] ;
00060      ofn.lpstrCustomFilter = NULL ;
00061      ofn.nMaxCustFilter    = 0 ;
00062      ofn.nFilterIndex      = 0 ;
00063      ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
00064      ofn.nMaxFile          = _MAX_PATH ;
00065      ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
00066      ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT ;
00067      ofn.lpstrInitialDir   = NULL ;
00068      ofn.lpstrTitle        = NULL ;
00069      ofn.Flags             = 0 ;             // Set in Open and Close functions
00070      ofn.nFileOffset       = 0 ;
00071      ofn.nFileExtension    = 0 ;
00072      ofn.lpstrDefExt       = "txt" ;
00073      ofn.lCustData         = 0L ;
00074      ofn.lpfnHook          = NULL ;
00075      ofn.lpTemplateName    = NULL ;
00076      }
00077 
00078 BOOL PopFileOpenDlg (HWND hwnd, LPSTR lpstrFileName, LPSTR lpstrTitleName)
00079      {
00080      ofn.hwndOwner         = hwnd ;
00081      ofn.lpstrFile         = lpstrFileName ;
00082      ofn.lpstrFileTitle    = lpstrTitleName ;
00083      ofn.Flags             = OFN_CREATEPROMPT ;
00084 
00085      return GetOpenFileName (&ofn) ;
00086      }
00087 
00088 BOOL PopFileSaveDlg (HWND hwnd, LPSTR lpstrFileName, LPSTR lpstrTitleName)
00089      {
00090      ofn.hwndOwner         = hwnd ;
00091      ofn.lpstrFile         = lpstrFileName ;
00092      ofn.lpstrFileTitle    = lpstrTitleName ;
00093      ofn.Flags             = OFN_OVERWRITEPROMPT ;
00094 
00095      return GetSaveFileName (&ofn) ;
00096      }
00097 
00098 static long PopFileLength (int hFile)
00099      {
00100      long lCurrentPos = _llseek (hFile, 0L, 1) ;
00101      long lFileLength = _llseek (hFile, 0L, 2) ;
00102      
00103      _llseek (hFile, lCurrentPos, 0) ;
00104 
00105      return lFileLength ;
00106      }
00107 
00108 BOOL PopFileRead (HWND hwndEdit, LPSTR lpstrFileName)
00109      {
00110      long   lLength ;
00111      HANDLE hBuffer ;
00112      int    hFile ;
00113      LPSTR  lpstrBuffer ;
00114 
00115      if (-1 == (hFile = _lopen (lpstrFileName, OF_READ | OF_SHARE_DENY_WRITE)))
00116           return FALSE ;
00117 
00118      if ((lLength = PopFileLength (hFile)) >= 32000)
00119           {
00120           _lclose (hFile) ;
00121           return FALSE ;
00122           }
00123 
00124      if (NULL == (hBuffer = GlobalAlloc (GHND, lLength + 1)))
00125           {
00126           _lclose (hFile) ;
00127           return FALSE ;
00128           }
00129 
00130      lpstrBuffer = GlobalLock (hBuffer) ;
00131      _lread (hFile, lpstrBuffer, (WORD) lLength) ;
00132      _lclose (hFile) ;
00133      lpstrBuffer [(WORD) lLength] = '\0' ;
00134 
00135      SetWindowText (hwndEdit, lpstrBuffer) ;
00136      GlobalUnlock (hBuffer) ;
00137      GlobalFree (hBuffer) ;
00138 
00139      return TRUE ;
00140      }
00141 
00142 BOOL PopFileWrite (HWND hwndEdit, LPSTR lpstrFileName)
00143      {
00144      HANDLE hBuffer ;
00145      int    hFile ;
00146      LPSTR  lpstrBuffer ;
00147      WORD   wLength ;
00148 
00149      if (-1 == (hFile = _lopen (lpstrFileName, OF_WRITE | OF_SHARE_EXCLUSIVE)))
00150           if (-1 == (hFile = _lcreat (lpstrFileName, 0)))
00151                return FALSE ;
00152 
00153      wLength = GetWindowTextLength (hwndEdit) ;
00154      hBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L) ;
00155      lpstrBuffer = (LPSTR) LocalLock (hBuffer) ;
00156 
00157      if (wLength != _lwrite (hFile, lpstrBuffer, wLength))
00158           {
00159           _lclose (hFile) ;
00160           return FALSE ;
00161           }
00162 
00163      _lclose (hFile) ;
00164      LocalUnlock (hBuffer) ;
00165 
00166      return TRUE ;
00167      }