~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to winpr/libwinpr/crt/string.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.2.5)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20141111122050-7z628f4ab38qxad5
Tags: upstream-1.1.0~git20140921.1.440916e+dfsg1
ImportĀ upstreamĀ versionĀ 1.1.0~git20140921.1.440916e+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * WinPR: Windows Portable Runtime
 
3
 * String Manipulation (CRT)
 
4
 *
 
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 
6
 *
 
7
 * Licensed under the Apache License, Version 2.0 (the "License");
 
8
 * you may not use this file except in compliance with the License.
 
9
 * You may obtain a copy of the License at
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 * Unless required by applicable law or agreed to in writing, software
 
14
 * distributed under the License is distributed on an "AS IS" BASIS,
 
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
 * See the License for the specific language governing permissions and
 
17
 * limitations under the License.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <errno.h>
 
25
#include <wctype.h>
 
26
 
 
27
#include <winpr/crt.h>
 
28
 
 
29
/* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */
 
30
 
 
31
#ifndef _WIN32
 
32
 
 
33
char* _strdup(const char* strSource)
 
34
{
 
35
        char* strDestination;
 
36
 
 
37
        if (strSource == NULL)
 
38
                return NULL;
 
39
 
 
40
        strDestination = strdup(strSource);
 
41
 
 
42
        if (strDestination == NULL)
 
43
                perror("strdup");
 
44
 
 
45
        return strDestination;
 
46
}
 
47
 
 
48
WCHAR* _wcsdup(const WCHAR* strSource)
 
49
{
 
50
        WCHAR* strDestination;
 
51
 
 
52
        if (strSource == NULL)
 
53
                return NULL;
 
54
 
 
55
#if sun
 
56
        strDestination = wsdup(strSource);
 
57
#elif defined(__APPLE__) && defined(__MACH__) || defined(ANDROID)
 
58
        strDestination = malloc(wcslen((wchar_t*)strSource));
 
59
 
 
60
        if (strDestination != NULL)
 
61
                wcscpy((wchar_t*)strDestination, (const wchar_t*)strSource);
 
62
#else
 
63
        strDestination = (WCHAR*) wcsdup((wchar_t*) strSource);
 
64
#endif
 
65
 
 
66
        if (strDestination == NULL)
 
67
                perror("wcsdup");
 
68
 
 
69
        return strDestination;
 
70
}
 
71
 
 
72
int _stricmp(const char* string1, const char* string2)
 
73
{
 
74
        return strcasecmp(string1, string2);
 
75
}
 
76
 
 
77
int _strnicmp(const char* string1, const char* string2, size_t count)
 
78
{
 
79
        return strncasecmp(string1, string2, count);
 
80
}
 
81
 
 
82
/* _wcscmp -> wcscmp */
 
83
 
 
84
int _wcscmp(const WCHAR* string1, const WCHAR* string2)
 
85
{
 
86
        while (*string1 && (*string1 == *string2))
 
87
        {
 
88
                string1++;
 
89
                string2++;
 
90
        }
 
91
 
 
92
        return *string1 - *string2;
 
93
}
 
94
 
 
95
/* _wcslen -> wcslen */
 
96
 
 
97
size_t _wcslen(const WCHAR* str)
 
98
{
 
99
        WCHAR* p = (WCHAR*) str;
 
100
 
 
101
        if (!p)
 
102
                return 0;
 
103
 
 
104
        while (*p)
 
105
                p++;
 
106
 
 
107
        return (p - str);
 
108
}
 
109
 
 
110
/* _wcschr -> wcschr */
 
111
 
 
112
WCHAR* _wcschr(const WCHAR* str, WCHAR c)
 
113
{
 
114
        WCHAR* p = (WCHAR*) str;
 
115
 
 
116
        while (*p && (*p != c))
 
117
                p++;
 
118
 
 
119
        return ((*p == c) ? p : NULL);
 
120
}
 
121
 
 
122
char* strtok_s(char* strToken, const char* strDelimit, char** context)
 
123
{
 
124
        return strtok_r(strToken, strDelimit, context);
 
125
}
 
126
 
 
127
WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)
 
128
{
 
129
        WCHAR* nextToken;
 
130
 
 
131
        if (!strToken)
 
132
                strToken = *context;
 
133
 
 
134
        while (*strToken && _wcschr(strDelimit, *strToken))
 
135
                strToken++;
 
136
 
 
137
        if (!*strToken)
 
138
                return NULL;
 
139
 
 
140
        nextToken = strToken++;
 
141
 
 
142
        while (*strToken && !(_wcschr(strDelimit, *strToken)))
 
143
                strToken++;
 
144
 
 
145
        if (*strToken)
 
146
                *strToken++ = 0;
 
147
 
 
148
        *context = strToken;
 
149
 
 
150
        return nextToken;
 
151
}
 
152
 
 
153
/* Windows API Sets - api-ms-win-core-string-l2-1-0.dll
 
154
 * http://msdn.microsoft.com/en-us/library/hh802935/
 
155
 */
 
156
 
 
157
LPSTR CharUpperA(LPSTR lpsz)
 
158
{
 
159
        int i;
 
160
        int length;
 
161
 
 
162
        length = strlen(lpsz);
 
163
 
 
164
        if (length < 1)
 
165
                return (LPSTR) NULL;
 
166
 
 
167
        if (length == 1)
 
168
        {
 
169
                LPSTR pc = NULL;
 
170
                char c = *lpsz;
 
171
 
 
172
                if ((c >= 'a') && (c <= 'z'))
 
173
                        c = c - 32;
 
174
 
 
175
                *pc = c;
 
176
 
 
177
                return pc;
 
178
        }
 
179
 
 
180
        for (i = 0; i < length; i++)
 
181
        {
 
182
                if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
 
183
                        lpsz[i] = lpsz[i] - 32;
 
184
        }
 
185
 
 
186
        return lpsz;
 
187
}
 
188
 
 
189
LPWSTR CharUpperW(LPWSTR lpsz)
 
190
{
 
191
        fprintf(stderr, "CharUpperW unimplemented!\n");
 
192
 
 
193
        return (LPWSTR) NULL;
 
194
}
 
195
 
 
196
DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
 
197
{
 
198
        int i;
 
199
 
 
200
        if (cchLength < 1)
 
201
                return 0;
 
202
 
 
203
        for (i = 0; i < cchLength; i++)
 
204
        {
 
205
                if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
 
206
                        lpsz[i] = lpsz[i] - 32;
 
207
        }
 
208
 
 
209
        return cchLength;
 
210
}
 
211
 
 
212
DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
 
213
{
 
214
        DWORD i;
 
215
        unsigned char* p;
 
216
        unsigned int wc, uwc;
 
217
 
 
218
        p = (unsigned char*) lpsz;
 
219
 
 
220
        for (i = 0; i < cchLength; i++)
 
221
        {
 
222
                wc = (unsigned int) (*p);
 
223
                wc += (unsigned int) (*(p + 1)) << 8;
 
224
 
 
225
                uwc = towupper(wc);
 
226
 
 
227
                if (uwc != wc)
 
228
                {
 
229
                        *p = uwc & 0xFF;
 
230
                        *(p + 1) = (uwc >> 8) & 0xFF;
 
231
                }
 
232
 
 
233
                p += 2;
 
234
        }
 
235
 
 
236
        return cchLength;
 
237
}
 
238
 
 
239
LPSTR CharLowerA(LPSTR lpsz)
 
240
{
 
241
        int i;
 
242
        int length;
 
243
 
 
244
        length = strlen(lpsz);
 
245
 
 
246
        if (length < 1)
 
247
                return (LPSTR) NULL;
 
248
 
 
249
        if (length == 1)
 
250
        {
 
251
                LPSTR pc = NULL;
 
252
                char c = *lpsz;
 
253
 
 
254
                if ((c >= 'A') && (c <= 'Z'))
 
255
                        c = c + 32;
 
256
 
 
257
                *pc = c;
 
258
 
 
259
                return pc;
 
260
        }
 
261
 
 
262
        for (i = 0; i < length; i++)
 
263
        {
 
264
                if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
 
265
                        lpsz[i] = lpsz[i] + 32;
 
266
        }
 
267
 
 
268
        return lpsz;
 
269
}
 
270
 
 
271
LPWSTR CharLowerW(LPWSTR lpsz)
 
272
{
 
273
        fprintf(stderr, "CharLowerW unimplemented!\n");
 
274
 
 
275
        return (LPWSTR) NULL;
 
276
}
 
277
 
 
278
DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
 
279
{
 
280
        int i;
 
281
 
 
282
        if (cchLength < 1)
 
283
                return 0;
 
284
 
 
285
        for (i = 0; i < cchLength; i++)
 
286
        {
 
287
                if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
 
288
                        lpsz[i] = lpsz[i] + 32;
 
289
        }
 
290
 
 
291
        return cchLength;
 
292
}
 
293
 
 
294
DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
 
295
{
 
296
        DWORD i;
 
297
        unsigned char* p;
 
298
        unsigned int wc, uwc;
 
299
 
 
300
        p = (unsigned char*) lpsz;
 
301
 
 
302
        for (i = 0; i < cchLength; i++)
 
303
        {
 
304
                wc = (unsigned int) (*p);
 
305
                wc += (unsigned int) (*(p + 1)) << 8;
 
306
 
 
307
                uwc = towlower(wc);
 
308
 
 
309
                if (uwc != wc)
 
310
                {
 
311
                        *p = uwc & 0xFF;
 
312
                        *(p + 1) = (uwc >> 8) & 0xFF;
 
313
                }
 
314
 
 
315
                p += 2;
 
316
        }
 
317
 
 
318
        return cchLength;
 
319
}
 
320
 
 
321
BOOL IsCharAlphaA(CHAR ch)
 
322
{
 
323
        if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))
 
324
                return 1;
 
325
        else
 
326
                return 0;
 
327
}
 
328
 
 
329
BOOL IsCharAlphaW(WCHAR ch)
 
330
{
 
331
        fprintf(stderr, "IsCharAlphaW unimplemented!\n");
 
332
        return 0;
 
333
}
 
334
 
 
335
BOOL IsCharAlphaNumericA(CHAR ch)
 
336
{
 
337
        if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
 
338
                        ((ch >= '0') && (ch <= '9')))
 
339
                return 1;
 
340
        else
 
341
                return 0;
 
342
}
 
343
 
 
344
BOOL IsCharAlphaNumericW(WCHAR ch)
 
345
{
 
346
        fprintf(stderr, "IsCharAlphaNumericW unimplemented!\n");
 
347
        return 0;
 
348
}
 
349
 
 
350
BOOL IsCharUpperA(CHAR ch)
 
351
{
 
352
        if ((ch >= 'A') && (ch <= 'Z'))
 
353
                return 1;
 
354
        else
 
355
                return 0;
 
356
}
 
357
 
 
358
BOOL IsCharUpperW(WCHAR ch)
 
359
{
 
360
        fprintf(stderr, "IsCharUpperW unimplemented!\n");
 
361
        return 0;
 
362
}
 
363
 
 
364
BOOL IsCharLowerA(CHAR ch)
 
365
{
 
366
        if ((ch >= 'a') && (ch <= 'z'))
 
367
                return 1;
 
368
        else
 
369
                return 0;
 
370
}
 
371
 
 
372
BOOL IsCharLowerW(WCHAR ch)
 
373
{
 
374
        fprintf(stderr, "IsCharLowerW unimplemented!\n");
 
375
        return 0;
 
376
}
 
377
 
 
378
int lstrlenA(LPCSTR lpString)
 
379
{
 
380
        return strlen(lpString);
 
381
}
 
382
 
 
383
int lstrlenW(LPCWSTR lpString)
 
384
{
 
385
        LPWSTR p;
 
386
 
 
387
        if (!lpString)
 
388
                return 0;
 
389
 
 
390
        p = (LPWSTR) lpString;
 
391
 
 
392
        while (*p)
 
393
                p++;
 
394
 
 
395
        return p - lpString;
 
396
}
 
397
 
 
398
int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2)
 
399
{
 
400
        return strcmp(lpString1, lpString2);
 
401
}
 
402
 
 
403
int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2)
 
404
{
 
405
        while (*lpString1 && (*lpString1 == *lpString2))
 
406
        {
 
407
                lpString1++;
 
408
                lpString2++;
 
409
        }
 
410
 
 
411
        return *lpString1 - *lpString2;
 
412
}
 
413
 
 
414
#endif