Imported Upstream version 1.1.1
[platform/upstream/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 #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   register unsigned char *dest, *source;
64
65   for (dest = (unsigned char *)dst, source = (unsigned char *)src;
66        *source;
67        source++, dest++)
68     *dest = XmuTolower(*source);
69   *dest = '\0';
70 }
71
72 void
73 XmuCopyISOLatin1Uppered(char *dst, _Xconst char *src)
74 {
75   register unsigned char *dest, *source;
76
77   for (dest = (unsigned char *)dst, source = (unsigned char *)src;
78        *source;
79        source++, dest++)
80     *dest = XmuToupper(*source);
81   *dest = '\0';
82 }
83
84 int
85 XmuCompareISOLatin1(_Xconst char *first, _Xconst char *second)
86 {
87   register unsigned char *ap, *bp;
88
89   for (ap = (unsigned char *)first, bp = (unsigned char *)second;
90        *ap && *bp && XmuTolower(*ap) == XmuTolower(*bp);
91        ap++, bp++)
92     ;
93
94   return ((int)XmuTolower(*ap) - (int)XmuTolower(*bp));
95 }
96
97 void
98 XmuNCopyISOLatin1Lowered(char *dst, _Xconst char *src, register int size)
99 {
100   register unsigned char *dest, *source;
101
102   if (size > 0)
103     {
104       for (dest = (unsigned char *)dst, source = (unsigned char *)src;
105            *source && size > 1;
106            source++, dest++, size--)
107         *dest = XmuTolower(*source);
108       *dest = '\0';
109     }
110 }
111
112 void
113 XmuNCopyISOLatin1Uppered(char *dst, _Xconst char *src, register int size)
114 {
115   register unsigned char *dest, *source;
116
117   if (size > 0)
118     {
119       for (dest = (unsigned char *)dst, source = (unsigned char *)src;
120            *source && size > 1;
121            source++, dest++, size--)
122         *dest = XmuToupper(*source);
123       *dest = '\0';
124     }
125 }
126
127 int
128 XmuSnprintf(char *str, int size, _Xconst char *fmt, ...)
129 {
130   va_list ap;
131   int retval;
132
133   if (size <= 0)
134     return (size);
135
136   va_start(ap, fmt);
137
138 #if 0
139   retval = vsprintf(str, fmt, ap);
140   if (retval >= size)
141     {
142       fprintf(stderr, "WARNING: buffer overflow detected!\n");
143       fflush(stderr);
144       abort();
145     }
146 #else
147   retval = vsnprintf(str, size, fmt, ap);
148 #endif
149
150   va_end(ap);
151
152   return (retval);
153 }