From 942d7195e4ad90fc107778b7f5536de992385c8b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Mon, 16 Nov 2015 22:34:30 +0100 Subject: [PATCH] Moving CBotString class in its own header and source files. --- src/CBot/CBot.h | 1 - src/CBot/CBotCall.h | 2 + src/CBot/CBotDll.h | 188 ------------------------- src/CBot/CBotString.h | 214 +++++++++++++++++++++++++++++ src/CBot/CBotStringArray.h | 2 +- src/CBot/CBotUtils.cpp | 18 +++ src/CBot/CBotUtils.h | 19 +-- src/CBot/CBotVar/CBotVar.h | 2 + test/unit/CBot/CBotString_test.cpp | 2 + 9 files changed, 246 insertions(+), 202 deletions(-) create mode 100644 src/CBot/CBotString.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 71c2279c..0b619fe9 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -67,7 +67,6 @@ extern bool ReadLong(FILE* pf, long& w); extern bool WriteFloat(FILE* pf, float w); extern bool WriteLong(FILE* pf, long w); extern bool ReadFloat(FILE* pf, float& w); -extern bool WriteString(FILE* pf, CBotString s); extern bool ReadString(FILE* pf, CBotString& s); extern bool WriteType(FILE* pf, CBotTypResult type); extern bool ReadType(FILE* pf, CBotTypResult& type); diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h index 79cab9a2..103d1fc4 100644 --- a/src/CBot/CBotCall.h +++ b/src/CBot/CBotCall.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotString.h" + // Local include // Global include diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 16b400e3..40e4931d 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -231,194 +231,6 @@ private: // for example exceptions returned by external routines // and " throw " with any number. - -//////////////////////////////////////////////////////////////////////// -/** - * \brief CBotString Class used to work on strings - */ -class CBotString -{ -public: - - /** - * \brief CBotString Default constructor. - */ - CBotString(); - - /** - * \brief CBotString - * \param p - */ - CBotString(const char* p); - - /** - * \brief CBotString - * \param p - */ - CBotString(const CBotString& p); - - /** - * \brief CBotString Destructor. - */ - ~CBotString(); - - /** - * \brief Empty Clear the internal string. - */ - void Empty(); - - /** - * \brief IsEmpty Check if the string is empty. - * \return True if the sting is empty false otherwise. - */ - bool IsEmpty() const; - - /** - * \brief GetLength Get the string length. - * \return The size of the string. - */ - int GetLength(); - - /** - * \brief Find Find the position of a character in a string starting from - * the beginning of the string. - * \param c The character to find. - * \return The position of the character or -1 if the character was not - * found. - * \see ReverseFind(const char c) - */ - int Find(const char c); - - /** - * \brief Find Find the position of a string in a string starting from the - * beginning of the string. - * \param lpsz The string to find. - * \return The position of the string or -1 if the string was not - * found. - * \see ReverseFind(const char* lpsz) - */ - int Find(const char* lpsz); - - /** - * \brief Find Find the position of a character in a string starting from - * the end of the string. - * \param c The character to find. - * \return The position of the character or -1 if the character was not - * found. - * \see Find(const char c) - */ - int ReverseFind(const char c); - - /** - * \brief Find Find the position of a string in a string starting from the - * end of the string. - * \param lpsz The string to find. - * \return The string of the character or -1 if the string was not - * found. - * \see Find(const char* lpsz) - */ - int ReverseFind(const char* lpsz); - - /** - * \brief LoadString Load the string associate with the id. - * \param id The id to load. - * \return True if the id exist false otherwise. - */ - bool LoadString(unsigned int id); - - /** - * \brief Mid Return a part of a string from a starting index and until - * the end of the string with a limited size. - * \param nFirst The start index of the character in the string. - * \param lg The size limit. Default value is 2000. - * \return The exctracted string. - */ - CBotString Mid(int start, int lg=-1); - - /** - * \brief Left Return a part of a string starting from the left. - * \param nCount The number of character to retreive. - * \return The exctracted string. - */ - CBotString Left(int nCount) const; - - /** - * \brief Right Return a part of a string starting from the right. - * \param nCount The number of character to retreive. - * \return The exctracted string. - */ - CBotString Right(int nCount) const; - - /** - * \brief Compare Compare a given string to an other. - * \param lpsz The string to compare. - * \return 0 if the two string matches. Less than 0 if the current - * string is less than lpsz. Greater than 0 if the current - * string is greater than lpsz. - */ - int Compare(const char* lpsz) const; - - /** - * \brief MakeUpper Uppercase the string. - */ - void MakeUpper(); - - /** - * \brief MakeLower Lowercase the string. - */ - void MakeLower(); - - /** - * @brief CStr Convert the CBotString to a C string. - * @return A C string string. - */ - const char* CStr() const; - - /** - * \brief Overloaded oprators to work on CBotString classes - */ - const CBotString& operator=(const CBotString& stringSrc); - const CBotString& operator=(const char ch); - const CBotString& operator=(const char* pString); - CBotString operator+(const CBotString& str); - - const CBotString& operator+=(const char ch); - const CBotString& operator+=(const CBotString& str); - bool operator==(const CBotString& str); - bool operator==(const char* p); - bool operator!=(const CBotString& str); - bool operator!=(const char* p); - bool operator>(const CBotString& str); - bool operator>(const char* p); - bool operator>=(const CBotString& str); - bool operator>=(const char* p); - bool operator<(const CBotString& str); - bool operator<(const char* p); - bool operator<=(const CBotString& str); - bool operator<=(const char* p); - - operator const char*() const; // as a C string - - -private: - - //! \brief Pointer to string - char* m_ptr; - - //! \brief Length of the string - int m_lg; - - //! \brief Keeps the string corresponding to keyword ID - static const std::map s_keywordString; - - /** - * \brief MapIdToString Maps given ID to its string equivalent. - * \param id Provided identifier. - * \return String if found, else NullString. - */ - static const char * MapIdToString(EID id); -}; - /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) FILE* fOpen(const char* name, const char* mode); diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h new file mode 100644 index 00000000..3297c2b7 --- /dev/null +++ b/src/CBot/CBotString.h @@ -0,0 +1,214 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotUtils.h" + +// Local include + +// Global include +#include + +/** + * \brief CBotString Class used to work on strings + */ +class CBotString +{ +public: + + /** + * \brief CBotString Default constructor. + */ + CBotString(); + + /** + * \brief CBotString + * \param p + */ + CBotString(const char* p); + + /** + * \brief CBotString + * \param p + */ + CBotString(const CBotString& p); + + /** + * \brief CBotString Destructor. + */ + ~CBotString(); + + /** + * \brief Empty Clear the internal string. + */ + void Empty(); + + /** + * \brief IsEmpty Check if the string is empty. + * \return True if the sting is empty false otherwise. + */ + bool IsEmpty() const; + + /** + * \brief GetLength Get the string length. + * \return The size of the string. + */ + int GetLength(); + + /** + * \brief Find Find the position of a character in a string starting from + * the beginning of the string. + * \param c The character to find. + * \return The position of the character or -1 if the character was not + * found. + * \see ReverseFind(const char c) + */ + int Find(const char c); + + /** + * \brief Find Find the position of a string in a string starting from the + * beginning of the string. + * \param lpsz The string to find. + * \return The position of the string or -1 if the string was not + * found. + * \see ReverseFind(const char* lpsz) + */ + int Find(const char* lpsz); + + /** + * \brief Find Find the position of a character in a string starting from + * the end of the string. + * \param c The character to find. + * \return The position of the character or -1 if the character was not + * found. + * \see Find(const char c) + */ + int ReverseFind(const char c); + + /** + * \brief Find Find the position of a string in a string starting from the + * end of the string. + * \param lpsz The string to find. + * \return The string of the character or -1 if the string was not + * found. + * \see Find(const char* lpsz) + */ + int ReverseFind(const char* lpsz); + + /** + * \brief LoadString Load the string associate with the id. + * \param id The id to load. + * \return True if the id exist false otherwise. + */ + bool LoadString(unsigned int id); + + /** + * \brief Mid Return a part of a string from a starting index and until + * the end of the string with a limited size. + * \param nFirst The start index of the character in the string. + * \param lg The size limit. Default value is 2000. + * \return The exctracted string. + */ + CBotString Mid(int start, int lg=-1); + + /** + * \brief Left Return a part of a string starting from the left. + * \param nCount The number of character to retreive. + * \return The exctracted string. + */ + CBotString Left(int nCount) const; + + /** + * \brief Right Return a part of a string starting from the right. + * \param nCount The number of character to retreive. + * \return The exctracted string. + */ + CBotString Right(int nCount) const; + + /** + * \brief Compare Compare a given string to an other. + * \param lpsz The string to compare. + * \return 0 if the two string matches. Less than 0 if the current + * string is less than lpsz. Greater than 0 if the current + * string is greater than lpsz. + */ + int Compare(const char* lpsz) const; + + /** + * \brief MakeUpper Uppercase the string. + */ + void MakeUpper(); + + /** + * \brief MakeLower Lowercase the string. + */ + void MakeLower(); + + /** + * @brief CStr Convert the CBotString to a C string. + * @return A C string string. + */ + const char* CStr() const; + + /** + * \brief Overloaded oprators to work on CBotString classes + */ + const CBotString& operator=(const CBotString& stringSrc); + const CBotString& operator=(const char ch); + const CBotString& operator=(const char* pString); + CBotString operator+(const CBotString& str); + + const CBotString& operator+=(const char ch); + const CBotString& operator+=(const CBotString& str); + bool operator==(const CBotString& str); + bool operator==(const char* p); + bool operator!=(const CBotString& str); + bool operator!=(const char* p); + bool operator>(const CBotString& str); + bool operator>(const char* p); + bool operator>=(const CBotString& str); + bool operator>=(const char* p); + bool operator<(const CBotString& str); + bool operator<(const char* p); + bool operator<=(const CBotString& str); + bool operator<=(const char* p); + + operator const char*() const; // as a C string + + +private: + + //! \brief Pointer to string + char* m_ptr; + + //! \brief Length of the string + int m_lg; + + //! \brief Keeps the string corresponding to keyword ID + static const std::map s_keywordString; + + /** + * \brief MapIdToString Maps given ID to its string equivalent. + * \param id Provided identifier. + * \return String if found, else NullString. + */ + static const char * MapIdToString(EID id); +}; diff --git a/src/CBot/CBotStringArray.h b/src/CBot/CBotStringArray.h index 761f874a..bc13233c 100644 --- a/src/CBot/CBotStringArray.h +++ b/src/CBot/CBotStringArray.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotDll.h" +#include "CBotString.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 824b3fd6..1a226dbc 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -141,6 +141,24 @@ bool WriteFloat(FILE* pf, float w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// +void ConstructElement(CBotString* pNewData) +{ + memset(pNewData, 0, sizeof(CBotString)); +} + +//////////////////////////////////////////////////////////////////////////////// +void DestructElement(CBotString* pOldData) +{ + pOldData->~CBotString(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CopyElement(CBotString* pSrc, CBotString* pDest) +{ + *pSrc = *pDest; +} + //////////////////////////////////////////////////////////////////////////////// void ConstructElements(CBotString* pNewData, int nCount) { diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index f25c8c60..e14b4748 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -22,10 +22,14 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotString.h" + // Local include // Global include +class CBotString; + /*! * \brief MakeListVars Transforms the array of pointers to variables in a * chained list of variables @@ -80,29 +84,20 @@ bool WriteFloat(FILE* pf, float w); * \brief ConstructElement * \param pNewData */ -static inline void ConstructElement(CBotString* pNewData) -{ - memset(pNewData, 0, sizeof(CBotString)); -} +void ConstructElement(CBotString* pNewData); /*! * \brief DestructElement * \param pOldData */ -static inline void DestructElement(CBotString* pOldData) -{ - pOldData->~CBotString(); -} +void DestructElement(CBotString* pOldData); /*! * \brief CopyElement * \param pSrc * \param pDest */ -static inline void CopyElement(CBotString* pSrc, CBotString* pDest) -{ - *pSrc = *pDest; -} +void CopyElement(CBotString* pSrc, CBotString* pDest); /*! * \brief ConstructElements diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 8f465fc8..68132ad2 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -24,6 +24,8 @@ #include "../CBotDefines.h" +#include "../CBotString.h" + // Local include // Global include diff --git a/test/unit/CBot/CBotString_test.cpp b/test/unit/CBot/CBotString_test.cpp index 97576a99..b0d1ede8 100644 --- a/test/unit/CBot/CBotString_test.cpp +++ b/test/unit/CBot/CBotString_test.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBot/CBotDll.h" +#include "CBot/CBotString.h" + // Local include // Global include