]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
* flower/libc-extension.cc (lrint)[!HAVE_LRINT]: Use round () and
[lilypond.git] / flower / libc-extension.cc
1 /*
2   libc-extension.cc --  compensate for lacking libc functions.
3
4
5   source file of the flowerlib
6
7   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8          Jan Nieuwenhuizen <janneke@gnu.org>
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "libc-extension.hh"
15
16 char* 
17 strnlwr (char* start ,int n)
18 {
19   char * p = start + n;
20   while (--p >= start) 
21     {
22       *p = tolower (*p);    /* a macro on some compilers */
23     }
24   return start;
25 }
26
27 char* 
28 strnupr (char* start, int n)
29 {
30   char * p = start + n;
31   while (--p >= start) 
32     {
33       *p = toupper (*p);    /* a macro on some compilers */
34     }
35   return start;
36 }
37
38
39 #if ! HAVE_LRINT
40 #define lrint(__x) ((long) (round (__x)
41 #endif
42
43
44 #if !HAVE_ISINF
45 int
46 isinf (double x)
47 {
48   return x && ( x == x/ 2) ;
49 }
50 #endif
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 Byte *
62 _memmem (Byte const *haystack, int haystack_len,
63         Byte const *needle,int needle_len)
64 {
65   Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
66   Byte 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       Byte const *subneedle = needle;
73       Byte const *subhaystack = haystack;
74       while (subneedle < end_needle) 
75         if (*subneedle++ != *subhaystack++)
76           goto next;
77         
78       /* Completed the needle.  Gotcha.  */
79       return (Byte *) 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   Byte const* haystack_byte_c = (Byte const*)haystack;
91   Byte const* needle_byte_c = (Byte const*)needle;
92   return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
93 }
94
95 #endif
96
97 Byte *
98 memrchr (Byte const * p, int n, char c)
99 {
100   const    Byte * q = p+n;
101   while (q > p) 
102     {
103       if (*--q == c)
104         return (Byte*)q;
105     }
106   return 0;
107 }
108
109
110 template<class T>
111 inline void
112 my_swap (T &t1, T &t2, T &tmp)
113 {
114   tmp = t1;
115   t1 = t2;
116   t2 = tmp;
117 }
118
119 Byte*
120 strrev (Byte* byte, int length_i)
121 {
122   Byte tmp_byte;
123   
124   Byte* left = byte;
125   Byte* right = byte + length_i;
126
127   while (right > left) 
128     {
129       my_swap (*right-- , *left++ , tmp_byte);
130     }
131   return byte;
132 }
133
134 #if ! HAVE_SNPRINTF
135 int 
136 snprintf (char *str, size_t, char const *format, ...)
137 {
138   va_list ap;
139   va_start (ap, format);
140   int i = vsprintf (str, format, ap);
141   va_end (ap);
142   return i;
143 }
144 #endif
145
146 #if ! HAVE_VSNPRINTF
147 int 
148 vsnprintf (char *str, size_t, char const *format, va_list args)
149 {
150   int i = vsprintf (str, format, args);
151   return i;
152 }
153 #endif