Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

974 lines
28 KiB

  1. /*
  2. * npfile.c - Routines for file i/o for notepad
  3. * Copyright (C) 1984-2001 Microsoft Inc.
  4. */
  5. #include "precomp.h"
  6. HANDLE hFirstMem;
  7. CHAR BOM_UTF8[3]= {(BYTE) 0xEF, (BYTE) 0xBB, (BYTE)0xBF};
  8. //****************************************************************
  9. //
  10. // ReverseEndian
  11. //
  12. // Purpose: copies unicode character from one endian source
  13. // to another.
  14. //
  15. // may work on lpDst == lpSrc
  16. //
  17. VOID ReverseEndian( PTCHAR lpDst, PTCHAR lpSrc, DWORD nChars )
  18. {
  19. DWORD cnt;
  20. for( cnt=0; cnt < nChars; cnt++,lpDst++,lpSrc++ )
  21. {
  22. *lpDst= (TCHAR) (((*lpSrc<<8) & 0xFF00) + ((*lpSrc>>8)&0xFF));
  23. }
  24. }
  25. //*****************************************************************
  26. //
  27. // AnsiWriteFile()
  28. //
  29. // Purpose : To simulate the effects of _lwrite() in a Unicode
  30. // environment by converting to ANSI buffer and
  31. // writing out the ANSI text.
  32. // Returns : TRUE is successful, FALSE if not
  33. // GetLastError() will have the error code.
  34. //
  35. //*****************************************************************
  36. BOOL AnsiWriteFile(HANDLE hFile, // file to write to
  37. UINT uCodePage, // code page to convert unicode to
  38. DWORD dwFlags, // flags for WideCharToMultiByte conversion
  39. LPVOID lpBuffer, // unicode buffer
  40. DWORD nChars) // number of unicode chars
  41. {
  42. LPSTR lpAnsi; // pointer to allocate buffer
  43. BOOL fDefCharUsed; // flag that conversion wasn't perfect
  44. BOOL* pfDefCharUsed; // pointer to flag
  45. DWORD nBytesWritten; // number of bytes written
  46. BOOL bStatus; // status from conversion and writefile
  47. DWORD nBytes; // number of ascii character to produce
  48. //
  49. // WideCharToMultiByte fails to convert zero characters.
  50. // If we get a request to write 0 chars, just return. We are done.
  51. //
  52. if( nChars == 0 )
  53. {
  54. return( TRUE );
  55. }
  56. pfDefCharUsed= NULL;
  57. if( uCodePage != CP_UTF8 )
  58. {
  59. pfDefCharUsed= &fDefCharUsed;
  60. }
  61. //
  62. // Calculate number of bytes to write
  63. // The caller calculated the number of bytes to write
  64. // but it might be wrong because the user approved data loss.
  65. // Do the calculation here (again) in case the dwFlags are different.
  66. // FEATURE: We could optimize this in the case where the dwFlags did not
  67. // change.
  68. //
  69. nBytes=
  70. WideCharToMultiByte( uCodePage, // code page
  71. dwFlags, // performance and mapping flags
  72. (LPWSTR) lpBuffer, // wide char buffer
  73. nChars, // chars in wide char buffer
  74. NULL, // output buffer
  75. 0, // size of output buffer
  76. NULL, // char to sub. for unmapped chars (use default)
  77. pfDefCharUsed); // flag to set if default char used
  78. if( nBytes == 0 )
  79. {
  80. return( FALSE );
  81. }
  82. //
  83. // Allocate buffer to convert to
  84. //
  85. lpAnsi= (LPSTR) LocalAlloc( LPTR, nBytes + 1 );
  86. if( !lpAnsi )
  87. {
  88. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  89. return (FALSE);
  90. }
  91. bStatus=
  92. WideCharToMultiByte( uCodePage, // code page
  93. dwFlags, // performance and mapping flags
  94. (LPWSTR) lpBuffer, // wide char buffer
  95. nChars, // chars in wide char buffer
  96. lpAnsi, // resultant ascii string
  97. nBytes, // size of ascii string buffer
  98. NULL, // char to sub. for unmapped chars (use default)
  99. pfDefCharUsed); // flag to set if default char used
  100. if( bStatus )
  101. {
  102. bStatus= WriteFile( hFile, lpAnsi, nBytes, &nBytesWritten, NULL );
  103. }
  104. LocalFree( lpAnsi );
  105. return( bStatus );
  106. } // end of AnsiWriteFile()
  107. // Routines to deal with the soft EOL formatting.
  108. //
  109. // MLE Actually inserts characters into the text being under edit, so they
  110. // have to be removed before saving the file.
  111. //
  112. // It turns out that MLE will get confused if the current line is bigger than
  113. // the current file, so we will reset the cursor to 0,0 to keep it from looking stupid.
  114. // Should be fixed in MLE, but...
  115. //
  116. static DWORD dwStartSel; // saved start of selection
  117. static DWORD dwEndSel; // saved end of selection
  118. VOID ClearFmt(VOID)
  119. {
  120. if( fWrap )
  121. {
  122. if( fMLE_is_broken )
  123. {
  124. GotoAndScrollInView( 1 );
  125. }
  126. else
  127. {
  128. SendMessage( hwndEdit, EM_GETSEL, (WPARAM) &dwStartSel, (LPARAM) &dwEndSel );
  129. SendMessage( hwndEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) 0 );
  130. }
  131. SendMessage( hwndEdit, EM_FMTLINES, (WPARAM)FALSE, 0 ); // remove soft EOLs
  132. }
  133. }
  134. VOID RestoreFmt(VOID)
  135. {
  136. UINT CharIndex;
  137. if( fWrap )
  138. {
  139. if( fMLE_is_broken )
  140. {
  141. NpReCreate( ES_STD ); // slow but it works
  142. }
  143. else
  144. {
  145. SendMessage( hwndEdit, EM_FMTLINES, (WPARAM)TRUE, 0 ); // add soft EOLs
  146. SendMessage( hwndEdit, EM_SETSEL, (WPARAM) dwStartSel, (LPARAM) dwEndSel);
  147. }
  148. }
  149. }
  150. /* Save notepad file to disk. szFileSave points to filename. fSaveAs
  151. is TRUE iff we are being called from SaveAsDlgProc. This implies we must
  152. open file on current directory, whether or not it already exists there
  153. or somewhere else in our search path.
  154. Assumes that text exists within hwndEdit. 30 July 1991 Clark Cyr
  155. */
  156. BOOL FAR SaveFile (HWND hwndParent, TCHAR *szFileSave, BOOL fSaveAs )
  157. {
  158. LPTSTR lpch;
  159. UINT nChars;
  160. BOOL flag;
  161. BOOL fNew = FALSE;
  162. BOOL fDefCharUsed = FALSE;
  163. BOOL* pfDefCharUsed;
  164. static WCHAR wchBOM = BYTE_ORDER_MARK;
  165. static WCHAR wchRBOM= REVERSE_BYTE_ORDER_MARK;
  166. HLOCAL hEText; // handle to MLE text
  167. DWORD nBytesWritten; // number of bytes written
  168. DWORD nAsciiLength; // length of equivalent ascii file
  169. UINT cpTemp= CP_ACP; // code page to convert to
  170. DWORD dwFlags; // flags for WideCharToMultiByte
  171. /* If saving to an existing file, make sure correct disk is in drive */
  172. if (!fSaveAs)
  173. {
  174. fp= CreateFile( szFileSave, // name of file
  175. GENERIC_READ|GENERIC_WRITE, // access mode
  176. FILE_SHARE_READ, // share mode
  177. NULL, // security descriptor
  178. OPEN_EXISTING, // how to create
  179. FILE_ATTRIBUTE_NORMAL, // file attributes
  180. NULL); // hnd of file with attrs
  181. }
  182. else
  183. {
  184. // Carefully open the file. Do not truncate it if it exists.
  185. // set the fNew flag if it had to be created.
  186. // We do all this in case of failures later in the process.
  187. fp= CreateFile( szFileSave, // name of file
  188. GENERIC_READ|GENERIC_WRITE, // access mode
  189. FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode
  190. NULL, // security descriptor
  191. OPEN_ALWAYS, // how to create
  192. FILE_ATTRIBUTE_NORMAL, // file attributes
  193. NULL); // hnd of file with attrs
  194. if( fp != INVALID_HANDLE_VALUE )
  195. {
  196. fNew= (GetLastError() != ERROR_ALREADY_EXISTS );
  197. }
  198. }
  199. if( fp == INVALID_HANDLE_VALUE )
  200. {
  201. AlertBox( hwndParent, szNN, szCREATEERR, szFileSave,
  202. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  203. return FALSE;
  204. }
  205. // if wordwrap, remove soft carriage returns
  206. // Also move the cursor to a safe place to get around MLE bugs
  207. if( fWrap )
  208. {
  209. ClearFmt();
  210. }
  211. /* Must get text length after formatting */
  212. nChars = (UINT)SendMessage (hwndEdit, WM_GETTEXTLENGTH, 0, (LPARAM)0);
  213. hEText= (HANDLE) SendMessage( hwndEdit, EM_GETHANDLE, 0,0 );
  214. if( !hEText || !(lpch= (LPTSTR) LocalLock(hEText) ))
  215. {
  216. AlertUser_FileFail( szFileSave );
  217. goto CleanUp;
  218. }
  219. // Determine the SaveAs file type, and write the appropriate BOM.
  220. // If the filetype is UTF-8 or Ansi, do the conversion.
  221. switch(g_ftSaveAs)
  222. {
  223. case FT_UNICODE:
  224. WriteFile( fp, &wchBOM, ByteCountOf(1), &nBytesWritten, NULL );
  225. flag= WriteFile(fp, lpch, ByteCountOf(nChars), &nBytesWritten, NULL);
  226. break;
  227. case FT_UNICODEBE:
  228. WriteFile( fp, &wchRBOM, ByteCountOf(1), &nBytesWritten, NULL );
  229. ReverseEndian( lpch, lpch,nChars );
  230. flag= WriteFile(fp, lpch, ByteCountOf(nChars), &nBytesWritten, NULL);
  231. ReverseEndian( lpch, lpch, nChars );
  232. break;
  233. // If it UTF-8, write the BOM (3 bytes), set the code page and fall
  234. // through to the default case.
  235. case FT_UTF8:
  236. WriteFile( fp, &BOM_UTF8, 3, &nBytesWritten, NULL );
  237. // fall through to convert and write the file
  238. default:
  239. if (g_ftSaveAs != FT_UTF8)
  240. {
  241. //
  242. // Always use the current locale code page to do the translation
  243. // If the user changes locales, they will need to know what locale
  244. // this version of the file was saved with. Since we don't save that
  245. // information, the user may be in trouble. Unicode would save his bacon.
  246. //
  247. cpTemp= GetACP();
  248. pfDefCharUsed= &fDefCharUsed;
  249. dwFlags= WC_NO_BEST_FIT_CHARS;
  250. }
  251. else
  252. {
  253. cpTemp= CP_UTF8;
  254. pfDefCharUsed= NULL; // these must be NULL and 0 for this code page
  255. dwFlags= 0;
  256. }
  257. nAsciiLength= WideCharToMultiByte( cpTemp,
  258. dwFlags,
  259. (LPWSTR)lpch,
  260. nChars,
  261. NULL,
  262. 0,
  263. NULL, // if no conversion, use default char
  264. pfDefCharUsed);
  265. // If we can't round-trip the character, warn the user.
  266. // If we don't use WC_NO_BEST_FIT_CHARS, WCTMB() will convert some characters
  267. // without setting the fDefCharUsed (for example alt-233)
  268. // This fixes ntbug9:367586
  269. if( fDefCharUsed || (nChars && (nAsciiLength==0) ) )
  270. {
  271. if ( AlertBox( hwndParent, szNN, szErrUnicode, szFileSave,
  272. MB_APPLMODAL|MB_OKCANCEL|MB_ICONEXCLAMATION) == IDCANCEL)
  273. goto CleanUp;
  274. // User has approved loss of data, so try conversion with flags
  275. // that allow loss. ntbug9: 435042
  276. dwFlags= 0;
  277. }
  278. flag= AnsiWriteFile( fp, cpTemp, dwFlags, lpch, nChars );
  279. break;
  280. }
  281. if (!flag)
  282. {
  283. SetCursor(hStdCursor); /* display normal cursor */
  284. AlertUser_FileFail( szFileSave );
  285. CleanUp:
  286. SetCursor( hStdCursor );
  287. CloseHandle (fp); fp=INVALID_HANDLE_VALUE;
  288. if( hEText )
  289. LocalUnlock( hEText );
  290. if (fNew)
  291. DeleteFile (szFileSave);
  292. /* if wordwrap, insert soft carriage returns */
  293. if (fWrap)
  294. {
  295. RestoreFmt();
  296. }
  297. return FALSE;
  298. }
  299. else
  300. {
  301. SetEndOfFile (fp);
  302. g_ftOpenedAs = g_ftSaveAs;
  303. SendMessage (hwndEdit, EM_SETMODIFY, FALSE, 0L);
  304. SetTitle (szFileSave);
  305. fUntitled = FALSE;
  306. }
  307. CloseHandle (fp); fp=INVALID_HANDLE_VALUE;
  308. if( hEText )
  309. LocalUnlock( hEText );
  310. /* if wordwrap, insert soft carriage returns */
  311. if (fWrap)
  312. {
  313. RestoreFmt();
  314. }
  315. /* Display the hour glass cursor */
  316. SetCursor(hStdCursor);
  317. return TRUE;
  318. } // end of SaveFile()
  319. /* Read contents of file from disk.
  320. * Do any conversions required.
  321. * File is already open, referenced by handle fp
  322. * Close the file when done.
  323. * If typeFlag>=0, then use it as filetype, otherwise do automagic guessing.
  324. */
  325. BOOL FAR LoadFile (TCHAR * sz, INT typeFlag )
  326. {
  327. UINT len, i, nChars;
  328. LPTSTR lpch=NULL;
  329. LPTSTR lpBuf;
  330. LPSTR lpBufAfterBOM;
  331. BOOL fLog=FALSE;
  332. TCHAR* p;
  333. TCHAR szSave[MAX_PATH]; /* Private copy of current filename */
  334. BOOL bUnicode=FALSE; /* true if file detected as unicode */
  335. BOOL bUTF8=FALSE; /* true if file detected as UTF-8 */
  336. DWORD nBytesRead; // number of bytes read
  337. BY_HANDLE_FILE_INFORMATION fiFileInfo;
  338. BOOL bStatus; // boolean status
  339. HLOCAL hNewEdit=NULL; // new handle for edit buffer
  340. HANDLE hMap; // file mapping handle
  341. TCHAR szNullFile[2]; // fake null mapped file
  342. INT cpTemp = CP_ACP;
  343. NP_FILETYPE ftOpenedAs=FT_UNKNOWN;
  344. if( fp == INVALID_HANDLE_VALUE )
  345. {
  346. AlertUser_FileFail( sz );
  347. return (FALSE);
  348. }
  349. //
  350. // Get size of file
  351. // We use this heavy duty GetFileInformationByHandle API
  352. // because it finds bugs. It takes longer, but it only is
  353. // called at user interaction time.
  354. //
  355. bStatus= GetFileInformationByHandle( fp, &fiFileInfo );
  356. len= (UINT) fiFileInfo.nFileSizeLow;
  357. // NT may delay giving this status until the file is accessed.
  358. // i.e. the open succeeds, but operations may fail on damaged files.
  359. if( !bStatus )
  360. {
  361. AlertUser_FileFail( sz );
  362. CloseHandle( fp ); fp=INVALID_HANDLE_VALUE;
  363. return( FALSE );
  364. }
  365. // If the file is too big, fail now.
  366. // -1 not valid because we need a zero on the end.
  367. // If the file is too big, fail now.
  368. // -1 not valid because we need a zero on the end.
  369. //
  370. // bug# 168148: silently fails to open 2.4 gig text file on win64
  371. // Caused by trying to convert ascii file to unicode which overflowed
  372. // the dword length handled by multibytetowidechar conversion.
  373. // Since no one will be happy with the performance of the MLE with
  374. // a file this big, we will just refuse to open it now.
  375. //
  376. // For example, on a Pentium 173 MHz with 192 Megs o'RAM (Tecra 8000)
  377. // I got these results:
  378. //
  379. // size CPU-time
  380. // 0 .12
  381. // 1 .46
  382. // 2 .77
  383. // 3 1.041
  384. // 4 1.662
  385. // 5 2.092
  386. // 6 2.543
  387. // 7 3.023
  388. // 8 3.534
  389. // 9 4.084
  390. // 10 4.576
  391. // 16 8.371
  392. // 32 23.142
  393. // 64 74.426
  394. //
  395. // Curve fitting these numbers to cpu-time=a+b*size+c*size*size
  396. // we get a really good fit with cpu= .24+.28*size+.013*size*size
  397. //
  398. // For 1 gig, this works out to be 3.68 hours. 2 gigs=14.6 hours
  399. //
  400. // And the user isn't going to be happy with adding or deleting characters
  401. // with the MLE control. It wants to keep the memory stuctures uptodate
  402. // at all times.
  403. //
  404. // Going to richedit isn't a near term solution either:
  405. //
  406. // size CPU-time
  407. // 2 3.8
  408. // 4 9.0
  409. // 6 21.9
  410. // 8 30.4
  411. // 10 65.3
  412. // 16 1721 or >3.5 hours (it was still running when I killed it)
  413. //
  414. //
  415. // feature: should we only bail if not unicode?
  416. //
  417. if( len >=0x40000000 || fiFileInfo.nFileSizeHigh != 0 )
  418. {
  419. AlertBox( hwndNP, szNN, szFTL, sz,
  420. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  421. CloseHandle (fp); fp=INVALID_HANDLE_VALUE;
  422. return (FALSE);
  423. }
  424. SetCursor(hWaitCursor); // physical I/O takes time
  425. //
  426. // Create a file mapping so we don't page the file to
  427. // the pagefile. This is a big win on small ram machines.
  428. //
  429. if( len != 0 )
  430. {
  431. lpBuf= NULL;
  432. hMap= CreateFileMapping( fp, NULL, PAGE_READONLY, 0, len, NULL );
  433. if( hMap )
  434. {
  435. lpBuf= MapViewOfFile( hMap, FILE_MAP_READ, 0,0,len);
  436. CloseHandle( hMap );
  437. }
  438. }
  439. else // file mapping doesn't work on zero length files
  440. {
  441. lpBuf= (LPTSTR) &szNullFile;
  442. *lpBuf= 0; // null terminate
  443. }
  444. CloseHandle( fp ); fp=INVALID_HANDLE_VALUE;
  445. if( lpBuf == NULL )
  446. {
  447. SetCursor( hStdCursor );
  448. AlertUser_FileFail( sz );
  449. return( FALSE );
  450. }
  451. //
  452. // protect access to the mapped file with a try/except so we
  453. // can detect I/O errors.
  454. //
  455. //
  456. // WARNING: be very very careful. This code is pretty fragile.
  457. // Files across the network, or RSM files (tape) may throw excepts
  458. // at random points in this code. Anywhere the code touches the
  459. // memory mapped file can cause an AV. Make sure variables are
  460. // in consistent state if an exception is thrown. Be very careful
  461. // with globals.
  462. __try
  463. {
  464. /* Determine the file type and number of characters
  465. * If the user overrides, use what is specified.
  466. * Otherwise, we depend on 'IsTextUnicode' getting it right.
  467. * If it doesn't, bug IsTextUnicode.
  468. */
  469. lpBufAfterBOM= (LPSTR) lpBuf;
  470. if( typeFlag == FT_UNKNOWN )
  471. {
  472. switch(*lpBuf)
  473. {
  474. case BYTE_ORDER_MARK:
  475. bUnicode= TRUE;
  476. ftOpenedAs= FT_UNICODE;
  477. // don't count the BOM.
  478. nChars= len / sizeof(TCHAR) -1;
  479. break;
  480. case REVERSE_BYTE_ORDER_MARK:
  481. bUnicode= TRUE;
  482. ftOpenedAs= FT_UNICODEBE;
  483. // don't count the BOM.
  484. nChars= len / sizeof(TCHAR) -1;
  485. break;
  486. // UTF bom has 3 bytes; if it doesn't have UTF BOM just fall through ..
  487. case BOM_UTF8_HALF:
  488. if (len > 2 && ((BYTE) *(((LPSTR)lpBuf)+2) == BOM_UTF8_2HALF) )
  489. {
  490. bUTF8= TRUE;
  491. cpTemp= CP_UTF8;
  492. ftOpenedAs= FT_UTF8;
  493. // Ignore the first three bytes.
  494. lpBufAfterBOM= (LPSTR)lpBuf + 3;
  495. len -= 3;
  496. break;
  497. }
  498. default:
  499. // Is the file unicode without BOM ?
  500. if ((bUnicode= IsInputTextUnicode((LPSTR) lpBuf, len)))
  501. {
  502. ftOpenedAs= FT_UNICODE;
  503. nChars= len / sizeof(TCHAR);
  504. }
  505. else
  506. {
  507. // Is the file UTF-8 even though it doesn't have UTF-8 BOM.
  508. if ((bUTF8= IsTextUTF8((LPSTR) lpBuf, len)))
  509. {
  510. ftOpenedAs= FT_UTF8;
  511. cpTemp= CP_UTF8;
  512. }
  513. // well, not it must be an ansi file!
  514. else
  515. {
  516. ftOpenedAs= FT_ANSI;
  517. cpTemp= CP_ACP;
  518. }
  519. }
  520. break;
  521. }
  522. }
  523. else
  524. {
  525. switch(typeFlag)
  526. {
  527. case FT_UNICODE:
  528. bUnicode= TRUE;
  529. ftOpenedAs= FT_UNICODE;
  530. nChars= len / sizeof(TCHAR);
  531. // don't count the BOM.
  532. if (*lpBuf == BYTE_ORDER_MARK)
  533. nChars--;
  534. break;
  535. case FT_UNICODEBE:
  536. bUnicode= TRUE;
  537. ftOpenedAs= FT_UNICODE;
  538. nChars= len / sizeof(TCHAR);
  539. // don't count the BOM.
  540. if (*lpBuf == REVERSE_BYTE_ORDER_MARK)
  541. nChars--;
  542. break;
  543. case FT_UTF8:
  544. bUTF8= TRUE;
  545. cpTemp= CP_UTF8;
  546. ftOpenedAs= FT_UTF8;
  547. if (len > 2 && ((*lpBuf == BOM_UTF8_HALF) && ((BYTE) *(((LPSTR)lpBuf)+2) == BOM_UTF8_2HALF)) )
  548. {
  549. // Ignore the first three bytes.
  550. lpBufAfterBOM= (LPSTR)lpBuf + 3;
  551. len -= 3;
  552. break;
  553. }
  554. break;
  555. case FT_ANSI:
  556. default:
  557. ftOpenedAs= FT_ANSI;
  558. cpTemp= CP_ACP;
  559. break;
  560. }
  561. }
  562. // find out no. of chars present in the string.
  563. if (!bUnicode)
  564. {
  565. nChars = MultiByteToWideChar (cpTemp,
  566. 0,
  567. (LPSTR)lpBufAfterBOM,
  568. len,
  569. NULL,
  570. 0);
  571. }
  572. //
  573. // Don't display text until all done.
  574. //
  575. SendMessage (hwndEdit, WM_SETREDRAW, (WPARAM)FALSE, (LPARAM)0);
  576. // Reset selection to 0
  577. SendMessage(hwndEdit, EM_SETSEL, 0, 0L);
  578. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  579. // resize the edit buffer
  580. // if we can't resize the memory, inform the user
  581. if (!(hNewEdit= LocalReAlloc(hEdit,ByteCountOf(nChars + 1),LMEM_MOVEABLE)))
  582. {
  583. /* Bug 7441: New() causes szFileName to be set to "Untitled". Save a
  584. * copy of the filename to pass to AlertBox.
  585. * 17 November 1991 Clark R. Cyr
  586. */
  587. lstrcpy(szSave, sz);
  588. New(FALSE);
  589. /* Display the hour glass cursor */
  590. SetCursor(hStdCursor);
  591. AlertBox( hwndNP, szNN, szFTL, szSave,
  592. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  593. if( lpBuf != (LPTSTR) &szNullFile )
  594. {
  595. UnmapViewOfFile( lpBuf );
  596. }
  597. // let user see old text
  598. SendMessage (hwndEdit, WM_SETREDRAW, (WPARAM)FALSE, (LPARAM)0);
  599. return FALSE;
  600. }
  601. /* Transfer file from temporary buffer to the edit buffer */
  602. lpch= (LPTSTR) LocalLock(hNewEdit);
  603. if( bUnicode )
  604. {
  605. /* skip the Byte Order Mark */
  606. if (*lpBuf == BYTE_ORDER_MARK)
  607. {
  608. CopyMemory (lpch, lpBuf + 1, ByteCountOf(nChars));
  609. }
  610. else if( *lpBuf == REVERSE_BYTE_ORDER_MARK )
  611. {
  612. ReverseEndian( lpch, lpBuf+1, nChars );
  613. }
  614. else
  615. {
  616. CopyMemory (lpch, lpBuf, ByteCountOf(nChars));
  617. }
  618. }
  619. else
  620. {
  621. nChars = MultiByteToWideChar (cpTemp,
  622. 0,
  623. (LPSTR)lpBufAfterBOM,
  624. len,
  625. (LPWSTR)lpch,
  626. nChars);
  627. }
  628. g_ftOpenedAs= ftOpenedAs; // got everything; update global safe now
  629. }
  630. __except(EXCEPTION_EXECUTE_HANDLER)
  631. {
  632. AlertBox( hwndNP, szNN, szDiskError, sz,
  633. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION );
  634. nChars= 0; // don't deal with it.
  635. }
  636. /* Free file mapping */
  637. if( lpBuf != (LPTSTR) &szNullFile )
  638. {
  639. UnmapViewOfFile( lpBuf );
  640. }
  641. if( lpch )
  642. {
  643. // Fix any NUL character that came in from the file to be spaces.
  644. for (i = 0, p = lpch; i < nChars; i++, p++)
  645. {
  646. if( *p == (TCHAR) 0 )
  647. *p= TEXT(' ');
  648. }
  649. // null terminate it. Safe even if nChars==0 because it is 1 TCHAR bigger
  650. *(lpch+nChars)= (TCHAR) 0; /* zero terminate the thing */
  651. // Set 'fLog' if first characters in file are ".LOG"
  652. fLog= *lpch++ == TEXT('.') && *lpch++ == TEXT('L') &&
  653. *lpch++ == TEXT('O') && *lpch == TEXT('G');
  654. }
  655. if( hNewEdit )
  656. {
  657. LocalUnlock( hNewEdit );
  658. // now it is safe to set the global edit handle
  659. hEdit= hNewEdit;
  660. }
  661. lstrcpy( szFileName, sz );
  662. SetTitle( sz );
  663. fUntitled= FALSE;
  664. /* Pass handle to edit control. This is more efficient than WM_SETTEXT
  665. * which would require twice the buffer space.
  666. */
  667. /* Bug 7443: If EM_SETHANDLE doesn't have enough memory to complete things,
  668. * it will send the EN_ERRSPACE message. If this happens, don't put up the
  669. * out of memory notification, put up the file to large message instead.
  670. * 17 November 1991 Clark R. Cyr
  671. */
  672. dwEmSetHandle = SETHANDLEINPROGRESS;
  673. SendMessage (hwndEdit, EM_SETHANDLE, (WPARAM)hEdit, (LPARAM)0);
  674. if (dwEmSetHandle == SETHANDLEFAILED)
  675. {
  676. SetCursor(hStdCursor);
  677. dwEmSetHandle = 0;
  678. AlertBox( hwndNP, szNN, szFTL, sz,MB_APPLMODAL|MB_OK|MB_ICONEXCLAMATION);
  679. New (FALSE);
  680. SendMessage (hwndEdit, WM_SETREDRAW, (WPARAM)TRUE, (LPARAM)0);
  681. return (FALSE);
  682. }
  683. dwEmSetHandle = 0;
  684. PostMessage (hwndEdit, EM_LIMITTEXT, (WPARAM)CCHNPMAX, 0L);
  685. /* If file starts with ".LOG" go to end and stamp date time */
  686. if (fLog)
  687. {
  688. SendMessage( hwndEdit, EM_SETSEL, (WPARAM)nChars, (LPARAM)nChars);
  689. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0);
  690. InsertDateTime(TRUE);
  691. }
  692. /* Move vertical thumb to correct position */
  693. SetScrollPos (hwndNP,
  694. SB_VERT,
  695. (int) SendMessage (hwndEdit, WM_VSCROLL, EM_GETTHUMB, 0L),
  696. TRUE);
  697. /* Now display text */
  698. SendMessage( hwndEdit, WM_SETREDRAW, (WPARAM)TRUE, (LPARAM)0 );
  699. InvalidateRect( hwndEdit, (LPRECT)NULL, TRUE );
  700. UpdateWindow( hwndEdit );
  701. SetCursor(hStdCursor);
  702. return( TRUE );
  703. } // end of LoadFile()
  704. /* New Command - reset everything
  705. */
  706. void FAR New (BOOL fCheck)
  707. {
  708. HANDLE hTemp;
  709. TCHAR* pSz;
  710. if (!fCheck || CheckSave (FALSE))
  711. {
  712. SendMessage( hwndEdit, WM_SETTEXT, (WPARAM)0, (LPARAM)TEXT("") );
  713. fUntitled= TRUE;
  714. lstrcpy( szFileName, szUntitled );
  715. SetTitle(szFileName );
  716. SendMessage( hwndEdit, EM_SETSEL, 0, 0L );
  717. SendMessage( hwndEdit, EM_SCROLLCARET, 0, 0 );
  718. // resize of 1 NULL character i.e. zero length
  719. hTemp= LocalReAlloc( hEdit, sizeof(TCHAR), LMEM_MOVEABLE );
  720. if( hTemp )
  721. {
  722. hEdit= hTemp;
  723. }
  724. // null terminate the buffer. LocalReAlloc won't do it
  725. // because in all cases it is not growing which is the
  726. // only time it would zero out anything.
  727. pSz= LocalLock( hEdit );
  728. *pSz= TEXT('\0');
  729. LocalUnlock( hEdit );
  730. SendMessage (hwndEdit, EM_SETHANDLE, (WPARAM)hEdit, 0L);
  731. szSearch[0] = (TCHAR) 0;
  732. }
  733. } // end of New()
  734. /* If sz does not have extension, append ".txt"
  735. * This function is useful for getting to undecorated filenames
  736. * that setup apps use. DO NOT CHANGE the extension. Too many setup
  737. * apps depend on this functionality.
  738. */
  739. void FAR AddExt( TCHAR* sz )
  740. {
  741. TCHAR* pch1;
  742. int ch;
  743. DWORD dwSize;
  744. dwSize= lstrlen(sz);
  745. pch1= sz + dwSize; // point to end
  746. ch= *pch1;
  747. while( ch != TEXT('.') && ch != TEXT('\\') && ch != TEXT(':') && pch1 > sz)
  748. {
  749. //
  750. // backup one character. Do NOT use CharPrev because
  751. // it sometimes doesn't actually backup. Some Thai
  752. // tone marks fit this category but there seems to be others.
  753. // This is safe since it will stop at the beginning of the
  754. // string or on delimiters listed above. bug# 139374 2/13/98
  755. //
  756. // pch1= (TCHAR*)CharPrev (sz, pch1);
  757. pch1--; // back up
  758. ch= *pch1;
  759. }
  760. if( *pch1 != TEXT('.') )
  761. {
  762. if( dwSize + sizeof(".txt") <= MAX_PATH ) { // avoid buffer overruns
  763. lstrcat( sz, TEXT(".txt") );
  764. }
  765. }
  766. }
  767. /* AlertUser_FileFail( LPTSTR szFileName )
  768. *
  769. * szFileName is the name of file that was attempted to open.
  770. * Some sort of failure on file open. Alert the user
  771. * with some monologue box. At least give him decent
  772. * error messages.
  773. */
  774. VOID FAR AlertUser_FileFail( LPTSTR szFileName )
  775. {
  776. TCHAR msg[256]; // buffer to format message into
  777. DWORD dwStatus; // status from FormatMessage
  778. UINT style= MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION;
  779. // Check GetLastError to see why we failed
  780. dwStatus=
  781. FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS |
  782. FORMAT_MESSAGE_FROM_SYSTEM,
  783. NULL,
  784. GetLastError(),
  785. GetUserDefaultLangID(),
  786. msg, // where message will end up
  787. CharSizeOf(msg), NULL );
  788. if( dwStatus )
  789. {
  790. MessageBox( hwndNP, msg, szNN, style );
  791. }
  792. else
  793. {
  794. AlertBox( hwndNP, szNN, szDiskError, szFileName, style );
  795. }
  796. }