lilypond-1.1.44
[lilypond.git] / flower / libc-extension.cc
blob494850a455d595e9bf863e381a6121679217f7c3
1 /*
2 libc-extension.cc -- compensate for lacking libc functions.
5 source file of the flowerlib
7 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
9 */
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "libc-extension.hh"
17 urg: why soo wierd?
19 char*
20 strnlwr (char* start_l ,int n)
22 char * p = start_l + n;
23 while (--p >= start_l)
25 *p = tolower (*p); /* a macro on some compilers */
27 return start_l;
30 char*
31 strnupr (char* start_l, int n)
33 char * p = start_l + n;
34 while (--p >= start_l)
36 *p = toupper (*p); /* a macro on some compilers */
38 return start_l;
42 #if !HAVE_MEMMEM
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 */
50 Byte *
51 memmem (Byte const *haystack, int haystack_len,
52 Byte const *needle,int needle_len)
54 Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
55 Byte 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 Byte const *subneedle_l = needle;
62 Byte const *subhaystack_l = haystack;
63 while (subneedle_l < end_needle)
64 if (*subneedle_l++ != *subhaystack_l++)
65 goto next;
67 // completed the needle. Gotcha.
68 return (Byte *) haystack;
69 next:
70 haystack++;
72 return 0;
75 #endif
77 Byte *
78 memrchr (Byte const * p, int n, char c)
80 const Byte * q = p+n;
81 while (q > p)
83 if (*--q == c)
84 return (Byte*)q;
86 return 0;
90 template<class T>
91 inline void
92 my_swap (T &t1, T &t2, T &tmp)
94 tmp = t1;
95 t1 = t2;
96 t2 = tmp;
99 Byte*
100 strrev (Byte* byte_l, int length_i)
102 Byte tmp_byte;
104 Byte* left_l = byte_l;
105 Byte* right_l = byte_l + length_i;
107 while (right_l > left_l)
109 my_swap (*right_l-- , *left_l++ , tmp_byte);
111 return byte_l;
114 #if ! HAVE_SNPRINTF
115 int
116 snprintf (char *str, size_t, char const *format, ...)
118 va_list ap;
119 va_start (ap, format);
120 int i = vsprintf (str, format, ap);
121 va_end (ap);
122 return i;
124 #endif
126 #if ! HAVE_VSNPRINTF
127 int
128 vsnprintf (char *str, size_t, char const *format, va_list args)
130 int i = vsprintf (str, format, args);
131 return i;
133 #endif