(svn r10389) -Add: CStrA, CStrCiA, CStrW, CStrCiW - case sensitive/insensitive ANSI/UNICODE string classes that support formatted (sprintf like) output.
authorKUDr
Fri, 29 Jun 2007 22:33:58 +0000
changeset 7612 3b444f6a973a
parent 7611 222dfb0b8df8
child 7613 0940d162ab76
(svn r10389) -Add: CStrA, CStrCiA, CStrW, CStrCiW - case sensitive/insensitive ANSI/UNICODE string classes that support formatted (sprintf like) output.
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/misc/str.hpp
src/misc/strapi.hpp
--- a/projects/openttd.vcproj	Fri Jun 29 22:16:15 2007 +0000
+++ b/projects/openttd.vcproj	Fri Jun 29 22:33:58 2007 +0000
@@ -1187,6 +1187,12 @@
 			<File
 				RelativePath=".\..\src\misc\hashtable.hpp">
 			</File>
+			<File
+				RelativePath=".\..\src\misc\str.hpp">
+			</File>
+			<File
+				RelativePath=".\..\src\misc\strapi.hpp">
+			</File>
 		</Filter>
 		<Filter
 			Name="Network Core"
--- a/projects/openttd_vs80.vcproj	Fri Jun 29 22:16:15 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Fri Jun 29 22:33:58 2007 +0000
@@ -1799,6 +1799,14 @@
 				RelativePath=".\..\src\misc\hashtable.hpp"
 				>
 			</File>
+			<File
+				RelativePath=".\..\src\misc\str.hpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\misc\strapi.hpp"
+				>
+			</File>
 		</Filter>
 		<Filter
 			Name="Network Core"
--- a/source.list	Fri Jun 29 22:16:15 2007 +0000
+++ b/source.list	Fri Jun 29 22:33:58 2007 +0000
@@ -371,6 +371,8 @@
 misc/crc32.hpp
 misc/fixedsizearray.hpp
 misc/hashtable.hpp
+misc/str.hpp
+misc/strapi.hpp
 
 # Network Core
 network/core/config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/misc/str.hpp	Fri Jun 29 22:33:58 2007 +0000
@@ -0,0 +1,175 @@
+/* $Id$ */
+
+/** @file str.hpp */
+
+#ifndef  STR_HPP
+#define  STR_HPP
+
+#include <errno.h>
+#include <stdarg.h>
+#include "strapi.hpp"
+
+/** Blob based string. */
+template <typename Tchar, bool TcaseInsensitive>
+struct CStrT : public CBlobT<Tchar>
+{
+	typedef CBlobT<Tchar> base;                    ///< base class
+	typedef CStrApiT<Tchar, TcaseInsensitive> Api; ///< string API abstraction layer
+	typedef typename base::bsize_t bsize_t;        ///< size type inherited from blob
+	typedef typename base::OnTransfer OnTransfer;  ///< temporary 'transfer ownership' object type
+
+	/** Construction from C zero ended string. */
+	FORCEINLINE CStrT(const Tchar* str = NULL)
+	{
+		AppendStr(str);
+	}
+
+	/** Construction from C string and given number of characters. */
+	FORCEINLINE CStrT(const Tchar* str, bsize_t num_chars) : base(str, num_chars)
+	{
+		base::FixTail();
+	}
+
+	/** Construction from C string determined by 'begin' and 'end' pointers. */
+	FORCEINLINE CStrT(const Tchar* str, const Tchar* end)
+		: base(str, end - str)
+	{
+		base::FixTail();
+	}
+
+	/** Construction from blob contents. */
+	FORCEINLINE CStrT(const CBlobBaseSimple& src)
+		: base(src)
+	{
+		base::FixTail();
+	}
+
+	/** Copy constructor. */
+	FORCEINLINE CStrT(const CStrT& src)
+		: base(src)
+	{
+		base::FixTail();
+	}
+
+	/** Take over ownership constructor */
+	FORCEINLINE CStrT(const OnTransfer& ot)
+		: base(ot)
+	{
+	}
+
+	/** Grow the actual buffer and fix the trailing zero at the end. */
+	FORCEINLINE Tchar* GrowSizeNC(bsize_t count)
+	{
+		Tchar* ret = base::GrowSizeNC(count);
+		base::FixTail();
+		return ret;
+	}
+
+	/** Append zero-ended C string. */
+	FORCEINLINE void AppendStr(const Tchar* str)
+	{
+		if (str != NULL && str[0] != '\0') {
+			base::Append(str, (bsize_t)Api::StrLen(str));
+			base::FixTail();
+		}
+	}
+
+	/** Append another CStrT or blob. */
+	FORCEINLINE void Append(const CBlobBaseSimple& src)
+	{
+		if (src.RawSize() > 0) {
+			base::AppendRaw(src);
+			base::FixTail();
+		}
+	}
+
+	/** Assignment from C string. */
+	FORCEINLINE CStrT& operator = (const Tchar* src)
+	{
+		base::Clear();
+		AppendStr(src);
+		return *this;
+	}
+
+	/** Assignment from another CStrT or blob. */
+	FORCEINLINE CStrT& operator = (const CBlobBaseSimple& src)
+	{
+		base::Clear();
+		base::AppendRaw(src);
+		base::FixTail();
+		return *this;
+	}
+
+	/** Assignment from another CStrT or blob. */
+	FORCEINLINE CStrT& operator = (const CStrT& src)
+	{
+		base::Clear();
+		base::AppendRaw(src);
+		base::FixTail();
+		return *this;
+	}
+
+	/** Lower-than operator (to support stl collections) */
+	FORCEINLINE bool operator < (const CStrT &other) const
+	{
+		return (Api::StrCmp(base::Data(), other.Data()) < 0);
+	}
+
+	/** Add formated string (like vsprintf) at the end of existing contents. */
+	int AddFormatL(const Tchar *format, va_list args)
+	{
+		bsize_t addSize = Api::StrLen(format);
+		if (addSize < 16) addSize = 16;
+		addSize += addSize > 1;
+		int ret, err;
+		do {
+			Tchar *buf = MakeFreeSpace(addSize);
+			ret = Api::SPrintFL(buf, base::GetReserve(), format, args);
+			addSize *= 2;
+			err = errno;
+		} while(ret < 0 && (err == ERANGE || err == ENOENT || err == 0));
+		if (ret > 0) {
+			GrowSizeNC(ret);
+		} else {
+//			int err = errno;
+			base::FixTail();
+		}
+		return ret;
+	}
+
+	/** Add formated string (like sprintf) at the end of existing contents. */
+	int AddFormat(const Tchar *format, ...)
+	{
+		va_list args;
+		va_start(args, format);
+		int ret = AddFormatL(format, args);
+		va_end(args);
+		return ret;
+	}
+
+	/** Assign formated string (like vsprintf). */
+	int FormatL(const Tchar *format, va_list args)
+	{
+		base::Free();
+		int ret = AddFormatL(format, args);
+		return ret;
+	}
+
+	/** Assign formated string (like sprintf). */
+	int Format(const Tchar *format, ...)
+	{
+		base::Free();
+		va_list args;
+		va_start(args, format);
+		int ret = AddFormatL(format, args);
+		va_end(args);
+		return ret;
+	}
+};
+
+typedef CStrT<char   , false> CStrA;   ///< Case sensitive ANSI/UTF-8 string
+typedef CStrT<char   , true > CStrCiA; ///< Case insensitive ANSI/UTF-8 string
+typedef CStrT<wchar_t, false> CStrW;   ///< Case sensitive unicode string
+typedef CStrT<wchar_t, true > CStrCiW; ///< Case insensitive unicode string
+
+#endif /* STR_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/misc/strapi.hpp	Fri Jun 29 22:33:58 2007 +0000
@@ -0,0 +1,94 @@
+/* $Id$ */
+
+/** @file strapi.hpp */
+
+#ifndef  STRAPI_HPP
+#define  STRAPI_HPP
+
+#include <string.h>
+#include <wchar.h>
+
+#if !defined(_MSC_VER)
+#define _stricmp strcmp
+#define _wcsicmp wcscmp
+#endif //!_MSC_VER
+
+/** String API mapper base - just mapping by character type, not by case sensitivity yet.
+	* Class template CStrApiBaseT declaration is general, but following inline method
+	* definitions are specialized by character type. Class is not used directly, but only
+	* as a base class for template class CStrApiT */
+template <typename Tchar>
+class CStrApiBaseT
+{
+public:
+	/** ::strlen wrapper */
+	static size_t StrLen(const Tchar *s);
+	static int SPrintFL(Tchar *buf, size_t count, const Tchar *fmt, va_list args);
+};
+
+/** ::strlen wrapper specialization for char */
+template <> /*static*/ inline size_t CStrApiBaseT<char>::StrLen(const char *s)
+{
+	return ::strlen(s);
+}
+
+/** ::strlen wrapper specialization for wchar_t */
+template <> /*static*/ inline size_t CStrApiBaseT<wchar_t>::StrLen(const wchar_t *s)
+{
+	return ::wcslen(s);
+}
+
+/** ::vsprintf wrapper specialization for char */
+template <> /*static*/ inline int CStrApiBaseT<char>::SPrintFL(char *buf, size_t count, const char *fmt, va_list args)
+{
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC 8.0 and above
+	return ::vsnprintf_s(buf, count, count - 1, fmt, args);
+#else // ! VC 8.0 and above
+	return ::vsnprintf(buf, count, fmt, args);
+#endif
+}
+
+/** ::vsprintf wrapper specialization for wchar_t */
+template <> /*static*/ inline int CStrApiBaseT<wchar_t>::SPrintFL(wchar_t *buf, size_t count, const wchar_t *fmt, va_list args)
+{
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC 8.0 and above
+	return ::_vsnwprintf_s(buf, count, count - 1, fmt, args);
+#else // ! VC 8.0 and above
+# if defined(_WIN32)
+	 return ::_vsnwprintf(buf, count, fmt, args);
+# else // !_WIN32
+	 return ::vswprintf(buf, count, fmt, args);
+# endif // !_WIN32
+#endif
+}
+
+
+
+template <typename Tchar, bool TcaseInsensitive>
+class CStrApiT : public CStrApiBaseT<Tchar>
+{
+public:
+	static int StrCmp(const Tchar *s1, const Tchar *s2);
+};
+
+template <> /*static*/ inline int CStrApiT<char, false>::StrCmp(const char *s1, const char *s2)
+{
+	return ::strcmp(s1, s2);
+}
+
+template <> /*static*/ inline int CStrApiT<char, true>::StrCmp(const char *s1, const char *s2)
+{
+	return ::_stricmp(s1, s2);
+}
+
+template <> /*static*/ inline int CStrApiT<wchar_t, false>::StrCmp(const wchar_t *s1, const wchar_t *s2)
+{
+	return ::wcscmp(s1, s2);
+}
+
+template <> /*static*/ inline int CStrApiT<wchar_t, true>::StrCmp(const wchar_t *s1, const wchar_t *s2)
+{
+	return ::_wcsicmp(s1, s2);
+}
+
+#endif /* STRAPI_HPP */