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/>.
29 #include "libc-extension.hh"
32 strnlwr (char *start, int n)
37 *p = tolower (*p); /* a macro on some compilers */
43 strnupr (char *start, int n)
48 *p = toupper (*p); /* a macro on some compilers */
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 */
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++)
78 /* Completed the needle. Gotcha. */
79 return (unsigned char *) haystack;
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);
98 memrchr (unsigned char const *p, int n, char c)
100 const unsigned char *q = p + n;
104 return (unsigned char *)q;
111 my_swap (T &t1, T &t2, T &tmp)
119 memrev (unsigned char *byte, int length)
121 unsigned char tmp_byte;
122 unsigned char *left = byte;
123 unsigned char *right = byte + length;
126 my_swap (*right--, *left++, tmp_byte);
131 There are some strange problems with round() on early glibcs.
136 return floor (x -0.5)+ 1.0;
139 /* namespace std { */
146 return x && (x == x/ 2);
153 snprintf (char *str, size_t n, char const *format, ...)
156 va_start (ap, format);
157 int i = vsprintf (str, format, ap);
158 if (i > 0 && (unsigned) i > n)
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)
176 /* } namespace std */