Home | History | Annotate | Line # | Download | only in src
      1 /*
      2 
      3 Copyright 1988, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in this Software without prior written authorization from The Open Group.
     24 
     25 */
     26 
     27 #define  XK_LATIN1
     28 #ifdef HAVE_CONFIG_H
     29 #include <config.h>
     30 #endif
     31 #include <X11/keysymdef.h>
     32 #include <X11/Xmu/CharSet.h>
     33 #include <X11/Xmu/SysUtil.h>
     34 
     35 #include <stdio.h>
     36 #include <stdarg.h>
     37 
     38 /*
     39  * ISO Latin-1 case conversion routine
     40  */
     41 #define XmuTolower(c)							 \
     42 ((c) >= XK_a && (c) <= XK_z ?						 \
     43  (c) : (c) >= XK_A && (c) <= XK_Z ?					 \
     44  (c) + (XK_a - XK_A) : (c) >= XK_Agrave && (c) <= XK_Odiaeresis ?	 \
     45  (c) + (XK_agrave - XK_Agrave) : (c) >= XK_Ooblique && (c) <= XK_Thorn ? \
     46  (c) + (XK_oslash - XK_Ooblique) :					 \
     47  (c))
     48 
     49 #define XmuToupper(c)							 \
     50 ((c) >= XK_A && (c) <= XK_Z ?						 \
     51  (c) : (c) >= XK_a && (c) <= XK_z ?					 \
     52  (c) - (XK_a - XK_A) : (c) >= XK_agrave && (c) <= XK_odiaeresis ?	 \
     53  (c) - (XK_agrave - XK_Agrave) : (c) >= XK_oslash && (c) <= XK_thorn ?	 \
     54  (c) - (XK_oslash - XK_Ooblique) :					 \
     55  (c))
     56 
     57 /*
     58  * Implementation
     59  */
     60 void
     61 XmuCopyISOLatin1Lowered(char *dst, _Xconst char *src)
     62 {
     63   unsigned char *dest;
     64   _Xconst unsigned char *source;
     65 
     66   for (dest = (unsigned char *)dst, source = (_Xconst unsigned char *)src;
     67        *source;
     68        source++, dest++)
     69     *dest = XmuTolower(*source);
     70   *dest = '\0';
     71 }
     72 
     73 void
     74 XmuCopyISOLatin1Uppered(char *dst, _Xconst char *src)
     75 {
     76   unsigned char *dest;
     77   _Xconst unsigned char *source;
     78 
     79   for (dest = (unsigned char *)dst, source = (_Xconst unsigned char *)src;
     80        *source;
     81        source++, dest++)
     82     *dest = XmuToupper(*source);
     83   *dest = '\0';
     84 }
     85 
     86 int
     87 XmuCompareISOLatin1(_Xconst char *first, _Xconst char *second)
     88 {
     89   _Xconst unsigned char *ap, *bp;
     90 
     91   for (ap = (_Xconst unsigned char *)first, bp = (_Xconst unsigned char *)second;
     92        *ap && *bp && XmuTolower(*ap) == XmuTolower(*bp);
     93        ap++, bp++)
     94     ;
     95 
     96   return ((int)XmuTolower(*ap) - (int)XmuTolower(*bp));
     97 }
     98 
     99 void
    100 XmuNCopyISOLatin1Lowered(char *dst, _Xconst char *src, register int size)
    101 {
    102   unsigned char *dest;
    103   _Xconst unsigned char *source;
    104 
    105   if (size > 0)
    106     {
    107       for (dest = (unsigned char *)dst, source = (_Xconst unsigned char *)src;
    108 	   *source && size > 1;
    109 	   source++, dest++, size--)
    110 	*dest = XmuTolower(*source);
    111       *dest = '\0';
    112     }
    113 }
    114 
    115 void
    116 XmuNCopyISOLatin1Uppered(char *dst, _Xconst char *src, register int size)
    117 {
    118   unsigned char *dest;
    119   _Xconst unsigned char *source;
    120 
    121   if (size > 0)
    122     {
    123       for (dest = (unsigned char *)dst, source = ( _Xconst unsigned char *)src;
    124 	   *source && size > 1;
    125 	   source++, dest++, size--)
    126 	*dest = XmuToupper(*source);
    127       *dest = '\0';
    128     }
    129 }
    130 
    131 int
    132 XmuSnprintf(char *str, int size, _Xconst char *fmt, ...)
    133 {
    134   va_list ap;
    135   int retval;
    136 
    137   if (size <= 0)
    138     return (size);
    139 
    140   va_start(ap, fmt);
    141 
    142 #if 0
    143   retval = vsprintf(str, fmt, ap);
    144   if (retval >= size)
    145     {
    146       fprintf(stderr, "WARNING: buffer overflow detected!\n");
    147       fflush(stderr);
    148       abort();
    149     }
    150 #else
    151   retval = vsnprintf(str, size, fmt, ap);
    152 #endif
    153 
    154   va_end(ap);
    155 
    156   return (retval);
    157 }
    158