Sync to trunk r38200
[reactos.git] / reactos / lib / sdk / crt / mbstring / mbsncpy.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/msvcrt/mbstring/mbsncpy.c
5 * PURPOSE: Copies a string to a maximum of n bytes or characters
6 * PROGRAMERS:
7 * Copyright 1999 Ariadne
8 * Copyright 1999 Alexandre Julliard
9 * Copyright 2000 Jon Griffths
10 *
11 */
12
13 #include <mbstring.h>
14
15 extern int g_mbcp_is_multibyte;
16
17 /*
18 * @implemented
19 */
20 unsigned char* _mbsncpy(unsigned char *dst, const unsigned char *src, size_t n)
21 {
22 unsigned char* ret = dst;
23 if(!n)
24 return dst;
25 if (g_mbcp_is_multibyte)
26 {
27 while (*src && n)
28 {
29 n--;
30 if (_ismbblead(*src))
31 {
32 if (!*(src+1))
33 {
34 *dst++ = 0;
35 *dst++ = 0;
36 break;
37 }
38
39 *dst++ = *src++;
40 }
41
42 *dst++ = *src++;
43 }
44 }
45 else
46 {
47 while (n)
48 {
49 n--;
50 if (!(*dst++ = *src++)) break;
51 }
52 }
53 while (n--) *dst++ = 0;
54 return ret;
55 }
56
57
58 /*
59 * The _mbsnbcpy function copies count bytes from src to dest. If src is shorter
60 * than dest, the string is padded with null characters. If dest is less than or
61 * equal to count it is not terminated with a null character.
62 *
63 * @implemented
64 */
65 unsigned char * _mbsnbcpy(unsigned char *dst, const unsigned char *src, size_t n)
66 {
67 unsigned char* ret = dst;
68 if(!n)
69 return dst;
70 if(g_mbcp_is_multibyte)
71 {
72 int is_lead = 0;
73 while (*src && n)
74 {
75 is_lead = (!is_lead && _ismbblead(*src));
76 n--;
77 *dst++ = *src++;
78 }
79
80 if (is_lead) /* if string ends with a lead, remove it */
81 *(dst - 1) = 0;
82 }
83 else
84 {
85 while (n)
86 {
87 n--;
88 if (!(*dst++ = *src++)) break;
89 }
90 }
91 while (n--) *dst++ = 0;
92 return ret;
93 }