2 libc-extension.cc -- compensate for lacking libc functions.
5 source file of the flowerlib
7 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
14 #include "libc-extension.hh"
17 strnlwr (char* start ,int n)
22 *p = tolower (*p); /* a macro on some compilers */
28 strnupr (char* start, int n)
33 *p = toupper (*p); /* a macro on some compilers */
44 return x && ( x == x/ 2) ;
51 /** locate a substring. #memmem# finds the first occurrence of
52 #needle# in #haystack#. This is not ANSI-C.
54 The prototype is not in accordance with the Linux Programmer's
55 Manual v1.15, but it is with /usr/include/string.h */
58 _memmem (Byte const *haystack, int haystack_len,
59 Byte const *needle,int needle_len)
61 Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
62 Byte const * end_needle = needle + needle_len ;
64 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
65 is the spice of life */
66 while (haystack < end_haystack)
68 Byte const *subneedle = needle;
69 Byte const *subhaystack = haystack;
70 while (subneedle < end_needle)
71 if (*subneedle++ != *subhaystack++)
74 /* Completed the needle. Gotcha. */
75 return (Byte *) haystack;
83 memmem (void const *haystack, int haystack_len,
84 void const *needle,int needle_len)
86 Byte const* haystack_byte_c = (Byte const*)haystack;
87 Byte const* needle_byte_c = (Byte const*)needle;
88 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
94 memrchr (Byte const * p, int n, char c)
108 my_swap (T &t1, T &t2, T &tmp)
116 strrev (Byte* byte, int length_i)
121 Byte* right = byte + length_i;
125 my_swap (*right-- , *left++ , tmp_byte);
132 snprintf (char *str, size_t, char const *format, ...)
135 va_start (ap, format);
136 int i = vsprintf (str, format, ap);
144 vsnprintf (char *str, size_t, char const *format, va_list args)
146 int i = vsprintf (str, format, args);