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