#ifndef _KVI_WSTRING_FUNCTIONS_H_
#define _KVI_WSTRING_FUNCTIONS_H_

//=============================================================================
//
//   File : kvi_wstring_functions.h
//   Creation date : Wed 04 Dec 2002 19:58:00 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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.
//
//=============================================================================

#include "kvi_wstring_config.h"
#include "kvi_wstring_wchar.h"
#include "kvi_wstring_wslen.h"

#ifdef _KVI_WSTRING_CPP_
	#define _WSTRING_FUNCTIONS_EXTERN
#else // !_KVI_WSTRING_CPP_
	#define _WSTRING_FUNCTIONS_EXTERN extern
#endif // !_KVI_WSTRING_CPP_ 

#include <stdarg.h> // for va_list & co.

#ifdef WSTRINGCONFIG_USE_QT
	#include <qstring.h>
	#include <qglobal.h>

	_WSTRING_FUNCTIONS_EXTERN _WSTRING_API QString kvi_wchararray_to_qstring_with_length(const kvi_wchar_t * ptr,kvi_wslen_t len);
	_WSTRING_FUNCTIONS_EXTERN _WSTRING_API void kvi_append_wchararray_to_qstring_with_length(QString * qstrptr,const kvi_wchar_t * ptr,kvi_wslen_t len);

	#if QT_VERSION >= 0x030100
		#define kvi_wcharToQString(_wcharptr) QString::fromUcs2(_wcharptr)
	#else // QT_VERSION < 0x030100
		_WSTRING_FUNCTIONS_EXTERN _WSTRING_API QString kvi_wchararray_to_qstring(const kvi_wchar_t * ptr);
		#define kvi_wcharToQString(_wcharptr) kvi_wchararray_to_qstring(_wcharptr)
	#endif // QT_VERSION < 0x030100

	#define kvi_wcharToQStringWithLength(_wcharptr,_wslen) kvi_wchararray_to_qstring_with_length(_wcharptr,_wslen)
	#define kvi_appendWCharToQStringWithLength(_qstrptr,_wcharptr,_wslen) kvi_append_wchararray_to_qstring_with_length(_qstrptr,_wcharptr,_wslen)


#endif // WSTRINGCONFIG_USE_QT

#ifdef WSTRINGCONFIG_USE_GCC_ix86_ASM
	extern inline kvi_wslen_t kvi_wstrlen(const kvi_wchar_t * str)
	{
		register kvi_wslen_t ecx;
		__asm__ __volatile__(
			"	cld\n"
			"	repne\n"
			"	scasw\n"
			"	notl %0\n"
			"	decl %0"
			: "=c" (ecx),        "=&D" (str)
			: "0"  (0xffffffff), "1"   (str), "a" (0)
		);
		return ecx;
	}
#else // !WSTRINGCONFIG_USE_GCC_ix86_ASM
	_WSTRING_FUNCTIONS_EXTERN _WSTRING_API kvi_wslen_t kvi_wstrlen(const kvi_wchar_t * str);
#endif // !WSTRINGCONFIG_USE_GCC_ix86_ASM


_WSTRING_FUNCTIONS_EXTERN _WSTRING_API bool kvi_wstrEqualCI(const kvi_wchar_t *str1,const kvi_wchar_t *str2);
// Returns true if the forst len characters of string str1 are equal to str2.
// case insensitive.
// Note that if str1 or str2 are shorter than len characters then are considered as NOT equal!
_WSTRING_FUNCTIONS_EXTERN _WSTRING_API bool kvi_wstrEqualCIN(const kvi_wchar_t *str1,const kvi_wchar_t *str2,int len);

// Hybrid versions

_WSTRING_FUNCTIONS_EXTERN _WSTRING_API bool kvi_hstrEqualCI(const kvi_wchar_t *str1,const char *str2);
// Returns true if the forst len characters of string str1 are equal to str2.
// case insensitive.
// Note that if str1 or str2 are shorter than len characters then are considered as NOT equal!
_WSTRING_FUNCTIONS_EXTERN _WSTRING_API bool kvi_hstrEqualCIN(const kvi_wchar_t *str1,const char *str2,int len);

_WSTRING_FUNCTIONS_EXTERN _WSTRING_API int kvi_wvsnprintcf(kvi_wchar_t * buffer,kvi_wslen_t len,const char *fmt,va_list list);
_WSTRING_FUNCTIONS_EXTERN _WSTRING_API int kvi_wvsnprintf(kvi_wchar_t * buffer,kvi_wslen_t len,const kvi_wchar_t *fmt,va_list list);

#endif // !_KVI_WSTRING_FUNCTIONS_H_
