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