Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmbsncpy.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS system libraries 00004 * FILE: lib/msvcrt/mbstring/mbsncpy.c 00005 * PURPOSE: Copies a string to a maximum of n bytes or characters 00006 * PROGRAMERS: 00007 * Copyright 1999 Ariadne 00008 * Copyright 1999 Alexandre Julliard 00009 * Copyright 2000 Jon Griffths 00010 * 00011 */ 00012 00013 #include <precomp.h> 00014 #include <mbstring.h> 00015 00016 extern int g_mbcp_is_multibyte; 00017 00018 /* 00019 * @implemented 00020 */ 00021 unsigned char* _mbsncpy(unsigned char *dst, const unsigned char *src, size_t n) 00022 { 00023 unsigned char* ret = dst; 00024 if(!n) 00025 return dst; 00026 if (g_mbcp_is_multibyte) 00027 { 00028 while (*src && n) 00029 { 00030 n--; 00031 if (_ismbblead(*src)) 00032 { 00033 if (!*(src+1)) 00034 { 00035 *dst++ = 0; 00036 *dst++ = 0; 00037 break; 00038 } 00039 00040 *dst++ = *src++; 00041 } 00042 00043 *dst++ = *src++; 00044 } 00045 } 00046 else 00047 { 00048 while (n) 00049 { 00050 n--; 00051 if (!(*dst++ = *src++)) break; 00052 } 00053 } 00054 while (n--) *dst++ = 0; 00055 return ret; 00056 } 00057 00058 00059 /* 00060 * The _mbsnbcpy function copies count bytes from src to dest. If src is shorter 00061 * than dest, the string is padded with null characters. If dest is less than or 00062 * equal to count it is not terminated with a null character. 00063 * 00064 * @implemented 00065 */ 00066 unsigned char * _mbsnbcpy(unsigned char *dst, const unsigned char *src, size_t n) 00067 { 00068 unsigned char* ret = dst; 00069 if(!n) 00070 return dst; 00071 if(g_mbcp_is_multibyte) 00072 { 00073 int is_lead = 0; 00074 while (*src && n) 00075 { 00076 is_lead = (!is_lead && _ismbblead(*src)); 00077 n--; 00078 *dst++ = *src++; 00079 } 00080 00081 if (is_lead) /* if string ends with a lead, remove it */ 00082 *(dst - 1) = 0; 00083 } 00084 else 00085 { 00086 while (n) 00087 { 00088 n--; 00089 if (!(*dst++ = *src++)) break; 00090 } 00091 } 00092 while (n--) *dst++ = 0; 00093 return ret; 00094 } 00095 00096 /* 00097 * Unlike _mbsnbcpy this function does not pad the rest of the dest 00098 * string with 0 00099 */ 00100 int CDECL _mbsnbcpy_s(unsigned char* dst, size_t size, const unsigned char* src, size_t n) 00101 { 00102 size_t pos = 0; 00103 00104 if(!dst || size == 0) 00105 return EINVAL; 00106 if(!src) 00107 { 00108 dst[0] = '\0'; 00109 return EINVAL; 00110 } 00111 if(!n) 00112 return 0; 00113 00114 if(g_mbcp_is_multibyte) 00115 { 00116 int is_lead = 0; 00117 while (*src && n) 00118 { 00119 if(pos == size) 00120 { 00121 dst[0] = '\0'; 00122 return ERANGE; 00123 } 00124 is_lead = (!is_lead && _ismbblead(*src)); 00125 n--; 00126 dst[pos++] = *src++; 00127 } 00128 00129 if (is_lead) /* if string ends with a lead, remove it */ 00130 dst[pos - 1] = 0; 00131 } 00132 else 00133 { 00134 while (n) 00135 { 00136 n--; 00137 if(pos == size) 00138 { 00139 dst[0] = '\0'; 00140 return ERANGE; 00141 } 00142 00143 if(!(*src)) break; 00144 dst[pos++] = *src++; 00145 } 00146 } 00147 00148 if(pos < size) 00149 dst[pos] = '\0'; 00150 else 00151 { 00152 dst[0] = '\0'; 00153 return ERANGE; 00154 } 00155 00156 return 0; 00157 } Generated on Fri May 25 2012 04:34:55 for ReactOS by
1.7.6.1
|