|
|
1.1 root 1: #include "porttool.h"
2: #include "port.h"
3:
4:
5: // define line types returned from GetNextLine
6: #define VALID_LINE 0
7: #define COMMENT_LINE 1
8: #define EOF_LINE 2
9:
10: #define CARRIAGE_RETURN 13
11: #define LINE_FEED 10
12:
13: // define worker functions for this module
14: int WINAPI GetNextLine (char *, int, char *, char *, BOOL *);
15: int WINAPI NextLineLength (char *, char *, int);
16: BOOL WINAPI BkFilePortThread (LPBKPORTFILESTRUCT);
17: void WINAPI DateTimeStamp (char *);
18:
19:
20:
21: // function creates a background porting thread
22: HANDLE WINAPI StartBkPortThread (LPBKPORTFILESTRUCT lpBkPort)
23: {
24: DWORD id;
25:
26: // create thread with initial structure
27: return (lpBkPort->hThread = CreateThread ((LPSECURITY_ATTRIBUTES)NULL,
28: 4096,
29: (LPTHREAD_START_ROUTINE)BkFilePortThread,
30: (LPVOID)lpBkPort,
31: 0,
32: &id));
33: }
34:
35:
36: // independent thread function that performs background file porting
37: BOOL WINAPI BkFilePortThread (
38: LPBKPORTFILESTRUCT lpBkPort)
39: {
40: HANDLE hEvents[nBKPORTEVENTS];
41: HANDLE hFile;
42: OFSTRUCT of;
43: DWORD nFileSize;
44: DWORD nBytes;
45: HANDLE hFileBuffer;
46: char *lpFile, *lpFilePtr;
47: WORD wComplete, wIssues = 0;
48: int nLines = 0;
49: BOOL bCommentOn;
50: HANDLE hLine;
51: HANDLE hToken, hIssue, hSuggest, hHelp;
52: RESULT rIssue;
53: char *lpLine;
54: char szHeader[MAX_PATH], szToken[50], szIssue[50], szdt[50],
55: szSuggest[50], szHelp[50], szHelpFile[50], szEOL[50], szNL[5];
56:
57:
58: // adjust our priority to below normal
59: SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_BELOW_NORMAL);
60:
61: // load file comment strings
62: LoadString (GetModuleHandle (NULL), IDS_BKPORTNEWLINE, szNL, 5);
63: LoadString (GetModuleHandle (NULL), IDS_BKPORTHEADER, szHeader, MAX_PATH);
64: LoadString (GetModuleHandle (NULL), IDS_BKPORTTOKEN, szToken, 50);
65: LoadString (GetModuleHandle (NULL), IDS_BKPORTISSUE, szIssue, 50);
66: LoadString (GetModuleHandle (NULL), IDS_BKPORTSUGGEST, szSuggest, 50);
67: LoadString (GetModuleHandle (NULL), IDS_BKPORTHELP, szHelp, 50);
68: LoadString (GetModuleHandle (NULL), IDS_BKPORTHELPFILE, szHelpFile, 50);
69: LoadString (GetModuleHandle (NULL), IDS_BKPORTEOL, szEOL, 50);
70:
71: // initialize wait events for communication between threads
72: if (!CreateEvents (hEvents, lpBkPort))
73: return FALSE;
74:
75: // open file for porting and read into buffer
76: if ((int)(hFile = (HANDLE)OpenFile (lpBkPort->szFilePath, &of, OF_READWRITE)) == -1)
77: {
78: DestroyEvents (hEvents);
79: return FALSE;
80: }
81:
82: // global allocate buffer for file
83: if (!(hFileBuffer = GlobalAlloc (GPTR, (nFileSize = GetFileSize (hFile, NULL))+1)) ||
84: !(lpFile = (char *)GlobalLock (hFileBuffer)))
85: {
86: CloseHandle (hFile);
87: DestroyEvents (hEvents);
88: return FALSE;
89: }
90:
91: // allocate initial line buffer of reasonable size
92: hLine = GlobalAlloc (GMEM_MOVEABLE, 1024);
93:
94: // allocate local memory segments for porttool RESULT strings
95: if (!(rIssue.lpszToken = LocalLock (hToken = LocalAlloc (LHND, MAXTOKENLEN))) ||
96: !(rIssue.lpszHelpStr = LocalLock (hHelp = LocalAlloc (LHND, MAXHELPLEN))) ||
97: !(rIssue.lpszIssue = LocalLock (hIssue = LocalAlloc (LHND, MAXISSUELEN))) ||
98: !(rIssue.lpszSuggest = LocalLock (hSuggest = LocalAlloc (LHND, MAXSUGGESTLEN))))
99: {
100: CloseHandle (hFile);
101: GlobalUnlock (lpFile);
102: GlobalFree (hFileBuffer);
103: DestroyEvents (hEvents);
104: GlobalFree (hLine);
105: return FALSE;
106: }
107:
108: // read entire file into buffer and zero terminate
109: ReadFile (hFile, lpFile, nFileSize, &nBytes, NULL);
110: lpFile[nFileSize] = 0;
111: lpFilePtr = lpFile;
112:
113: // if bytes read not equal to size of file, abort
114: if (nBytes != nFileSize)
115: {
116: CloseHandle (hFile);
117: GlobalUnlock (lpFile);
118: GlobalFree (hFileBuffer);
119: DestroyEvents (hEvents);
120: GlobalFree (hLine);
121: return FALSE;
122: }
123:
124: // reset file to size zero before beginning porting
125: SetFilePointer (hFile, 0, NULL, FILE_BEGIN);
126: SetEndOfFile (hFile);
127: WriteFile (hFile, szHeader, strlen (szHeader), &nBytes, NULL);
128: WriteFile (hFile, lpBkPort->szFile, strlen (lpBkPort->szFile), &nBytes, NULL);
129: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
130: WriteFile (hFile, szNL, strlen (szNL), &nBytes, NULL);
131:
132: while (TRUE)
133: {
134: // wait 1ms for either abort or status events to signal
135: switch (WaitForMultipleObjects (nBKPORTEVENTS, hEvents, FALSE, 1))
136: {
137: case BKPORT_ABORT:
138: {
139: char szAbort[MAX_PATH];
140:
141: // create time and date
142: DateTimeStamp (szdt);
143:
144: // write header line
145: WriteFile (hFile, szHeader, strlen (szHeader), &nBytes, NULL);
146: WriteFile (hFile, szdt, strlen (szdt), &nBytes, NULL);
147: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
148: WriteFile (hFile, szNL, strlen (szNL), &nBytes, NULL);
149:
150: // load abort string and write to file
151: LoadString (GetModuleHandle (NULL),
152: IDS_BKPORTABORT,
153: szAbort,
154: MAX_PATH);
155: WriteFile (hFile, szAbort, strlen (szAbort), (LPDWORD)&nBytes, NULL);
156:
157: // write rest of file to disk
158: WriteFile (hFile,
159: (VOID *)lpFilePtr,
160: (nFileSize - (lpFilePtr-lpFile)),
161: (LPDWORD)&nBytes,
162: NULL);
163:
164: // clean up
165: CloseHandle (hFile);
166: GlobalUnlock (lpFile);
167: GlobalFree (hFileBuffer);
168: GlobalFree (hLine);
169: DestroyEvents (hEvents);
170:
171: // free RESULT strings
172: LocalUnlock (hToken); LocalFree (hToken);
173: LocalUnlock (hHelp); LocalFree (hHelp);
174: LocalUnlock (hIssue); LocalFree (hIssue);
175: LocalUnlock (hSuggest); LocalFree (hSuggest);
176:
177: // send message to parent that thread is dead
178: PostMessage (lpBkPort->hDlg,
179: UM_THREADCOMPLETE,
180: (UINT)lpBkPort->hThread,
181: 0);
182:
183: // exit thread
184: return FALSE;
185: }
186: break;
187:
188: case BKPORT_STATUS:
189: // send message to dialog with status info
190: PostMessage (lpBkPort->hDlg,
191: UM_STATUSUPDATE,
192: MAKELONG (wIssues, wComplete),
193: nLines);
194: break;
195: }
196:
197: // reset line buffer
198: GlobalReAlloc (hLine,
199: NextLineLength (lpFilePtr, lpFile, nFileSize),
200: GMEM_MOVEABLE);
201: lpLine = (char *)GlobalLock (hLine);
202: *lpLine = 0;
203:
204: // get next line from file buffer
205: switch (GetNextLine (lpFile, nFileSize, lpFilePtr, lpLine, &bCommentOn))
206: {
207: // check valid strings for porting issues
208: case VALID_LINE:
209: // initialize rIssue string lengths
210: *(WORD *)rIssue.lpszToken = MAXTOKENLEN;
211: *(WORD *)rIssue.lpszHelpStr = MAXHELPLEN;
212: *(WORD *)rIssue.lpszIssue = MAXISSUELEN;
213: *(WORD *)rIssue.lpszSuggest = MAXSUGGESTLEN;
214:
215: if (CheckString (lpLine, lpBkPort->dwPTFlags, &rIssue))
216: {
217: // create time and date
218: DateTimeStamp (szdt);
219:
220: // write header line
221: WriteFile (hFile, szNL, strlen (szNL), &nBytes, NULL);
222: WriteFile (hFile, szHeader, strlen (szHeader), &nBytes, NULL);
223: WriteFile (hFile, szdt, strlen (szdt), &nBytes, NULL);
224: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
225:
226: // write token line
227: WriteFile (hFile, szToken, strlen (szToken), &nBytes, NULL);
228: WriteFile (hFile,
229: rIssue.lpszToken,
230: strlen (rIssue.lpszToken),
231: &nBytes,
232: NULL);
233: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
234:
235: // write issue line
236: WriteFile (hFile, szIssue, strlen (szIssue), &nBytes, NULL);
237: WriteFile (hFile,
238: rIssue.lpszIssue,
239: strlen (rIssue.lpszIssue),
240: &nBytes,
241: NULL);
242: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
243:
244: // if suggestion
245: if (*(rIssue.lpszSuggest))
246: {
247: WriteFile (hFile, szSuggest, strlen (szSuggest), &nBytes, NULL);
248: WriteFile (hFile,
249: rIssue.lpszSuggest,
250: strlen (rIssue.lpszSuggest),
251: &nBytes,
252: NULL);
253: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
254: }
255:
256: // if help string
257: if (*(rIssue.lpszSuggest))
258: {
259: WriteFile (hFile, szHelp, strlen (szHelp), &nBytes, NULL);
260: WriteFile (hFile,
261: rIssue.lpszHelpStr,
262: strlen (rIssue.lpszHelpStr),
263: &nBytes,
264: NULL);
265: WriteFile (hFile, szHelpFile, strlen (szHelpFile), &nBytes, NULL);
266: WriteFile (hFile, szEOL, strlen (szEOL), &nBytes, NULL);
267: }
268:
269: wIssues++;
270: }
271:
272: case COMMENT_LINE:
273: // write line to file whether comment or not
274: WriteFile (hFile, lpLine, strlen (lpLine), &nBytes, NULL);
275: break;
276:
277: case EOF_LINE:
278: if (*lpLine)
279: WriteFile (hFile, lpLine, strlen (lpLine), &nBytes, NULL);
280: goto DONE;
281: break;
282: }
283:
284: // unlock line buffer
285: GlobalUnlock (hLine);
286:
287: // update status counts
288: lpFilePtr += strlen (lpLine);
289: nLines++;
290: wComplete = (WORD)(((lpFilePtr-lpFile)*100)/nFileSize);
291: }
292:
293: DONE:
294: // clean up
295: CloseHandle (hFile);
296: GlobalUnlock (lpFile);
297: GlobalFree (hFileBuffer);
298: GlobalFree (hLine);
299: DestroyEvents (hEvents);
300:
301: // free RESULT strings
302: LocalUnlock (hToken); LocalFree (hToken);
303: LocalUnlock (hHelp); LocalFree (hHelp);
304: LocalUnlock (hIssue); LocalFree (hIssue);
305: LocalUnlock (hSuggest); LocalFree (hSuggest);
306:
307: // send message to parent that thread is dead
308: PostMessage (lpBkPort->hDlg,
309: UM_THREADCOMPLETE,
310: (UINT)lpBkPort->hThread,
311: 0);
312:
313: // exit thread
314: return TRUE;
315: }
316:
317:
318:
319: void WINAPI DateTimeStamp (
320: char *lpszDT)
321: {
322: SYSTEMTIME dt;
323: char Buff[10];
324:
325: // create time and date stamp
326: GetSystemTime (&dt);
327: strcpy (lpszDT, itoa (dt.wMonth, Buff, 10));
328: strcat (lpszDT, "/");
329: strcat (lpszDT, itoa (dt.wDay, Buff, 10));
330: strcat (lpszDT, "/");
331: strcat (lpszDT, itoa (dt.wYear, Buff, 10));
332: strcat (lpszDT, " ");
333: strcat (lpszDT, itoa (dt.wHour, Buff, 10));
334: strcat (lpszDT, ":");
335: strcat (lpszDT, itoa (dt.wMinute, Buff, 10));
336: }
337:
338:
339:
340:
341:
342: BOOL WINAPI CreateEvents (
343: HANDLE *lphEvents,
344: LPBKPORTFILESTRUCT lpBkPort)
345: {
346: char szEvent[MAX_PATH];
347:
348:
349: LoadString (GetModuleHandle (NULL), IDS_BKPORTABORT, szEvent, MAX_PATH);
350: strcat (szEvent, lpBkPort->szFile);
351: if (!(lphEvents[BKPORT_ABORT] = CreateEvent (NULL, TRUE, FALSE, szEvent)))
352: return FALSE;
353:
354: LoadString (GetModuleHandle (NULL), IDS_BKPORTSTATUS, szEvent, MAX_PATH);
355: strcat (szEvent, lpBkPort->szFile);
356: if (!(lphEvents[BKPORT_STATUS] = CreateEvent (NULL, TRUE, FALSE, szEvent)))
357: {
358: CloseHandle (lphEvents[BKPORT_ABORT]);
359: return FALSE;
360: }
361:
362: // return success
363: return TRUE;
364: }
365:
366:
367:
368:
369: void WINAPI DestroyEvents (
370: HANDLE *lphEvents)
371: {
372: // close event handles
373: CloseHandle (lphEvents[BKPORT_ABORT]);
374: CloseHandle (lphEvents[BKPORT_STATUS]);
375: }
376:
377:
378:
379:
380: int WINAPI NextLineLength (
381: char *lpFilePtr,
382: char *lpFile,
383: int nFileSize)
384: {
385: int nCnt;
386: char *lpf = lpFilePtr;
387:
388: // count all characters up to end of file or CR/LF sequence
389: while (lpf &&
390: lpf-lpFile < nFileSize &&
391: *lpf != CARRIAGE_RETURN &&
392: *(lpf+1) != LINE_FEED)
393: {
394: lpf++;
395: nCnt++;
396: }
397:
398: // length plus 3 for CR/LF/0 terminator sequence
399: return nCnt + 3;
400: }
401:
402:
403:
404:
405: int WINAPI GetNextLine (
406: char *lpFile,
407: int nFileSize,
408: char *lpFilePtr,
409: char *lpLine,
410: BOOL *bCommentOn)
411: {
412: char *lpf = lpFilePtr;
413: char *lpl = lpLine;
414:
415: // copy all characters up to end of file or CR/LF sequence
416: while (lpf &&
417: lpf-lpFile < nFileSize &&
418: *lpf != CARRIAGE_RETURN &&
419: *(lpf+1) != LINE_FEED)
420: *lpl++ = *lpf++;
421:
422: // check for end of buffer
423: if (lpf-lpFile >= nFileSize)
424: return EOF_LINE;
425:
426: // copy carriage return and line feed to line and terminate
427: *lpl++ = *lpf++;
428: *lpl++ = *lpf++;
429: *lpl = 0;
430:
431: // increment lpl to first non space character in line
432: lpl = lpLine;
433: while (*lpl == ' ')
434: lpl++;
435:
436: // see if single line comments exist
437: if (*lpl == '/' &&
438: *(lpl+1) == '/')
439: return COMMENT_LINE;
440:
441: // see if comments begin
442: if (*lpl == '/' &&
443: *(lpl+1) == '*')
444: *bCommentOn = TRUE;
445:
446: // if comment on, see if it terminates yet
447: if (*bCommentOn)
448: {
449: lpl = lpLine;
450: while (*lpl)
451: {
452: if (*lpl == '*' &&
453: *(lpl+1) == '/')
454: {
455: *bCommentOn = FALSE;
456: break;
457: }
458: lpl++;
459: }
460:
461: // if more text on line, valid line
462: while (*lpl)
463: if (*lpl != '*' &&
464: *(lpl+1) != '/')
465: return VALID_LINE;
466: else
467: return COMMENT_LINE;
468: }
469:
470: // if haven't returned yet, must be valid line
471: return VALID_LINE;
472: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.