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

📄 mstring.cpp

📁 机甲指挥官2源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	Check_Object(&temp);
	return temp;
}

//
//#############################################################################
//#############################################################################
//
int
	MStringRepresentation::Compare(const MStringRepresentation &str) const
{
	Check_Object(this);
	Check_Object(&str);

	// handle special cases where one string is empty
	if (stringText == NULL)
	{
		if (str.stringText == NULL)
			return 0;
		else
			return -1;
	}
	if (str.stringText == NULL)
   {
		return 1;
   }

	return strcmp(stringText, str.stringText);
}

//
//#############################################################################
//#############################################################################
//
MStringRepresentation
	MStringRepresentation::GetNthToken(
		size_t nth_token,
		char *delimiters
	) const
{
	Check_Object(this);

	//
	// Which delimters to use
	//
	char *delimter_string = " \t,";

	if (delimiters != NULL)
	{
		delimter_string = delimiters;
	}
	Check_Pointer(delimter_string);

   //
	// Make temporary
   //
	MStringRepresentation temp(*this);

   if (temp.stringText == NULL)
		return temp;

   //
	// Parse string with strtok
   //
	size_t i;
	char *ptr;

	Check_Pointer(temp.stringText);
	ptr = strtok(temp.stringText, delimter_string);
	for (i = 0; i < nth_token; i++)
	{
		if ((ptr = strtok(NULL, delimter_string)) == NULL)
			break;
	}

	if (ptr == NULL)
	{
		MStringRepresentation null_return;
		return null_return;
	}
	MStringRepresentation token_return(ptr);
	return token_return;
}

//
//#############################################################################
//#############################################################################
//
void
	MStringRepresentation::ToUpper()
{
	Check_Object(this);
	if (stringText != NULL)
	{
		for (int i = 0; i < stringLength; i++)
		{
			stringText[i] = (char)toupper(stringText[i]);
		}
	}
}

//
//#############################################################################
//#############################################################################
//
void MStringRepresentation::ToLower()
{
	Check_Object(this);
	if (stringText != NULL)
	{
		CharLower(stringText);
	}
}

//
//#############################################################################
//#############################################################################
//
MemoryStream&
	MemoryStreamIO::Read(
		MemoryStream *stream,
		MStringRepresentation *str
   )
{
	Check_Object(stream);
   Check_Object(str);

   size_t string_length;

	Read(stream, &string_length);
	if (string_length > 0)
	{
		char *ptr = new char[string_length + 1];
		Register_Pointer(ptr);
		stream->ReadBytes(ptr, string_length + 1);

		*str = ptr;

		Unregister_Pointer(ptr);
		delete[] ptr;
	}

	Check_Object(str);
	return *stream;
}

//
//#############################################################################
//#############################################################################
//
MemoryStream&
	MemoryStreamIO::Write(
		MemoryStream *stream,
		const MStringRepresentation &str
	)
{
	Check_Object(stream);
	Check_Object(&str);

	Write(stream, &str.stringLength);
	if (str.stringLength > 0)
	{
		stream->WriteBytes(str.stringText, str.stringLength + 1);
	}
	return *stream;
}

//
//#############################################################################
//#############################################################################
//
void
	Stuff::Convert_From_Ascii(
		const char *str,
		MStringRepresentation *value
	)
{
	#if 0
		Check_Pointer(str);
		Check_Object(value);
		*value = str;
	#else
		if (str == NULL)
		{
			STOP(("Convert_From_Ascii - str == NULL"));
		}
		if (value == NULL)
		{
			STOP(("Convert_From_Ascii - value == NULL"));
		}
		*value = str;
	#endif
}

//#############################################################################
//##############################    MString    ################################
//#############################################################################

//
//#############################################################################
//#############################################################################
//
MString&
	MString::operator = (const MString &str)
{
	Check_Object(this);
	Check_Object(&str);

	if (this == &str)
		return *this;

	Check_Object(representation);
	representation->DecrementReferenceCount();
	representation = str.representation;
	representation->IncrementReferenceCount();
	return *this;
}

//
//#############################################################################
//#############################################################################
//
MString&
	MString::operator = (const char *cstr)
{
	Check_Object(representation);
	representation->DecrementReferenceCount();
	representation = new MStringRepresentation(cstr);
	Register_Object(representation);
	representation->IncrementReferenceCount();
	return *this;
}

//
//#############################################################################
//#############################################################################
//
void
	MString::ToUpper()
{
	MStringRepresentation *old = representation;
	Check_Object(old);

	representation = new MStringRepresentation(*old);
	Register_Object(representation);
	representation->IncrementReferenceCount();

	old->DecrementReferenceCount();

	representation->ToUpper();
}

//
//#############################################################################
//#############################################################################
//
void
	MString::ToLower()
{
	MStringRepresentation *old = representation;
	Check_Object(old);

	representation = new MStringRepresentation(*old);
	Register_Object(representation);
	representation->IncrementReferenceCount();

	old->DecrementReferenceCount();

	representation->ToLower();
}

//
//#############################################################################
//#############################################################################
//
MemoryStream&
	MemoryStreamIO::Read(
		MemoryStream *stream,
		MString *str
	)
{
	Check_Object(str);
	Check_Object(str->representation);
	str->representation->DecrementReferenceCount();
	str->representation = new MStringRepresentation;
	Register_Object(str->representation);
	str->representation->IncrementReferenceCount();
	Verify(str->representation->referenceCount == 1);
	return Read(stream, str->representation);
}

//
//#############################################################################
//#############################################################################
//
void
	Stuff::Convert_From_Ascii(
		const char *str,
		MString *value
	)
{
	Check_Object(value);
	Check_Object(value->representation);
	value->representation->DecrementReferenceCount();
	value->representation = new MStringRepresentation;
	Register_Object(value->representation);
	value->representation->IncrementReferenceCount();
	Verify(value->representation->referenceCount == 1);
	Convert_From_Ascii(str, value->representation);
}
//
//#############################################################################
//#############################################################################
//
bool
	Stuff::Close_Enough(
		const char *str1,
		const char *str2,
		Scalar e
	)
{
	Check_Pointer(str1);
	Check_Pointer(str2);

	return
		!_stricmp(str1, str2);
}

		IteratorPosition
			GetHashFunctions::GetHashValue(const MString &value)
				{
					return value.GetHashValue();
				}

⌨️ 快捷键说明

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