Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / flower / libc-extension.cc
blobcca8771e7348b92d0bdd614f2fdcd7d860622546
1 /*
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>
8 */
10 #include <cmath>
11 #include <cstdio>
12 #include <cstring>
13 #include <cctype>
14 #include <cassert>
16 using namespace std;
18 #include "libc-extension.hh"
20 char *
21 strnlwr (char *start, int n)
23 char *p = start + n;
24 while (--p >= start)
26 *p = tolower (*p); /* a macro on some compilers */
28 return start;
31 char *
32 strnupr (char *start, int n)
34 char *p = start + n;
35 while (--p >= start)
37 *p = toupper (*p); /* a macro on some compilers */
39 return start;
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 unsigned char *
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++)
65 goto next;
67 /* Completed the needle. Gotcha. */
68 return (unsigned char *) haystack;
69 next:
70 haystack++;
72 return 0;
75 void *
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);
84 #endif
86 unsigned char *
87 memrchr (unsigned char const *p, int n, char c)
89 const unsigned char *q = p + n;
90 while (q > p)
92 if (*--q == c)
93 return (unsigned char *)q;
95 return 0;
98 template<class T>
99 inline void
100 my_swap (T &t1, T &t2, T &tmp)
102 tmp = t1;
103 t1 = t2;
104 t2 = tmp;
107 unsigned char *
108 memrev (unsigned char *byte, int length)
110 unsigned char tmp_byte;
111 unsigned char *left = byte;
112 unsigned char *right = byte + length;
114 while (right > left)
115 my_swap (*right--, *left++, tmp_byte);
116 return byte;
120 There are some strange problems with round() on early glibcs.
122 double
123 my_round (double x)
125 return floor (x -0.5)+ 1.0;
128 /* namespace std { */
130 #ifndef isinf
131 #if !HAVE_ISINF
133 isinf (double x)
135 return x && (x == x/ 2);
137 #endif
138 #endif
140 #if ! HAVE_SNPRINTF
142 snprintf (char *str, size_t n, char const *format, ...)
144 va_list ap;
145 va_start (ap, format);
146 int i = vsprintf (str, format, ap);
147 if (i > 0 && (unsigned) i > n)
148 assert (false);
149 va_end (ap);
150 return i;
152 #endif
154 #if ! HAVE_VSNPRINTF
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)
160 assert (false);
161 return i;
163 #endif
165 /* } namespace std */