xref: /reactos/sdk/lib/ucrt/mbstring/mbscoll.cpp (revision 04e0dc4a)
1*04e0dc4aSTimo Kreuzer /***
2*04e0dc4aSTimo Kreuzer *mbscoll.c - Collate MBCS strings
3*04e0dc4aSTimo Kreuzer *
4*04e0dc4aSTimo Kreuzer *       Copyright (c) Microsoft Corporation. All rights reserved.
5*04e0dc4aSTimo Kreuzer *
6*04e0dc4aSTimo Kreuzer *Purpose:
7*04e0dc4aSTimo Kreuzer *       Collate MBCS strings
8*04e0dc4aSTimo Kreuzer *
9*04e0dc4aSTimo Kreuzer *******************************************************************************/
10*04e0dc4aSTimo Kreuzer #ifndef _MBCS
11*04e0dc4aSTimo Kreuzer     #error This file should only be compiled with _MBCS defined
12*04e0dc4aSTimo Kreuzer #endif
13*04e0dc4aSTimo Kreuzer 
14*04e0dc4aSTimo Kreuzer #include <corecrt_internal_mbstring.h>
15*04e0dc4aSTimo Kreuzer #include <locale.h>
16*04e0dc4aSTimo Kreuzer #include <string.h>
17*04e0dc4aSTimo Kreuzer 
18*04e0dc4aSTimo Kreuzer 
19*04e0dc4aSTimo Kreuzer /***
20*04e0dc4aSTimo Kreuzer * _mbscoll - Collate MBCS strings
21*04e0dc4aSTimo Kreuzer *
22*04e0dc4aSTimo Kreuzer *Purpose:
23*04e0dc4aSTimo Kreuzer *       Collates two strings for lexical order.   Strings
24*04e0dc4aSTimo Kreuzer *       are collated on a character basis, not a byte basis.
25*04e0dc4aSTimo Kreuzer *
26*04e0dc4aSTimo Kreuzer *Entry:
27*04e0dc4aSTimo Kreuzer *       char *s1, *s2 = strings to collate
28*04e0dc4aSTimo Kreuzer *
29*04e0dc4aSTimo Kreuzer *Exit:
30*04e0dc4aSTimo Kreuzer *       Returns <0 if s1 < s2
31*04e0dc4aSTimo Kreuzer *       Returns  0 if s1 == s2
32*04e0dc4aSTimo Kreuzer *       Returns >0 if s1 > s2
33*04e0dc4aSTimo Kreuzer *       Returns _NLSCMPERROR is something went wrong
34*04e0dc4aSTimo Kreuzer *
35*04e0dc4aSTimo Kreuzer *Exceptions:
36*04e0dc4aSTimo Kreuzer *       Input parameters are validated. Refer to the validation section of the function.
37*04e0dc4aSTimo Kreuzer *
38*04e0dc4aSTimo Kreuzer *******************************************************************************/
39*04e0dc4aSTimo Kreuzer 
_mbscoll_l(const unsigned char * s1,const unsigned char * s2,_locale_t plocinfo)40*04e0dc4aSTimo Kreuzer extern "C" int __cdecl _mbscoll_l(
41*04e0dc4aSTimo Kreuzer         const unsigned char *s1,
42*04e0dc4aSTimo Kreuzer         const unsigned char *s2,
43*04e0dc4aSTimo Kreuzer         _locale_t plocinfo
44*04e0dc4aSTimo Kreuzer         )
45*04e0dc4aSTimo Kreuzer {
46*04e0dc4aSTimo Kreuzer         int ret;
47*04e0dc4aSTimo Kreuzer         _LocaleUpdate _loc_update(plocinfo);
48*04e0dc4aSTimo Kreuzer 
49*04e0dc4aSTimo Kreuzer         /* validation section */
50*04e0dc4aSTimo Kreuzer         _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR);
51*04e0dc4aSTimo Kreuzer         _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR);
52*04e0dc4aSTimo Kreuzer 
53*04e0dc4aSTimo Kreuzer         if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
54*04e0dc4aSTimo Kreuzer             return _strcoll_l((const char *)s1, (const char *)s2, plocinfo);
55*04e0dc4aSTimo Kreuzer 
56*04e0dc4aSTimo Kreuzer         if (0 == (ret = __acrt_CompareStringA(
57*04e0dc4aSTimo Kreuzer                         _loc_update.GetLocaleT(),
58*04e0dc4aSTimo Kreuzer                         _loc_update.GetLocaleT()->mbcinfo->mblocalename,
59*04e0dc4aSTimo Kreuzer                         SORT_STRINGSORT,
60*04e0dc4aSTimo Kreuzer                         (LPCSTR)s1,
61*04e0dc4aSTimo Kreuzer                         -1,
62*04e0dc4aSTimo Kreuzer                         (LPSTR)s2,
63*04e0dc4aSTimo Kreuzer                         -1,
64*04e0dc4aSTimo Kreuzer                         _loc_update.GetLocaleT()->mbcinfo->mbcodepage )))
65*04e0dc4aSTimo Kreuzer         {
66*04e0dc4aSTimo Kreuzer             errno = EINVAL;
67*04e0dc4aSTimo Kreuzer 
68*04e0dc4aSTimo Kreuzer             return _NLSCMPERROR;
69*04e0dc4aSTimo Kreuzer         }
70*04e0dc4aSTimo Kreuzer 
71*04e0dc4aSTimo Kreuzer         return ret - 2;
72*04e0dc4aSTimo Kreuzer 
73*04e0dc4aSTimo Kreuzer }
74*04e0dc4aSTimo Kreuzer 
75*04e0dc4aSTimo Kreuzer extern "C" int (__cdecl _mbscoll)(
76*04e0dc4aSTimo Kreuzer         const unsigned char *s1,
77*04e0dc4aSTimo Kreuzer         const unsigned char *s2
78*04e0dc4aSTimo Kreuzer         )
79*04e0dc4aSTimo Kreuzer {
80*04e0dc4aSTimo Kreuzer     return _mbscoll_l(s1, s2, nullptr);
81*04e0dc4aSTimo Kreuzer }
82