Annotation of mstools/samples/sdktools/porttool/ptfile.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: #include "PortTool.h"
                     13: #include <string.h>
                     14: 
                     15: /* global file name variables */
                     16: char    lpszFilePath[MAX_PATH];
                     17: char   lpszFilterSpec[128] =      /* load here since resource compiler hoses it */
                     18:       "C Source Files (*.C)\0*.C\0C Header Files (*.H)\0*.H\0All Files (*.*)\0*.*\0";
                     19: 
                     20: 
                     21: /* call the OpenFile common dialog to get a filename */
                     22: BOOL WINAPI GetFileName (
                     23:     HWND    hWnd,
                     24:     char    *lpszFileTitle,
                     25:     char    *lpszFilePath)
                     26: {
                     27:     OPENFILENAME    ofn;
                     28:     char        lpszFileOpen[25];
                     29:     char        lpszExt[10];
                     30: 
                     31:     strcpy (lpszFileTitle, "");
                     32:     LoadString ((HANDLE)GetModuleHandle (NULL),
                     33:         IDS_DEFAULTFILEEXT,
                     34:         lpszExt,
                     35:         sizeof (lpszExt));
                     36:     LoadString ((HANDLE)GetModuleHandle (NULL),
                     37:         IDS_FILEOPENTITLE,
                     38:         lpszFileOpen,
                     39:         sizeof (lpszFileOpen));
                     40: 
                     41:     /* fill in non-variant fields of OPENFILENAME struct. */
                     42:     ofn.lStructSize       = sizeof(OPENFILENAME);
                     43:     ofn.hwndOwner     = NULL;
                     44:     ofn.lpstrFilter   = lpszFilterSpec;
                     45:     ofn.lpstrCustomFilter = NULL;
                     46:     ofn.nMaxCustFilter    = 0;
                     47:     ofn.nFilterIndex      = 0;
                     48:     ofn.lpstrFile     = lpszFilePath;
                     49:     ofn.nMaxFile      = MAX_PATH;
                     50:     ofn.lpstrInitialDir   = NULL;
                     51:     ofn.lpstrFileTitle    = lpszFileTitle;
                     52:     ofn.nMaxFileTitle     = MAX_PATH;
                     53:     ofn.lpstrTitle    = lpszFileOpen;
                     54:     ofn.lpstrDefExt   = lpszExt;
                     55:     ofn.Flags         = OFN_FILEMUSTEXIST;
                     56: 
                     57:     /* call common open dialog and return result */
                     58:     return (GetOpenFileName ((LPOPENFILENAME)&ofn));
                     59: }
                     60: 
                     61: 
                     62: 
                     63: /* function retrieves the filename from the path */
                     64: BOOL WINAPI GetFileFromPath (
                     65:     char    *lpszFullPath,
                     66:     char    *lpszFile)
                     67: {
                     68:     char    *lpPtr = lpszFullPath + strlen (lpszFullPath);
                     69: 
                     70:     /* file is at end of path, so search backwards to first \ or : char */
                     71:     while (lpPtr > lpszFullPath)
                     72:        {
                     73:        if (*lpPtr == '\\' ||
                     74:            *lpPtr == ':')
                     75:            {
                     76:            lpPtr++;
                     77:            break;
                     78:            }
                     79:        lpPtr--;
                     80:        }
                     81: 
                     82:     /* return filename if found, or full path passed in */
                     83:     strcpy (lpszFile, lpPtr);
                     84: 
                     85:     return (lpPtr > lpszFullPath);
                     86: }
                     87: 
                     88: 
                     89: 
                     90: /* retrieve ini file and path */
                     91: BOOL WINAPI GetIniFile (
                     92:     char    *lpszFile)
                     93: {
                     94:     char    lpszPath[MAX_PATH];
                     95:     char    *lpPath;
                     96:     char    lpszFileName[MAX_PATH];
                     97:     OFSTRUCT    of;
                     98: 
                     99:     /* get module directory and path */
                    100:     GetModuleFileName (NULL, lpszPath, MAX_PATH);
                    101:     lpPath = lpszPath + strlen (lpszPath);
                    102: 
                    103:     /* find end of path by searching backwards from end to first '\' or ':' */
                    104:     while (lpPath > lpszPath)
                    105:        {
                    106:        if (*lpPath == '\\' ||
                    107:            *lpPath == ':')
                    108:            {
                    109:            lpPath++;
                    110:            break;
                    111:            }
                    112:        lpPath--;
                    113:        }
                    114: 
                    115:     /*terminate at end of path */
                    116:     *lpPath = 0;
                    117: 
                    118:     /* append ini filename to path */
                    119:     LoadString (GetModuleHandle (NULL), IDS_INIFILE, lpszFileName, MAX_PATH);
                    120:     strcat (lpPath, lpszFileName);
                    121: 
                    122:     /* test for existance */
                    123:     if (!(OpenFile (lpszPath, &of, OF_EXIST)))
                    124:        {
                    125:        GetWindowsDirectory (lpszPath, MAX_PATH);
                    126:        strcat (lpszPath, lpszFileName);
                    127:        if (!(OpenFile (lpszPath, &of, OF_EXIST)))
                    128:            return FALSE;
                    129:        else
                    130:            {
                    131:            strcpy (lpszFile, lpszPath);
                    132:            return TRUE;
                    133:            }
                    134:        }
                    135:     else
                    136:        {
                    137:        strcpy (lpszFile, lpszPath);
                    138:        return TRUE;
                    139:        }
                    140: }
                    141: 
                    142: 
                    143: /* open a file and load into edit control */
                    144: int WINAPI LoadFile (
                    145:     HWND    hWnd,
                    146:     char    *lpszName)
                    147: {
                    148:     LONG    lLength;
                    149:     HWND    hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
                    150:     HANDLE  hEditData;
                    151:     char    *lpEditData;
                    152:     HFILE   hFile;
                    153:     OFSTRUCT    of;
                    154: 
                    155:     /* open file for read & write */
                    156:     if ((hFile = OpenFile(lpszName, &of, OF_READWRITE)) == -1)
                    157:     /* return error */
                    158:     return IDS_OPENFAILED;
                    159: 
                    160:     /* get file length */
                    161:     if (lLength = _llseek(hFile, 0L, 2))
                    162:        _llseek(hFile, 0L, 0);
                    163:     else
                    164:     {
                    165:     /* close file and return error */
                    166:     CloseHandle ((HANDLE)hFile);
                    167:     return IDS_NOSIZE;
                    168:     }
                    169: 
                    170:     /* get the edit control's memory handle */
                    171:     if (!(hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L)))
                    172:     {
                    173:     /* close file and return error */
                    174:     CloseHandle ((HANDLE)hFile);
                    175:     return IDS_GETHANDLEFAILED;
                    176:     }
                    177: 
                    178:     /* realloc the memory to fit the new file size */
                    179:     if (((hEditData = LocalReAlloc(hEditData, lLength+1, LHND)) == NULL) ||
                    180:     (!(lpEditData = (char *)LocalLock (hEditData))))
                    181:     {
                    182:     /* close file and return error */
                    183:     CloseHandle ((HANDLE)hFile);
                    184:     return IDS_REALLOCFAILED;
                    185:     }
                    186: 
                    187:     /* read the file into hEditData buffer */
                    188:     if (_lread(hFile, lpEditData, lLength) == -1)
                    189:     {
                    190:     /* close file and return error */
                    191:     CloseHandle ((HANDLE)hFile);
                    192:     return IDS_READFAILED;
                    193:     }
                    194: 
                    195:     /* null terminate edit buffer */
                    196:     lpEditData[lLength] = 0;
                    197:     LocalUnlock (hEditData);
                    198: 
                    199:     /* load buffer into edit control and close file */
                    200:     SendMessage (hWndEdit, EM_SETHANDLE, (UINT)hEditData, 0L);
                    201:     CloseHandle ((HANDLE)hFile);
                    202: 
                    203:     /* return success */
                    204:     return TRUE;
                    205: }
                    206: 
                    207: 
                    208: 
                    209: /* save file to disk */
                    210: int WINAPI SaveFile (
                    211:     HWND    hWnd,
                    212:     char    *lpszFile)
                    213: {
                    214:     HANDLE   hEditData;
                    215:     int      nLength;
                    216:     DWORD    dwWritten;
                    217:     HANDLE   hFile;
                    218:     HWND     hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
                    219:     char     *lpEditData;
                    220: 
                    221: 
                    222:     /* open the file for writing */
                    223:     hFile = CreateFile (lpszFile,
                    224:             GENERIC_WRITE,
                    225:             0,
                    226:             NULL,
                    227:             CREATE_ALWAYS,
                    228:             FILE_ATTRIBUTE_NORMAL,
                    229:             NULL);
                    230: 
                    231:     /* validate file handle */
                    232:     if (!hFile)
                    233:     return IDS_WRITEOPENFAILED;
                    234: 
                    235:     /* find out the length of the text in the edit control */
                    236:     nLength = GetWindowTextLength (hWndEdit);
                    237: 
                    238:     /* get handle to Edit text and lock pointer */
                    239:     hEditData  = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
                    240:     lpEditData = (char *)LocalLock (hEditData);
                    241: 
                    242:     /* write edit data to file. */
                    243:     if (!WriteFile(hFile, lpEditData, nLength, &dwWritten, NULL))
                    244:     {
                    245:     /* unlock memory, restore edit handle, close file and return error */
                    246:     LocalUnlock (hEditData);
                    247:     CloseHandle (hFile);
                    248:     return IDS_WRITEFAILED;
                    249:     }
                    250: 
                    251:     /* clean up and go away */
                    252:     LocalUnlock (hEditData);
                    253:     CloseHandle (hFile);
                    254: 
                    255:     return TRUE;
                    256: }
                    257: 
                    258: 
                    259: /* invokes the saveas common dialog to retrieve a file name */
                    260: BOOL WINAPI SaveAsFileName (
                    261:     HWND    hWnd,
                    262:     char    *lpszFileTitle,
                    263:     char    *lpszFilePath)
                    264: {
                    265:     OPENFILENAME    ofn;
                    266:     char        lpszSaveAs[25];
                    267:     char        lpszExt[10];
                    268: 
                    269:     *lpszFileTitle = 0;
                    270:     *lpszFilePath = 0;
                    271: 
                    272:     /* load strings from resource string table */
                    273:     LoadString ((HANDLE)GetModuleHandle (NULL),
                    274:         IDS_DEFAULTFILEEXT,
                    275:         lpszExt,
                    276:         sizeof (lpszExt));
                    277:     LoadString ((HANDLE)GetModuleHandle (NULL),
                    278:         IDS_SAVEASTITLE,
                    279:         lpszSaveAs,
                    280:         sizeof (lpszSaveAs));
                    281: 
                    282:     /* fill in non-variant fields of OPENFILENAME struct. */
                    283:     ofn.lStructSize   = sizeof (OPENFILENAME);
                    284:     ofn.hwndOwner     = hWnd;
                    285:     ofn.lpstrFilter   = lpszFilterSpec;
                    286:     ofn.lpstrCustomFilter = NULL;
                    287:     ofn.nMaxCustFilter    = 0;
                    288:     ofn.nFilterIndex      = 0;
                    289:     ofn.lpstrFile     = lpszFilePath;
                    290:     ofn.nMaxFile      = MAX_PATH;
                    291:     ofn.lpstrInitialDir   = NULL;
                    292:     ofn.lpstrFileTitle    = lpszFileTitle;
                    293:     ofn.nMaxFileTitle     = MAX_PATH;
                    294:     ofn.lpstrTitle    = lpszSaveAs;
                    295:     ofn.lpstrDefExt   = lpszExt;
                    296:     ofn.Flags         = 0;
                    297: 
                    298:     /* call common saveas dialog and return success */
                    299:     return (GetSaveFileName ((LPOPENFILENAME)&ofn));
                    300: }

unix.superglobalmegacorp.com

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