string.c 7.35 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6 7 8 9
/*
 * String functions
 *
 * Copyright 1993 Yngvi Sigurjonsson
 * Copyright 1996 Alexandre Julliard
 */

#include <ctype.h>
#include <string.h>
10

11
#include "windef.h"
12
#include "winbase.h"
13
#include "wine/winbase16.h"
14
#include "wine/exception.h"
15
#include "wine/unicode.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
16
#include "winerror.h"
17
#include "winnls.h"
18
#include "debugtools.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
19

20
DEFAULT_DEBUG_CHANNEL(string);
21

22 23 24 25 26 27 28
/* filter for page-fault exceptions */
static WINE_EXCEPTION_FILTER(page_fault)
{
    if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
        return EXCEPTION_EXECUTE_HANDLER;
    return EXCEPTION_CONTINUE_SEARCH;
}
29

Alexandre Julliard's avatar
Alexandre Julliard committed
30

31 32 33 34 35 36 37 38 39
/***********************************************************************
 *           hmemcpy16   (KERNEL.348)
 */
void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
{
    memcpy( dst, src, count );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
40 41 42
/***********************************************************************
 *           lstrcat16   (KERNEL.89)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
43
SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
44
{
45
    /* Windows does not check for NULL pointers here, so we don't either */
46
    strcat( MapSL(dst), src );
Alexandre Julliard's avatar
Alexandre Julliard committed
47 48 49 50 51
    return dst;
}


/***********************************************************************
52
 *           lstrcatA   (KERNEL32.599)
Alexandre Julliard's avatar
Alexandre Julliard committed
53
 */
54
LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
55
{
56 57 58 59 60 61 62 63 64 65
    __TRY
    {
        strcat( dst, src );
    }
    __EXCEPT(page_fault)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }
    __ENDTRY
Alexandre Julliard's avatar
Alexandre Julliard committed
66 67 68 69 70
    return dst;
}


/***********************************************************************
71
 *           lstrcatW   (KERNEL32.600)
Alexandre Julliard's avatar
Alexandre Julliard committed
72
 */
73
LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
74
{
75 76
    __TRY
    {
77
        strcatW( dst, src );
78 79 80 81 82 83 84
    }
    __EXCEPT(page_fault)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }
    __ENDTRY
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86 87 88 89 90 91
    return dst;
}


/***********************************************************************
 *           lstrcatn16   (KERNEL.352)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
92
SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
Alexandre Julliard's avatar
Alexandre Julliard committed
93
{
94 95
    LPSTR p = MapSL(dst);
    LPSTR start = p;
Alexandre Julliard's avatar
Alexandre Julliard committed
96 97

    while (*p) p++;
98
    if ((n -= (p - start)) <= 0) return dst;
99
    lstrcpynA( p, src, n );
Alexandre Julliard's avatar
Alexandre Julliard committed
100 101 102 103 104
    return dst;
}


/***********************************************************************
105
 *           lstrcmpA   (KERNEL.602)
Alexandre Julliard's avatar
Alexandre Julliard committed
106
 */
107
INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
Alexandre Julliard's avatar
Alexandre Julliard committed
108
{
109
    return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
Alexandre Julliard's avatar
Alexandre Julliard committed
110 111 112 113
}


/***********************************************************************
114 115
 *           lstrcmpW   (KERNEL.603)
 * FIXME : should call CompareStringW, when it is implemented.
Alexandre Julliard's avatar
Alexandre Julliard committed
116
 *    This implementation is not "word sort", as it should.
Alexandre Julliard's avatar
Alexandre Julliard committed
117
 */
118
INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
Alexandre Julliard's avatar
Alexandre Julliard committed
119
{
120
    TRACE("%s and %s\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
121
		   debugstr_w (str1), debugstr_w (str2));
Alexandre Julliard's avatar
Alexandre Julliard committed
122 123 124 125
    if (!str1 || !str2) {
    	SetLastError(ERROR_INVALID_PARAMETER);
	return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
126
    while (*str1 && (*str1 == *str2)) { str1++; str2++; }
127
    return (INT)(*str1 - *str2);
Alexandre Julliard's avatar
Alexandre Julliard committed
128 129 130 131
}


/***********************************************************************
132
 *           lstrcmpiA   (KERNEL32.605)
Alexandre Julliard's avatar
Alexandre Julliard committed
133
 */
134
INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
135
{    TRACE("strcmpi %s and %s\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
136
		   debugstr_a (str1), debugstr_a (str2));
137
    return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
Alexandre Julliard's avatar
Alexandre Julliard committed
138 139 140 141
}


/***********************************************************************
142
 *           lstrcmpiW   (KERNEL32.606)
Alexandre Julliard's avatar
Alexandre Julliard committed
143
 */
144
INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
Alexandre Julliard's avatar
Alexandre Julliard committed
145
{
Alexandre Julliard's avatar
Alexandre Julliard committed
146 147 148 149
    if (!str1 || !str2) {
    	SetLastError(ERROR_INVALID_PARAMETER);
	return 0;
    }
150
    return strcmpiW( str1, str2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
151 152 153 154 155 156
}


/***********************************************************************
 *           lstrcpy16   (KERNEL.88)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
157
SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
158
{
159
    if (!lstrcpyA( MapSL(dst), src )) dst = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
160 161 162 163 164
    return dst;
}


/***********************************************************************
165
 *           lstrcpyA   (KERNEL32.608)
Alexandre Julliard's avatar
Alexandre Julliard committed
166
 */
167
LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
168
{
169 170 171 172
    __TRY
    {
        /* this is how Windows does it */
        memmove( dst, src, strlen(src)+1 );
173
    }
174 175
    __EXCEPT(page_fault)
    {
176
        ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
177 178 179 180
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }
    __ENDTRY
Alexandre Julliard's avatar
Alexandre Julliard committed
181 182 183 184 185
    return dst;
}


/***********************************************************************
186
 *           lstrcpyW   (KERNEL32.609)
Alexandre Julliard's avatar
Alexandre Julliard committed
187
 */
188
LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
Alexandre Julliard's avatar
Alexandre Julliard committed
189
{
190 191
    __TRY
    {
192
        strcpyW( dst, src );
193
    }
194 195 196 197 198 199
    __EXCEPT(page_fault)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }
    __ENDTRY
Alexandre Julliard's avatar
Alexandre Julliard committed
200 201 202 203 204 205 206
    return dst;
}


/***********************************************************************
 *           lstrcpyn16   (KERNEL.353)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
207
SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
Alexandre Julliard's avatar
Alexandre Julliard committed
208
{
209
    lstrcpynA( MapSL(dst), src, n );
Alexandre Julliard's avatar
Alexandre Julliard committed
210 211 212 213 214
    return dst;
}


/***********************************************************************
215
 *           lstrcpynA   (KERNEL32.611)
Alexandre Julliard's avatar
Alexandre Julliard committed
216 217
 * Note: this function differs from the UNIX strncpy, it _always_ writes
 * a terminating \0
Alexandre Julliard's avatar
Alexandre Julliard committed
218
 */
219
LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
Alexandre Julliard's avatar
Alexandre Julliard committed
220 221
{
    LPSTR p = dst;
222
    TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
223 224 225 226 227 228 229 230
    /* In real windows the whole function is protected by an exception handler
     * that returns ERROR_INVALID_PARAMETER on faulty parameters
     * We currently just check for NULL.
     */
    if (!dst || !src) {
    	SetLastError(ERROR_INVALID_PARAMETER);
	return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
231
    while ((n-- > 1) && *src) *p++ = *src++;
Alexandre Julliard's avatar
Alexandre Julliard committed
232
    if (n >= 0) *p = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
233 234 235 236 237
    return dst;
}


/***********************************************************************
238
 *           lstrcpynW   (KERNEL32.612)
Alexandre Julliard's avatar
Alexandre Julliard committed
239 240
 * Note: this function differs from the UNIX strncpy, it _always_ writes
 * a terminating \0
Alexandre Julliard's avatar
Alexandre Julliard committed
241
 */
242
LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
Alexandre Julliard's avatar
Alexandre Julliard committed
243 244
{
    LPWSTR p = dst;
245
    TRACE("(%p, %s, %i)\n", dst,  debugstr_wn(src,n), n);
246 247 248 249 250 251 252 253
    /* In real windows the whole function is protected by an exception handler
     * that returns ERROR_INVALID_PARAMETER on faulty parameters
     * We currently just check for NULL.
     */
    if (!dst || !src) {
    	SetLastError(ERROR_INVALID_PARAMETER);
	return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
254
    while ((n-- > 1) && *src) *p++ = *src++;
Alexandre Julliard's avatar
Alexandre Julliard committed
255
    if (n >= 0) *p = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
256 257 258 259 260 261 262
    return dst;
}


/***********************************************************************
 *           lstrlen16   (KERNEL.90)
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
263
INT16 WINAPI lstrlen16( LPCSTR str )
Alexandre Julliard's avatar
Alexandre Julliard committed
264
{
265
    return (INT16)lstrlenA( str );
Alexandre Julliard's avatar
Alexandre Julliard committed
266 267 268 269
}


/***********************************************************************
270
 *           lstrlenA   (KERNEL32.614)
Alexandre Julliard's avatar
Alexandre Julliard committed
271
 */
272
INT WINAPI lstrlenA( LPCSTR str )
Alexandre Julliard's avatar
Alexandre Julliard committed
273
{
274 275 276 277 278 279 280 281 282 283 284 285
    INT ret;
    __TRY
    {
        ret = strlen(str);
    }
    __EXCEPT(page_fault)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
286 287 288 289
}


/***********************************************************************
290
 *           lstrlenW   (KERNEL32.615)
Alexandre Julliard's avatar
Alexandre Julliard committed
291
 */
292
INT WINAPI lstrlenW( LPCWSTR str )
Alexandre Julliard's avatar
Alexandre Julliard committed
293
{
294 295 296
    INT ret;
    __TRY
    {
297
        ret = strlenW(str);
298 299 300 301 302 303 304 305
    }
    __EXCEPT(page_fault)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
306 307 308
}


309 310 311
/***********************************************************************
 *           UnicodeToAnsi   (KERNEL.434)
 */
312
INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
313
{
314 315
    if ( codepage == -1 )
	codepage = CP_ACP;
316

317
    return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
318
}