OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [stdlib/] [wctomb_r.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
#include <stdlib.h>
2
#include <locale.h>
3
#include "mbctype.h"
4
 
5
int
6
_DEFUN (_wctomb_r, (r, s, wchar, state),
7
        struct _reent *r     _AND
8
        char          *s     _AND
9
        wchar_t        wchar _AND
10
        int           *state)
11
{
12
  if (strlen (r->_current_locale) <= 1)
13
    { /* fall-through */ }
14
  else if (!strcmp (r->_current_locale, "C-SJIS"))
15
    {
16
      unsigned char char2 = (unsigned char)wchar;
17
      unsigned char char1 = (unsigned char)(wchar >> 8);
18
 
19
      if (s == NULL)
20
        return 0;  /* not state-dependent */
21
 
22
      if (char1 != 0x00)
23
        {
24
        /* first byte is non-zero..validate multi-byte char */
25
          if (_issjis1(char1) && _issjis2(char2))
26
            {
27
              *s++ = (char)char1;
28
              *s = (char)char2;
29
              return 2;
30
            }
31
          else
32
            return -1;
33
        }
34
    }
35
  else if (!strcmp (r->_current_locale, "C-EUCJP"))
36
    {
37
      unsigned char char2 = (unsigned char)wchar;
38
      unsigned char char1 = (unsigned char)(wchar >> 8);
39
 
40
      if (s == NULL)
41
        return 0;  /* not state-dependent */
42
 
43
      if (char1 != 0x00)
44
        {
45
        /* first byte is non-zero..validate multi-byte char */
46
          if (_iseucjp (char1) && _iseucjp (char2))
47
            {
48
              *s++ = (char)char1;
49
              *s = (char)char2;
50
              return 2;
51
            }
52
          else
53
            return -1;
54
        }
55
    }
56
  else if (!strcmp (r->_current_locale, "C-JIS"))
57
    {
58
      int cnt = 0;
59
      unsigned char char2 = (unsigned char)wchar;
60
      unsigned char char1 = (unsigned char)(wchar >> 8);
61
 
62
      if (s == NULL)
63
        return 1;  /* state-dependent */
64
 
65
      if (char1 != 0x00)
66
        {
67
        /* first byte is non-zero..validate multi-byte char */
68
          if (_isjis (char1) && _isjis (char2))
69
            {
70
              if (*state == 0)
71
                {
72
                  /* must switch from ASCII to JIS state */
73
                  *state = 1;
74
                  *s++ = ESC_CHAR;
75
                  *s++ = '$';
76
                  *s++ = 'B';
77
                  cnt = 3;
78
                }
79
              *s++ = (char)char1;
80
              *s = (char)char2;
81
              return cnt + 2;
82
            }
83
          else
84
            return -1;
85
        }
86
      else
87
        {
88
          if (*state != 0)
89
            {
90
              /* must switch from JIS to ASCII state */
91
              *state = 0;
92
              *s++ = ESC_CHAR;
93
              *s++ = '(';
94
              *s++ = 'B';
95
              cnt = 3;
96
            }
97
          *s = (char)char2;
98
          return cnt + 1;
99
        }
100
    }
101
 
102
  if (s == NULL)
103
    return 0;
104
 
105
  /* otherwise we are dealing with a single byte character */
106
  *s = (char) wchar;
107
  return 1;
108
}
109
 
110
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.