1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2008-2011 |
---|
3 | // Created by Roman Ryltsov roman@alax.info |
---|
4 | // |
---|
5 | // $Id: MainDialog.h 43 2011-12-19 10:49:49Z roman $ |
---|
6 | |
---|
7 | #pragma once |
---|
8 | |
---|
9 | #include "rofiles.h" |
---|
10 | |
---|
11 | //////////////////////////////////////////////////////////// |
---|
12 | // CMainDialog |
---|
13 | |
---|
14 | class CMainDialog : |
---|
15 | public CAxDialogImpl<CMainDialog> |
---|
16 | { |
---|
17 | public: |
---|
18 | enum { IDD = IDD_MAIN }; |
---|
19 | |
---|
20 | BEGIN_MSG_MAP_EX(CMainDialog) |
---|
21 | CHAIN_MSG_MAP(CAxDialogImpl<CMainDialog>) |
---|
22 | MSG_WM_INITDIALOG(OnInitDialog) |
---|
23 | MSG_WM_DESTROY(OnDestroy) |
---|
24 | COMMAND_ID_HANDLER_EX(IDCANCEL, OnCommand) |
---|
25 | COMMAND_HANDLER_EX(IDC_UPDATE, BN_CLICKED, OnUpdateButtonClicked) |
---|
26 | REFLECT_NOTIFICATIONS() |
---|
27 | END_MSG_MAP() |
---|
28 | |
---|
29 | public: |
---|
30 | |
---|
31 | private: |
---|
32 | CFont m_TextFont; |
---|
33 | CButton m_UpdateButton; |
---|
34 | CRoEdit m_TextEdit; |
---|
35 | |
---|
36 | public: |
---|
37 | // CMainDialog |
---|
38 | CMainDialog() |
---|
39 | { |
---|
40 | } |
---|
41 | VOID UpdateText(CPath sInputFilePath, CPath sOutputFilePath) |
---|
42 | { |
---|
43 | m_UpdateButton.EnableWindow(FALSE); |
---|
44 | SetThreadLocale(MAKELCID(LANG_RUSSIAN, SORT_DEFAULT)); |
---|
45 | #pragma region Read Input File |
---|
46 | CString sInputText; |
---|
47 | { |
---|
48 | CAtlFile File; |
---|
49 | __C(File.Create(sInputFilePath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING)); |
---|
50 | ULONGLONG nFileSize; |
---|
51 | __C(File.GetSize(nFileSize)); |
---|
52 | CHeapPtr<BYTE> pnData; |
---|
53 | __D(pnData.Allocate((SIZE_T) nFileSize + 1), E_OUTOFMEMORY); |
---|
54 | __C(File.Read(pnData, (DWORD) nFileSize)); |
---|
55 | pnData[(SIZE_T) nFileSize] = 0; |
---|
56 | CStringA sInputTextA((LPCSTR) (const BYTE*) pnData); |
---|
57 | sInputText = CString(sInputTextA); |
---|
58 | } |
---|
59 | #pragma endregion |
---|
60 | sInputText.Replace(_T("\r\n"), _T("\n")); |
---|
61 | if(sInputText[sInputText.GetLength() - 1] != _T('\n')) |
---|
62 | sInputText.AppendChar(_T('\n')); |
---|
63 | CString sOutputText; |
---|
64 | static const INT g_nWrapWidth = 350; |
---|
65 | { |
---|
66 | CClientDC Dc(m_TextEdit); |
---|
67 | CGdiSelector FontSelector(Dc, m_TextFont); |
---|
68 | CString sBreakSeparators = _T(" -:,."); |
---|
69 | for(INT nPosition = 0; nPosition < sInputText.GetLength(); ) |
---|
70 | { |
---|
71 | const INT nSeparatorPosition = sInputText.Find(_T('\n'), nPosition); |
---|
72 | if(nSeparatorPosition < 0) |
---|
73 | break; |
---|
74 | CString sLineText = sInputText.Mid(nPosition, nSeparatorPosition - nPosition); |
---|
75 | #pragma region Fancify Line |
---|
76 | { |
---|
77 | INT nPosition = 0; |
---|
78 | while(nPosition < sLineText.GetLength() && sLineText[nPosition] == _T(' ')) |
---|
79 | nPosition++; |
---|
80 | if(nPosition + 1 < sLineText.GetLength() && sLineText[nPosition] == _T('-') && sLineText[nPosition] != _T(' ')) |
---|
81 | { |
---|
82 | sLineText.Delete(nPosition); |
---|
83 | sLineText.Insert(nPosition, _T(" ")); // En Dash, Space |
---|
84 | } |
---|
85 | } |
---|
86 | #pragma endregion |
---|
87 | for(; ; ) |
---|
88 | { |
---|
89 | if(sLineText.IsEmpty()) |
---|
90 | break; |
---|
91 | SIZE Extent; |
---|
92 | INT nCharacterCount = 0; |
---|
93 | __D(Dc.GetTextExtentExPoint(sLineText, sLineText.GetLength(), &Extent, g_nWrapWidth, &nCharacterCount), E_UNNAMED); |
---|
94 | const INT nLineTextLength = sLineText.GetLength(); |
---|
95 | if(nCharacterCount >= nLineTextLength) |
---|
96 | break; |
---|
97 | #pragma region Fancify Line Break |
---|
98 | for(INT nSpacePosition = 0; nSpacePosition < 30 && nCharacterCount - nSpacePosition > 0; nSpacePosition++) |
---|
99 | { |
---|
100 | if(sBreakSeparators.Find(sLineText[nCharacterCount - nSpacePosition]) >= 0) |
---|
101 | { |
---|
102 | nCharacterCount -= nSpacePosition; |
---|
103 | break; |
---|
104 | } |
---|
105 | } |
---|
106 | sOutputText.Append(sLineText.Mid(0, nCharacterCount)); |
---|
107 | sOutputText.Append(_T("\n")); |
---|
108 | sLineText.Delete(0, nCharacterCount); |
---|
109 | } |
---|
110 | sOutputText.Append(sLineText); |
---|
111 | sOutputText.Append(_T("\n")); |
---|
112 | nPosition = nSeparatorPosition + 1; |
---|
113 | } |
---|
114 | } |
---|
115 | if(_tcslen(sOutputFilePath) > 0) |
---|
116 | #pragma region Write Output File |
---|
117 | { |
---|
118 | CAtlFile File; |
---|
119 | __C(File.Create(sOutputFilePath, GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS)); |
---|
120 | #if TRUE |
---|
121 | CStringW sOutputTextW = CStringW(sOutputText); |
---|
122 | static const BYTE g_pnByteOrderMark[] = { 0xFF, 0xFE }; |
---|
123 | __C(File.Write(g_pnByteOrderMark, DIM(g_pnByteOrderMark))); |
---|
124 | __C(File.Write(sOutputTextW, sOutputTextW.GetLength() * sizeof (WCHAR))); |
---|
125 | #else |
---|
126 | CStringA sOutputTextA = CStringA(sOutputText); |
---|
127 | __C(File.Write(sOutputTextA, sOutputTextA.GetLength() * sizeof (CHAR))); |
---|
128 | #endif |
---|
129 | } |
---|
130 | #pragma endregion |
---|
131 | #pragma region Update UI |
---|
132 | sOutputText.Replace(_T("\n"), _T("\r\n")); |
---|
133 | m_TextEdit.SetValue(sOutputText); |
---|
134 | m_UpdateButton.EnableWindow(TRUE); |
---|
135 | #pragma endregion |
---|
136 | MessageBeep(MB_OK); |
---|
137 | } |
---|
138 | VOID UpdateText() |
---|
139 | { |
---|
140 | static LPCTSTR g_pszInputFilePath = _T("F:\\Torrents\\marinina_[tfile.ru]\\Marinina_Kamenskaya_2_Igra_na_chuzhom_pole.35634.txt\\Marinina_Kamenskaya_2_Igra_na_chuzhom_pole.35634.txt"); |
---|
141 | static LPCTSTR g_pszOutputDirectoryPath = _T("F:\\Temp\\62"); |
---|
142 | #if defined(_DEBUG) |
---|
143 | static LPCTSTR g_pszInputDirectoryPath = _T("F:\\Temp\\62\\Input"); |
---|
144 | CFindFiles FindFiles; |
---|
145 | for(BOOL bFound = FindFiles.FindFirstFile(g_pszInputDirectoryPath, _T("*.txt")); bFound; bFound = FindFiles.FindNextFile()) |
---|
146 | { |
---|
147 | CPath sInputFilePath, sOutputFilePath; |
---|
148 | sInputFilePath.Combine(g_pszInputDirectoryPath, FindFiles.GetFindData().cFileName); |
---|
149 | sOutputFilePath.Combine(g_pszOutputDirectoryPath, FindFileName(sInputFilePath)); |
---|
150 | UpdateText(sInputFilePath, sOutputFilePath); |
---|
151 | } |
---|
152 | return; |
---|
153 | #endif // defined(_DEBUG) |
---|
154 | CPath sOutputFilePath; |
---|
155 | sOutputFilePath.Combine(g_pszOutputDirectoryPath, FindFileName(g_pszInputFilePath)); |
---|
156 | UpdateText(g_pszInputFilePath, sOutputFilePath); |
---|
157 | } |
---|
158 | |
---|
159 | // Window message handelrs |
---|
160 | LRESULT OnInitDialog(HWND, LPARAM) |
---|
161 | { |
---|
162 | SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE); |
---|
163 | SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE); |
---|
164 | CLogFont LogFont; |
---|
165 | LogFont.SetMessageBoxFont(); |
---|
166 | _tcsncpy_s(LogFont.lfFaceName, DIM(LogFont.lfFaceName), _T("Arial"), _TRUNCATE); |
---|
167 | LogFont.lfCharSet = RUSSIAN_CHARSET; |
---|
168 | m_TextFont = LogFont.CreateFontIndirect(); |
---|
169 | m_UpdateButton = GetDlgItem(IDC_UPDATE); |
---|
170 | m_UpdateButton.EnableWindow(FALSE); |
---|
171 | m_TextEdit = GetDlgItem(IDC_STATUS); |
---|
172 | m_TextEdit.SetFont(m_TextFont); |
---|
173 | _W(CenterWindow()); |
---|
174 | #if _DEVELOPMENT |
---|
175 | // TODO: ... |
---|
176 | m_UpdateButton.EnableWindow(TRUE); |
---|
177 | m_UpdateButton.Click(); |
---|
178 | #endif // _DEVELOPMENT |
---|
179 | return TRUE; |
---|
180 | } |
---|
181 | LRESULT OnDestroy() throw() |
---|
182 | { |
---|
183 | return 0; |
---|
184 | } |
---|
185 | LRESULT OnCommand(UINT, INT nIdentifier, HWND) |
---|
186 | { |
---|
187 | _W(EndDialog(nIdentifier)); |
---|
188 | return 0; |
---|
189 | } |
---|
190 | LRESULT OnUpdateButtonClicked(UINT, INT, HWND) |
---|
191 | { |
---|
192 | UpdateText(); |
---|
193 | return 0; |
---|
194 | } |
---|
195 | }; |
---|