summaryrefslogtreecommitdiffstats
path: root/newlib/libc/stdlib/mbstowcs.c
blob: 09543451cf877f8d60d83428062afb713c16479f (plain)
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
/*
FUNCTION
<<mbstowcs>>---minimal multibyte string to wide char converter

INDEX
	mbstowcs

ANSI_SYNOPSIS
	#include <stdlib.h>
	int mbstowcs(wchar_t *restrict <[pwc]>, const char *restrict <[s]>, size_t <[n]>);

TRAD_SYNOPSIS
	#include <stdlib.h>
	int mbstowcs(<[pwc]>, <[s]>, <[n]>)
	wchar_t *<[pwc]>;
	const char *<[s]>;
	size_t <[n]>;

DESCRIPTION
When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming 
implementation of <<mbstowcs>>.  In this case, the
only ``multi-byte character sequences'' recognized are single bytes,
and they are ``converted'' to wide-char versions simply by byte
extension.

When _MB_CAPABLE is defined, this routine calls <<_mbstowcs_r>> to perform
the conversion, passing a state variable to allow state dependent
decoding.  The result is based on the locale setting which may
be restricted to a defined set of locales.

RETURNS
This implementation of <<mbstowcs>> returns <<0>> if
<[s]> is <<NULL>> or is the empty string; 
it returns <<-1>> if _MB_CAPABLE and one of the
multi-byte characters is invalid or incomplete;
otherwise it returns the minimum of: <<n>> or the
number of multi-byte characters in <<s>> plus 1 (to
compensate for the nul character).
If the return value is -1, the state of the <<pwc>> string is
indeterminate.  If the input has a length of 0, the output
string will be modified to contain a wchar_t nul terminator.

PORTABILITY
<<mbstowcs>> is required in the ANSI C standard.  However, the precise
effects vary with the locale.

<<mbstowcs>> requires no supporting OS subroutines.
*/

#ifndef _REENT_ONLY

#include <newlib.h>
#include <stdlib.h>
#include <wchar.h>

size_t
_DEFUN (mbstowcs, (pwcs, s, n),
        wchar_t *__restrict pwcs _AND
        const char *__restrict s _AND
        size_t n)
{
#ifdef _MB_CAPABLE
  mbstate_t state;
  state.__count = 0;
  
  return _mbstowcs_r (_REENT, pwcs, s, n, &state);
#else /* not _MB_CAPABLE */
  
  int count = 0;
  
  if (n != 0) {
    do {
      if ((*pwcs++ = (wchar_t) *s++) == 0)
	break;
      count++;
    } while (--n != 0);
  }
  
  return count;
#endif /* not _MB_CAPABLE */
}

#endif /* !_REENT_ONLY */
-terminated string of 0 to 6 characters. <<a64l>> returns the 32-bit translated value from the input character string. PORTABILITY <<l64a>> and <<a64l>> are non-ANSI and are defined by the Single Unix Specification. Supporting OS subroutines required: None. */ #include <_ansi.h> #include <stdlib.h> #include <limits.h> long _DEFUN (a64l, (input), const char *input) { char *ptr; char ch; int i, digit; unsigned long result = 0; if (input == NULL) return 0; ptr = input; /* it easiest to go from most significant digit to least so find end of input or up to 6 characters worth */ for (i = 0; i < 6; ++i) { if (*ptr) ++ptr; } while (ptr > input) { ch = *(--ptr); #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) if (ch >= 'a') digit = (ch - 'a') + 38; else if (ch >= 'A') digit = (ch - 'A') + 12; else if (ch >= '0') digit = (ch - '0') + 2; else if (ch == '/') digit = 1; else digit = 0; #else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */ switch (ch) { case '/': digit = 1; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': digit = (ch - '0') + 2; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': digit = (ch - 'A') + 12; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': digit = (ch - 'A') + 38; break; default: digit = 0; break; } #endif /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */ result = (result << 6) + digit; } #if LONG_MAX > 2147483647 /* for implementations where long is > 32 bits, the result must be sign-extended */ if (result & 0x80000000) return (((long)-1 >> 32) << 32) + result; #endif return result; }