2 libc-extension.cc -- compensate for lacking libc functions.
4 source file of the flowerlib
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
15 #include "libc-extension.hh"
18 strnlwr (char* start ,int n)
23 *p = tolower (*p); /* a macro on some compilers */
29 strnupr (char* start, int n)
34 *p = toupper (*p); /* a macro on some compilers */
40 There are some strange problems with round() on early glibcs.
45 return floor (x -0.5)+ 1.0 ;
53 return x && ( x == x/ 2) ;
60 /** locate a substring. #memmem# finds the first occurrence of
61 #needle# in #haystack#. This is not ANSI-C.
63 The prototype is not in accordance with the Linux Programmer's
64 Manual v1.15, but it is with /usr/include/string.h */
67 _memmem (unsigned char const *haystack, int haystack_len,
68 unsigned char const *needle,int needle_len)
70 unsigned char const * end_haystack = haystack + haystack_len - needle_len + 1;
71 unsigned char const * end_needle = needle + needle_len ;
73 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
74 is the spice of life */
75 while (haystack < end_haystack)
77 unsigned char const *subneedle = needle;
78 unsigned char const *subhaystack = haystack;
79 while (subneedle < end_needle)
80 if (*subneedle++ != *subhaystack++)
83 /* Completed the needle. Gotcha. */
84 return (unsigned char *) haystack;
92 memmem (void const *haystack, int haystack_len,
93 void const *needle,int needle_len)
95 unsigned char const* haystack_byte_c = (unsigned char const*)haystack;
96 unsigned char const* needle_byte_c = (unsigned char const*)needle;
97 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
103 memrchr (unsigned char const *p, int n, char c)
105 const unsigned char *q = p + n;
109 return (unsigned char*)q;
117 my_swap (T &t1, T &t2, T &tmp)
125 strrev (unsigned char *byte, int length)
127 unsigned char tmp_byte;
128 unsigned char *left = byte;
129 unsigned char *right = byte + length;
132 my_swap (*right--, *left++ , tmp_byte);
138 snprintf (char *str, size_t, char const *format, ...)
141 va_start (ap, format);
142 int i = vsprintf (str, format, ap);
150 vsnprintf (char *str, size_t, char const *format, va_list args)
152 int i = vsprintf (str, format, args);
161 #if ! HAVE_FOPENCOOKIE
165 fopencookie (void *cookie, char const *mode, cookie_io_functions_t fun)
169 return funopen (cookie, fun.read, fun.write, fun.seek, fun.close);
171 return funopen (cookie,
172 (int (*) (void *, char *, int)) fun.read,
173 (int (*) (void *, char const *, int)) fun.write,
174 (fpos_t (*) (void *, fpos_t, int)) fun.seek,
175 (int (*) (void *)) fun.close);
180 #else /* ! HAVE_FUNOPEN */
183 #include "memory-stream.hh"
186 is_memory_stream (void *foo)
188 Memory_out_stream* cookie = (Memory_out_stream*) foo;
189 return dynamic_cast<Memory_out_stream*> (cookie);
193 fopencookie (void *cookie, char const *modes, cookie_io_functions_t io_funcs)
198 if (is_memory_stream (cookie))
199 return (FILE*) cookie;
205 handle_cookie_io_fclose (FILE *file)
207 if (is_memory_stream (file))
208 return Memory_out_stream::cleaner (file);
209 return fclose (file);
213 handle_cookie_io_fprintf (FILE *file, char const *format, ...)
216 va_start (ap, format);
217 if (is_memory_stream (file))
219 static char buf[1024];
220 int i = vsnprintf (buf, sizeof (buf), format, ap);
223 return Memory_out_stream::writer (file, buf, i);
225 int i = vfprintf (file, format, ap);
231 handle_cookie_io_putc (int c, FILE *file)
233 if (is_memory_stream (file))
237 return Memory_out_stream::writer (file, buf, 1);
239 return putc (c, file);
242 #endif /* ! HAVE_FUNOPEN */
243 #endif /* ! HAVE_FOPENCOOKIE */