update for HEAD-2003091401
[reactos.git] / lib / crtdll / mbstring / mbsncpy.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/crtdll/mbstring/mbsncpy.c
5  * PURPOSE:     Copies a string to a maximum of n bytes or characters
6  * PROGRAMER:   Boudewijn Dekker
7  * UPDATE HISTORY:
8  *              12/04/99: Created
9  */
10
11 #include <msvcrt/mbstring.h>
12
13
14 /*
15  * @implemented
16  */
17 unsigned char *_mbsncpy(unsigned char *str1, const unsigned char *str2, size_t n)
18 {
19         unsigned char *s1 = (unsigned char *)str1;
20         unsigned char *s2 = (unsigned char *)str2;
21
22         unsigned short *short_s1, *short_s2;
23
24         if (n == 0)
25                 return 0;
26         do {
27                 
28                 if (*s2 == 0)
29                         break;  
30
31                 if (  !_ismbblead(*s2) ) {
32
33                         *s1 = *s2;
34                         s1 += 1;
35                         s2 += 1;
36                         n--;
37                 }
38                 else {
39                         short_s1 = (unsigned short *)s1;
40                         short_s2 = (unsigned short *)s2;
41                         *short_s1 = *short_s2;
42                         s1 += 2;
43                         s2 += 2;
44                         n--;
45                 }
46         } while (n > 0);
47         return str1;
48 }
49
50
51
52 /*
53  * The _mbsnbcpy function copies count bytes from src to dest. If src is shorter 
54  * than dest, the string is padded with null characters. If dest is less than or 
55  * equal to count it is not terminated with a null character.
56  *
57  * @implemented
58  */
59 unsigned char * _mbsnbcpy(unsigned char *str1, const unsigned char *str2, size_t n)
60 {
61         unsigned char *s1 = (unsigned char *)str1;
62         unsigned char *s2 = (unsigned char *)str2;
63
64         unsigned short *short_s1, *short_s2;
65
66         if (n == 0)
67                 return 0;
68         do {
69                 
70         if (*s2 == 0) {
71                         *s1 = *s2;
72                         break;  
73         }
74
75                 if (  !_ismbblead(*s2) ) {
76
77                         *s1 = *s2;
78                         s1 += 1;
79                         s2 += 1;
80                         n--;
81                 }
82                 else {
83                         short_s1 = (unsigned short *)s1;
84                         short_s2 = (unsigned short *)s2;
85                         *short_s1 = *short_s2;
86                         s1 += 2;
87                         s2 += 2;
88                         n-=2;
89                 }
90         } while (n > 0);
91         return str1;
92 }