]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / flower / libc-extension.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
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.
11
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.
16
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/>.
19 */
20
21 #include <cmath>
22 #include <cstdio>
23 #include <cstring>
24 #include <cctype>
25 #include <cassert>
26
27
28 #include "libc-extension.hh"
29
30 char *
31 strnlwr (char *start, int n)
32 {
33   char *p = start + n;
34   while (--p >= start)
35     {
36       *p = (char)tolower (*p);    /* a macro on some compilers */
37     }
38   return start;
39 }
40
41 char *
42 strnupr (char *start, int n)
43 {
44   char *p = start + n;
45   while (--p >= start)
46     {
47       *p = (char)toupper (*p);    /* a macro on some compilers */
48     }
49   return start;
50 }
51
52 #if !HAVE_MEMMEM
53
54 /** locate a substring. #memmem# finds the first occurrence of
55     #needle# in #haystack#.  This is not ANSI-C.
56
57     The prototype is not in accordance with the Linux Programmer's
58     Manual v1.15, but it is with /usr/include/string.h   */
59
60 unsigned char *
61 _memmem (unsigned char const *haystack, int haystack_len,
62          unsigned char const *needle, int needle_len)
63 {
64   unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
65   unsigned char const *end_needle = needle + needle_len;
66
67   /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
68      is the spice of life */
69   while (haystack < end_haystack)
70     {
71       unsigned char const *subneedle = needle;
72       unsigned char const *subhaystack = haystack;
73       while (subneedle < end_needle)
74         if (*subneedle++ != *subhaystack++)
75           goto next;
76
77       /* Completed the needle.  Gotcha.  */
78       return (unsigned char *) haystack;
79 next:
80       haystack++;
81     }
82   return 0;
83 }
84
85 void *
86 memmem (void const *haystack, int haystack_len,
87         void const *needle, int needle_len)
88 {
89   unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
90   unsigned char const *needle_byte_c = (unsigned char const *)needle;
91   return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
92 }
93
94 #endif
95
96 unsigned char *
97 memrchr (unsigned char const *p, int n, char c)
98 {
99   const unsigned char *q = p + n;
100   while (q > p)
101     {
102       if (*--q == c)
103         return (unsigned char *)q;
104     }
105   return 0;
106 }
107
108 template<class T>
109 inline void
110 my_swap (T &t1, T &t2, T &tmp)
111 {
112   tmp = t1;
113   t1 = t2;
114   t2 = tmp;
115 }
116
117 unsigned char *
118 memrev (unsigned char *byte, int length)
119 {
120   unsigned char tmp_byte;
121   unsigned char *left = byte;
122   unsigned char *right = byte + length;
123
124   while (right > left)
125     my_swap (*right--, *left++, tmp_byte);
126   return byte;
127 }
128
129 /*
130   There are some strange problems with round() on early glibcs.
131 */
132 double
133 my_round (double x)
134 {
135   return floor (x - 0.5) + 1.0;
136 }
137
138 /* namespace std { */
139
140 #ifndef isinf
141 #if !HAVE_ISINF
142 int
143 isinf (double x)
144 {
145   return x && (x == x / 2);
146 }
147 #endif
148 #endif
149
150 #if ! HAVE_SNPRINTF
151 int
152 snprintf (char *str, size_t n, char const *format, ...)
153 {
154   va_list ap;
155   va_start (ap, format);
156   int i = vsprintf (str, format, ap);
157   if (i > 0 && (unsigned) i > n)
158     assert (false);
159   va_end (ap);
160   return i;
161 }
162 #endif
163
164 #if ! HAVE_VSNPRINTF
165 int
166 vsnprintf (char *str, size_t n, char const *format, va_list args)
167 {
168   int i = vsprintf (str, format, args);
169   if (i > 0 && (unsigned) i > n)
170     assert (false);
171   return i;
172 }
173 #endif
174
175 /* } namespace std */