4e44be148954a40085a60b73f40005f9557e7115
[framework/uifw/xorg/lib/libxmu.git] / src / Lower.c
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
37 #ifndef HAS_SNPRINTF
38 #undef SCOPE
39 #define SCOPE static
40 #include "snprintf.c"
41 #endif
42
43 #include <stdarg.h>
44
45 /*
46  * ISO Latin-1 case conversion routine
47  */
48 #define XmuTolower(c)                                                    \
49 ((c) >= XK_a && (c) <= XK_z ?                                            \
50  (c) : (c) >= XK_A && (c) <= XK_Z ?                                      \
51  (c) + (XK_a - XK_A) : (c) >= XK_Agrave && (c) <= XK_Odiaeresis ?        \
52  (c) + (XK_agrave - XK_Agrave) : (c) >= XK_Ooblique && (c) <= XK_Thorn ? \
53  (c) + (XK_oslash - XK_Ooblique) :                                       \
54  (c))
55
56 #define XmuToupper(c)                                                    \
57 ((c) >= XK_A && (c) <= XK_Z ?                                            \
58  (c) : (c) >= XK_a && (c) <= XK_z ?                                      \
59  (c) - (XK_a - XK_A) : (c) >= XK_agrave && (c) <= XK_odiaeresis ?        \
60  (c) - (XK_agrave - XK_Agrave) : (c) >= XK_oslash && (c) <= XK_thorn ?   \
61  (c) - (XK_oslash - XK_Ooblique) :                                       \
62  (c))
63
64 /*
65  * Implementation
66  */
67 void
68 XmuCopyISOLatin1Lowered(char *dst, _Xconst char *src)
69 {
70   register unsigned char *dest, *source;
71
72   for (dest = (unsigned char *)dst, source = (unsigned char *)src;
73        *source;
74        source++, dest++)
75     *dest = XmuTolower(*source);
76   *dest = '\0';
77 }
78
79 void
80 XmuCopyISOLatin1Uppered(char *dst, _Xconst char *src)
81 {
82   register unsigned char *dest, *source;
83
84   for (dest = (unsigned char *)dst, source = (unsigned char *)src;
85        *source;
86        source++, dest++)
87     *dest = XmuToupper(*source);
88   *dest = '\0';
89 }
90
91 int
92 XmuCompareISOLatin1(_Xconst char *first, _Xconst char *second)
93 {
94   register unsigned char *ap, *bp;
95
96   for (ap = (unsigned char *)first, bp = (unsigned char *)second;
97        *ap && *bp && XmuTolower(*ap) == XmuTolower(*bp);
98        ap++, bp++)
99     ;
100
101   return ((int)XmuTolower(*ap) - (int)XmuTolower(*bp));
102 }
103
104 void
105 XmuNCopyISOLatin1Lowered(char *dst, _Xconst char *src, register int size)
106 {
107   register unsigned char *dest, *source;
108
109   if (size > 0)
110     {
111       for (dest = (unsigned char *)dst, source = (unsigned char *)src;
112            *source && size > 1;
113            source++, dest++, size--)
114         *dest = XmuTolower(*source);
115       *dest = '\0';
116     }
117 }
118
119 void
120 XmuNCopyISOLatin1Uppered(char *dst, _Xconst char *src, register int size)
121 {
122   register unsigned char *dest, *source;
123
124   if (size > 0)
125     {
126       for (dest = (unsigned char *)dst, source = (unsigned char *)src;
127            *source && size > 1;
128            source++, dest++, size--)
129         *dest = XmuToupper(*source);
130       *dest = '\0';
131     }
132 }
133
134 int
135 XmuSnprintf(char *str, int size, _Xconst char *fmt, ...)
136 {
137   va_list ap;
138   int retval;
139
140   if (size <= 0)
141     return (size);
142
143   va_start(ap, fmt);
144
145 #if 0
146   retval = vsprintf(str, fmt, ap);
147   if (retval >= size)
148     {
149       fprintf(stderr, "WARNING: buffer overflow detected!\n");
150       fflush(stderr);
151       abort();
152     }
153 #else
154   retval = vsnprintf(str, size, fmt, ap);
155 #endif
156
157   va_end(ap);
158
159   return (retval);
160 }