/*
 * CINELERRA
 * Copyright (C) 2007-2020 William Morrow
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


#ifndef __CSTRDUP_H__
#define __CSTRDUP_H__

#include <stdarg.h>
#include <stdint.h>
#include <string.h>

static inline char *cstrcat(int n, ...) {
  int len = 0;  va_list va;  va_start(va,n);
  for(int i=0; i<n; ++i) len += strlen(va_arg(va,char*));
  va_end(va);  char *cp = new char[len+1], *bp = cp;  va_start(va,n);
  for(int i=0; i<n; ++i) for(char*ap=va_arg(va,char*); *ap; *bp++=*ap++);
  va_end(va);  *bp = 0;
  return cp;
}
static inline char *cstrdup(const char *cp) {
  return strcpy(new char[strlen(cp)+1],cp);
}

#ifndef lengthof
#define lengthof(ary) ((int)(sizeof(ary)/sizeof(ary[0])))
#endif

static inline int butf8(const char *&cp)
{
	const unsigned char *bp = (const unsigned char *)cp;
	int ret = *bp++;
	if( ret >= 0x80 ) {
		int v = ret - 0xc0;
		static const int64_t sz = 0x5433222211111111;
		int n = v < 0 ? 0 : (sz >> (v&0x3c)) & 0x0f;
		for( int i=n; --i>=0; ret+=*bp++ ) ret <<= 6;
		static const uint32_t ofs[6] = {
			0x00000000U, 0x00003080U, 0x000E2080U,
			0x03C82080U, 0xFA082080U, 0x82082080U
		};
		ret -= ofs[n];
	}
	cp = (const char *)bp;
	return ret;
}
static inline int butf8(unsigned int v, char *&cp)
{
	unsigned char *bp = (unsigned char *)cp;
	if( v >= 0x00000080 ) {
		int i = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
			v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
		int m = 0xff00 >> i;
		*bp++ = (v>>(6*--i)) | m;
		while( --i >= 0 ) *bp++ = ((v>>(6*i)) & 0x3f) | 0x80;
	}
	else
		*bp++ = v;
	int ret = bp - (unsigned char *)cp;
	cp = (char *)bp;
	return ret;
}

static inline int bstrcasecmp(const char *ap, const char *bp)
{ // not really correct, but what was left after MS port
	int ret, a, b;
	do {
		if( (a=butf8(ap)) >= 'A' && a <= 'Z' ) a += 'a' - 'A';
		if( (b=butf8(bp)) >= 'A' && b <= 'Z' ) b += 'a' - 'A';
	} while( !(ret=a-b) && a && b );
	return ret;
}

static inline const char *bstrcasestr(const char *src, const char *tgt)
{
	int ssz = strlen(src), tsz = strlen(tgt);
	const char *cp = tgt;
	uint32_t wtgt[tsz+1], *tp = wtgt;
	while( *cp ) {
		int wch = butf8(cp);
		if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
		*tp++ = wch;
	}
	*tp = 0;
	for( tsz=tp-wtgt; ssz>=tsz; ++src,--ssz ) {
		cp = src;   tp = wtgt;
		int ret = 0, wch = 0;
		for( int i=tsz; --i>=0 && !ret && (wch=butf8(cp)); ) {
			if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
			ret = wch - *tp++;
		}
		if( !ret ) return src;
	}
	return 0;
}

#endif
