* lily/vaticana-ligature-engraver.cc: bugfix: fixed programming
[lilypond.git] / flower / libc-extension.cc
blobc10eca00af775afeb1a419cd75a98c61cab8c247
1 /*
2 libc-extension.cc -- compensate for lacking libc functions.
5 source file of the flowerlib
7 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
9 */
11 #include <math.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "libc-extension.hh"
18 char*
19 strnlwr (char* start ,int n)
21 char * p = start + n;
22 while (--p >= start)
24 *p = tolower (*p); /* a macro on some compilers */
26 return start;
29 char*
30 strnupr (char* start, int n)
32 char * p = start + n;
33 while (--p >= start)
35 *p = toupper (*p); /* a macro on some compilers */
37 return start;
41 There are some strange problems with round() on early glibcs.
43 double
44 my_round (double x)
46 return floor (x -0.5)+ 1.0 ;
49 #ifndef isinf
50 #if !HAVE_ISINF
51 int
52 isinf (double x)
54 return x && ( x == x/ 2) ;
56 #endif
57 #endif
59 #if !HAVE_MEMMEM
61 /** locate a substring. #memmem# finds the first occurrence of
62 #needle# in #haystack#. This is not ANSI-C.
64 The prototype is not in accordance with the Linux Programmer's
65 Manual v1.15, but it is with /usr/include/string.h */
67 Byte *
68 _memmem (Byte const *haystack, int haystack_len,
69 Byte const *needle,int needle_len)
71 Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
72 Byte const * end_needle = needle + needle_len ;
74 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
75 is the spice of life */
76 while (haystack < end_haystack)
78 Byte const *subneedle = needle;
79 Byte const *subhaystack = haystack;
80 while (subneedle < end_needle)
81 if (*subneedle++ != *subhaystack++)
82 goto next;
84 /* Completed the needle. Gotcha. */
85 return (Byte *) haystack;
86 next:
87 haystack++;
89 return 0;
92 void *
93 memmem (void const *haystack, int haystack_len,
94 void const *needle,int needle_len)
96 Byte const* haystack_byte_c = (Byte const*)haystack;
97 Byte const* needle_byte_c = (Byte const*)needle;
98 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
101 #endif
103 Byte *
104 memrchr (Byte const * p, int n, char c)
106 const Byte * q = p+n;
107 while (q > p)
109 if (*--q == c)
110 return (Byte*)q;
112 return 0;
116 template<class T>
117 inline void
118 my_swap (T &t1, T &t2, T &tmp)
120 tmp = t1;
121 t1 = t2;
122 t2 = tmp;
125 Byte*
126 strrev (Byte* byte, int length_i)
128 Byte tmp_byte;
130 Byte* left = byte;
131 Byte* right = byte + length_i;
133 while (right > left)
135 my_swap (*right-- , *left++ , tmp_byte);
137 return byte;
140 #if ! HAVE_SNPRINTF
141 int
142 snprintf (char *str, size_t, char const *format, ...)
144 va_list ap;
145 va_start (ap, format);
146 int i = vsprintf (str, format, ap);
147 va_end (ap);
148 return i;
150 #endif
152 #if ! HAVE_VSNPRINTF
153 int
154 vsnprintf (char *str, size_t, char const *format, va_list args)
156 int i = vsprintf (str, format, args);
157 return i;
159 #endif