]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
release: 0.1.41
[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 Han-Wen Nienhuys <hanwen@stack.nl>
8 */
9 #include <stdarg.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include "libc-extension.hh"
13
14
15 char* 
16 strnlwr (char* start_l ,int n)
17 {
18   char * p = start_l + n;
19   while (--p >= start_l) 
20     {
21       *p = tolower (*p);    /* a macro on some compilers */
22     }
23   return start_l;
24 }
25
26 char* 
27 strnupr (char* start_l, int n)
28 {
29   char * p = start_l + n;
30   while (--p >= start_l) 
31     {
32       *p = toupper (*p);    /* a macro on some compilers */
33     }
34   return start_l;
35 }
36
37 #if !HAVE_MEMMEM
38
39 /** locate a substring. #memmem# finds the first occurrence of
40   #needle# in #haystack#.  This is not ANSI-C.
41
42   The prototype is not in accordance with the Linux Programmer's
43   Manual v1.15, but it is with /usr/include/string.h   */
44
45 Byte *
46 memmem (Byte const *needle,int needle_len,
47         Byte const *haystack, int haystack_len)
48 {
49   Byte const * end_haystack = haystack + haystack_len - needle_len + 1;
50   Byte const * end_needle = needle + needle_len ;
51
52   /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
53      is the spice of life */
54   while (haystack < end_haystack) 
55     {
56       Byte const *subneedle_l = needle;
57       Byte const *subhaystack_l = haystack;
58       while (subneedle_l < end_needle) 
59         {
60           if (*subneedle_l++ != *subhaystack_l++)
61             {
62               haystack ++;
63               continue;
64             }
65         }
66         
67       // completed the needle. Gotcha.
68       return (Byte *) haystack;
69     }
70   return 0;
71 }
72
73 #endif
74
75 Byte *
76 memrchr (Byte const * p, int n, char c)
77 {
78   const    Byte * q = p+n;
79   while (q > p) 
80     {
81       if (*--q == c)
82         return (Byte*)q;
83     }
84   return 0;
85 }
86
87
88 template<class T>
89 inline void
90 my_swap (T &t1, T &t2, T &tmp)
91 {
92   tmp = t1;
93   t1 = t2;
94   t2 = tmp;
95 }
96
97 Byte*
98 strrev (Byte* byte_l, int length_i)
99 {
100   Byte tmp_byte;
101   
102   Byte* left_l = byte_l;
103   Byte* right_l = byte_l + length_i;
104
105   while (right_l > left_l) 
106     {
107       my_swap (*right_l-- , *left_l++ , tmp_byte);
108     }
109   return byte_l;
110 }
111
112 #if ! HAVE_SNPRINTF
113 int snprintf (char *str, size_t,
114               char const *format, ...)
115 {
116   va_list ap;
117   va_start (ap, format);
118   int i = vsprintf (str, format, ap);
119   va_end (ap);
120   return i;
121 }
122 #endif