Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / cstrdup.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2007-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21
22 #ifndef __CSTRDUP_H__
23 #define __CSTRDUP_H__
24
25 #include <stdarg.h>
26 #include <stdint.h>
27 #include <string.h>
28
29 static inline char *cstrcat(int n, ...) {
30   int len = 0;  va_list va;  va_start(va,n);
31   for(int i=0; i<n; ++i) len += strlen(va_arg(va,char*));
32   va_end(va);  char *cp = new char[len+1], *bp = cp;  va_start(va,n);
33   for(int i=0; i<n; ++i) for(char*ap=va_arg(va,char*); *ap; *bp++=*ap++);
34   va_end(va);  *bp = 0;
35   return cp;
36 }
37 static inline char *cstrdup(const char *cp) {
38   return strcpy(new char[strlen(cp)+1],cp);
39 }
40
41 #ifndef lengthof
42 #define lengthof(ary) ((int)(sizeof(ary)/sizeof(ary[0])))
43 #endif
44
45 static inline int butf8(const char *&cp)
46 {
47         const unsigned char *bp = (const unsigned char *)cp;
48         int ret = *bp++;
49         if( ret >= 0x80 ) {
50                 int v = ret - 0xc0;
51                 static const int64_t sz = 0x5433222211111111;
52                 int n = v < 0 ? 0 : (sz >> (v&0x3c)) & 0x0f;
53                 for( int i=n; --i>=0; ret+=*bp++ ) ret <<= 6;
54                 static const uint32_t ofs[6] = {
55                         0x00000000U, 0x00003080U, 0x000E2080U,
56                         0x03C82080U, 0xFA082080U, 0x82082080U
57                 };
58                 ret -= ofs[n];
59         }
60         cp = (const char *)bp;
61         return ret;
62 }
63 static inline int butf8(unsigned int v, char *&cp)
64 {
65         unsigned char *bp = (unsigned char *)cp;
66         if( v >= 0x00000080 ) {
67                 int i = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
68                         v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
69                 int m = 0xff00 >> i;
70                 *bp++ = (v>>(6*--i)) | m;
71                 while( --i >= 0 ) *bp++ = ((v>>(6*i)) & 0x3f) | 0x80;
72         }
73         else
74                 *bp++ = v;
75         int ret = bp - (unsigned char *)cp;
76         cp = (char *)bp;
77         return ret;
78 }
79
80 static inline int bstrcasecmp(const char *ap, const char *bp)
81 { // not really correct, but what was left after MS port
82         int ret, a, b;
83         do {
84                 if( (a=butf8(ap)) >= 'A' && a <= 'Z' ) a += 'a' - 'A';
85                 if( (b=butf8(bp)) >= 'A' && b <= 'Z' ) b += 'a' - 'A';
86         } while( !(ret=a-b) && a && b );
87         return ret;
88 }
89
90 static inline const char *bstrcasestr(const char *src, const char *tgt)
91 {
92         int ssz = strlen(src), tsz = strlen(tgt);
93         const char *cp = tgt;
94         uint32_t wtgt[tsz+1], *tp = wtgt;
95         while( *cp ) {
96                 int wch = butf8(cp);
97                 if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
98                 *tp++ = wch;
99         }
100         *tp = 0;
101         for( tsz=tp-wtgt; ssz>=tsz; ++src,--ssz ) {
102                 cp = src;   tp = wtgt;
103                 int ret = 0, wch = 0;
104                 for( int i=tsz; --i>=0 && !ret && (wch=butf8(cp)); ) {
105                         if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
106                         ret = wch - *tp++;
107                 }
108                 if( !ret ) return src;
109         }
110         return 0;
111 }
112
113 #endif