DWinProgramming /Samples/Chap13/PopPad/PopFile.d

Language D Lines 202
MD5 Hash efe0385eb1c627056dea438cbb641352
Repository git://github.com/AndrejMitrovic/DWinProgramming.git View Raw File View Project SPDX
  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
/+
 + Copyright (c) Charles Petzold, 1998.
 + Ported to the D Programming Language by Andrej Mitrovic, 2011.
 +/

module PopFile;

import core.memory;
import core.runtime;
import core.thread;
import std.conv;
import std.math;
import std.range;
import std.string;
import std.utf;

auto toUTF16z(S)(S s)
{
    return toUTFz!(const(wchar)*)(s);
}

pragma(lib, "gdi32.lib");
pragma(lib, "comdlg32.lib");
pragma(lib, "winmm.lib");
import win32.windef;
import win32.winuser;
import win32.wingdi;
import win32.winbase;
import win32.commdlg;
import win32.mmsystem;
import win32.winnls;

LOGFONT logfont;
HFONT hFont;
OPENFILENAME ofn;

void PopFileInitialize(HWND hwnd)
{
    static string szFilter = "Text Files (*.TXT)\0*.txt\0"
                             "ASCII Files (*.ASC)\0*.asc\0"
                             "All Files (*.*)\0*.*\0\0";

    ofn.hwndOwner         = hwnd;
    ofn.hInstance         = NULL;
    ofn.lpstrFilter       = szFilter.toUTF16z;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter    = 0;
    ofn.nFilterIndex      = 0;
    ofn.lpstrFile         = NULL;            // Set in Open and Close functions
    ofn.nMaxFile          = MAX_PATH;
    ofn.lpstrFileTitle    = NULL;            // Set in Open and Close functions
    ofn.nMaxFileTitle     = MAX_PATH;
    ofn.lpstrInitialDir   = NULL;
    ofn.lpstrTitle        = NULL;
    ofn.Flags             = 0;               // Set in Open and Close functions
    ofn.nFileOffset       = 0;
    ofn.nFileExtension    = 0;
    ofn.lpstrDefExt       = "txt";
    ofn.lCustData         = 0L;
    ofn.lpfnHook          = NULL;
    ofn.lpTemplateName    = NULL;
}

BOOL PopFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
    ofn.hwndOwner      = hwnd;
    ofn.lpstrFile      = pstrFileName;
    ofn.lpstrFileTitle = pstrTitleName;
    ofn.Flags          = OFN_HIDEREADONLY | OFN_CREATEPROMPT;

    return GetOpenFileName(&ofn);
}

BOOL PopFileSaveDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
    ofn.hwndOwner      = hwnd;
    ofn.lpstrFile      = pstrFileName;
    ofn.lpstrFileTitle = pstrTitleName;
    ofn.Flags          = OFN_OVERWRITEPROMPT;

    return GetSaveFileName(&ofn);
}

BOOL PopFileRead(HWND hwndEdit, PTSTR pstrFileName)
{
    BYTE   bySwap;
    DWORD  dwBytesRead;
    HANDLE hFile;
    int i, iFileLength, iUniTest;
    PBYTE pBuffer, pText, pConv;

    // Open the file.
    if (INVALID_HANDLE_VALUE ==
        (hFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ,
                            NULL, OPEN_EXISTING, 0, NULL)))
        return FALSE;

    // Get file size in bytes and allocate memory for read.
    // Add an extra two bytes for zero termination.
    iFileLength = GetFileSize(hFile, NULL);
    pBuffer     = cast(typeof(pBuffer))GC.malloc(iFileLength + 2);

    // Read file and put terminating zeros at end.
    ReadFile(hFile, pBuffer, iFileLength, &dwBytesRead, NULL);
    CloseHandle(hFile);
    pBuffer[iFileLength]     = 0;
    pBuffer[iFileLength + 1] = 0;

    // Test to see if the text is Unicode
    iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;

    if (IsTextUnicode(pBuffer, iFileLength, &iUniTest))
    {
        pText        = pBuffer + 2;
        iFileLength -= 2;

        if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
        {
            for (i = 0; i < iFileLength / 2; i++)
            {
                bySwap = (cast(BYTE*)pText) [2 * i];
                (cast(BYTE*)pText) [2 * i]     = (cast(BYTE*)pText) [2 * i + 1];
                (cast(BYTE*)pText) [2 * i + 1] = bySwap;
            }
        }

        // Allocate memory for possibly converted string
        pConv = cast(typeof(pConv))GC.malloc(iFileLength + 2);

        // If the edit control is not Unicode, convert Unicode text to
        // non-Unicode (ie, in general, wide character).

        lstrcpy(cast(PTSTR)pConv, cast(PTSTR)pText);

    }
    else  // the file is not Unicode
    {
        pText = pBuffer;

        // Allocate memory for possibly converted string.
        pConv = cast(typeof(pConv))GC.malloc(2 * iFileLength + 2);

        // If the edit control is Unicode, convert ASCII text.
        MultiByteToWideChar(CP_ACP, 0, cast(char*)pText, -1, cast(PTSTR)pConv, iFileLength + 1);

        // If not, just copy buffer

    }

    SetWindowText(hwndEdit, cast(PTSTR)pConv);
    GC.free(pBuffer);
    GC.free(pConv);

    return TRUE;
}

BOOL PopFileWrite(HWND hwndEdit, PTSTR pstrFileName)
{
    DWORD  dwBytesWritten;
    HANDLE hFile;
    int iLength;
    PTSTR pstrBuffer;
    WORD  wByteOrderMark = 0xFEFF;

    // Open the file, creating it if necessary
    if (INVALID_HANDLE_VALUE ==
        (hFile = CreateFile(pstrFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)))
        return FALSE;

    // Get the number of characters in the edit control and allocate
    // memory for them.
    iLength    = GetWindowTextLength(hwndEdit);
    pstrBuffer = cast(PTSTR)GC.malloc((iLength + 1) * TCHAR.sizeof);

    if (!pstrBuffer)
    {
        CloseHandle(hFile);
        return FALSE;
    }

    // If the edit control will return Unicode text, write the
    // byte order mark to the file.
    WriteFile(hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL);

    // Get the edit buffer and write that out to the file.
    GetWindowText(hwndEdit, pstrBuffer, iLength + 1);
    WriteFile(hFile, pstrBuffer, iLength * TCHAR.sizeof,
              &dwBytesWritten, NULL);

    if ((iLength * TCHAR.sizeof) != cast(int)dwBytesWritten)
    {
        CloseHandle(hFile);
        GC.free(pstrBuffer);
        return FALSE;
    }

    CloseHandle(hFile);
    GC.free(pstrBuffer);

    return TRUE;
}
Back to Top