]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
release: 1.3.49
[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--2000 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 /*
17   urg: why soo wierd?
18  */
19 char* 
20 strnlwr (char* start_l ,int n)
21 {
22   char * p = start_l + n;
23   while (--p >= start_l) 
24     {
25       *p = tolower (*p);    /* a macro on some compilers */
26     }
27   return start_l;
28 }
29
30 char* 
31 strnupr (char* start_l, int n)
32 {
33   char * p = start_l + n;
34   while (--p >= start_l) 
35     {
36       *p = toupper (*p);    /* a macro on some compilers */
37     }
38   return start_l;
39 }
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 Byte *
51 _memmem (Byte const *haystack, int haystack_len,
52         Byte const *needle,int needle_len)
53 {
54   Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
55   Byte 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       Byte const *subneedle_l = needle;
62       Byte const *subhaystack_l = haystack;
63       while (subneedle_l < end_needle) 
64         if (*subneedle_l++ != *subhaystack_l++)
65           goto next;
66         
67       // completed the needle. Gotcha.
68       return (Byte *) 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   Byte const* haystack_byte_c_l = (Byte const*)haystack;
80   Byte const* needle_byte_c_l = (Byte const*)needle;
81   return _memmem (haystack_byte_c_l, haystack_len, needle_byte_c_l, needle_len);
82 }
83
84 #endif
85
86 Byte *
87 memrchr (Byte const * p, int n, char c)
88 {
89   const    Byte * q = p+n;
90   while (q > p) 
91     {
92       if (*--q == c)
93         return (Byte*)q;
94     }
95   return 0;
96 }
97
98
99 template<class T>
100 inline void
101 my_swap (T &t1, T &t2, T &tmp)
102 {
103   tmp = t1;
104   t1 = t2;
105   t2 = tmp;
106 }
107
108 Byte*
109 strrev (Byte* byte_l, int length_i)
110 {
111   Byte tmp_byte;
112   
113   Byte* left_l = byte_l;
114   Byte* right_l = byte_l + length_i;
115
116   while (right_l > left_l) 
117     {
118       my_swap (*right_l-- , *left_l++ , tmp_byte);
119     }
120   return byte_l;
121 }
122
123 #if ! HAVE_SNPRINTF
124 int 
125 snprintf (char *str, size_t, char const *format, ...)
126 {
127   va_list ap;
128   va_start (ap, format);
129   int i = vsprintf (str, format, ap);
130   va_end (ap);
131   return i;
132 }
133 #endif
134
135 #if ! HAVE_VSNPRINTF
136 int 
137 vsnprintf (char *str, size_t, char const *format, va_list args)
138 {
139   int i = vsprintf (str, format, args);
140   return i;
141 }
142 #endif
143
144
145 #if !HAVE_ISINF
146 int
147 isinf (double x)
148 {
149   return x && ( x == x/ 2) ;
150 }
151
152 #endif