--- mstools/samples/mcitest/edit.c 2018/08/09 18:22:16 1.1.1.1 +++ mstools/samples/mcitest/edit.c 2018/08/09 18:24:31 1.1.1.2 @@ -49,32 +49,44 @@ BOOL EditOpenFile( HWND hwndEdit, - LPSTR lszFile) + LPTSTR lszFile) { - OFSTRUCT of; /* structure used by the OpenFile routine */ +#ifdef UNICODE + HANDLE fh; /* DOS file handle returned by OpenFile */ +#else HFILE fh; /* DOS file handle returned by OpenFile */ - LPSTR lszText; /* pointer to the opened file's text */ + OFSTRUCT of; /* structure used by the OpenFile routine */ +#endif + LPTSTR lszText; /* pointer to the opened file's text */ UINT nFileLen; /* length, in bytes, of the opened file */ HCURSOR hcur; /* handle to the pre-hourglass cursor */ /* If a valid window handle or a filename was not specified, then exit. */ if (!hwndEdit || !lszFile) { - dprintf1("EditOpenFile: Invalid window or filename"); + dprintf1((TEXT("EditOpenFile: Invalid window or filename"))); return FALSE; } /* Open the file for reading */ +#ifdef UNICODE + fh = CreateFile(lszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); + if (fh == INVALID_HANDLE_VALUE) { + dprintf1((TEXT("Failed to open: %s"), lszFile)); + return FALSE; + } +#else fh = OpenFile(lszFile, &of, OF_READ); if (fh == HFILE_ERROR) { - dprintf1("Failed to open: %s", lszFile); + dprintf1((TEXT("Failed to open: %s"), lszFile)); return FALSE; } +#endif nFileLen = (UINT)GetFileSize((HANDLE)fh, NULL); if (HFILE_ERROR == nFileLen) { - dprintf1("Failed to find file size: %s", lszFile); + dprintf1((TEXT("Failed to find file size: %s"), lszFile)); return FALSE; } @@ -86,7 +98,7 @@ BOOL EditOpenFile( * */ - lszText = (LPTSTR)GAllocPtr(nFileLen+1); + lszText = (LPTSTR)GAllocPtr(nFileLen+1); // Note: no *sizeof(TCHAR) if (NULL != lszText) { BOOL fReturn; @@ -96,22 +108,43 @@ BOOL EditOpenFile( /* Read the file and copy the contents into the edit control */ - dprintf3("reading file..."); + dprintf3((TEXT("reading file..."))); + +#ifdef UNICODE + { + DWORD bytesRead; + + if (ReadFile(fh, lszText, nFileLen, &bytesRead, NULL)) { + lszText[bytesRead]=0; + dprintf2((TEXT("File loaded ok"))); + SetWindowTextA(hwndEdit, (LPSTR)lszText); // Until we have UNICODE files + fReturn = TRUE; + } else { + dprintf2((TEXT("Error loading file"))); + fReturn = FALSE; + } + + } + /* Free up the memory, close the file, and restore the cursor */ - if ((_lread(fh, lszText, nFileLen)) == nFileLen) { - lszText[nFileLen]=0; - dprintf2("File loaded ok"); + GFreePtr(lszText); + CloseHandle(fh); +#else + if ((_lread((HFILE)fh, lszText, nFileLen)) == nFileLen) { + lszText[nFileLen/sizeof(TCHAR)]=0; + dprintf2((TEXT("File loaded ok"))); SetWindowText(hwndEdit, lszText); fReturn = TRUE; } else { - dprintf2("Error loading file"); + dprintf2((TEXT("Error loading file"))); fReturn = FALSE; } /* Free up the memory, close the file, and restore the cursor */ GFreePtr(lszText); - _lclose(fh); + _lclose((HFILE)fh); +#endif SetCursor(hcur); return fReturn; @@ -123,8 +156,12 @@ BOOL EditOpenFile( * */ - dprintf1("Failed memory allocation for file"); - _lclose(fh); + dprintf1((TEXT("Failed memory allocation for file"))); +#ifdef UNICODE + CloseHandle(fh); +#else + _lclose((HFILE)fh); +#endif return FALSE; } @@ -147,32 +184,47 @@ BOOL EditOpenFile( BOOL EditSaveFile( HWND hwndEdit, - LPSTR lszFile) + LPTSTR lszFile) { +#ifdef UNICODE + HANDLE fh; /* DOS file handle returned by OpenFile */ + DWORD dwBytesWritten; +#else OFSTRUCT of; /* structure used by the OpenFile routine */ HFILE fh; /* DOS file handle returned by OpenFile */ - LPSTR lszText; /* pointer to the saved file's text */ +#endif + LPTSTR lszText; /* pointer to the saved file's text */ int nFileLen; /* length, in bytes, of the saved file */ HCURSOR hcur; /* handle to the pre-hourglass cursor */ /* If a valid window handle or a filename was not specified, then exit */ + dprintf2((TEXT("EditSaveFile: Saving %s"), lszFile)); if (!hwndEdit || !lszFile) { - dprintf1("EditSaveFile: Invalid window or filename"); + dprintf1((TEXT("EditSaveFile: Invalid window or filename"))); return FALSE; } /* Create (or overwrite) the save file */ +#ifdef UNICODE + fh = CreateFile(lszFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0); + if (fh == INVALID_HANDLE_VALUE) { + dprintf1((TEXT("EditSaveFile: Error opening file"))); + return FALSE; + } +#else fh = OpenFile(lszFile, &of, OF_CREATE); if (fh == HFILE_ERROR) { - dprintf1("EditSaveFile: Error opening file"); + dprintf1((TEXT("EditSaveFile: Error opening file"))); return FALSE; } +#endif /* Find out how big the contents of the edit box are */ - nFileLen = GetWindowTextLength(hwndEdit) + 1; + nFileLen = GetWindowTextLength(hwndEdit); + // nFileLen = nFileLen*sizeof(TCHAR); We write ASCII /* * Create a pointer to a region of memory large enough to hold the entire @@ -183,7 +235,7 @@ BOOL EditSaveFile( * */ - lszText = GAllocPtr(nFileLen); + lszText = (LPTSTR)GAllocPtr(nFileLen); if (NULL != lszText) { /* This could take a while - show the hourglass cursor */ @@ -192,13 +244,23 @@ BOOL EditSaveFile( /* Read the contents of the edit box, and write it to the save file */ - GetWindowText(hwndEdit, lszText, nFileLen); + GetWindowTextA(hwndEdit, (LPSTR)lszText, nFileLen); // Save ASCII file + +#ifdef UNICODE + WriteFile(fh, lszText, nFileLen, &dwBytesWritten, NULL); +#else _lwrite(fh, lszText, nFileLen); +#endif + /* Free up the memory, close the file, and restore the cursor */ GFreePtr(lszText); +#ifdef UNICODE + CloseHandle(fh); +#else _lclose(fh); +#endif SetCursor(hcur); return TRUE; @@ -209,7 +271,7 @@ BOOL EditSaveFile( * return FALSE. */ - _lclose(fh); + _lclose((HFILE)fh); return FALSE; } @@ -261,7 +323,7 @@ DWORD EditGetLineCount( BOOL EditGetLine( HWND hwndEdit, int iLine, - LPSTR lszLineBuffer, + LPTSTR lszLineBuffer, int cch) { int nLines; /* total number of lines in the edit box */ @@ -273,7 +335,10 @@ BOOL EditGetLine( nLines = (int)EditGetLineCount(hwndEdit); if (iLine < 0 || iLine >= nLines) { - dprintf1("Requested line count %d is out of range (%d)", iLine, nLines); + if (iLine!= nLines) { // Probably because the user pressed Enter + // and the line number is beyond the end + dprintf1((TEXT("Requested line count %d is out of range (%d)"), iLine, nLines)); + } return *lszLineBuffer = 0; /* This sets the buffer to null and returns FALSE */ } @@ -282,11 +347,10 @@ BOOL EditGetLine( /* NOTE: This routine is always called with cch at least TWO */ *((LPWORD)lszLineBuffer) = (WORD)cch; - cch = (int)SendMessage(hwndEdit, EM_GETLINE, iLine, (LONG)(LPSTR)lszLineBuffer); + cch = (int)SendMessage(hwndEdit, EM_GETLINE, iLine, (LONG)(LPTSTR)lszLineBuffer); /* The returned string is NOT null terminated */ /* Strip trailing white spaces from the string, and null-terminate it */ - while(cch > 0 && ISWHITE(lszLineBuffer[cch-1])) { cch--; }