00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef STRNGS_H
00021 #define STRNGS_H
00022
00023 #include <string.h>
00024 #include "memry.h"
00025 #include "serialis.h"
00026 #include "genericvector.h"
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #define STRING_IS_PROTECTED 0
00037
00038 #ifdef CCUTIL_EXPORTS
00039 #define CCUTIL_API __declspec(dllexport)
00040 #elif defined(CCUTIL_IMPORTS)
00041 #define CCUTIL_API __declspec(dllimport)
00042 #else
00043 #define CCUTIL_API
00044 #endif
00045
00046
00047 class DLLSYM STRING
00048 {
00049 public:
00050 STRING();
00051 STRING(const STRING &string);
00052 STRING(const char *string);
00053 ~STRING ();
00054
00055
00056 bool Serialize(FILE* fp) const;
00057
00058
00059 bool DeSerialize(bool swap, FILE* fp);
00060
00061 BOOL8 contains(const char c) const;
00062 inT32 length() const;
00063 inT32 size() const { return length(); }
00064 const char *string() const;
00065
00066 #if STRING_IS_PROTECTED
00067 const char &operator[] (inT32 index) const;
00068
00069 void insert_range(inT32 index, const char*s, int len);
00070 void erase_range(inT32 index, int len);
00071 #else
00072 char &operator[] (inT32 index) const;
00073 #endif
00074 void split(const char c, GenericVector<STRING> *splited);
00075 void truncate_at(inT32 index);
00076
00077 BOOL8 operator== (const STRING & string) const;
00078 BOOL8 operator!= (const STRING & string) const;
00079 BOOL8 operator!= (const char *string) const;
00080
00081 STRING & operator= (const char *string);
00082 STRING & operator= (const STRING & string);
00083
00084 STRING operator+ (const STRING & string) const;
00085 STRING operator+ (const char ch) const;
00086
00087 STRING & operator+= (const char *string);
00088 STRING & operator+= (const STRING & string);
00089 STRING & operator+= (const char ch);
00090
00091
00092
00093
00094
00095 void add_str_int(const char* str, int number);
00096
00097
00098 inline void ensure(inT32 min_capacity) { ensure_cstr(min_capacity); }
00099
00100 private:
00101 typedef struct STRING_HEADER {
00102
00103 int capacity_;
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 mutable int used_;
00117 } STRING_HEADER;
00118
00119
00120
00121
00122
00123 STRING_HEADER* data_;
00124
00125
00126 inline STRING_HEADER* GetHeader() {
00127 return data_;
00128 }
00129 inline const STRING_HEADER* GetHeader() const {
00130 return data_;
00131 }
00132
00133
00134 inline char* GetCStr() {
00135 return ((char *)data_) + sizeof(STRING_HEADER);
00136 };
00137
00138 inline const char* GetCStr() const {
00139 return ((const char *)data_) + sizeof(STRING_HEADER);
00140 };
00141 inline bool InvariantOk() const {
00142 #if STRING_IS_PROTECTED
00143 return (GetHeader()->used_ == 0) ?
00144 (string() == NULL) : (GetHeader()->used_ == (strlen(string()) + 1));
00145 #else
00146 return true;
00147 #endif
00148 }
00149
00150
00151
00152
00153 char* ensure_cstr(inT32 min_capacity);
00154
00155 void FixHeader() const;
00156
00157 char* AllocData(int used, int capacity);
00158 void DiscardData();
00159 };
00160 #endif