File:  [WindowsNT SDKs] / mstools / samples / sdktools / porttool / ptfile.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:24:28 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-nov-1993, ntsdk-jul-1993, HEAD
Microsoft Windows NT Build 511 (SDK Final Release) 07-24-1993


/******************************************************************************\
*       This is a part of the Microsoft Source Code Samples. 
*       Copyright (C) 1993 Microsoft Corporation.
*       All rights reserved. 
*       This source code is only intended as a supplement to 
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the 
*       Microsoft samples programs.
\******************************************************************************/

#include "PortTool.h"
#include <string.h>

/* global file name variables */
char    lpszFilePath[MAX_PATH];
char	lpszFilterSpec[128] =	   /* load here since resource compiler hoses it */
      "C Source Files (*.C)\0*.C\0C Header Files (*.H)\0*.H\0All Files (*.*)\0*.*\0";


/* call the OpenFile common dialog to get a filename */
BOOL WINAPI GetFileName (
    HWND    hWnd,
    char    *lpszFileTitle,
    char    *lpszFilePath)
{
    OPENFILENAME    ofn;
    char        lpszFileOpen[25];
    char        lpszExt[10];

    strcpy (lpszFileTitle, "");
    LoadString ((HANDLE)GetModuleHandle (NULL),
        IDS_DEFAULTFILEEXT,
        lpszExt,
        sizeof (lpszExt));
    LoadString ((HANDLE)GetModuleHandle (NULL),
        IDS_FILEOPENTITLE,
        lpszFileOpen,
        sizeof (lpszFileOpen));

    /* fill in non-variant fields of OPENFILENAME struct. */
    ofn.lStructSize       = sizeof(OPENFILENAME);
    ofn.hwndOwner     = NULL;
    ofn.lpstrFilter   = lpszFilterSpec;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter    = 0;
    ofn.nFilterIndex      = 0;
    ofn.lpstrFile     = lpszFilePath;
    ofn.nMaxFile      = MAX_PATH;
    ofn.lpstrInitialDir   = NULL;
    ofn.lpstrFileTitle    = lpszFileTitle;
    ofn.nMaxFileTitle     = MAX_PATH;
    ofn.lpstrTitle    = lpszFileOpen;
    ofn.lpstrDefExt   = lpszExt;
    ofn.Flags         = OFN_FILEMUSTEXIST;

    /* call common open dialog and return result */
    return (GetOpenFileName ((LPOPENFILENAME)&ofn));
}



/* function retrieves the filename from the path */
BOOL WINAPI GetFileFromPath (
    char    *lpszFullPath,
    char    *lpszFile)
{
    char    *lpPtr = lpszFullPath + strlen (lpszFullPath);

    /* file is at end of path, so search backwards to first \ or : char */
    while (lpPtr > lpszFullPath)
	{
	if (*lpPtr == '\\' ||
	    *lpPtr == ':')
	    {
	    lpPtr++;
	    break;
	    }
	lpPtr--;
	}

    /* return filename if found, or full path passed in */
    strcpy (lpszFile, lpPtr);

    return (lpPtr > lpszFullPath);
}



/* retrieve ini file and path */
BOOL WINAPI GetIniFile (
    char    *lpszFile)
{
    char    lpszPath[MAX_PATH];
    char    *lpPath;
    char    lpszFileName[MAX_PATH];
    OFSTRUCT    of;

    /* get module directory and path */
    GetModuleFileName (NULL, lpszPath, MAX_PATH);
    lpPath = lpszPath + strlen (lpszPath);

    /* find end of path by searching backwards from end to first '\' or ':' */
    while (lpPath > lpszPath)
	{
	if (*lpPath == '\\' ||
	    *lpPath == ':')
	    {
	    lpPath++;
	    break;
	    }
	lpPath--;
	}

    /*terminate at end of path */
    *lpPath = 0;

    /* append ini filename to path */
    LoadString (GetModuleHandle (NULL), IDS_INIFILE, lpszFileName, MAX_PATH);
    strcat (lpPath, lpszFileName);

    /* test for existance */
    if (!(OpenFile (lpszPath, &of, OF_EXIST)))
	{
	GetWindowsDirectory (lpszPath, MAX_PATH);
	strcat (lpszPath, lpszFileName);
	if (!(OpenFile (lpszPath, &of, OF_EXIST)))
	    return FALSE;
	else
	    {
	    strcpy (lpszFile, lpszPath);
	    return TRUE;
	    }
	}
    else
	{
	strcpy (lpszFile, lpszPath);
	return TRUE;
	}
}


/* open a file and load into edit control */
int WINAPI LoadFile (
    HWND    hWnd,
    char    *lpszName)
{
    LONG    lLength;
    HWND    hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
    HANDLE  hEditData;
    char    *lpEditData;
    HFILE   hFile;
    OFSTRUCT    of;

    /* open file for read & write */
    if ((hFile = OpenFile(lpszName, &of, OF_READWRITE)) == -1)
    /* return error */
    return IDS_OPENFAILED;

    /* get file length */
    if (lLength = _llseek(hFile, 0L, 2))
       _llseek(hFile, 0L, 0);
    else
    {
    /* close file and return error */
    CloseHandle ((HANDLE)hFile);
    return IDS_NOSIZE;
    }

    /* get the edit control's memory handle */
    if (!(hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L)))
    {
    /* close file and return error */
    CloseHandle ((HANDLE)hFile);
    return IDS_GETHANDLEFAILED;
    }

    /* realloc the memory to fit the new file size */
    if (((hEditData = LocalReAlloc(hEditData, lLength+1, LHND)) == NULL) ||
    (!(lpEditData = (char *)LocalLock (hEditData))))
    {
    /* close file and return error */
    CloseHandle ((HANDLE)hFile);
    return IDS_REALLOCFAILED;
    }

    /* read the file into hEditData buffer */
    if (_lread(hFile, lpEditData, lLength) == -1)
    {
    /* close file and return error */
    CloseHandle ((HANDLE)hFile);
    return IDS_READFAILED;
    }

    /* null terminate edit buffer */
    lpEditData[lLength] = 0;
    LocalUnlock (hEditData);

    /* load buffer into edit control and close file */
    SendMessage (hWndEdit, EM_SETHANDLE, (UINT)hEditData, 0L);
    CloseHandle ((HANDLE)hFile);

    /* return success */
    return TRUE;
}



/* save file to disk */
int WINAPI SaveFile (
    HWND    hWnd,
    char    *lpszFile)
{
    HANDLE   hEditData;
    int      nLength;
    DWORD    dwWritten;
    HANDLE   hFile;
    HWND     hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
    char     *lpEditData;


    /* open the file for writing */
    hFile = CreateFile (lpszFile,
            GENERIC_WRITE,
            0,
            NULL,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL,
            NULL);

    /* validate file handle */
    if (!hFile)
    return IDS_WRITEOPENFAILED;

    /* find out the length of the text in the edit control */
    nLength = GetWindowTextLength (hWndEdit);

    /* get handle to Edit text and lock pointer */
    hEditData  = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
    lpEditData = (char *)LocalLock (hEditData);

    /* write edit data to file. */
    if (!WriteFile(hFile, lpEditData, nLength, &dwWritten, NULL))
    {
    /* unlock memory, restore edit handle, close file and return error */
    LocalUnlock (hEditData);
    CloseHandle (hFile);
    return IDS_WRITEFAILED;
    }

    /* clean up and go away */
    LocalUnlock (hEditData);
    CloseHandle (hFile);

    return TRUE;
}


/* invokes the saveas common dialog to retrieve a file name */
BOOL WINAPI SaveAsFileName (
    HWND    hWnd,
    char    *lpszFileTitle,
    char    *lpszFilePath)
{
    OPENFILENAME    ofn;
    char        lpszSaveAs[25];
    char        lpszExt[10];

    *lpszFileTitle = 0;
    *lpszFilePath = 0;

    /* load strings from resource string table */
    LoadString ((HANDLE)GetModuleHandle (NULL),
        IDS_DEFAULTFILEEXT,
        lpszExt,
        sizeof (lpszExt));
    LoadString ((HANDLE)GetModuleHandle (NULL),
        IDS_SAVEASTITLE,
        lpszSaveAs,
        sizeof (lpszSaveAs));

    /* fill in non-variant fields of OPENFILENAME struct. */
    ofn.lStructSize   = sizeof (OPENFILENAME);
    ofn.hwndOwner     = hWnd;
    ofn.lpstrFilter   = lpszFilterSpec;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter    = 0;
    ofn.nFilterIndex      = 0;
    ofn.lpstrFile     = lpszFilePath;
    ofn.nMaxFile      = MAX_PATH;
    ofn.lpstrInitialDir   = NULL;
    ofn.lpstrFileTitle    = lpszFileTitle;
    ofn.nMaxFileTitle     = MAX_PATH;
    ofn.lpstrTitle    = lpszSaveAs;
    ofn.lpstrDefExt   = lpszExt;
    ofn.Flags         = 0;

    /* call common saveas dialog and return success */
    return (GetSaveFileName ((LPOPENFILENAME)&ofn));
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.