lilypond-1.3.100
[lilypond.git] / flower / lib / libc-extension.cc
blob3a89e81008bf5fc5500899355f1bfdeccb2b2700
1 /*
2 libc-extension.cc -- implement some string.h extensions
4 source file of the flowerlib
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"
14 compensate for lacking libc functions.
16 char*
17 strnlwr( char* start_l ,int n)
19 char * p = start_l + n;
20 while ( --p >= start_l) {
21 *p = tolower( *p ); /* a macro on some compilers */
23 return start_l;
26 char*
27 strnupr( char* start_l, int n)
29 char * p = start_l + n;
30 while ( --p >= start_l) {
31 *p = toupper( *p ); /* a macro on some compilers */
33 return start_l;
36 #ifndef HAVE_MEMMEM
38 /** locate a substring. #memmem# finds the first occurrence of
39 #needle# in #haystack#
42 char *
43 memmem(const Byte * haystack, int haystack_len,
44 const Byte *needle,int needle_len)
46 const Byte * end_haystack = haystack + haystack_len - needle_len;
47 const Byte * end_needle = needle + needle_len ;
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.
59 // completed the needle. Gotcha.
60 return (char*) haystack;
61 next:
62 haystack++;
64 return 0;
66 #endif
68 Byte *
69 memrchr(const Byte * p, int n, char c)
71 const Byte * q = p+n;
72 while (q > p) {
73 if (*--q == c)
74 return (Byte*)q;
76 return 0;
80 template<class T>
81 inline void
82 my_swap(T &t1, T &t2, T &tmp)
84 tmp = t1;
85 t1 = t2;
86 t2 = tmp;
89 Byte*
90 strrev( Byte* byte_l, int length_i )
92 Byte tmp_byte;
94 Byte* left_l = byte_l;
95 Byte* right_l = byte_l + length_i;
97 while ( right_l > left_l ) {
98 my_swap(*right_l-- , *left_l++ , tmp_byte);
100 return byte_l;
103 #ifndef HAVE_SNPRINTF
104 int snprintf ( char *str, size_t,
105 const char *format, ... )
107 va_list ap;
108 va_start(ap, format);
109 int i = vsprintf(str, format, ap);
110 va_end(ap);
111 return i;
113 #endif