ReactOS 0.4.16-dev-197-g92996da
misc.c
Go to the documentation of this file.
1#include "intl.h"
2
3/* Insert the space */
5InsSpacePos(PCWSTR szInsStr, const int nPos)
6{
7 PWSTR pszDestStr;
8 INT nDestStrCnt = 0;
9 INT nStrCnt;
10 INT nStrSize;
11
12 pszDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
13 if (pszDestStr == NULL)
14 return NULL;
15
16 wcscpy(pszDestStr, szInsStr);
17
18 nStrSize = wcslen(szInsStr);
19
20 for (nStrCnt = 0; nStrCnt < nStrSize; nStrCnt++)
21 {
22 if (nStrCnt == nStrSize - nPos)
23 {
24 pszDestStr[nDestStrCnt] = L' ';
25 nDestStrCnt++;
26 }
27
28 pszDestStr[nDestStrCnt] = szInsStr[nStrCnt];
29 nDestStrCnt++;
30 }
31
32 pszDestStr[nDestStrCnt] = L'\0';
33
34 return pszDestStr;
35}
36
37/* Insert the spaces by format string separated by ';' */
39InsSpacesFmt(PCWSTR szSourceStr, PCWSTR szFmtStr)
40{
41 PWSTR pszDestStr;
42 PWSTR pszTempStr;
43 WCHAR szFmtVal[255];
44 UINT nFmtCount = 0;
45 INT nValCount = 0;
46 INT nLastVal = 0;
47 INT nSpaceOffset = 0;
48 BOOL wasNul=FALSE;
49
50 pszDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, 255 * sizeof(WCHAR));
51 if (pszDestStr == NULL)
52 return NULL;
53
54 wcscpy(pszDestStr, szSourceStr);
55
56 /* If format is clean return source string */
57 if (!*szFmtStr)
58 return pszDestStr;
59
60 /* Search for all format values */
61 for (nFmtCount = 0; nFmtCount <= wcslen(szFmtStr); nFmtCount++)
62 {
63 if (szFmtStr[nFmtCount] == L';' || szFmtStr[nFmtCount] == L'\0')
64 {
65 if (_wtoi(szFmtVal) == 0 && !wasNul)
66 {
67 wasNul=TRUE;
68 break;
69 }
70
71 /* If was 0, repeat spaces */
72 if (wasNul)
73 {
74 nSpaceOffset += nLastVal;
75 }
76 else
77 {
78 nSpaceOffset += _wtoi(szFmtVal);
79 }
80
81 szFmtVal[nValCount] = L'\0';
82 nValCount=0;
83
84 /* Insert space to finded position plus all pos before */
85 pszTempStr = InsSpacePos(pszDestStr, nSpaceOffset);
86 wcscpy(pszDestStr,pszTempStr);
87 HeapFree(GetProcessHeap(), 0, pszTempStr);
88
89 /* Num of spaces total increment */
90 if (!wasNul)
91 {
92 nSpaceOffset++;
93 nLastVal = _wtoi(szFmtVal);
94 }
95 }
96 else
97 {
98 szFmtVal[nValCount++] = szFmtStr[nFmtCount];
99 }
100 }
101
102 /* Create spaces for rest part of string */
103 if (wasNul && nLastVal != 0)
104 {
105 for (nFmtCount = nSpaceOffset + nLastVal; nFmtCount < wcslen(pszDestStr); nFmtCount += nLastVal + 1)
106 {
107 pszTempStr = InsSpacePos(pszDestStr, nFmtCount);
108 wcscpy(pszDestStr, pszTempStr);
109 HeapFree(GetProcessHeap(), 0, pszTempStr);
110 }
111 }
112
113 return pszDestStr;
114}
115
116/* Replace given template in source string with string to replace and return received string */
117PWSTR
119 PCWSTR szStrToReplace,
120 PCWSTR szTempl)
121{
122 PWSTR szDestStr;
123 UINT nCharCnt;
124 UINT nSubStrCnt;
125 UINT nDestStrCnt;
126 UINT nFirstCharCnt;
127
128 szDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
129 if (szDestStr == NULL)
130 return NULL;
131
132 nDestStrCnt = 0;
133 nFirstCharCnt = 0;
134
135 wcscpy(szDestStr, L"");
136
137 while (nFirstCharCnt < wcslen(szSourceStr))
138 {
139 if (szSourceStr[nFirstCharCnt] == szTempl[0])
140 {
141 nSubStrCnt = 0;
142 for (nCharCnt = nFirstCharCnt; nCharCnt < nFirstCharCnt + wcslen(szTempl); nCharCnt++)
143 {
144 if (szSourceStr[nCharCnt] == szTempl[nSubStrCnt])
145 {
146 nSubStrCnt++;
147 }
148 else
149 {
150 break;
151 }
152
153 if (wcslen(szTempl) == nSubStrCnt)
154 {
155 wcscat(szDestStr, szStrToReplace);
156 nDestStrCnt = wcslen(szDestStr);
157 nFirstCharCnt += wcslen(szTempl) - 1;
158 break;
159 }
160 }
161 }
162 else
163 {
164 szDestStr[nDestStrCnt++] = szSourceStr[nFirstCharCnt];
165 szDestStr[nDestStrCnt] = L'\0';
166 }
167
168 nFirstCharCnt++;
169 }
170
171 return szDestStr;
172}
173
174
175VOID
177 HWND hwndDlg,
178 INT nIdDlgItem,
180 UINT uSize)
181{
182 HWND hChildWnd;
183 PWSTR tmp;
184 INT nIndex;
185 UINT uReqSize;
186
187 /* Get handle to time format control */
188 hChildWnd = GetDlgItem(hwndDlg, nIdDlgItem);
189 if (hChildWnd == NULL)
190 return;
191
192 /* Get index to selected time format */
193 nIndex = SendMessageW(hChildWnd, CB_GETCURSEL, 0, 0);
194 if (nIndex == CB_ERR)
195 {
196 /* No selection? Get content of the edit control */
197 SendMessageW(hChildWnd, WM_GETTEXT, uSize, (LPARAM)Buffer);
198 }
199 else
200 {
201 /* Get requested size, including the null terminator;
202 * it shouldn't be required because the previous CB_LIMITTEXT,
203 * but it would be better to check it anyways */
204 uReqSize = SendMessageW(hChildWnd, CB_GETLBTEXTLEN, (WPARAM)nIndex, 0) + 1;
205
206 /* Allocate enough space to be more safe */
207 tmp = (PWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uReqSize * sizeof(WCHAR));
208 if (tmp != NULL)
209 {
210 /* Get selected time format text */
211 SendMessageW(hChildWnd, CB_GETLBTEXT, (WPARAM)nIndex, (LPARAM)tmp);
212
213 /* Finally, copy the result into the output */
214 wcsncpy(Buffer, tmp, uSize);
215
216 HeapFree(GetProcessHeap(), 0, tmp);
217 }
218 }
219}
220
221
222VOID
224 HWND hwndDlg,
225 INT nIdDlgItem,
226 PINT pValue)
227{
228 *pValue = SendDlgItemMessageW(hwndDlg, nIdDlgItem, CB_GETCURSEL, 0, 0);
229}
230
231/* EOF */
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
VOID GetSelectedComboBoxText(HWND hwndDlg, INT nIdDlgItem, PWSTR Buffer, UINT uSize)
Definition: misc.c:176
PWSTR ReplaceSubStr(PCWSTR szSourceStr, PCWSTR szStrToReplace, PCWSTR szTempl)
Definition: misc.c:118
PWSTR InsSpacePos(PCWSTR szInsStr, const int nPos)
Definition: misc.c:5
VOID GetSelectedComboBoxIndex(HWND hwndDlg, INT nIdDlgItem, PINT pValue)
Definition: misc.c:223
PWSTR InsSpacesFmt(PCWSTR szSourceStr, PCWSTR szFmtStr)
Definition: misc.c:39
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
unsigned int BOOL
Definition: ntddk_ex.h:94
PWCHAR pValue
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define MAX_SAMPLES_STR_SIZE
Definition: intl.h:24
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
int32_t INT
Definition: typedefs.h:58
LONG_PTR LPARAM
Definition: windef.h:208
UINT_PTR WPARAM
Definition: windef.h:207
int * PINT
Definition: windef.h:177
#define CB_GETLBTEXTLEN
Definition: winuser.h:1956
#define CB_GETLBTEXT
Definition: winuser.h:1955
#define CB_ERR
Definition: winuser.h:2438
#define WM_GETTEXT
Definition: winuser.h:1621
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define CB_GETCURSEL
Definition: winuser.h:1946
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
__wchar_t WCHAR
Definition: xmlstorage.h:180