]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
* Documentation/user/internals.itely (Manipulating music
[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
40 #if !HAVE_ISINF
41 int
42 isinf (double x)
43 {
44   return x && ( x == x/ 2) ;
45 }
46 #endif
47
48
49 #if !HAVE_MEMMEM
50
51 /** locate a substring. #memmem# finds the first occurrence of
52   #needle# in #haystack#.  This is not ANSI-C.
53
54   The prototype is not in accordance with the Linux Programmer's
55   Manual v1.15, but it is with /usr/include/string.h   */
56
57 Byte *
58 _memmem (Byte const *haystack, int haystack_len,
59         Byte const *needle,int needle_len)
60 {
61   Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
62   Byte const * end_needle = needle + needle_len ;
63
64   /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
65      is the spice of life */
66   while (haystack < end_haystack) 
67     {
68       Byte const *subneedle = needle;
69       Byte const *subhaystack = haystack;
70       while (subneedle < end_needle) 
71         if (*subneedle++ != *subhaystack++)
72           goto next;
73         
74       /* Completed the needle.  Gotcha.  */
75       return (Byte *) haystack;
76       next:
77         haystack++;
78     }
79   return 0;
80 }
81
82 void *
83 memmem (void const *haystack, int haystack_len,
84         void const *needle,int needle_len)
85 {
86   Byte const* haystack_byte_c = (Byte const*)haystack;
87   Byte const* needle_byte_c = (Byte const*)needle;
88   return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
89 }
90
91 #endif
92
93 Byte *
94 memrchr (Byte const * p, int n, char c)
95 {
96   const    Byte * q = p+n;
97   while (q > p) 
98     {
99       if (*--q == c)
100         return (Byte*)q;
101     }
102   return 0;
103 }
104
105
106 template<class T>
107 inline void
108 my_swap (T &t1, T &t2, T &tmp)
109 {
110   tmp = t1;
111   t1 = t2;
112   t2 = tmp;
113 }
114
115 Byte*
116 strrev (Byte* byte, int length_i)
117 {
118   Byte tmp_byte;
119   
120   Byte* left = byte;
121   Byte* right = byte + length_i;
122
123   while (right > left) 
124     {
125       my_swap (*right-- , *left++ , tmp_byte);
126     }
127   return byte;
128 }
129
130 #if ! HAVE_SNPRINTF
131 int 
132 snprintf (char *str, size_t, char const *format, ...)
133 {
134   va_list ap;
135   va_start (ap, format);
136   int i = vsprintf (str, format, ap);
137   va_end (ap);
138   return i;
139 }
140 #endif
141
142 #if ! HAVE_VSNPRINTF
143 int 
144 vsnprintf (char *str, size_t, char const *format, va_list args)
145 {
146   int i = vsprintf (str, format, args);
147   return i;
148 }
149 #endif