GasFlowMeter/User/AGA10/win/file.cpp

197 lines
4.0 KiB
C++
Raw Normal View History

/*************************************************************************
* File : file.cpp
* Description : Supports file access to AGA10 functions
* Author : W.B. Peterson
* Version : 1.7 2002.11.17
* Revisions :
* Copyright (c) 2002 American Gas Association
**************************************************************************/
#include "aga10.h"
// declare a Windows-defined structure for file names static OPENFILENAME ofn ;
/**************************************************************************
* Function : FileInitialize()
* Arguments : HWND
* Returns : void
* Purpose : Prepares for Windows file access
* Revisions :
**************************************************************************/
void FileInitialize (HWND hWnd)
{
/* set file filters; assign the filename extension 'sos' for files of this type */
static char szFilter[] = "AGA10 Files (*.sos)\0*.sos\0" ; static char szExt[] = "sos" ;
// populate a OPENFILENAME structure
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hWnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = NULL ;
ofn.nMaxFile = _MAX_PATH ;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = szExt ;
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
}
/**************************************************************************
* Function : FileOpenDlg()
* Arguments : HWND, PSTR, PSTR
* Returns : BOOL
* Purpose : Access common controls for file-open operation
* Revisions :
**************************************************************************/
BOOL FileOpenDlg (HWND hWnd, PSTR pstrFileName, PSTR pstrTitleName)
{
ofn.hwndOwner = hWnd ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
return GetOpenFileName (&ofn) ;
}
/**************************************************************************
* Function : FileSaveDlg()
* Arguments : HWND, PSTR, PSTR
* Returns : BOOL
* Purpose : Access common controls for file-save operation
* Revisions :
**************************************************************************/
BOOL FileSaveDlg (HWND hWnd, PSTR pstrFileName, PSTR pstrTitleName)
{
ofn.hwndOwner = hWnd ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = OFN_OVERWRITEPROMPT ;
return GetSaveFileName (&ofn) ;
}
/**************************************************************************
* Function : FileRead()
* Arguments : HWND, PSTR, AGA10STRUCT
* Returns : bool
* Purpose : Reads contents of a .sos file into a AGA10STRUCT
* Revisions :
**************************************************************************/
bool FileRead(HWND hWnd, PSTR pstrFileName, AGA10STRUCT *A10)
{
FILE *file ;
// open the file in binary mode, if it exists
if (NULL == (file = fopen (pstrFileName, "rb"))) return false ;
//set file position to beginning rewind(file) ;
//read one (only) data structure
if (fread(A10, sizeof (AGA10STRUCT), 1, file))
{
fclose (file) ; return true;
}
else
{
// some problem encountered
fclose (file) ; return false ;
}
}
/**************************************************************************
* Function : FileWrite()
* Arguments : HWND, PSTR, AGA10STRUCT
* Returns : bool
* Purpose : Writes contents of a AGA10STRUCT into a .vos file
* Revisions :
**************************************************************************/
bool FileWrite(HWND hWnd, PSTR pstrFileName, AGA10STRUCT *A10)
{
FILE *file ;
// open the file in binary mode; create if necessary
if (NULL == (file = fopen (pstrFileName, "wb"))) return false ;
//set file position to beginning rewind(file) ;
//write one (only) data structure
if (fwrite(A10, sizeof (AGA10STRUCT), 1, file))
{
fclose (file) ; return true;
}
else
{
// problem encountered; close and return 'false' fclose (file) ;
return false ;
}
}