root/src/gauche/char_sjis.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2  * char-sjis.h
   3  *
   4  *   Copyright (c) 2000-2004 Shiro Kawai, All rights reserved.
   5  * 
   6  *   Redistribution and use in source and binary forms, with or without
   7  *   modification, are permitted provided that the following conditions
   8  *   are met:
   9  * 
  10  *   1. Redistributions of source code must retain the above copyright
  11  *      notice, this list of conditions and the following disclaimer.
  12  *
  13  *   2. Redistributions in binary form must reproduce the above copyright
  14  *      notice, this list of conditions and the following disclaimer in the
  15  *      documentation and/or other materials provided with the distribution.
  16  *
  17  *   3. Neither the name of the authors nor the names of its contributors
  18  *      may be used to endorse or promote products derived from this
  19  *      software without specific prior written permission.
  20  *
  21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  *
  33  *  $Id: char_sjis.h,v 1.6 2004/09/17 03:42:10 shirok Exp $
  34  */
  35 
  36 #ifndef SCM_CHAR_ENCODING_BODY
  37 /*===============================================================
  38  * Header part
  39  */
  40 
  41 /* The name of the encoding.  Scheme procedure 
  42  * gauche-character-encoding returns a symbol with this name.
  43  */
  44 #define SCM_CHAR_ENCODING_NAME "sjis"
  45 
  46 /* Given first byte of the multibyte character, returns # of
  47  * bytes that follows, i.e. if the byte consists a single-byte
  48  * character, it returns 0; if the byte is the first byte of
  49  * two-byte character, it returns 1.   It may return -1 if
  50  * the given byte can't be a valid first byte of multibyte characters.
  51  */
  52 #define SCM_CHAR_NFOLLOWS(byte)                   \
  53     (((unsigned char)(byte)) < 0x81? 0 :          \
  54      (((unsigned char)(byte)) < 0xa0? 1 :         \
  55       (((unsigned char)(byte)) < 0xe0? 0 : 1)))
  56 
  57 /* Given wide character CH, returns # of bytes used when CH is
  58  * encoded in multibyte string.
  59  */
  60 #define SCM_CHAR_NBYTES(ch) (((ch) > 0x0ff) ? 2 : 1)
  61 
  62 /* Maximun # of multibyte character */
  63 #define SCM_CHAR_MAX_BYTES     2
  64 
  65 /* From a multibyte string pointed by const char *cp, extract a character
  66  * and store it in ScmChar ch.  If cp doesn't point to valid multibyte
  67  * character, store SCM_CHAR_INVALID to ch.  cp is not modified.
  68  */
  69 #define SCM_CHAR_GET(cp, ch)                                                \
  70     do {                                                                    \
  71         (ch) = (unsigned char)*(cp);                                        \
  72         if ((unsigned char)(ch) >= 0x80) {                                  \
  73           if ((unsigned char)(ch) < 0xa0 || (unsigned char)(ch) >= 0xe0) {  \
  74              (ch) = (((unsigned char)(ch)) << 8) + (unsigned char)*(cp+1);  \
  75           }                                                                 \
  76         }                                                                   \
  77     } while (0)
  78 
  79 /* Convert a character CH to multibyte form and put it to the buffer
  80  * starting from char *cp.  You can assume the buffer has enough length
  81  * to contain the multibyte char.   cp is not modified.
  82  */
  83 #define SCM_CHAR_PUT(cp, ch)                    \
  84     do {                                        \
  85         if ((ch) > 0xff) {                      \
  86             (cp)[0] = (ch >> 8) & 0xff;         \
  87             (cp)[1] = ch & 0xff;                \
  88         } else {                                \
  89             (cp)[0] = ch & 0xff;                \
  90         }                                       \
  91     } while (0)
  92 
  93 /* const char *cp points to a multibyte string.  Set const char *result
  94  * to point to the previous character of the one cp points to.
  95  * const char *start points to the beginning of the buffer.
  96  * result is set to NULL if there's no valid multibyte char found
  97  * just before cp.   cp and start is not modified.
  98  */
  99 #define SCM_CHAR_BACKWARD(cp, start, result)                    \
 100     do {                                                        \
 101         (result) = (cp);                                        \
 102         if ((result) == (start)) (result) = NULL;               \
 103         else if ((result) == (start) + 1) (result) = (start);   \
 104         else if (SCM_CHAR_NFOLLOWS(*((result)-2)) == 1) {       \
 105              (result) -= 2;                                     \
 106         } else {                                                \
 107              (result) -= 1;                                     \
 108         }                                                       \
 109     } while (0)
 110 
 111 #else  /* !SCM_CHAR_ENCODING_BODY */
 112 /*==================================================================
 113  * This part is included in char.c
 114  */
 115 
 116 /* Array of character encoding names, recognizable by iconv, that are
 117    compatible with this native encoding. */
 118 static const char *supportedCharacterEncodings[] = {
 119     "SHIFT_JIS",
 120     "SHIFT-JIS",
 121     "SHIFT_JISX0213",
 122     "SHIFT-JISX0213",
 123     "SJIS",
 124     NULL
 125 };
 126 
 127 #endif /* !SCM_CHAR_ENCODING_BODY */

/* [<][>][^][v][top][bottom][index][help] */