451536c08e9e4ca09b018e6a1682aca0ad5e8e83
[reactos.git] / reactos / dll / cpl / intl / misc.c
1 #include "intl.h"
2
3 /* Insert the space */
4 PWSTR
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)malloc(MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
13
14 wcscpy(pszDestStr, szInsStr);
15
16 nStrSize = wcslen(szInsStr);
17
18 for (nStrCnt = 0; nStrCnt < nStrSize; nStrCnt++)
19 {
20 if (nStrCnt == nStrSize - nPos)
21 {
22 pszDestStr[nDestStrCnt] = L' ';
23 nDestStrCnt++;
24 }
25
26 pszDestStr[nDestStrCnt] = szInsStr[nStrCnt];
27 nDestStrCnt++;
28 }
29
30 pszDestStr[nDestStrCnt] = L'\0';
31
32 return pszDestStr;
33 }
34
35 /* Insert the spaces by format string separated by ';' */
36 PWSTR
37 InsSpacesFmt(PCWSTR szSourceStr, PCWSTR szFmtStr)
38 {
39 PWSTR pszDestStr;
40 PWSTR pszTempStr;
41 WCHAR szFmtVal[255];
42 UINT nFmtCount = 0;
43 INT nValCount = 0;
44 INT nLastVal = 0;
45 INT nSpaceOffset = 0;
46 BOOL wasNul=FALSE;
47
48 pszDestStr = (PWSTR)malloc(255 * sizeof(WCHAR));
49
50 wcscpy(pszDestStr, szSourceStr);
51
52 /* If format is clean return source string */
53 if (!*szFmtStr)
54 return pszDestStr;
55
56 /* Search for all format values */
57 for (nFmtCount = 0; nFmtCount <= wcslen(szFmtStr); nFmtCount++)
58 {
59 if (szFmtStr[nFmtCount] == L';' || szFmtStr[nFmtCount] == L'\0')
60 {
61 if (_wtoi(szFmtVal) == 0 && !wasNul)
62 {
63 wasNul=TRUE;
64 break;
65 }
66
67 /* If was 0, repeat spaces */
68 if (wasNul)
69 {
70 nSpaceOffset += nLastVal;
71 }
72 else
73 {
74 nSpaceOffset += _wtoi(szFmtVal);
75 }
76
77 szFmtVal[nValCount] = L'\0';
78 nValCount=0;
79
80 /* Insert space to finded position plus all pos before */
81 pszTempStr = InsSpacePos(pszDestStr, nSpaceOffset);
82 wcscpy(pszDestStr,pszTempStr);
83 free(pszTempStr);
84
85 /* Num of spaces total increment */
86 if (!wasNul)
87 {
88 nSpaceOffset++;
89 nLastVal = _wtoi(szFmtVal);
90 }
91 }
92 else
93 {
94 szFmtVal[nValCount++] = szFmtStr[nFmtCount];
95 }
96 }
97
98 /* Create spaces for rest part of string */
99 if (wasNul && nLastVal != 0)
100 {
101 for (nFmtCount = nSpaceOffset + nLastVal; nFmtCount < wcslen(pszDestStr); nFmtCount += nLastVal + 1)
102 {
103 pszTempStr = InsSpacePos(pszDestStr, nFmtCount);
104 wcscpy(pszDestStr, pszTempStr);
105 free(pszTempStr);
106 }
107 }
108
109 return pszDestStr;
110 }
111
112 /* Replace given template in source string with string to replace and return received string */
113 PWSTR
114 ReplaceSubStr(PCWSTR szSourceStr,
115 PCWSTR szStrToReplace,
116 PCWSTR szTempl)
117 {
118 PWSTR szDestStr;
119 UINT nCharCnt;
120 UINT nSubStrCnt;
121 UINT nDestStrCnt;
122 UINT nFirstCharCnt;
123
124 szDestStr = (PWSTR)malloc(MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
125
126 nDestStrCnt = 0;
127 nFirstCharCnt = 0;
128
129 wcscpy(szDestStr, L"");
130
131 while (nFirstCharCnt < wcslen(szSourceStr))
132 {
133 if (szSourceStr[nFirstCharCnt] == szTempl[0])
134 {
135 nSubStrCnt = 0;
136 for (nCharCnt = nFirstCharCnt; nCharCnt < nFirstCharCnt + wcslen(szTempl); nCharCnt++)
137 {
138 if (szSourceStr[nCharCnt] == szTempl[nSubStrCnt])
139 {
140 nSubStrCnt++;
141 }
142 else
143 {
144 break;
145 }
146
147 if (wcslen(szTempl) == nSubStrCnt)
148 {
149 wcscat(szDestStr, szStrToReplace);
150 nDestStrCnt = wcslen(szDestStr);
151 nFirstCharCnt += wcslen(szTempl) - 1;
152 break;
153 }
154 }
155 }
156 else
157 {
158 szDestStr[nDestStrCnt++] = szSourceStr[nFirstCharCnt];
159 szDestStr[nDestStrCnt] = L'\0';
160 }
161
162 nFirstCharCnt++;
163 }
164
165 return szDestStr;
166 }
167
168 /* EOF */