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>
16 #include "libc-extension.hh"
19 strnlwr (char *start, int n)
24 *p = tolower (*p); /* a macro on some compilers */
30 strnupr (char *start, int n)
35 *p = toupper (*p); /* a macro on some compilers */
42 /** locate a substring. #memmem# finds the first occurrence of
43 #needle# in #haystack#. This is not ANSI-C.
45 The prototype is not in accordance with the Linux Programmer's
46 Manual v1.15, but it is with /usr/include/string.h */
49 _memmem (unsigned char const *haystack, int haystack_len,
50 unsigned char const *needle, int needle_len)
52 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
53 unsigned char const *end_needle = needle + needle_len;
55 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
56 is the spice of life */
57 while (haystack < end_haystack)
59 unsigned char const *subneedle = needle;
60 unsigned char const *subhaystack = haystack;
61 while (subneedle < end_needle)
62 if (*subneedle++ != *subhaystack++)
65 /* Completed the needle. Gotcha. */
66 return (unsigned char *) haystack;
74 memmem (void const *haystack, int haystack_len,
75 void const *needle, int needle_len)
77 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
78 unsigned char const *needle_byte_c = (unsigned char const *)needle;
79 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
85 memrchr (unsigned char const *p, int n, char c)
87 const unsigned char *q = p + n;
91 return (unsigned char *)q;
98 my_swap (T &t1, T &t2, T &tmp)
106 memrev (unsigned char *byte, int length)
108 unsigned char tmp_byte;
109 unsigned char *left = byte;
110 unsigned char *right = byte + length;
113 my_swap (*right--, *left++, tmp_byte);
118 There are some strange problems with round() on early glibcs.
123 return floor (x -0.5)+ 1.0;
126 /* namespace std { */
133 return x && (x == x/ 2);
140 snprintf (char *str, size_t n, char const *format, ...)
143 va_start (ap, format);
144 int i = vsprintf (str, format, ap);
145 if (i > 0 && (unsigned) i > n)
154 vsnprintf (char *str, size_t n, char const *format, va_list args)
156 int i = vsprintf (str, format, args);
157 if (i > 0 && (unsigned) i > n)
163 /* } namespace std */