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