ReactOS  0.4.15-dev-5109-g2469ce2
main.h File Reference
#include "notepad_res.h"
Include dependency graph for main.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  NOTEPAD_GLOBALS
 

Macros

#define ARRAY_SIZE(a)   (sizeof(a)/sizeof((a)[0]))
 
#define EDIT_STYLE_WRAP
 
#define EDIT_STYLE   (EDIT_STYLE_WRAP | WS_HSCROLL | ES_AUTOHSCROLL)
 
#define EDIT_CLASS   _T("EDIT")
 
#define MAX_STRING_LEN   255
 
#define EOLN_CRLF   0
 
#define EOLN_LF   1
 
#define EOLN_CR   2
 

Enumerations

enum  ENCODING {
  ENCODING_ANSI = 0, ENCODING_UTF16LE = 1, ENCODING_UTF16BE = 2, ENCODING_UTF8 = 3,
  ENCODING_AUTO = -1, ENCODING_ANSI = 0, ENCODING_UTF16LE = 1, ENCODING_UTF16BE = 2,
  ENCODING_UTF8 = 3, ENCODING_UTF8BOM = 4, ENCODING_ANSI = 0, ENCODING_UTF16LE = 1,
  ENCODING_UTF16BE = 2, ENCODING_UTF8 = 3, ENCODING_ANSI = 1, ENCODING_UTF8,
  ENCODING_UTF16LE, ENCODING_UTF16BE
}
 

Functions

VOID SetFileName (LPCTSTR szFileName)
 
BOOL ReadText (HANDLE hFile, LPWSTR *ppszText, DWORD *pdwTextLen, ENCODING *pencFile, int *piEoln)
 
BOOL WriteText (HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile, int iEoln)
 
void NOTEPAD_LoadSettingsFromRegistry (void)
 
void NOTEPAD_SaveSettingsToRegistry (void)
 
BOOL NOTEPAD_FindNext (FINDREPLACE *, BOOL, BOOL)
 
VOID NOTEPAD_EnableSearchMenu (VOID)
 

Variables

NOTEPAD_GLOBALS Globals
 

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   a)    (sizeof(a)/sizeof((a)[0]))

Definition at line 24 of file main.h.

◆ EDIT_CLASS

#define EDIT_CLASS   _T("EDIT")

Definition at line 32 of file main.h.

◆ EDIT_STYLE

Definition at line 30 of file main.h.

◆ EDIT_STYLE_WRAP

#define EDIT_STYLE_WRAP
Value:
#define ES_MULTILINE
Definition: pedump.c:667
#define WS_CHILD
Definition: pedump.c:617
#define ES_AUTOVSCROLL
Definition: pedump.c:671
#define ES_NOHIDESEL
Definition: pedump.c:673
#define WS_VSCROLL
Definition: pedump.c:627

Definition at line 28 of file main.h.

◆ EOLN_CR

#define EOLN_CR   2

Definition at line 52 of file main.h.

◆ EOLN_CRLF

#define EOLN_CRLF   0

Definition at line 50 of file main.h.

◆ EOLN_LF

#define EOLN_LF   1

Definition at line 51 of file main.h.

◆ MAX_STRING_LEN

#define MAX_STRING_LEN   255

Definition at line 34 of file main.h.

Enumeration Type Documentation

◆ ENCODING

Enumerator
ENCODING_ANSI 
ENCODING_UTF16LE 
ENCODING_UTF16BE 
ENCODING_UTF8 
ENCODING_AUTO 
ENCODING_ANSI 
ENCODING_UTF16LE 
ENCODING_UTF16BE 
ENCODING_UTF8 
ENCODING_UTF8BOM 
ENCODING_ANSI 
ENCODING_UTF16LE 
ENCODING_UTF16BE 
ENCODING_UTF8 
ENCODING_ANSI 
ENCODING_UTF8 
ENCODING_UTF16LE 
ENCODING_UTF16BE 

Definition at line 37 of file main.h.

38 {
39  ENCODING_AUTO = -1,
40  ENCODING_ANSI = 0,
41  ENCODING_UTF16LE = 1,
42  ENCODING_UTF16BE = 2,
43  ENCODING_UTF8 = 3,
45 } ENCODING;
ENCODING
Definition: main.h:37

Function Documentation

◆ NOTEPAD_EnableSearchMenu()

VOID NOTEPAD_EnableSearchMenu ( VOID  )

◆ NOTEPAD_FindNext()

BOOL NOTEPAD_FindNext ( FINDREPLACE ,
BOOL  ,
BOOL   
)

Definition at line 144 of file main.c.

145 {
146  int iTextLength, iTargetLength;
147  size_t iAdjustment = 0;
148  LPTSTR pszText = NULL;
149  DWORD dwPosition, dwBegin, dwEnd;
150  BOOL bMatches = FALSE;
151  TCHAR szResource[128], szText[128];
152  BOOL bSuccess;
153 
154  iTargetLength = (int) _tcslen(pFindReplace->lpstrFindWhat);
155 
156  /* Retrieve the window text */
157  iTextLength = GetWindowTextLength(Globals.hEdit);
158  if (iTextLength > 0)
159  {
160  pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
161  if (!pszText)
162  return FALSE;
163 
164  GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
165  }
166 
167  SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwBegin, (LPARAM) &dwEnd);
168  if (bReplace && ((dwEnd - dwBegin) == (DWORD) iTargetLength))
169  {
170  if (NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwBegin))
171  {
172  SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
173  iAdjustment = _tcslen(pFindReplace->lpstrReplaceWith) - (dwEnd - dwBegin);
174  }
175  }
176 
177  if (pFindReplace->Flags & FR_DOWN)
178  {
179  /* Find Down */
180  dwPosition = dwEnd;
181  while(dwPosition < (DWORD) iTextLength)
182  {
183  bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
184  if (bMatches)
185  break;
186  dwPosition++;
187  }
188  }
189  else
190  {
191  /* Find Up */
192  dwPosition = dwBegin;
193  while(dwPosition > 0)
194  {
195  dwPosition--;
196  bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
197  if (bMatches)
198  break;
199  }
200  }
201 
202  if (bMatches)
203  {
204  /* Found target */
205  if (dwPosition > dwBegin)
206  dwPosition += (DWORD) iAdjustment;
207  SendMessage(Globals.hEdit, EM_SETSEL, dwPosition, dwPosition + iTargetLength);
209  bSuccess = TRUE;
210  }
211  else
212  {
213  /* Can't find target */
214  if (bShowAlert)
215  {
216  LoadString(Globals.hInstance, STRING_CANNOTFIND, szResource, ARRAY_SIZE(szResource));
217  _sntprintf(szText, ARRAY_SIZE(szText), szResource, pFindReplace->lpstrFindWhat);
218  LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
219  MessageBox(Globals.hFindReplaceDlg, szText, szResource, MB_OK);
220  }
221  bSuccess = FALSE;
222  }
223 
224  if (pszText)
225  HeapFree(GetProcessHeap(), 0, pszText);
226  return bSuccess;
227 }
HWND hFindReplaceDlg
Definition: main.h:58
NOTEPAD_GLOBALS Globals
Definition: main.c:31
#define TRUE
Definition: types.h:120
UINT_PTR WPARAM
Definition: windef.h:207
#define DWORD
Definition: nt_native.h:44
static BOOLEAN bSuccess
Definition: drive.cpp:430
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define GetWindowText
Definition: winuser.h:5788
HINSTANCE hInstance
Definition: main.h:56
#define EM_GETSEL
Definition: winuser.h:1987
#define STRING_NOTEPAD
Definition: notepad_res.h:75
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL NOTEPAD_FindTextAt(FINDREPLACE *pFindReplace, LPCTSTR pszText, int iTextLength, DWORD dwPosition)
Definition: main.c:110
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
LONG_PTR LPARAM
Definition: windef.h:208
char TCHAR
Definition: xmlstorage.h:189
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define EM_SETSEL
Definition: winuser.h:2008
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _sntprintf
Definition: xmlstorage.h:201
#define MessageBox
Definition: winuser.h:5812
#define ARRAY_SIZE(a)
Definition: main.h:24
#define NULL
Definition: types.h:112
#define MB_OK
Definition: winuser.h:784
#define FR_DOWN
Definition: commdlg.h:127
#define STRING_CANNOTFIND
Definition: notepad_res.h:89
#define SendMessage
Definition: winuser.h:5833
#define GetWindowTextLength
Definition: winuser.h:5789
HWND hEdit
Definition: main.h:59
#define HeapFree(x, y, z)
Definition: compat.h:594
#define LoadString
Definition: winuser.h:5809
#define EM_REPLACESEL
Definition: winuser.h:1996
#define EM_SCROLLCARET
Definition: richedit.h:81
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by DIALOG_SearchNext(), NOTEPAD_ReplaceAll(), and NOTEPAD_WndProc().

◆ NOTEPAD_LoadSettingsFromRegistry()

void NOTEPAD_LoadSettingsFromRegistry ( void  )

Definition at line 114 of file settings.c.

115 {
116  HKEY hKey = NULL;
117  HFONT hFont;
118  DWORD dwPointSize = 0;
119  INT base_length, dx, dy;
120 
123 
124  dx = (INT)(base_length * .95);
125  dy = dx * 3 / 4;
126  SetRect(&Globals.main_rect, 0, 0, dx, dy);
127 
129  {
130  QueryByte(hKey, _T("lfCharSet"), &Globals.lfFont.lfCharSet);
131  QueryByte(hKey, _T("lfClipPrecision"), &Globals.lfFont.lfClipPrecision);
132  QueryDword(hKey, _T("lfEscapement"), (DWORD*)&Globals.lfFont.lfEscapement);
133  QueryString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName, ARRAY_SIZE(Globals.lfFont.lfFaceName));
134  QueryByte(hKey, _T("lfItalic"), &Globals.lfFont.lfItalic);
135  QueryDword(hKey, _T("lfOrientation"), (DWORD*)&Globals.lfFont.lfOrientation);
136  QueryByte(hKey, _T("lfOutPrecision"), &Globals.lfFont.lfOutPrecision);
137  QueryByte(hKey, _T("lfPitchAndFamily"), &Globals.lfFont.lfPitchAndFamily);
138  QueryByte(hKey, _T("lfQuality"), &Globals.lfFont.lfQuality);
139  QueryByte(hKey, _T("lfStrikeOut"), &Globals.lfFont.lfStrikeOut);
140  QueryByte(hKey, _T("lfUnderline"), &Globals.lfFont.lfUnderline);
141  QueryDword(hKey, _T("lfWeight"), (DWORD*)&Globals.lfFont.lfWeight);
142  QueryDword(hKey, _T("iPointSize"), &dwPointSize);
143  QueryBool(hKey, _T("fWrap"), &Globals.bWrapLongLines);
144  QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
145  QueryString(hKey, _T("szHeader"), Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
146  QueryString(hKey, _T("szTrailer"), Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
147  QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMargins.left);
148  QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMargins.top);
149  QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
150  QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
151 
152  QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
153  QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
154  QueryDword(hKey, _T("iWindowPosDX"), (DWORD*)&dx);
155  QueryDword(hKey, _T("iWindowPosDY"), (DWORD*)&dy);
156 
157  Globals.main_rect.right = Globals.main_rect.left + dx;
158  Globals.main_rect.bottom = Globals.main_rect.top + dy;
159 
160  /* invert value because DIALOG_ViewStatusBar will be called to show it */
161  Globals.bShowStatusBar = !Globals.bShowStatusBar;
162 
163  if (dwPointSize != 0)
164  Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
165  else
166  Globals.lfFont.lfHeight = HeightFromPointSize(100);
167 
168  RegCloseKey(hKey);
169  }
170  else
171  {
172  /* If no settings are found in the registry, then use default values */
173  Globals.bShowStatusBar = FALSE;
174  Globals.bWrapLongLines = FALSE;
175  SetRect(&Globals.lMargins, 750, 1000, 750, 1000);
176 
177  /* FIXME: Globals.fSaveWindowPositions = FALSE; */
178  /* FIXME: Globals.fMLE_is_broken = FALSE; */
179 
181  ARRAY_SIZE(Globals.szHeader));
183  ARRAY_SIZE(Globals.szFooter));
184 
185  ZeroMemory(&Globals.lfFont, sizeof(Globals.lfFont));
186  Globals.lfFont.lfCharSet = DEFAULT_CHARSET;
187  Globals.lfFont.lfClipPrecision = CLIP_STROKE_PRECIS;
188  Globals.lfFont.lfEscapement = 0;
190  ARRAY_SIZE(Globals.lfFont.lfFaceName));
191  Globals.lfFont.lfItalic = FALSE;
192  Globals.lfFont.lfOrientation = 0;
193  Globals.lfFont.lfOutPrecision = OUT_STRING_PRECIS;
194 
195  /* WORKAROUND: Far East Asian users may not have suitable fixed-pitch fonts. */
197  {
198  case LANG_CHINESE:
199  case LANG_JAPANESE:
200  case LANG_KOREAN:
201  Globals.lfFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
202  break;
203  default:
204  Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
205  break;
206  }
207 
208  Globals.lfFont.lfQuality = PROOF_QUALITY;
209  Globals.lfFont.lfStrikeOut = FALSE;
210  Globals.lfFont.lfUnderline = FALSE;
211  Globals.lfFont.lfWeight = FW_NORMAL;
212  Globals.lfFont.lfHeight = HeightFromPointSize(100);
213  }
214 
215  hFont = CreateFontIndirect(&Globals.lfFont);
217  if (hFont)
218  {
219  if (Globals.hFont)
220  DeleteObject(Globals.hFont);
221  Globals.hFont = hFont;
222  }
223 }
static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
Definition: settings.c:83
int base_length[LENGTH_CODES]
Definition: trees.c:107
#define ERROR_SUCCESS
Definition: deptool.c:10
#define FF_DONTCARE
Definition: wingdi.h:448
#define STRING_DEFAULTFONT
Definition: notepad_res.h:103
#define TRUE
Definition: types.h:120
#define HKEY_CURRENT_USER
Definition: winreg.h:11
GLint dy
Definition: linetemp.h:97
#define INT
Definition: polytest.cpp:20
#define ZeroMemory
Definition: winbase.h:1667
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
UINT_PTR WPARAM
Definition: windef.h:207
#define DEFAULT_CHARSET
Definition: wingdi.h:384
#define PROOF_QUALITY
Definition: wingdi.h:438
int32_t INT
Definition: typedefs.h:58
HFONT hFont
Definition: main.c:53
#define STRING_PAGESETUP_HEADERVALUE
Definition: notepad_res.h:68
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
#define SM_CXSCREEN
Definition: winuser.h:953
#define LANG_JAPANESE
Definition: nls.h:76
#define FALSE
Definition: types.h:117
#define FIXED_PITCH
Definition: wingdi.h:444
#define OUT_STRING_PRECIS
Definition: wingdi.h:416
static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
Definition: settings.c:94
LONG_PTR LPARAM
Definition: windef.h:208
#define SM_CYSCREEN
Definition: winuser.h:954
#define _T(x)
Definition: vfdio.h:22
static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
Definition: settings.c:78
static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
Definition: settings.c:103
#define DEFAULT_PITCH
Definition: wingdi.h:443
#define STRING_PAGESETUP_FOOTERVALUE
Definition: notepad_res.h:69
int WINAPI GetSystemMetrics(_In_ int)
unsigned long DWORD
Definition: ntddk_ex.h:95
static LPCTSTR s_szRegistryKey
Definition: settings.c:27
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)
FxAutoRegKey hKey
#define ARRAY_SIZE(a)
Definition: main.h:24
#define FF_MODERN
Definition: wingdi.h:449
#define CreateFontIndirect
Definition: wingdi.h:4444
#define NULL
Definition: types.h:112
GLint dx
Definition: linetemp.h:97
#define WM_SETFONT
Definition: winuser.h:1640
#define FW_NORMAL
Definition: wingdi.h:373
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define SendMessage
Definition: winuser.h:5833
#define LANG_CHINESE
Definition: nls.h:42
#define LANG_KOREAN
Definition: nls.h:84
LANGID WINAPI GetUserDefaultLangID(void)
Definition: lang.c:738
static LONG HeightFromPointSize(DWORD dwPointSize)
Definition: settings.c:30
HINSTANCE hInstance
Definition: precomp.h:43
#define RegOpenKey
Definition: winreg.h:519
#define CLIP_STROKE_PRECIS
Definition: wingdi.h:428
#define RegCloseKey(hKey)
Definition: registry.h:47
#define LoadString
Definition: winuser.h:5809
#define PRIMARYLANGID(l)
Definition: nls.h:16

Referenced by _tWinMain().

◆ NOTEPAD_SaveSettingsToRegistry()

void NOTEPAD_SaveSettingsToRegistry ( void  )

Definition at line 241 of file settings.c.

242 {
243  HKEY hKey;
244  DWORD dwDisposition;
245 
246  GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
247 
249  0, NULL, 0, KEY_SET_VALUE, NULL,
250  &hKey, &dwDisposition) == ERROR_SUCCESS)
251  {
252  SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
253  SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
254  SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
255  SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
256  SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
257  SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
258  SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
259  SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
260  SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
261  SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
262  SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
263  SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
264  SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
265  SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
266  SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
267  SaveString(hKey, _T("szHeader"), Globals.szHeader);
268  SaveString(hKey, _T("szTrailer"), Globals.szFooter);
269  SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
270  SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
271  SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
272  SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
273  SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
274  SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
275  SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
276  SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
277 
278  RegCloseKey(hKey);
279  }
280 }
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define HKEY_CURRENT_USER
Definition: winreg.h:11
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
#define _T(x)
Definition: vfdio.h:22
unsigned long DWORD
Definition: ntddk_ex.h:95
static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
Definition: settings.c:225
static LPCTSTR s_szRegistryKey
Definition: settings.c:27
static DWORD PointSizeFromHeight(LONG lHeight)
Definition: settings.c:42
FxAutoRegKey hKey
#define NULL
Definition: types.h:112
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
Definition: settings.c:230
#define RegCloseKey(hKey)
Definition: registry.h:47
#define RegCreateKeyEx
Definition: winreg.h:501

Referenced by NOTEPAD_WndProc().

◆ ReadText()

BOOL ReadText ( HANDLE  hFile,
LPWSTR ppszText,
DWORD pdwTextLen,
ENCODING pencFile,
int piEoln 
)

Definition at line 96 of file text.c.

97 {
98  DWORD dwSize;
99  LPBYTE pBytes = NULL;
100  LPWSTR pszText;
101  LPWSTR pszAllocText = NULL;
102  DWORD dwPos, i;
103  DWORD dwCharCount;
104  BOOL bSuccess = FALSE;
105  BYTE b = 0;
106  ENCODING encFile = ENCODING_ANSI;
107  int iCodePage = 0;
108  WCHAR szCrlf[2] = {'\r', '\n'};
109  DWORD adwEolnCount[3] = {0, 0, 0};
110 
111  *ppszText = NULL;
112  *pdwTextLen = 0;
113 
115  if (dwSize == INVALID_FILE_SIZE)
116  goto done;
117 
118  pBytes = HeapAlloc(GetProcessHeap(), 0, dwSize + 2);
119  if (!pBytes)
120  goto done;
121 
122  if (!ReadFile(hFile, pBytes, dwSize, &dwSize, NULL))
123  goto done;
124  dwPos = 0;
125 
126  /* Make sure that there is a NUL character at the end, in any encoding */
127  pBytes[dwSize + 0] = '\0';
128  pBytes[dwSize + 1] = '\0';
129 
130  /* Look for Byte Order Marks */
131  if ((dwSize >= 2) && (pBytes[0] == 0xFF) && (pBytes[1] == 0xFE))
132  {
133  encFile = ENCODING_UTF16LE;
134  dwPos += 2;
135  }
136  else if ((dwSize >= 2) && (pBytes[0] == 0xFE) && (pBytes[1] == 0xFF))
137  {
138  encFile = ENCODING_UTF16BE;
139  dwPos += 2;
140  }
141  else if ((dwSize >= 3) && (pBytes[0] == 0xEF) && (pBytes[1] == 0xBB) && (pBytes[2] == 0xBF))
142  {
143  encFile = ENCODING_UTF8BOM;
144  dwPos += 3;
145  }
146  else
147  {
148  encFile = AnalyzeEncoding((const char *)pBytes, dwSize);
149  }
150 
151  switch(encFile)
152  {
153  case ENCODING_UTF16BE:
154  for (i = dwPos; i < dwSize-1; i += 2)
155  {
156  b = pBytes[i+0];
157  pBytes[i+0] = pBytes[i+1];
158  pBytes[i+1] = b;
159  }
160  /* fall through */
161 
162  case ENCODING_UTF16LE:
163  pszText = (LPWSTR) &pBytes[dwPos];
164  dwCharCount = (dwSize - dwPos) / sizeof(WCHAR);
165  break;
166 
167  case ENCODING_ANSI:
168  case ENCODING_UTF8:
169  case ENCODING_UTF8BOM:
170  if (encFile == ENCODING_UTF8 || encFile == ENCODING_UTF8BOM)
171  iCodePage = CP_UTF8;
172  else
173  iCodePage = CP_ACP;
174 
175  if ((dwSize - dwPos) > 0)
176  {
177  dwCharCount = MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], dwSize - dwPos, NULL, 0);
178  if (dwCharCount == 0)
179  goto done;
180  }
181  else
182  {
183  /* special case for files with no characters (other than BOMs) */
184  dwCharCount = 0;
185  }
186 
187  pszAllocText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (dwCharCount + 1) * sizeof(WCHAR));
188  if (!pszAllocText)
189  goto done;
190 
191  if ((dwSize - dwPos) > 0)
192  {
193  if (!MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], dwSize - dwPos, pszAllocText, dwCharCount))
194  goto done;
195  }
196 
197  pszAllocText[dwCharCount] = '\0';
198  pszText = pszAllocText;
199  break;
201  }
202 
203  dwPos = 0;
204  for (i = 0; i < dwCharCount; i++)
205  {
206  switch(pszText[i])
207  {
208  case '\r':
209  if ((i < dwCharCount-1) && (pszText[i+1] == '\n'))
210  {
211  i++;
212  adwEolnCount[EOLN_CRLF]++;
213  break;
214  }
215  /* fall through */
216 
217  case '\n':
218  if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos))
219  return FALSE;
220  if (!Append(ppszText, pdwTextLen, szCrlf, ARRAY_SIZE(szCrlf)))
221  return FALSE;
222  dwPos = i + 1;
223 
224  if (pszText[i] == '\r')
225  adwEolnCount[EOLN_CR]++;
226  else
227  adwEolnCount[EOLN_LF]++;
228  break;
229 
230  case '\0':
231  pszText[i] = ' ';
232  break;
233  }
234  }
235 
236  if (!*ppszText && (pszText == pszAllocText))
237  {
238  /* special case; don't need to reallocate */
239  *ppszText = pszAllocText;
240  *pdwTextLen = dwCharCount;
241  pszAllocText = NULL;
242  }
243  else
244  {
245  /* append last remaining text */
246  if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos + 1))
247  return FALSE;
248  }
249 
250  /* chose which eoln to use */
251  *piEoln = EOLN_CRLF;
252  if (adwEolnCount[EOLN_LF] > adwEolnCount[*piEoln])
253  *piEoln = EOLN_LF;
254  if (adwEolnCount[EOLN_CR] > adwEolnCount[*piEoln])
255  *piEoln = EOLN_CR;
256  *pencFile = encFile;
257 
258  bSuccess = TRUE;
259 
260 done:
261  if (pBytes)
262  HeapFree(GetProcessHeap(), 0, pBytes);
263  if (pszAllocText)
264  HeapFree(GetProcessHeap(), 0, pszAllocText);
265 
266  if (!bSuccess && *ppszText)
267  {
268  HeapFree(GetProcessHeap(), 0, *ppszText);
269  *ppszText = NULL;
270  *pdwTextLen = 0;
271  }
272  return bSuccess;
273 }
#define DEFAULT_UNREACHABLE
#define EOLN_CR
Definition: main.h:52
#define TRUE
Definition: types.h:120
#define CP_ACP
Definition: compat.h:109
static BOOL Append(LPWSTR *ppszText, DWORD *pdwTextLen, LPCWSTR pszAppendText, DWORD dwAppendLen)
Definition: text.c:26
#define INVALID_FILE_SIZE
Definition: winbase.h:545
static BOOLEAN bSuccess
Definition: drive.cpp:430
unsigned char * LPBYTE
Definition: typedefs.h:53
#define CP_UTF8
Definition: nls.h:20
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
const char * LPCSTR
Definition: xmlstorage.h:183
#define b
Definition: ke_i.h:79
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EOLN_CRLF
Definition: main.h:50
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
#define EOLN_LF
Definition: main.h:51
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned char BYTE
Definition: xxhash.c:193
ENCODING
Definition: more.c:491
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define ARRAY_SIZE(a)
Definition: main.h:24
#define ReadFile(a, b, c, d, e)
Definition: compat.h:601
#define NULL
Definition: types.h:112
#define MultiByteToWideChar
Definition: compat.h:110
ENCODING AnalyzeEncoding(const char *pBytes, DWORD dwSize)
Definition: text.c:64
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define HeapFree(x, y, z)
Definition: compat.h:594
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56

Referenced by DoOpenFile().

◆ SetFileName()

VOID SetFileName ( LPCTSTR  szFileName)

Definition at line 48 of file main.c.

49 {
51  Globals.szFileTitle[0] = 0;
53 
54  if (szFileName && szFileName[0])
55  SHAddToRecentDocs(SHARD_PATHW, szFileName);
56 }
NOTEPAD_GLOBALS Globals
Definition: main.c:31
TCHAR szFileName[MAX_PATH]
Definition: main.h:70
void WINAPI SHAddToRecentDocs(UINT uFlags, LPCVOID pv)
Definition: shellord.c:809
#define GetFileTitle
Definition: commdlg.h:664
#define StringCchCopy
Definition: strsafe.h:139
#define ARRAY_SIZE(a)
Definition: main.h:24
#define SHARD_PATHW
Definition: shlobj.h:1103
TCHAR szFileTitle[MAX_PATH]
Definition: main.h:71

◆ WriteText()

BOOL WriteText ( HANDLE  hFile,
LPCWSTR  pszText,
DWORD  dwTextLen,
ENCODING  encFile,
int  iEoln 
)

Definition at line 370 of file text.c.

371 {
372  WCHAR wcBom;
373  LPCWSTR pszLF = L"\n";
374  DWORD dwPos, dwNext;
375 
376  /* Write the proper byte order marks if not ANSI or UTF-8 without BOM */
377  if (encFile != ENCODING_ANSI && encFile != ENCODING_UTF8)
378  {
379  wcBom = 0xFEFF;
380  if (!WriteEncodedText(hFile, &wcBom, 1, encFile))
381  return FALSE;
382  }
383 
384  dwPos = 0;
385 
386  /* pszText eoln are always \r\n */
387 
388  do
389  {
390  /* Find the next eoln */
391  dwNext = dwPos;
392  while(dwNext < dwTextLen)
393  {
394  if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n')
395  break;
396  dwNext++;
397  }
398 
399  if (dwNext != dwTextLen)
400  {
401  switch (iEoln)
402  {
403  case EOLN_LF:
404  /* Write text (without eoln) */
405  if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
406  return FALSE;
407  /* Write eoln */
408  if (!WriteEncodedText(hFile, pszLF, 1, encFile))
409  return FALSE;
410  break;
411  case EOLN_CR:
412  /* Write text (including \r as eoln) */
413  if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 1, encFile))
414  return FALSE;
415  break;
416  case EOLN_CRLF:
417  /* Write text (including \r\n as eoln) */
418  if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 2, encFile))
419  return FALSE;
420  break;
421  default:
422  return FALSE;
423  }
424  }
425  else
426  {
427  /* Write text (without eoln, since this is the end of the file) */
428  if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
429  return FALSE;
430  }
431 
432  /* Skip \r\n */
433  dwPos = dwNext + 2;
434  }
435  while (dwPos < dwTextLen);
436 
437  return TRUE;
438 }
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define EOLN_CR
Definition: main.h:52
#define TRUE
Definition: types.h:120
static BOOL WriteEncodedText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile)
Definition: text.c:275
#define L(x)
Definition: ntvdm.h:50
#define FALSE
Definition: types.h:117
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EOLN_CRLF
Definition: main.h:50
#define EOLN_LF
Definition: main.h:51
_In_ HANDLE hFile
Definition: mswsock.h:90

Referenced by DoSaveFile().

Variable Documentation

◆ Globals

NOTEPAD_GLOBALS Globals

Definition at line 13 of file clipbrd.c.