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