From cde92a8bbe0227f40b0f528ab9d4b20de56f96e6 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 15 Apr 2012 00:53:09 +0100 Subject: [PATCH] [Add] FREE FREE FREE AS A BIRD I'... I mean. Adding string class, so I can do some conversions. --- LibDQt/LibDQt.pro | 9 ++-- src/System/FileReader.h | 6 ++- src/System/String.cpp | 106 ++++++++++++++++++++++++++++++++++++++++ src/System/String.h | 32 ++++++++++++ 4 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 src/System/String.cpp create mode 100644 src/System/String.h diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro index cbeec78..a5c0d68 100644 --- a/LibDQt/LibDQt.pro +++ b/LibDQt/LibDQt.pro @@ -57,7 +57,9 @@ HEADERS += ../src/Actor/Player.h \ ../src/Sound/SoundEffect.h \ ../src/Actor/Actor.h \ ../src/Animation/AnimimationSequence.h \ - ../src/System/FileReader.h + ../src/System/FileReader.h \ + ../src/Animation/AnimationSequence.h \ + ../src/System/String.h SOURCES += ../src/Actor/Player.cpp \ ../src/Collision/AABB.cpp \ ../src/Global/Globals.cpp \ @@ -90,6 +92,7 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/Actor/NPC.cpp \ ../src/Sound/SoundEffect.cpp \ ../src/Actor/Actor.cpp \ - ../src/Animation/AnimimationSequence.cpp \ - ../src/System/FileReader.cpp + ../src/System/FileReader.cpp \ + ../src/Animation/AnimationSequence.cpp \ + ../src/System/String.cpp OTHER_FILES += diff --git a/src/System/FileReader.h b/src/System/FileReader.h index 970f039..6845a16 100644 --- a/src/System/FileReader.h +++ b/src/System/FileReader.h @@ -2,6 +2,8 @@ #include #include +#include "../System/String.h" + class FileReader { public: FileReader(void); @@ -23,6 +25,6 @@ public: private: FILE* _file; - std::string _filename; - std::string _accessType; + String _filename; + String _accessType; }; diff --git a/src/System/String.cpp b/src/System/String.cpp new file mode 100644 index 0000000..eac51f5 --- /dev/null +++ b/src/System/String.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +#include "String.h" +#define _CRT_SECURE_NO_WARNINGS + +String::String(void) { + +} + +String::~String(void) { + +} + +String::String(char letter) { + // Char to string conversion. + char temp[1]; + temp[0] = letter; + strcpy(_string, temp); +} + +String::String(char* text) { + // char* or char array. + strcpy(_string, text); +} + +String::String(String& text) { + // Copy the String. + strcpy(_string, text.GetPointer()); +} + +const char* String::GetPointer(void) { + // Return a pointer to the memory address of the string. + return _string; +} + +int String::Length(void) { + // Return the length of the string. + return strlen(_string); +} + +void String::Concatenate(char value) { + // Concatenate a char on the end of a string. + assert(strlen(_string) + 1 < MAX_STRING_LEN); + char temp[1] = ""; + temp[0] = value; + strncat(_string, temp, 1); +} + +void String::Concatenate(const char* value) { + // Concatenate a char* or array to the end of the string. + assert(strlen(_string) + strlen(value) < MAX_STRING_LEN); + strcat(_string, value); +} + +void String::Concatenate(String& value) { + assert(strlen(_string) + strlen(value) < MAX_STRING_LEN); + strcat(_string, value.GetPointer()); +} + +// Operator overloads, can't be bothered to comment +// them, use your brain. :) +String& String::operator=(const char* value) { + assert(strlen(value) < MAX_STRING_LEN); + strcpy(_string, value); + return *this; +} + +String& String::operator=(String& value) { + assert(strlen(value) < MAX_STRING_LEN); + strcpy(_string, value.GetPointer()); + return *this; +} + +bool String::operator==(const char* value) const { + if(strcmp(_string, value) == 0) { + return true; + } + return false; +} + +bool String::operator==(String& value) const { + if(strcmp(_string, value.GetPointer) == 0) { + return true; + } + return false; +} + +bool String::operator!=(String& value) const { + if(strcmp(_string, value.GetPointer()) != 0) { + return true; + } + return false; +} + +bool String::operator!=(const char* value) const { + if(strcmp(_string, value) != 0) { + return true; + } + return false; +} + +String::operator const char*() const { + return _string; +} diff --git a/src/System/String.h b/src/System/String.h new file mode 100644 index 0000000..276f8f6 --- /dev/null +++ b/src/System/String.h @@ -0,0 +1,32 @@ +#pragma once +#define _CRT_SECURE_NO_WARNINGS +#define MAX_STRING_LEN 255 + +class String { +public: + String(void); + String(char letter); + String(char text[]); + String(String& text); + ~String(void); + + const char* GetPointer(void); + void Concatenate(const char* value); + void Concatenate(String& value); + void Concatenate(char value); + int Length(void); + + // Operator overloads. + String& operator=(const char* value); + String& operator=(String& value); + + bool operator==(const char* value) const; + bool operator==(String& value) const; + bool operator!=(String& value) const; + bool operator!=(const char* value) const; + + operator const char*() const; + +private: + char _string[MAX_STRING_LEN]; +};