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

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

unix.superglobalmegacorp.com

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