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