]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
release: 0.1.43
[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 *haystack, int haystack_len,
47         Byte const *needle,int needle_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         if (*subneedle_l++ != *subhaystack_l++)
60           goto next;
61         
62       // completed the needle. Gotcha.
63       return (Byte *) haystack;
64       next:
65         haystack++;
66     }
67   return 0;
68 }
69
70 #endif
71
72 Byte *
73 memrchr (Byte const * p, int n, char c)
74 {
75   const    Byte * q = p+n;
76   while (q > p) 
77     {
78       if (*--q == c)
79         return (Byte*)q;
80     }
81   return 0;
82 }
83
84
85 template<class T>
86 inline void
87 my_swap (T &t1, T &t2, T &tmp)
88 {
89   tmp = t1;
90   t1 = t2;
91   t2 = tmp;
92 }
93
94 Byte*
95 strrev (Byte* byte_l, int length_i)
96 {
97   Byte tmp_byte;
98   
99   Byte* left_l = byte_l;
100   Byte* right_l = byte_l + length_i;
101
102   while (right_l > left_l) 
103     {
104       my_swap (*right_l-- , *left_l++ , tmp_byte);
105     }
106   return byte_l;
107 }
108
109 #if ! HAVE_SNPRINTF
110 int snprintf (char *str, size_t,
111               char const *format, ...)
112 {
113   va_list ap;
114   va_start (ap, format);
115   int i = vsprintf (str, format, ap);
116   va_end (ap);
117   return i;
118 }
119 #endif