2 libc-extension.cc -- compensate for lacking libc functions.
4 source file of the flowerlib
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
18 #include "libc-extension.hh"
21 strnlwr (char *start, int n)
26 *p = tolower (*p); /* a macro on some compilers */
32 strnupr (char *start, int n)
37 *p = toupper (*p); /* a macro on some compilers */
44 /** locate a substring. #memmem# finds the first occurrence of
45 #needle# in #haystack#. This is not ANSI-C.
47 The prototype is not in accordance with the Linux Programmer's
48 Manual v1.15, but it is with /usr/include/string.h */
51 _memmem (unsigned char const *haystack, int haystack_len,
52 unsigned char const *needle, int needle_len)
54 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
55 unsigned char const *end_needle = needle + needle_len;
57 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
58 is the spice of life */
59 while (haystack < end_haystack)
61 unsigned char const *subneedle = needle;
62 unsigned char const *subhaystack = haystack;
63 while (subneedle < end_needle)
64 if (*subneedle++ != *subhaystack++)
67 /* Completed the needle. Gotcha. */
68 return (unsigned char *) haystack;
76 memmem (void const *haystack, int haystack_len,
77 void const *needle, int needle_len)
79 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
80 unsigned char const *needle_byte_c = (unsigned char const *)needle;
81 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
87 memrchr (unsigned char const *p, int n, char c)
89 const unsigned char *q = p + n;
93 return (unsigned char *)q;
100 my_swap (T &t1, T &t2, T &tmp)
108 memrev (unsigned char *byte, int length)
110 unsigned char tmp_byte;
111 unsigned char *left = byte;
112 unsigned char *right = byte + length;
115 my_swap (*right--, *left++, tmp_byte);
120 There are some strange problems with round() on early glibcs.
125 return floor (x -0.5)+ 1.0;
128 /* namespace std { */
135 return x && (x == x/ 2);
142 snprintf (char *str, size_t n, char const *format, ...)
145 va_start (ap, format);
146 int i = vsprintf (str, format, ap);
147 if (i > 0 && (unsigned) i > n)
156 vsnprintf (char *str, size_t n, char const *format, va_list args)
158 int i = vsprintf (str, format, args);
159 if (i > 0 && (unsigned) i > n)
165 /* } namespace std */