2 libc-extension.cc -- compensate for lacking libc functions.
5 source file of the flowerlib
7 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
12 #include "libc-extension.hh"
16 strnlwr (char* start_l ,int n)
18 char * p = start_l + n;
19 while (--p >= start_l)
21 *p = tolower (*p); /* a macro on some compilers */
27 strnupr (char* start_l, int n)
29 char * p = start_l + n;
30 while (--p >= start_l)
32 *p = toupper (*p); /* a macro on some compilers */
39 /** locate a substring. #memmem# finds the first occurrence of
40 #needle# in #haystack#. This is not ANSI-C.
42 The prototype is not in accordance with the Linux Programmer's
43 Manual v1.15, but it is with /usr/include/string.h */
46 memmem (Byte const *needle,int needle_len,
47 Byte const *haystack, int haystack_len)
49 Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
50 Byte const * end_needle = needle + needle_len ;
52 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
53 is the spice of life */
54 while (haystack < end_haystack)
56 Byte const *subneedle_l = needle;
57 Byte const *subhaystack_l = haystack;
58 while (subneedle_l < end_needle)
60 if (*subneedle_l++ != *subhaystack_l++)
67 // completed the needle. Gotcha.
68 return (Byte *) haystack;
76 memrchr (Byte const * p, int n, char c)
90 my_swap (T &t1, T &t2, T &tmp)
98 strrev (Byte* byte_l, int length_i)
102 Byte* left_l = byte_l;
103 Byte* right_l = byte_l + length_i;
105 while (right_l > left_l)
107 my_swap (*right_l-- , *left_l++ , tmp_byte);
113 int snprintf (char *str, size_t,
114 char const *format, ...)
117 va_start (ap, format);
118 int i = vsprintf (str, format, ap);