⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstring.hpp

📁 机甲指挥官2源代码
💻 HPP
📖 第 1 页 / 共 2 页
字号:
//===========================================================================//
// File:     string.hpp                                                      //
// Title:    Definition of MString class.                                    //
//---------------------------------------------------------------------------//
// Copyright (C) Microsoft Corporation. All rights reserved.                 //
//===========================================================================//

#pragma once

#include "Stuff.hpp"
#include "Hash.hpp"
#include "Scalar.hpp"

namespace Stuff
{
	class MString;
	class MStringRepresentation;
}

#if !defined(Spew)
	void
		Spew(
			const char* group,
			const Stuff::MStringRepresentation& string
		);
	void
		Spew(
			const char* group,
			const Stuff::MString& string
		);
#endif

namespace MemoryStreamIO {

	Stuff::MemoryStream&
		Read(
			Stuff::MemoryStream* stream,
			Stuff::MStringRepresentation *str
		);
	Stuff::MemoryStream&
		Write(
			Stuff::MemoryStream* stream,
			const Stuff::MStringRepresentation& str
		);

	Stuff::MemoryStream&
		Read(
			Stuff::MemoryStream* stream,
			Stuff::MString *str
		);
	Stuff::MemoryStream&
		Write(
			Stuff::MemoryStream* stream,
			const Stuff::MString* str
		);

}

namespace GetHashFunctions {
	Stuff::IteratorPosition
		GetHashValue(const Stuff::MString &value);
}

namespace Stuff {

	class MStringRepresentation;
	class MString;

	class MemoryStream;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ASCII Conversions
	//
	void
		Convert_From_Ascii(
			const char* str,
			char* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			BYTE* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			short* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			WORD* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			int* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			unsigned* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			long* value
		);
	void
		Convert_From_Ascii(
			const char* str,
			DWORD* value
		);

	//##########################################################################
	//#####################    MStringRepresentation    ########################
	//##########################################################################

	class MStringRepresentation
		#if defined(_ARMOR)
			: public Stuff::Signature
		#endif
	{
		friend MString;

		friend MString
			operator + (
				const MString &str1,
				const MString &str2
			);

		friend MString
			operator + (
				const MString &str1,
				char ch
			);

		friend MemoryStream&
			MemoryStreamIO::Read(
				MemoryStream* stream,
				MString *str
			);

		friend void
			Convert_From_Ascii(
				const char *str,
				MString *value
			);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Construction, Destruction
	//
	private:
		MStringRepresentation();
		MStringRepresentation(const MStringRepresentation &str);
		MStringRepresentation(const char *cstr);

	public:
		~MStringRepresentation();

		void
			TestInstance() const;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Length, Size
	//
	private:
		//
		// Length returns strlen of string
		//
		size_t
			GetLength() const;
		void
			SetLength(size_t length);
		void
			AllocateLength(size_t length);

		//
		// Size returns memory allocation size
		//
		size_t
			GetSize() const;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Accesors & Manipulation
	//
	private:
		//
		// create a c-string from MStringRepresentation method
		// HACK - ECH 11/1/95 - Remove const to support 3rd party libs
		//
		operator char*() const;

		//
		// assignment method
		//
		MStringRepresentation
			operator = (const MStringRepresentation &str);
		MStringRepresentation
			operator = (const char *cstr);

		//
		// concatenation methods
		//
		friend MStringRepresentation
			operator + (
				const MStringRepresentation &str1,
				const MStringRepresentation &str2
			);

		friend MStringRepresentation
			operator + (
				const MStringRepresentation & str1,
				char ch
			);

		void
			operator += (const MStringRepresentation &str);
		void
			operator += (char ch);

		//
		// comparison methods
		//
		int
			Compare(const MStringRepresentation &str) const;

		bool
			operator < (const MStringRepresentation &str) const;
		bool
			operator > (const MStringRepresentation &str) const;
		bool
			operator <= (const MStringRepresentation &str) const;
		bool
			operator >= (const MStringRepresentation &str) const;
		bool
			operator == (const MStringRepresentation &str) const;
		bool
			operator != (const MStringRepresentation &str) const;

		bool
			operator == (const char *cstr) const;

		//
		// character retrieval method
		//
		char
			operator [] (size_t pos) const;

		MStringRepresentation
			GetNthToken(
				size_t nth_token,
				char *delimiters=NULL
			) const;

		//
		// case-modification methods
		//
		void
			ToUpper();
		void
			ToLower();

		//
		// stream input/output methods
		//
		#if !defined(Spew)
			friend void
				::Spew(
					const char* group,
					const MStringRepresentation& string
				);
		#endif

		friend MemoryStream&
			MemoryStreamIO::Read(
				MemoryStream* stream,
				MStringRepresentation *str
			);

		friend MemoryStream&
			MemoryStreamIO::Write(
				MemoryStream* stream,
				const MStringRepresentation& str
			);

		friend void
			Convert_From_Ascii(
				const char *str,
				MStringRepresentation *value
			);

		IteratorPosition
			GetHashValue();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Reference count methods
	//
	private:
		void
			IncrementReferenceCount();

		void
			DecrementReferenceCount();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Private Data
	//
	private:
		//
		// class constant
		//
		static size_t
			allocationIncrement;

		//
		// calc alloc size for needed bytes
		//
		static size_t
			CalculateSize(size_t needed);

		//
		// instance variables
		//
		size_t
			stringSize;
		size_t
			stringLength;
		char
			*stringText;

		//
		// reference count
		//
		int
			referenceCount;
	};

	//~~~~~~~~~~~~~~~~~~~~~~~~ MStringRepresentation inlines ~~~~~~~~~~~~~~~~~~~

	// value return methods
	inline size_t
		MStringRepresentation::GetLength() const
	{
		Check_Object(this);
		Verify(
			(stringText != NULL) ? 
				(stringLength == strlen(stringText)) : 
				(stringLength == 0)
		);
		return stringLength;
	}

	inline void
		MStringRepresentation::SetLength(size_t length)
	{
		Check_Object(this);
		Verify(length < GetSize());
		stringLength = length;
		Verify(strlen(stringText) == stringLength);
	}

	inline size_t
		MStringRepresentation::GetSize() const
	{
		Check_Object(this);
		return stringSize;
	}

	// create a c-string from MStringRepresentation method
	// HACK - ECH 11/1/95 - Remove const to support 3rd party libs
	inline
		MStringRepresentation::operator char*() const
   {
      Check_Object(this);
//		Verify(stringText != NULL);
		return stringText;
   }

	// concatenation methods
	inline void
		MStringRepresentation::operator += (const MStringRepresentation &str)
	{
		Check_Object(this);
		*this = *this + str;
	}

	inline void
		MStringRepresentation::operator += (char ch)
	{
		Check_Object(this);
		*this = *this + ch;
	}

	// comparison methods
	inline bool
		MStringRepresentation::operator < (const MStringRepresentation &str) const
	{
		return (Compare(str) < 0);
   }

	inline bool
		MStringRepresentation::operator > (const MStringRepresentation &str) const
   {
		return (Compare(str) > 0);
	}

	inline bool
		MStringRepresentation::operator <= (const MStringRepresentation &str) const
	{
      return !(Compare(str) > 0);
   }

	inline bool
		MStringRepresentation::operator >= (const MStringRepresentation &str) const
	{
      return !(Compare(str) < 0);
	}

	inline bool
		MStringRepresentation::operator == (const MStringRepresentation &str) const
   {
      return (Compare(str) == 0);
   }

	inline bool
		MStringRepresentation::operator != (const MStringRepresentation &str) const
	{
		return (Compare(str) != 0);
   }

	inline bool
		MStringRepresentation::operator == (const char *cstr) const
   {
      return (Compare(cstr) == 0);
   }

	// character retrieval method
	inline char
		MStringRepresentation::operator [] (size_t pos) const
	{
		Check_Object(this);
		return (pos >= stringLength) ? ('\x00') : (stringText[pos]);
	}

	// Reference count methods
	inline void
		MStringRepresentation::IncrementReferenceCount()
	{
		Check_Object(this);
		Verify(referenceCount >= 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -