[Add] FREE FREE FREE AS A BIRD I'... I mean. Adding string class, so I can do some conversions.

This commit is contained in:
Rtch90 2012-04-15 00:53:09 +01:00
parent aca5147ddc
commit cde92a8bbe
4 changed files with 148 additions and 5 deletions

View File

@ -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 +=

View File

@ -2,6 +2,8 @@
#include <string.h>
#include <stdio.h>
#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;
};

106
src/System/String.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#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;
}

32
src/System/String.h Normal file
View File

@ -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];
};