ReactOS  0.4.14-dev-593-g1793dcc
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_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 51 of file main.h.

◆ EOLN_CRLF

#define EOLN_CRLF   0

Definition at line 49 of file main.h.

◆ EOLN_LF

#define EOLN_LF   1

Definition at line 50 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_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
44 } 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 138 of file main.c.

139 {
140  int iTextLength, iTargetLength;
141  size_t iAdjustment = 0;
142  LPTSTR pszText = NULL;
143  DWORD dwPosition, dwBegin, dwEnd;
144  BOOL bMatches = FALSE;
145  TCHAR szResource[128], szText[128];
146  BOOL bSuccess;
147 
148  iTargetLength = (int) _tcslen(pFindReplace->lpstrFindWhat);
149 
150  /* Retrieve the window text */
151  iTextLength = GetWindowTextLength(Globals.hEdit);
152  if (iTextLength > 0)
153  {
154  pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
155  if (!pszText)
156  return FALSE;
157 
158  GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
159  }
160 
161  SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwBegin, (LPARAM) &dwEnd);
162  if (bReplace && ((dwEnd - dwBegin) == (DWORD) iTargetLength))
163  {
164  if (NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwBegin))
165  {
166  SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
167  iAdjustment = _tcslen(pFindReplace->lpstrReplaceWith) - (dwEnd - dwBegin);
168  }
169  }
170 
171  if (pFindReplace->Flags & FR_DOWN)
172  {
173  /* Find Down */
174  dwPosition = dwEnd;
175  while(dwPosition < (DWORD) iTextLength)
176  {
177  bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
178  if (bMatches)
179  break;
180  dwPosition++;
181  }
182  }
183  else
184  {
185  /* Find Up */
186  dwPosition = dwBegin;
187  while(dwPosition > 0)
188  {
189  dwPosition--;
190  bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
191  if (bMatches)
192  break;
193  }
194  }
195 
196  if (bMatches)
197  {
198  /* Found target */
199  if (dwPosition > dwBegin)
200  dwPosition += (DWORD) iAdjustment;
201  SendMessage(Globals.hEdit, EM_SETSEL, dwPosition, dwPosition + iTargetLength);
203  bSuccess = TRUE;
204  }
205  else
206  {
207  /* Can't find target */
208  if (bShowAlert)
209  {
210  LoadString(Globals.hInstance, STRING_CANNOTFIND, szResource, ARRAY_SIZE(szResource));
211  _sntprintf(szText, ARRAY_SIZE(szText), szResource, pFindReplace->lpstrFindWhat);
212  LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
214  }
215  bSuccess = FALSE;
216  }
217 
218  if (pszText)
219  HeapFree(GetProcessHeap(), 0, pszText);
220  return bSuccess;
221 }
HWND hFindReplaceDlg
Definition: main.h:57
#define TRUE
Definition: types.h:120
NOTEPAD_GLOBALS Globals
Definition: main.c:29
UINT_PTR WPARAM
Definition: windef.h:207
#define DWORD
Definition: nt_native.h:44
static BOOLEAN bSuccess
Definition: drive.cpp:419
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define GetWindowText
Definition: winuser.h:5773
HINSTANCE hInstance
Definition: main.h:55
#define EM_GETSEL
Definition: winuser.h:1979
#define STRING_NOTEPAD
Definition: notepad_res.h:74
static const WCHAR szText[]
Definition: dialog.c:139
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL NOTEPAD_FindTextAt(FINDREPLACE *pFindReplace, LPCTSTR pszText, int iTextLength, DWORD dwPosition)
Definition: main.c:104
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
char TCHAR
Definition: xmlstorage.h:189
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define EM_SETSEL
Definition: winuser.h:2000
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _sntprintf
Definition: xmlstorage.h:201
#define MessageBox
Definition: winuser.h:5797
#define ARRAY_SIZE(a)
Definition: main.h:24
#define MB_OK
Definition: winuser.h:784
#define FR_DOWN
Definition: commdlg.h:127
#define STRING_CANNOTFIND
Definition: notepad_res.h:88
#define SendMessage
Definition: winuser.h:5818
#define GetWindowTextLength
Definition: winuser.h:5774
HWND hEdit
Definition: main.h:58
#define HeapFree(x, y, z)
Definition: compat.h:402
#define LoadString
Definition: winuser.h:5794
#define EM_REPLACESEL
Definition: winuser.h:1988
#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 = ANSI_CHARSET;
187  Globals.lfFont.lfClipPrecision = CLIP_STROKE_PRECIS;
188  Globals.lfFont.lfEscapement = 0;
189  _tcscpy(Globals.lfFont.lfFaceName, _T("Lucida Console"));
190  Globals.lfFont.lfItalic = FALSE;
191  Globals.lfFont.lfOrientation = 0;
192  Globals.lfFont.lfOutPrecision = OUT_STRING_PRECIS;
193  Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
194  Globals.lfFont.lfQuality = PROOF_QUALITY;
195  Globals.lfFont.lfStrikeOut = FALSE;
196  Globals.lfFont.lfUnderline = FALSE;
197  Globals.lfFont.lfWeight = FW_NORMAL;
198  Globals.lfFont.lfHeight = HeightFromPointSize(100);
199  }
200 
201  hFont = CreateFontIndirect(&Globals.lfFont);
203  if (hFont)
204  {
205  if (Globals.hFont)
206  DeleteObject(Globals.hFont);
207  Globals.hFont = hFont;
208  }
209 }
static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
Definition: settings.c:83
#define TRUE
Definition: types.h:120
int base_length[LENGTH_CODES]
Definition: trees.c:107
#define ERROR_SUCCESS
Definition: deptool.c:10
#define HKEY_CURRENT_USER
Definition: winreg.h:11
GLint dy
Definition: linetemp.h:97
#define INT
Definition: polytest.cpp:20
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
#define ZeroMemory
Definition: winbase.h:1642
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
UINT_PTR WPARAM
Definition: windef.h:207
#define PROOF_QUALITY
Definition: wingdi.h:437
int32_t INT
Definition: typedefs.h:56
HFONT hFont
Definition: main.c:53
#define STRING_PAGESETUP_HEADERVALUE
Definition: notepad_res.h:67
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
#define SM_CXSCREEN
Definition: winuser.h:949
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
#define FIXED_PITCH
Definition: wingdi.h:443
#define OUT_STRING_PRECIS
Definition: wingdi.h:415
smooth NULL
Definition: ftsmooth.c: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:950
#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 STRING_PAGESETUP_FOOTERVALUE
Definition: notepad_res.h:68
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)
#define ARRAY_SIZE(a)
Definition: main.h:24
#define FF_MODERN
Definition: wingdi.h:448
#define CreateFontIndirect
Definition: wingdi.h:4443
GLint dx
Definition: linetemp.h:97
#define WM_SETFONT
Definition: winuser.h:1632
#define FW_NORMAL
Definition: wingdi.h:372
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define SendMessage
Definition: winuser.h:5818
static LONG HeightFromPointSize(DWORD dwPointSize)
Definition: settings.c:30
#define ANSI_CHARSET
Definition: wingdi.h:382
HINSTANCE hInstance
Definition: precomp.h:43
#define RegOpenKey
Definition: winreg.h:519
#define CLIP_STROKE_PRECIS
Definition: wingdi.h:427
#define LoadString
Definition: winuser.h:5794

Referenced by _tWinMain().

◆ NOTEPAD_SaveSettingsToRegistry()

void NOTEPAD_SaveSettingsToRegistry ( void  )

Definition at line 227 of file settings.c.

228 {
229  HKEY hKey;
230  DWORD dwDisposition;
231 
232  GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
233 
235  0, NULL, 0, KEY_SET_VALUE, NULL,
236  &hKey, &dwDisposition) == ERROR_SUCCESS)
237  {
238  SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
239  SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
240  SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
241  SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
242  SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
243  SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
244  SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
245  SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
246  SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
247  SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
248  SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
249  SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
250  SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
251  SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
252  SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
253  SaveString(hKey, _T("szHeader"), Globals.szHeader);
254  SaveString(hKey, _T("szTrailer"), Globals.szFooter);
255  SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
256  SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
257  SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
258  SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
259  SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
260  SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
261  SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
262  SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
263 
264  RegCloseKey(hKey);
265  }
266 }
#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
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
smooth NULL
Definition: ftsmooth.c:416
#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:211
static LPCTSTR s_szRegistryKey
Definition: settings.c:27
static DWORD PointSizeFromHeight(LONG lHeight)
Definition: settings.c:42
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
Definition: settings.c:216
#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_UTF8;
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  if (encFile == ENCODING_ANSI)
170  iCodePage = CP_ACP;
171  else if (encFile == ENCODING_UTF8)
172  iCodePage = CP_UTF8;
173 
174  if ((dwSize - dwPos) > 0)
175  {
176  dwCharCount = MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], dwSize - dwPos, NULL, 0);
177  if (dwCharCount == 0)
178  goto done;
179  }
180  else
181  {
182  /* special case for files with no characters (other than BOMs) */
183  dwCharCount = 0;
184  }
185 
186  pszAllocText = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (dwCharCount + 1) * sizeof(WCHAR));
187  if (!pszAllocText)
188  goto done;
189 
190  if ((dwSize - dwPos) > 0)
191  {
192  if (!MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], dwSize - dwPos, pszAllocText, dwCharCount))
193  goto done;
194  }
195 
196  pszAllocText[dwCharCount] = '\0';
197  pszText = pszAllocText;
198  break;
200  }
201 
202  dwPos = 0;
203  for (i = 0; i < dwCharCount; i++)
204  {
205  switch(pszText[i])
206  {
207  case '\r':
208  if ((i < dwCharCount-1) && (pszText[i+1] == '\n'))
209  {
210  i++;
211  adwEolnCount[EOLN_CRLF]++;
212  break;
213  }
214  /* fall through */
215 
216  case '\n':
217  if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos))
218  return FALSE;
219  if (!Append(ppszText, pdwTextLen, szCrlf, ARRAY_SIZE(szCrlf)))
220  return FALSE;
221  dwPos = i + 1;
222 
223  if (pszText[i] == '\r')
224  adwEolnCount[EOLN_CR]++;
225  else
226  adwEolnCount[EOLN_LF]++;
227  break;
228 
229  case '\0':
230  pszText[i] = ' ';
231  break;
232  }
233  }
234 
235  if (!*ppszText && (pszText == pszAllocText))
236  {
237  /* special case; don't need to reallocate */
238  *ppszText = pszAllocText;
239  *pdwTextLen = dwCharCount;
240  pszAllocText = NULL;
241  }
242  else
243  {
244  /* append last remaining text */
245  if (!Append(ppszText, pdwTextLen, &pszText[dwPos], i - dwPos + 1))
246  return FALSE;
247  }
248 
249  /* chose which eoln to use */
250  *piEoln = EOLN_CRLF;
251  if (adwEolnCount[EOLN_LF] > adwEolnCount[*piEoln])
252  *piEoln = EOLN_LF;
253  if (adwEolnCount[EOLN_CR] > adwEolnCount[*piEoln])
254  *piEoln = EOLN_CR;
255  *pencFile = encFile;
256 
257  bSuccess = TRUE;
258 
259 done:
260  if (pBytes)
261  HeapFree(GetProcessHeap(), 0, pBytes);
262  if (pszAllocText)
263  HeapFree(GetProcessHeap(), 0, pszAllocText);
264 
265  if (!bSuccess && *ppszText)
266  {
267  HeapFree(GetProcessHeap(), 0, *ppszText);
268  *ppszText = NULL;
269  *pdwTextLen = 0;
270  }
271  return bSuccess;
272 }
#define DEFAULT_UNREACHABLE
#define TRUE
Definition: types.h:120
#define EOLN_CR
Definition: main.h:51
#define CP_ACP
Definition: compat.h:99
static BOOL Append(LPWSTR *ppszText, DWORD *pdwTextLen, LPCWSTR pszAppendText, DWORD dwAppendLen)
Definition: text.c:26
#define INVALID_FILE_SIZE
Definition: winbase.h:529
static BOOLEAN bSuccess
Definition: drive.cpp:419
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
unsigned char * LPBYTE
Definition: typedefs.h:52
#define CP_UTF8
Definition: nls.h:20
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
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:403
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:49
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
#define EOLN_LF
Definition: main.h:50
unsigned char BYTE
Definition: mem.h:68
_In_ HANDLE hFile
Definition: mswsock.h:90
ENCODING
Definition: more.c:155
#define ARRAY_SIZE(a)
Definition: main.h:24
#define MultiByteToWideChar
Definition: compat.h:100
ENCODING AnalyzeEncoding(const char *pBytes, DWORD dwSize)
Definition: text.c:64
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
#define HeapFree(x, y, z)
Definition: compat.h:402
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by DoOpenFile().

◆ SetFileName()

VOID SetFileName ( LPCTSTR  szFileName)

Definition at line 46 of file main.c.

47 {
49  Globals.szFileTitle[0] = 0;
51 }
NOTEPAD_GLOBALS Globals
Definition: main.c:29
TCHAR szFileName[MAX_PATH]
Definition: main.h:69
#define GetFileTitle
Definition: commdlg.h:664
#define StringCchCopy
Definition: strsafe.h:139
#define ARRAY_SIZE(a)
Definition: main.h:24
TCHAR szFileTitle[MAX_PATH]
Definition: main.h:70

◆ WriteText()

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

Definition at line 368 of file text.c.

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

Referenced by DoSaveFile().

Variable Documentation

◆ Globals

NOTEPAD_GLOBALS Globals

Definition at line 13 of file clipbrd.c.