]> git.donarmstrong.com Git - lilypond.git/blob - flower/libc-extension.cc
release: 0.0.40
[lilypond.git] / flower / libc-extension.cc
1 /*
2   libc-extension.cc -- implement some string.h extensions
3
4   source file of the flowerlib
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include <stdarg.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include "libc-extension.hh"
12
13 /*
14   compensate for lacking libc functions.
15  */
16 char* 
17 strnlwr( char* start_l ,int n)
18 {
19     char * p = start_l + n;
20     while ( --p >= start_l) {
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         *p = toupper( *p );    /* a macro on some compilers */
32     }
33     return start_l;
34 }
35
36 #ifndef HAVE_MEMMEM
37
38 /** locate a substring. #memmem# finds the first occurrence of
39   #needle# in #haystack#
40   */
41
42 char *
43 memmem(const Byte * haystack, int haystack_len,
44        const Byte *needle,int needle_len)
45 {
46     const Byte * end_haystack = haystack + haystack_len - needle_len;
47     const Byte * end_needle = needle + needle_len ;
48
49     /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
50       is the spice of life */
51     while (haystack < end_haystack) {
52         const Byte *subneedle_l = needle;
53         const Byte *subhaystack_l = haystack;
54         while (subneedle_l < end_needle) {
55             if (*subneedle_l++ != *subhaystack_l++)
56                 goto next;      // yeah. I should be prosecuted.
57         }
58         
59         // completed the needle. Gotcha.
60         return (char*) haystack;
61     next:
62         haystack++;
63     }
64     return 0;
65 }
66 #endif
67
68 Byte *
69 memrchr(const Byte * p, int n, char c)
70 {
71     const    Byte * q = p+n;
72     while (q > p) {
73         if (*--q == c)
74             return (Byte*)q;
75     }
76     return 0;
77 }
78
79
80 template<class T>
81 inline void
82 my_swap(T &t1, T &t2, T &tmp)
83 {
84     tmp = t1;
85     t1 = t2;
86     t2 = tmp;
87 }
88
89 Byte*
90 strrev( Byte* byte_l, int length_i )
91 {
92   Byte tmp_byte;
93   
94   Byte* left_l = byte_l;
95   Byte* right_l = byte_l + length_i;
96
97   while ( right_l > left_l ) {
98       my_swap(*right_l-- , *left_l++ , tmp_byte);
99   }
100   return byte_l;
101 }
102
103 #ifndef HAVE_SNPRINTF
104 int snprintf ( char *str, size_t n,
105                const char *format, ... )
106 {
107     va_list ap;
108     va_start(ap, format);
109     int i = vsprintf(str, format, ap);
110     va_end(ap);
111     return i;
112 }
113 #endif