branch update for HEAD-2003021201
[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 unsigned char *_mbsncpy(unsigned char *str1, const unsigned char *str2, size_t n)
15 {
16         unsigned char *s1 = (unsigned char *)str1;
17         unsigned char *s2 = (unsigned char *)str2;
18
19         unsigned short *short_s1, *short_s2;
20
21         if (n == 0)
22                 return 0;
23         do {
24                 
25                 if (*s2 == 0)
26                         break;  
27
28                 if (  !_ismbblead(*s2) ) {
29
30                         *s1 = *s2;
31                         s1 += 1;
32                         s2 += 1;
33                         n--;
34                 }
35                 else {
36                         short_s1 = (unsigned short *)s1;
37                         short_s2 = (unsigned short *)s2;
38                         *short_s1 = *short_s2;
39                         s1 += 2;
40                         s2 += 2;
41                         n--;
42                 }
43         } while (n > 0);
44         return str1;
45 }
46
47
48 //
49 //The _mbsnbcpy function copies count bytes from src to dest. If src is shorter 
50 //than dest, the string is padded with null characters. If dest is less than or 
51 //equal to count it is not terminated with a null character.
52 //
53
54 unsigned char * _mbsnbcpy(unsigned char *str1, const unsigned char *str2, size_t n)
55 {
56         unsigned char *s1 = (unsigned char *)str1;
57         unsigned char *s2 = (unsigned char *)str2;
58
59         unsigned short *short_s1, *short_s2;
60
61         if (n == 0)
62                 return 0;
63         do {
64                 
65         if (*s2 == 0) {
66                         *s1 = *s2;
67                         break;  
68         }
69
70                 if (  !_ismbblead(*s2) ) {
71
72                         *s1 = *s2;
73                         s1 += 1;
74                         s2 += 1;
75                         n--;
76                 }
77                 else {
78                         short_s1 = (unsigned short *)s1;
79                         short_s2 = (unsigned short *)s2;
80                         *short_s1 = *short_s2;
81                         s1 += 2;
82                         s2 += 2;
83                         n-=2;
84                 }
85         } while (n > 0);
86         return str1;
87 }