summaryrefslogtreecommitdiffstats
path: root/private/crt32/mbstring/mbsnbset.c
blob: 36efbb26aa647859d5273e30e9f82d6563a10a14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/***
*mbsnbset.asm - Sets first n bytes of string to given character (MBCS)
*
*	Copyright (c) 1985-1993, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Sets first n bytes of string to given character (MBCS)
*
*Revision History:
*	08-03-93  KRS	Ported from 16-bit sources.
*	08-20-93  CFW   Change short params to int for 32-bit tree.
*
*******************************************************************************/

#ifdef _MBCS
#include <cruntime.h>
#include <mbdata.h>
#include <mbctype.h>
#include <mbstring.h>

/***
* _mbsnbset - Sets first n bytes of string to given character (MBCS)
*
*Purpose:
*	Sets the first n bytes of string to the supplied
*	character value.  If the length of string is less than n,
*	the length of string is used in place of n.  Handles
*	MBCS chars correctly.
*
*	There are several factors that make this routine complicated:
*		(1) The fill value may be 1 or 2 bytes long.
*		(2) The fill operation may end by hitting the count value
*		or by hitting the end of the string.
*		(3) A null terminating char is NOT placed at the end of
*		the string.
*
*	Cases to be careful of (both of these can occur at once):
*		(1) Leaving an "orphaned" trail byte in the string (e.g.,
*		overwriting a lead byte but not the corresponding trail byte).
*		(2) Writing only the 1st byte of a 2-byte fill value because the
*		end of string was encountered.
*
*Entry:
*	unsigned char *string = string to modify
*	unsigned int val = value to fill string with
*	size_t count = count of characters to fill
*
*
*Exit:
*	Returns string = now filled with char val
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

unsigned char * _CRTAPI1 _mbsnbset(
    unsigned char *string,
    unsigned int val,
    size_t count
    )
{
	unsigned char  *start = string;
	unsigned char highval, lowval;

	/*
	 * leadbyte flag indicates if the last byte we overwrote was
	 * a lead byte or not.
	 */

	if (highval = (unsigned char)(val>>8)) {

		/* double byte value */

		lowval = (unsigned char)(val & 0x00ff);

		while ((count--) && *string) {

			/* pad with ' ' if no room for both bytes -- odd len */
			if ((!count--) || (!*(string+1))) {
				*string = ' ';
				break;
			}
			    
			*string++ = highval;
			*string++ = lowval;
		}
	}

	else {
		/* single byte value */

		while (count-- && *string) {
	    		*string++ = (unsigned char)val;
		}
        	
    	}

	return( start );
}
#endif	/* _MBCS */