Merge commit 'origin' into release/unstable
[lilypond/mpolesky.git] / flower / libc-extension.cc
blob78cd6be16062210dbbf4fab0d58121bfc3b67190
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include <cmath>
22 #include <cstdio>
23 #include <cstring>
24 #include <cctype>
25 #include <cassert>
27 using namespace std;
29 #include "libc-extension.hh"
31 char *
32 strnlwr (char *start, int n)
34 char *p = start + n;
35 while (--p >= start)
37 *p = tolower (*p); /* a macro on some compilers */
39 return start;
42 char *
43 strnupr (char *start, int n)
45 char *p = start + n;
46 while (--p >= start)
48 *p = toupper (*p); /* a macro on some compilers */
50 return start;
53 #if !HAVE_MEMMEM
55 /** locate a substring. #memmem# finds the first occurrence of
56 #needle# in #haystack#. This is not ANSI-C.
58 The prototype is not in accordance with the Linux Programmer's
59 Manual v1.15, but it is with /usr/include/string.h */
61 unsigned char *
62 _memmem (unsigned char const *haystack, int haystack_len,
63 unsigned char const *needle, int needle_len)
65 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
66 unsigned char const *end_needle = needle + needle_len;
68 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
69 is the spice of life */
70 while (haystack < end_haystack)
72 unsigned char const *subneedle = needle;
73 unsigned char const *subhaystack = haystack;
74 while (subneedle < end_needle)
75 if (*subneedle++ != *subhaystack++)
76 goto next;
78 /* Completed the needle. Gotcha. */
79 return (unsigned char *) haystack;
80 next:
81 haystack++;
83 return 0;
86 void *
87 memmem (void const *haystack, int haystack_len,
88 void const *needle, int needle_len)
90 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
91 unsigned char const *needle_byte_c = (unsigned char const *)needle;
92 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
95 #endif
97 unsigned char *
98 memrchr (unsigned char const *p, int n, char c)
100 const unsigned char *q = p + n;
101 while (q > p)
103 if (*--q == c)
104 return (unsigned char *)q;
106 return 0;
109 template<class T>
110 inline void
111 my_swap (T &t1, T &t2, T &tmp)
113 tmp = t1;
114 t1 = t2;
115 t2 = tmp;
118 unsigned char *
119 memrev (unsigned char *byte, int length)
121 unsigned char tmp_byte;
122 unsigned char *left = byte;
123 unsigned char *right = byte + length;
125 while (right > left)
126 my_swap (*right--, *left++, tmp_byte);
127 return byte;
131 There are some strange problems with round() on early glibcs.
133 double
134 my_round (double x)
136 return floor (x -0.5)+ 1.0;
139 /* namespace std { */
141 #ifndef isinf
142 #if !HAVE_ISINF
144 isinf (double x)
146 return x && (x == x/ 2);
148 #endif
149 #endif
151 #if ! HAVE_SNPRINTF
153 snprintf (char *str, size_t n, char const *format, ...)
155 va_list ap;
156 va_start (ap, format);
157 int i = vsprintf (str, format, ap);
158 if (i > 0 && (unsigned) i > n)
159 assert (false);
160 va_end (ap);
161 return i;
163 #endif
165 #if ! HAVE_VSNPRINTF
167 vsnprintf (char *str, size_t n, char const *format, va_list args)
169 int i = vsprintf (str, format, args);
170 if (i > 0 && (unsigned) i > n)
171 assert (false);
172 return i;
174 #endif
176 /* } namespace std */