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