summaryrefslogtreecommitdiffstats
path: root/private/nw/convert/logview/fvfile.c
blob: 466ec9c4cefba975c9dee3d76260e53ff6556c54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
  +-------------------------------------------------------------------------+
  |                MDI Text File Viewer - File IO Routines                  |
  +-------------------------------------------------------------------------+
  |                        (c) Copyright 1994                               |
  |                          Microsoft Corp.                                |
  |                        All rights reserved                              |
  |                                                                         |
  | Program               : [mpfile.c]                                      |
  | Programmer            : Arthur Hanson                                   |
  | Original Program Date : [Jul 27, 1993                                   |
  | Last Update           : [Jul 30, 1993]  Time : 18:30                    |
  |                                                                         |
  | Version:  0.10                                                          |
  |                                                                         |
  | Description:                                                            |
  |                                                                         |
  | History:                                                                |
  |   arth  Jul 27, 1993    0.10    Original Version.                       |
  |                                                                         |
  +-------------------------------------------------------------------------+
*/

#include "LogView.h"
#include <fcntl.h>
#include <SYS\types.h>
#include <SYS\stat.h>
#include <io.h>
#include <string.h>

VOID APIENTRY GetFileName(HWND hwnd, PSTR);

OFSTRUCT        of;


/*+-------------------------------------------------------------------------+
  | AlreadyOpen()                                                           |
  |                                                                         |
  |   Checks to see if a file is already opened.  Returns a handle to       |
  |   the file's window if it is opened, otherwise NULL.                    |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
HWND AlreadyOpen(CHAR *szFile) {
    INT     iDiff;
    HWND    hwndCheck;
    CHAR    szChild[64];
    LPSTR   lpChild, lpFile;
    HFILE     wFileTemp;

    // Open the file with the OF_PARSE flag to obtain the fully qualified
    // pathname in the OFSTRUCT structure.
    wFileTemp = OpenFile((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE);
    if (! wFileTemp)
        return(NULL);
    _lclose(wFileTemp);

    // Check each MDI child window in LogView
    for (   hwndCheck = GetWindow(hwndMDIClient, GW_CHILD);
            hwndCheck;
            hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT)   ) {
        // Initialization  for comparison
        lpChild = szChild;
        lpFile = (LPSTR)AnsiUpper((LPSTR) of.szPathName);
        iDiff = 0;

        // Skip icon title windows
        if (GetWindow(hwndCheck, GW_OWNER))
            continue;

        // Get current child window's name
        GetWindowText(hwndCheck, lpChild, 64);

        // Compare window name with given name
        while ((*lpChild) && (*lpFile) && (!iDiff)) {
            if (*lpChild++ != *lpFile++)
                iDiff = 1;
        }

        // If the two names matched, the file is already open - return handle to matching
        // child window.
        if (!iDiff)
            return(hwndCheck);
    }
    
    // No match found -- file is not open -- return NULL handle
    return(NULL);
    
} // AlreadyOpen


/*+-------------------------------------------------------------------------+
  | AddFile()                                                               |
  |                                                                         |
  |   Create a new MDI Window, and loads specified file into Window.        |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
HWND APIENTRY AddFile(CHAR * pName) {
    HWND hwnd;

    CHAR            sz[160];
    MDICREATESTRUCT mcs;

    if (!pName) {
        // The pName parameter is NULL -- load the "Untitled" string from STRINGTABLE
        // and set the title field of the MDI CreateStruct.
        LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz));
        mcs.szTitle = (LPSTR)sz;
    }
    else
        // Title the window with the fully qualified pathname obtained by calling
        // OpenFile() with the OF_PARSE flag (in function AlreadyOpen(), which is called
        // before AddFile().
        mcs.szTitle = pName;

    mcs.szClass = szChild;
    mcs.hOwner  = hInst;

    // Use the default size for the window
    mcs.x = mcs.cx = CW_USEDEFAULT;
    mcs.y = mcs.cy = CW_USEDEFAULT;

    // Set the style DWORD of the window to default
    mcs.style = 0L;

    // tell the MDI Client to create the child
    hwnd = (HWND)SendMessage (hwndMDIClient,
                              WM_MDICREATE,
                              0,
                              (LONG)(LPMDICREATESTRUCT)&mcs);

    // Did we get a file? Read it into the window
    if (pName){
        if (!LoadFile(hwnd, pName)){
            // File couldn't be loaded -- close window
            SendMessage(hwndMDIClient, WM_MDIDESTROY, (DWORD) hwnd, 0L);
        }
    }

    return hwnd;
    
} // AddFile


/*+-------------------------------------------------------------------------+
  | LoadFile()                                                              |
  |                                                                         |
  |    Loads file into specified MDI window's edit control.                 |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
INT APIENTRY LoadFile ( HWND hwnd, CHAR * pName) {
    LONG   wLength;
    HANDLE hT;
    LPSTR  lpB;
    HWND   hwndEdit;
    HFILE  fh;
    OFSTRUCT  of;
    
    hwndEdit = (HWND)GetWindowLong (hwnd, GWL_HWNDEDIT);

    // The file has a title, so reset the UNTITLED flag.
    SetWindowWord(hwnd, GWW_UNTITLED, FALSE);

    fh = OpenFile(pName, &of, OF_READ);  // JAP was 0, which is OF_READ)

    // Make sure file has been opened correctly
    if ( fh < 0 )
        goto error;

    // Find the length of the file
    wLength = (DWORD)_llseek(fh, 0L, 2);
    _llseek(fh, 0L, 0);

    // Attempt to reallocate the edit control's buffer to the file size
    hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
    if (LocalReAlloc(hT, wLength+1, LHND) == NULL) {
        // Couldn't reallocate to new size -- error
        _lclose(fh);
        goto error;
    }

    // read the file into the buffer
    if (wLength != (LONG)_lread(fh, (lpB = (LPSTR)LocalLock (hT)), (UINT)wLength))
        MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName);

    // Zero terminate the edit buffer
    lpB[wLength] = 0;
    LocalUnlock (hT);

    SendMessage (hwndEdit, EM_SETHANDLE, (UINT)hT, 0L);
    _lclose(fh);

    return TRUE;

error:
    // Report the error and quit
    MPError(hwnd, MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPSTR)pName);
    return FALSE;
    
} // LoadFile


/*+-------------------------------------------------------------------------+
  | MyReadFile()                                                            |
  |                                                                         |
  |   Asks user for a filename.                                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
VOID APIENTRY MyReadFile(HWND hwnd) {
    CHAR    szFile[128];
    HWND    hwndFile;

    GetFileName (hwnd, szFile);

    // If the result is not the empty string -- take appropriate action
    if (*szFile) {
            // Is file already open??
            if (hwndFile = AlreadyOpen(szFile)) {
                // Yes -- bring the file's window to the top
                BringWindowToTop(hwndFile);
            }
            else {
                // No -- make a new window and load file into it
                AddFile(szFile);
            }
    }
        UNREFERENCED_PARAMETER(hwnd);
        
} // MyReadFile