ReactOS 0.4.16-dev-1025-gd3456f5
mbsncat.cpp
Go to the documentation of this file.
1/***
2*mbsncat.c - concatenate string2 onto string1, max length n
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* defines mbsncat() - concatenate maximum of n characters
8*
9*******************************************************************************/
10#ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12#endif
13
15#include <locale.h>
16#include <string.h>
17
18
19/***
20* _mbsncat - concatenate max cnt characters onto dst
21*
22*Purpose:
23* Concatenates src onto dst, with a maximum of cnt characters copied.
24* Handles 2-byte MBCS characters correctly.
25*
26*Entry:
27* unsigned char *dst - string to concatenate onto
28* unsigned char *src - string to concatenate from
29* int cnt - number of characters to copy
30*
31*Exit:
32* returns dst, with src (at least part) concatenated on
33*
34*Exceptions:
35*
36*******************************************************************************/
37
38extern "C" unsigned char * __cdecl _mbsncat_l(
39 unsigned char *dst,
40 const unsigned char *src,
41 size_t cnt,
43 )
44{
45 unsigned char *start;
46
47 if (!cnt)
48 return(dst);
49
50 /* validation section */
51 _VALIDATE_RETURN(dst != nullptr, EINVAL, nullptr);
52 _VALIDATE_RETURN(src != nullptr, EINVAL, nullptr);
53
54 _LocaleUpdate _loc_update(plocinfo);
55
57 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
58 return (unsigned char *)strncat((char *)dst, (const char *)src, cnt);
60
61 start = dst;
62 while (*dst++)
63 ;
64 --dst; // dst now points to end of dst string
65
66
67 /* if last char in string is a lead byte, back up pointer */
68
69 if ( _ismbslead_l(start, dst, _loc_update.GetLocaleT()) )
70 --dst;
71
72 /* copy over the characters */
73
74 while (cnt--) {
75 if ( _ismbblead_l(*src, _loc_update.GetLocaleT()) ) {
76 *dst++ = *src++;
77 if ((*dst++ = *src++) == '\0') {
78 dst[-2] = '\0';
79 break;
80 }
81 }
82
83 else if ((*dst++ = *src++) == '\0')
84 break;
85
86 }
87
88 /* enter final nul, if necessary */
89 if ( dst!=start && _mbsbtype_l(start, (int) ((dst - start) - 1), _loc_update.GetLocaleT()) == _MBC_LEAD )
90 {
91 dst[-1] = '\0';
92 }
93 else
94 {
95 *dst = '\0';
96 }
97
98 return(start);
99}
100
101extern "C" unsigned char * (__cdecl _mbsncat)(
102 unsigned char *dst,
103 const unsigned char *src,
104 size_t cnt
105 )
106{
108 return _mbsncat_l(dst, src, cnt, nullptr);
110}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
#define _ismbblead_l(_c, p)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
_Check_return_ _CRTIMP int __cdecl _ismbslead_l(_In_reads_z_(_Pos - _Str+1) const unsigned char *_Str, _In_z_ const unsigned char *_Pos, _In_opt_ _locale_t _Locale)
GLuint start
Definition: gl.h:1545
GLenum src
Definition: glext.h:6340
GLenum GLenum dst
Definition: glext.h:6340
_CRTIMP int __cdecl _mbsbtype_l(_In_reads_bytes_(_Pos) _Pre_z_ const unsigned char *_Str, _In_ size_t _Pos, _In_opt_ _locale_t _Locale)
#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
#define _END_SECURE_CRT_DEPRECATION_DISABLE
_locale_t plocinfo
Definition: ismbbyte.cpp:75
unsigned char *__cdecl _mbsncat_l(unsigned char *dst, const unsigned char *src, size_t cnt, _locale_t plocinfo)
Definition: mbsncat.cpp:38
unsigned char *__cdecl _mbsncat(unsigned char *dst, const unsigned char *src, size_t cnt)
Definition: mbsncat.cpp:101
#define _MBC_LEAD
Definition: msvcrt.h:824
_In_opt_ _Locale strncat
Definition: string.h:263
_In_ size_t cnt
Definition: wcstombs.cpp:43