PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap13/PopPad/PopFile.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 201 lines | 143 code | 37 blank | 21 comment | 9 complexity | 4a73bb2a5099a690ad0ec804891348a1 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module PopFile;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. pragma(lib, "winmm.lib");
  21. import core.sys.windows.windef;
  22. import core.sys.windows.winuser;
  23. import core.sys.windows.wingdi;
  24. import core.sys.windows.winbase;
  25. import core.sys.windows.commdlg;
  26. import core.sys.windows.mmsystem;
  27. import core.sys.windows.winnls;
  28. LOGFONT logfont;
  29. HFONT hFont;
  30. OPENFILENAME ofn;
  31. void PopFileInitialize(HWND hwnd)
  32. {
  33. static string szFilter = "Text Files (*.TXT)\0*.txt\0"
  34. "ASCII Files (*.ASC)\0*.asc\0"
  35. "All Files (*.*)\0*.*\0\0";
  36. ofn.hwndOwner = hwnd;
  37. ofn.hInstance = NULL;
  38. ofn.lpstrFilter = szFilter.toUTF16z;
  39. ofn.lpstrCustomFilter = NULL;
  40. ofn.nMaxCustFilter = 0;
  41. ofn.nFilterIndex = 0;
  42. ofn.lpstrFile = NULL; // Set in Open and Close functions
  43. ofn.nMaxFile = MAX_PATH;
  44. ofn.lpstrFileTitle = NULL; // Set in Open and Close functions
  45. ofn.nMaxFileTitle = MAX_PATH;
  46. ofn.lpstrInitialDir = NULL;
  47. ofn.lpstrTitle = NULL;
  48. ofn.Flags = 0; // Set in Open and Close functions
  49. ofn.nFileOffset = 0;
  50. ofn.nFileExtension = 0;
  51. ofn.lpstrDefExt = "txt";
  52. ofn.lCustData = 0L;
  53. ofn.lpfnHook = NULL;
  54. ofn.lpTemplateName = NULL;
  55. }
  56. BOOL PopFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  57. {
  58. ofn.hwndOwner = hwnd;
  59. ofn.lpstrFile = pstrFileName;
  60. ofn.lpstrFileTitle = pstrTitleName;
  61. ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;
  62. return GetOpenFileName(&ofn);
  63. }
  64. BOOL PopFileSaveDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  65. {
  66. ofn.hwndOwner = hwnd;
  67. ofn.lpstrFile = pstrFileName;
  68. ofn.lpstrFileTitle = pstrTitleName;
  69. ofn.Flags = OFN_OVERWRITEPROMPT;
  70. return GetSaveFileName(&ofn);
  71. }
  72. BOOL PopFileRead(HWND hwndEdit, PTSTR pstrFileName)
  73. {
  74. BYTE bySwap;
  75. DWORD dwBytesRead;
  76. HANDLE hFile;
  77. int i, iFileLength, iUniTest;
  78. PBYTE pBuffer, pText, pConv;
  79. // Open the file.
  80. if (INVALID_HANDLE_VALUE ==
  81. (hFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ,
  82. NULL, OPEN_EXISTING, 0, NULL)))
  83. return FALSE;
  84. // Get file size in bytes and allocate memory for read.
  85. // Add an extra two bytes for zero termination.
  86. iFileLength = GetFileSize(hFile, NULL);
  87. pBuffer = cast(typeof(pBuffer))GC.malloc(iFileLength + 2);
  88. // Read file and put terminating zeros at end.
  89. ReadFile(hFile, pBuffer, iFileLength, &dwBytesRead, NULL);
  90. CloseHandle(hFile);
  91. pBuffer[iFileLength] = 0;
  92. pBuffer[iFileLength + 1] = 0;
  93. // Test to see if the text is Unicode
  94. iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
  95. if (IsTextUnicode(pBuffer, iFileLength, &iUniTest))
  96. {
  97. pText = pBuffer + 2;
  98. iFileLength -= 2;
  99. if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
  100. {
  101. for (i = 0; i < iFileLength / 2; i++)
  102. {
  103. bySwap = (cast(BYTE*)pText) [2 * i];
  104. (cast(BYTE*)pText) [2 * i] = (cast(BYTE*)pText) [2 * i + 1];
  105. (cast(BYTE*)pText) [2 * i + 1] = bySwap;
  106. }
  107. }
  108. // Allocate memory for possibly converted string
  109. pConv = cast(typeof(pConv))GC.malloc(iFileLength + 2);
  110. // If the edit control is not Unicode, convert Unicode text to
  111. // non-Unicode (ie, in general, wide character).
  112. lstrcpy(cast(PTSTR)pConv, cast(PTSTR)pText);
  113. }
  114. else // the file is not Unicode
  115. {
  116. pText = pBuffer;
  117. // Allocate memory for possibly converted string.
  118. pConv = cast(typeof(pConv))GC.malloc(2 * iFileLength + 2);
  119. // If the edit control is Unicode, convert ASCII text.
  120. MultiByteToWideChar(CP_ACP, 0, cast(char*)pText, -1, cast(PTSTR)pConv, iFileLength + 1);
  121. // If not, just copy buffer
  122. }
  123. SetWindowText(hwndEdit, cast(PTSTR)pConv);
  124. GC.free(pBuffer);
  125. GC.free(pConv);
  126. return TRUE;
  127. }
  128. BOOL PopFileWrite(HWND hwndEdit, PTSTR pstrFileName)
  129. {
  130. DWORD dwBytesWritten;
  131. HANDLE hFile;
  132. int iLength;
  133. PTSTR pstrBuffer;
  134. WORD wByteOrderMark = 0xFEFF;
  135. // Open the file, creating it if necessary
  136. if (INVALID_HANDLE_VALUE ==
  137. (hFile = CreateFile(pstrFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)))
  138. return FALSE;
  139. // Get the number of characters in the edit control and allocate
  140. // memory for them.
  141. iLength = GetWindowTextLength(hwndEdit);
  142. pstrBuffer = cast(PTSTR)GC.malloc((iLength + 1) * TCHAR.sizeof);
  143. if (!pstrBuffer)
  144. {
  145. CloseHandle(hFile);
  146. return FALSE;
  147. }
  148. // If the edit control will return Unicode text, write the
  149. // byte order mark to the file.
  150. WriteFile(hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL);
  151. // Get the edit buffer and write that out to the file.
  152. GetWindowText(hwndEdit, pstrBuffer, iLength + 1);
  153. WriteFile(hFile, pstrBuffer, iLength * TCHAR.sizeof,
  154. &dwBytesWritten, NULL);
  155. if ((iLength * TCHAR.sizeof) != cast(int)dwBytesWritten)
  156. {
  157. CloseHandle(hFile);
  158. GC.free(pstrBuffer);
  159. return FALSE;
  160. }
  161. CloseHandle(hFile);
  162. GC.free(pstrBuffer);
  163. return TRUE;
  164. }