update for HEAD-2003091401
[reactos.git] / lib / msvcrt / 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  * 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  * The _mbsnbcpy function copies count bytes from src to dest. If src is shorter 
53  * than dest, the string is padded with null characters. If dest is less than or 
54  * equal to count it is not terminated with a null character.
55  *
56  * @implemented
57  */
58 unsigned char * _mbsnbcpy(unsigned char *str1, const unsigned char *str2, size_t n)
59 {
60         unsigned char *s1 = (unsigned char *)str1;
61         unsigned char *s2 = (unsigned char *)str2;
62
63         unsigned short *short_s1, *short_s2;
64
65         if (n == 0)
66                 return 0;
67         do {
68                 
69         if (*s2 == 0) {
70                         *s1 = *s2;
71                         break;  
72         }
73
74                 if (  !_ismbblead(*s2) ) {
75
76                         *s1 = *s2;
77                         s1 += 1;
78                         s2 += 1;
79                         n--;
80                 }
81                 else {
82                         short_s1 = (unsigned short *)s1;
83                         short_s2 = (unsigned short *)s2;
84                         *short_s1 = *short_s2;
85                         s1 += 2;
86                         s2 += 2;
87                         n-=2;
88                 }
89         } while (n > 0);
90         return str1;
91 }