1 /*
2 ===========================================================================
3 Copyright (C) 2000 - 2013, Raven Software, Inc.
4 Copyright (C) 2001 - 2013, Activision, Inc.
5 Copyright (C) 2013 - 2015, OpenJK contributors
6 
7 This file is part of the OpenJK source code.
8 
9 OpenJK is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 ===========================================================================
21 */
22 
23 // Filename:-	sstring.h
24 //
25 // Gil's string template, used to replace Microsoft's <string> vrsion which doesn't compile under certain stl map<>
26 //	conditions...
27 
28 
29 #ifndef SSTRING_H
30 #define SSTRING_H
31 
32 #include "../qcommon/q_shared.h"
33 #include "qcommon/safe/gsl.h"
34 #include <algorithm>
35 
36 template<int MaxSize>
37 class sstring
38 {
39 	struct SStorage
40 	{
41 		char data[MaxSize];
42 	};
43 	SStorage mStorage;
44 public:
45 /* don't figure we need this
46 	template<int oMaxSize>
47 	sstring(const sstring<oMaxSize> &o)
48 	{
49 		assert(strlen(o.mStorage.data)<MaxSize);
50 		strcpy(mStorage.data,o.mStorage.data);
51 	}
52 */
sstring(const sstring<MaxSize> & o)53 	sstring(const sstring<MaxSize> &o)
54 	{
55 		//strcpy(mStorage.data,o.mStorage.data);
56 		Q_strncpyz(mStorage.data,o.mStorage.data,sizeof(mStorage.data));
57 	}
sstring(const char * s)58 	sstring(const char *s)
59 	{
60 		//assert(strlen(s)<MaxSize);
61 		//strcpy(mStorage.data,s);
62 		Q_strncpyz(mStorage.data,s,sizeof(mStorage.data));
63 	}
sstring(const gsl::cstring_view & v)64 	sstring( const gsl::cstring_view& v )
65 	{
66 		if( v.size() + 1 > sizeof( mStorage.data ) )
67 		{
68 			Com_Error( ERR_FATAL, "String dest buffer too small (%d) to hold string of length %d", sizeof( mStorage.data ), v.size() );
69 		}
70 		std::copy( v.begin(), v.end(), mStorage.data );
71 		mStorage.data[ v.size() ] = '\0';
72 	}
sstring()73 	sstring()
74 	{
75 		mStorage.data[0]=0;
76 	}
77 /* don't figure we need this
78 	template<int oMaxSize>
79 	sstring<oMaxSize> & operator =(const sstring<oMaxSize> &o)
80 	{
81 		assert(strlen(o.mStorage.data)<MaxSize);
82 		strcpy(mStorage.data,o.mStorage.data);
83 		return *this;
84 	}
85 */
86 	sstring<MaxSize> & operator=(const sstring<MaxSize> &o)
87 	{
88 		//strcpy(mStorage.data,o.mStorage.data);
89 		Q_strncpyz(mStorage.data,o.mStorage.data,sizeof(mStorage.data));
90 		return *this;
91 	}
92 	sstring<MaxSize> & operator=(const char *s)
93 	{
94 		assert(strlen(s)<MaxSize);
95 		//strcpy(mStorage.data,s);
96 		Q_strncpyz(mStorage.data,s,sizeof(mStorage.data));
97 		return *this;
98 	}
c_str()99 	char *c_str()
100 	{
101 		return mStorage.data;
102 	}
c_str()103 	const char *c_str() const
104 	{
105 		return mStorage.data;
106 	}
capacity()107 	int capacity() const
108 	{
109 		return MaxSize;	// not sure if this should be MaxSize-1? depends if talking bytes or strlen space I guess
110 	}
length()111 	int length() const
112 	{
113 		return strlen(mStorage.data);
114 	}
115 	bool operator==(const sstring<MaxSize> &o) const
116 	{
117 		if (!Q_stricmp(mStorage.data,o.mStorage.data))
118 		{
119 			return true;
120 		}
121 		return false;
122 	}
123 	bool operator!=(const sstring<MaxSize> &o) const
124 	{
125 		if (Q_stricmp(mStorage.data,o.mStorage.data)!=0)
126 		{
127 			return true;
128 		}
129 		return false;
130 	}
131 	bool operator<(const sstring<MaxSize> &o) const
132 	{
133 		if (Q_stricmp(mStorage.data,o.mStorage.data)<0)
134 		{
135 			return true;
136 		}
137 		return false;
138 	}
139 	bool operator>(const sstring<MaxSize> &o) const
140 	{
141 		if (Q_stricmp(mStorage.data,o.mStorage.data)>0)
142 		{
143 			return true;
144 		}
145 		return false;
146 	}
147 };
148 
149 typedef sstring<MAX_QPATH> sstring_t;
150 
151 #endif	// #ifndef SSTRING_H
152 
153 /////////////////// eof ////////////////////
154 
155