⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memmapfile.cpp

📁 打印数据生成和模拟
💻 CPP
字号:
/*************************************************************
Module name: FileRev.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
*************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <string.h>            // For _strrev
#include "Resource.H"


//////////////////////////////////////////////////////////////


#define FILENAME  __TEXT("FILEREV.DAT")


//////////////////////////////////////////////////////////////


BOOL FileReverse (LPCTSTR pszPathname, PBOOL pfIsTextUnicode) {

   HANDLE hFile, hFileMap;
   LPVOID lpvFile;
   LPSTR lpchANSI;      // Always ANSI
   LPWSTR lpchUnicode;  // Always Unicode 
   DWORD dwFileSize;
   int iUnicodeTestFlags = -1;   // Try all tests

   // Assume that the text is Unicode.
   *pfIsTextUnicode = FALSE;

   // Open the file for reading and writing.
   hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ,
      0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

   if (hFile == INVALID_HANDLE_VALUE) {
      // File open failed.
      chMB(__TEXT("File could not be opened."));
      return(FALSE);
   }

   // Get the size of the file. I am assuming here 
   // that the file is smaller than 4 GB.
   dwFileSize = GetFileSize(hFile, NULL);

   // Create the file-mapping object. The file-mapping object 
   // is 1 character bigger than the file size so that a zero
   // character can be placed at the end of the file to terminate
   // the string (file). Because I don't yet know if the file
   // contains ANSI or Unicode characters, I assume worst case
   // and add the size of a WCHAR instead of CHAR.
   hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE,
      0, dwFileSize + sizeof(WCHAR), NULL);

   if (hFileMap == NULL) {
      // File-Mapping open failed.
      chMB(__TEXT("File map could not be opened."));
      CloseHandle(hFile);

      return(FALSE);
   }

   // Get the address where the first byte of the file 
   // is mapped into memory.
   lpvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);

   if (lpvFile == NULL) {
      // Map view of file failed.
      chMB(__TEXT("Could not map view of file."));
      CloseHandle(hFileMap);
      CloseHandle(hFile);
      return(FALSE);
   }



   // Take an educated guess as to whether the text file
   // contains ANSI or Unicode characters.
   *pfIsTextUnicode = IsTextUnicode(lpvFile, 
      dwFileSize, &iUnicodeTestFlags);

   if (!*pfIsTextUnicode) {
      // For all the file manipulations below, we explicitly 
      // use ANSI functions instead of Unicode functions 
      // because, although the application can be ANSI or 
      // Unicode, it is processing an ANSI file.

      // Put a zero character at the very end of the file.
      lpchANSI = (LPSTR) lpvFile;
      lpchANSI[dwFileSize / sizeof(CHAR)] = 0;

      // Reverse the contents of the file.
      _strrev(lpchANSI);

      // Convert all "\n\r" combinations back to "\r\n" to 
      // preserve the normal end-of-line sequence.
      lpchANSI = strchr(lpchANSI, '\n'); // Find first '\n'.

      while (lpchANSI != NULL) {
         // We have found an occurrence....
         *lpchANSI++ = '\r';   // Change '\n' to '\r'.
         *lpchANSI++ = '\n';   // Change '\r' to '\n'.
         lpchANSI = strchr(lpchANSI, '\n'); // Find the
                                            // next
                                            // occurrence.
      }

   } else {
      // For all the file manipulations below, we explicitly 
      // use Unicode functions instead of ANSI functions 
      // because, although the application can be ANSI or 
      // Unicode, it is processing a Unicode file.

      // Put a zero character at the very end of the file.
      lpchUnicode = (LPWSTR) lpvFile;
      lpchUnicode[dwFileSize / sizeof(WCHAR)] = 0;

      if ((iUnicodeTestFlags & IS_TEXT_UNICODE_SIGNATURE) != 0) {
         // If the first character is the Unicode BOM
         // (byte-order-mark), 0xFEFF, keep this character
         // at the beginning of the file.
         lpchUnicode++;
      }

      // Reverse the contents of the file.
      _wcsrev(lpchUnicode);

      // Convert all "\n\r" combinations back to "\r\n" to 
      // preserve the normal end-of-line sequence.
      lpchUnicode = wcschr(lpchUnicode, L'\n'); // Find first
                                                // '\n'.

      while (lpchUnicode != NULL) {
         // We have found an occurrence....
         *lpchUnicode++ = L'\r';   // Change '\n' to '\r'.
         *lpchUnicode++ = L'\n';   // Change '\r' to '\n'.
         lpchUnicode = wcschr(lpchUnicode, L'\n'); // Find the
                                                   // next
                                                   // occurrence.
      }
   }


   // Clean up everything before exiting.
   UnmapViewOfFile(lpvFile);
   CloseHandle(hFileMap);

   // Remove the trailing zero byte added earlier by 
   // positioning the file pointer at the end of the file,
   // not including the zero byte, and setting
   // the end-of-file.
   SetFilePointer(hFile, dwFileSize, NULL, FILE_BEGIN);

   // SetEndOfFile must be called after the
   // file-mapping kernel object is closed.
   SetEndOfFile(hFile);
   CloseHandle(hFile);

   return(TRUE);
}


/////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus, 
   LPARAM lParam) {

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_FILEREV, IDI_FILEREV);

   // Initialize the dialog box by disabling all 
   // the nonsetup controls.
   EnableWindow(GetDlgItem(hwnd, IDC_REVERSE), FALSE);

   return(TRUE);
}



/////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl, 
   UINT codeNotify) {
   
   TCHAR szPathname[_MAX_PATH];
   OPENFILENAME ofn;
   BOOL fIsTextUnicode;
   STARTUPINFO si;
   PROCESS_INFORMATION pi;

   switch (id) {
      case IDC_FILENAME:
         EnableWindow(GetDlgItem(hwnd, IDC_REVERSE), 
            Edit_GetTextLength(hwndCtl) > 0);
         break;

      case IDC_FILESELECT:
         chINITSTRUCT(ofn, TRUE);
         ofn.hwndOwner = hwnd;
         ofn.lpstrFile = szPathname;
         ofn.lpstrFile[0] = 0;
         ofn.nMaxFile = chDIMOF(szPathname);
         ofn.lpstrTitle = __TEXT("Select file for reversing");
         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST;
         GetOpenFileName(&ofn);
         SetDlgItemText(hwnd, IDC_FILENAME, ofn.lpstrFile);
         SetFocus(GetDlgItem(hwnd, IDC_REVERSE));
         break;

      case IDC_REVERSE:
         GetDlgItemText(hwnd, IDC_FILENAME, szPathname, chDIMOF(szPathname));

         // Copy input file to FILEREV.dat so that 
         // the original is not destroyed. 
         if (!CopyFile(szPathname, FILENAME, FALSE)) {
            // Copy failed.
            chMB(__TEXT("New file could not be created."));
            break;
         }

         if (FileReverse(FILENAME, &fIsTextUnicode)) {
            // Indicate whether we reversed a file containing
            // Unicode or an ANSI characters
            SetDlgItemText(hwnd, IDC_TEXTTYPE,
               fIsTextUnicode 
                  ? __TEXT("Unicode") : __TEXT("ANSI"));

            // Spawn Notepad to see the fruits of our labors.
            chINITSTRUCT(si, TRUE);
            //si.wShowWindow = SW_SHOW;
            //si.dwFlags = STARTF_USESHOWWINDOW;
            if (CreateProcess(NULL, _TEXT("NOTEPAD.EXE ") FILENAME,
               NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {

               CloseHandle(pi.hThread);
               CloseHandle(pi.hProcess);
            }
         }
         break;
         
      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


/////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg, 
   WPARAM wParam, LPARAM lParam) {
   
   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG,  Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND,  Dlg_OnCommand);
   }
   return(FALSE);
}


/////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_FILEREV), 
      NULL, Dlg_Proc);
   return(0);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -