xref: /reactos/dll/cpl/intl/misc.c (revision c2c66aff)
1 #include "intl.h"
2 
3 /* Insert the space  */
4 PWSTR
InsSpacePos(PCWSTR szInsStr,const int nPos)5 InsSpacePos(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 ';' */
38 PWSTR
InsSpacesFmt(PCWSTR szSourceStr,PCWSTR szFmtStr)39 InsSpacesFmt(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 */
117 PWSTR
ReplaceSubStr(PCWSTR szSourceStr,PCWSTR szStrToReplace,PCWSTR szTempl)118 ReplaceSubStr(PCWSTR szSourceStr,
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 
175 VOID
GetSelectedComboBoxText(HWND hwndDlg,INT nIdDlgItem,PWSTR Buffer,UINT uSize)176 GetSelectedComboBoxText(
177     HWND hwndDlg,
178     INT nIdDlgItem,
179     PWSTR Buffer,
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 
222 VOID
GetSelectedComboBoxIndex(HWND hwndDlg,INT nIdDlgItem,PINT pValue)223 GetSelectedComboBoxIndex(
224     HWND hwndDlg,
225     INT nIdDlgItem,
226     PINT pValue)
227 {
228     *pValue = SendDlgItemMessageW(hwndDlg, nIdDlgItem, CB_GETCURSEL, 0, 0);
229 }
230 
231 /* EOF */
232