lilypond-0.1.34
[lilypond.git] / flower / libc-extension.cc
blobb380b1fd2d90671ac2a7e1b5d731ad6a139e484f
1 /*
2 libc-extension.cc -- compensate for lacking libc functions.
5 source file of the flowerlib
7 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
8 */
9 #include <stdarg.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include "libc-extension.hh"
15 char*
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 */
23 return start_l;
26 char*
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 */
34 return start_l;
37 #if !HAVE_MEMMEM
39 /** locate a substring. #memmem# finds the first occurrence of
40 #needle# in #haystack#
43 char *
44 memmem (Byte const * haystack, int haystack_len,
45 Byte const *needle,int needle_len)
47 Byte const * end_haystack = haystack + haystack_len - needle_len;
48 Byte const * end_needle = needle + needle_len ;
50 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
51 is the spice of life */
52 while (haystack < end_haystack)
54 Byte const *subneedle_l = needle;
55 Byte const *subhaystack_l = haystack;
56 while (subneedle_l < end_needle)
58 if (*subneedle_l++ != *subhaystack_l++)
59 goto next; // yeah. I should be prosecuted.
62 // completed the needle. Gotcha.
63 return (char*) haystack;
64 next:
65 haystack++;
67 return 0;
69 #endif
71 Byte *
72 memrchr (Byte const * p, int n, char c)
74 const Byte * q = p+n;
75 while (q > p)
77 if (*--q == c)
78 return (Byte*)q;
80 return 0;
84 template<class T>
85 inline void
86 my_swap (T &t1, T &t2, T &tmp)
88 tmp = t1;
89 t1 = t2;
90 t2 = tmp;
93 Byte*
94 strrev (Byte* byte_l, int length_i)
96 Byte tmp_byte;
98 Byte* left_l = byte_l;
99 Byte* right_l = byte_l + length_i;
101 while (right_l > left_l)
103 my_swap (*right_l-- , *left_l++ , tmp_byte);
105 return byte_l;
108 #if ! HAVE_SNPRINTF
109 int snprintf (char *str, size_t,
110 char const *format, ...)
112 va_list ap;
113 va_start (ap, format);
114 int i = vsprintf (str, format, ap);
115 va_end (ap);
116 return i;
118 #endif