]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uniquenames.h
Fix compile on MSYS2
[xonotic/netradiant.git] / libs / uniquenames.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_UNIQUENAMES_H )
23 #define INCLUDED_UNIQUENAMES_H
24
25 #include "debugging/debugging.h"
26 #include <map>
27 #include "string/string.h"
28 #include "generic/static.h"
29
30 #if 1
31 class Postfix
32 {
33 unsigned int m_value;
34 public:
35 Postfix( const char* postfix ) : m_value( atoi( postfix ) ){
36 }
37 unsigned int number() const {
38         return m_value;
39 }
40 void write( char* buffer ) const {
41         sprintf( buffer, "%u", m_value );
42 }
43 Postfix& operator++(){
44         ++m_value;
45         return *this;
46 }
47 bool operator<( const Postfix& other ) const {
48         return m_value < other.m_value;
49 }
50 bool operator==( const Postfix& other ) const {
51         return m_value == other.m_value;
52 }
53 bool operator!=( const Postfix& other ) const {
54         return !operator==( other );
55 }
56 };
57
58 #else
59 class Postfix
60 {
61 std::pair<unsigned int, unsigned int> m_value;
62 public:
63 Postfix( unsigned int number, unsigned int leading_zeros )
64         : m_value( leading_zeros, number ){
65 }
66 Postfix( const char* postfix )
67         : m_value( number_count_leading_zeros( postfix ), atoi( postfix ) ){
68 }
69 unsigned int number() const {
70         return m_value.second;
71 }
72 unsigned int leading_zeros() const {
73         return m_value.first;
74 }
75 void write( char* buffer ){
76         for ( unsigned int count = 0; count < m_value.first; ++count, ++buffer )
77                 *buffer = '0';
78         sprintf( buffer, "%u", m_value.second );
79 }
80 Postfix& operator++(){
81         ++m_value.second;
82         if ( m_value.first != 0 && m_value.second % 10 == 0 ) {
83                 --m_value.first;
84         }
85         return *this;
86 }
87 bool operator<( const Postfix& other ) const {
88         return m_value < other.m_value;
89 }
90 bool operator==( const Postfix& other ) const {
91         return m_value == other.m_value;
92 }
93 bool operator!=( const Postfix& other ) const {
94         return !operator==( other );
95 }
96 };
97
98 #endif
99
100 typedef std::pair<CopiedString, Postfix> name_t;
101
102 inline void name_write( char* buffer, name_t name ){
103         strcpy( buffer, name.first.c_str() );
104         name.second.write( buffer + strlen( name.first.c_str() ) );
105 }
106
107 inline name_t name_read( const char* name ){
108         const char* end = name + strlen( name );
109         for ( const char* p = end; end != name; --p )
110         {
111                 if ( strrchr( "1234567890", *p ) == NULL ) {
112                         break;
113                 }
114                 end = p;
115         }
116
117         return name_t( CopiedString( StringRange( name, end ) ), Postfix( end ) );
118 }
119
120
121 class PostFixes
122 {
123 public:
124 typedef std::map<Postfix, unsigned int> postfixes_t;
125 postfixes_t m_postfixes;
126
127 private:
128 Postfix find_first_empty() const {
129         Postfix postfix( "1" );
130         for ( postfixes_t::const_iterator i = m_postfixes.find( postfix ); i != m_postfixes.end(); ++i, ++postfix )
131         {
132                 if ( ( *i ).first != postfix ) {
133                         break;
134                 }
135         }
136         return postfix;
137 }
138
139 public:
140 Postfix make_unique( Postfix postfix ) const {
141         postfixes_t::const_iterator i = m_postfixes.find( postfix );
142         if ( i == m_postfixes.end() ) {
143                 return postfix;
144         }
145         else
146         {
147                 return find_first_empty();
148         }
149 }
150
151 void insert( Postfix postfix ){
152         postfixes_t::iterator i = m_postfixes.find( postfix );
153         if ( i == m_postfixes.end() ) {
154                 m_postfixes.insert( postfixes_t::value_type( postfix, 1 ) );
155         }
156         else
157         {
158                 ++( *i ).second;
159         }
160 }
161
162 void erase( Postfix postfix ){
163         postfixes_t::iterator i = m_postfixes.find( postfix );
164         if ( i == m_postfixes.end() ) {
165                 // error
166         }
167         else
168         {
169                 if ( --( *i ).second == 0 ) {
170                         m_postfixes.erase( i );
171                 }
172         }
173 }
174
175 bool empty() const {
176         return m_postfixes.empty();
177 }
178 };
179
180
181 class UniqueNames
182 {
183 typedef std::map<CopiedString, PostFixes> names_t;
184 names_t m_names;
185 public:
186 name_t make_unique( const name_t& name ) const {
187         char buf[80];
188         name_t r( "","" );
189         name_write( buf, name );
190         globalErrorStream() << "find unique name for " << buf << "\n";
191         globalErrorStream() << "> currently registered names:\n";
192         for ( names_t::const_iterator i = m_names.begin(); i != m_names.end(); ++i )
193         {
194                 globalErrorStream() << ">> " << i->first.c_str() << ": ";
195                 for ( PostFixes::postfixes_t::const_iterator j = i->second.m_postfixes.begin(); j != i->second.m_postfixes.end(); ++j )
196                 {
197                         j->first.write( buf );
198                         globalErrorStream() << " '" << buf << "'";
199                 }
200                 globalErrorStream() << "\n";
201         }
202         names_t::const_iterator i = m_names.find( name.first );
203         if ( i == m_names.end() ) {
204                 r = name;
205         }
206         else
207         {
208                 r = name_t( name.first, ( *i ).second.make_unique( name.second ) );
209         }
210         name_write( buf, r );
211         globalErrorStream() << "> unique name is " << buf << "\n";
212         return r;
213 }
214
215 void insert( const name_t& name ){
216         m_names[name.first].insert( name.second );
217 }
218
219 void erase( const name_t& name ){
220         names_t::iterator i = m_names.find( name.first );
221         if ( i == m_names.end() ) {
222                 ASSERT_MESSAGE( true, "erase: name not found" );
223         }
224         else
225         {
226                 ( *i ).second.erase( name.second );
227                 if ( ( *i ).second.empty() ) {
228                         m_names.erase( i );
229                 }
230         }
231 }
232
233 bool empty() const {
234         return m_names.empty();
235 }
236 };
237
238
239
240 #if 0
241
242 #undef ERROR_MESSAGE
243 #define ERROR_MESSAGE( message )
244
245 class TestUniqueName
246 {
247 void name_check_equal( const name_t& name, const char* string, unsigned int postfix ){
248         ASSERT_MESSAGE( strcmp( name.first.c_str(), string ) == 0
249                                         && name.second.number() == postfix,
250                                         "test failed!" );
251 }
252 void test_refcount(){
253         Names names;
254
255         names.insert( name_t( "func_bleh_", "100" ) );
256         names.insert( name_t( "func_bleh_", "100" ) );
257         names.insert( name_t( "func_bleh_", "100" ) );
258
259
260         names.erase( name_t( "func_bleh_", "100" ) );
261         names.erase( name_t( "func_bleh_", "100" ) );
262         names.erase( name_t( "func_bleh_", "100" ) );
263
264         ASSERT_MESSAGE( names.empty(), "test failed!" );
265 }
266
267 void test_make_unique(){
268         Names names;
269
270         {
271                 name_t name( names.make_unique( name_t( "func_bleh_", "01" ) ) );
272                 name_check_equal( name, "func_bleh_", 1 );
273                 names.insert( name );
274         }
275         {
276                 name_t name( names.make_unique( name_t( "func_bleh_", "04" ) ) );
277                 name_check_equal( name, "func_bleh_", 4 );
278                 names.insert( name );
279         }
280         {
281                 name_t name( names.make_unique( name_t( "func_bleh_", "04" ) ) );
282                 name_check_equal( name, "func_bleh_", 2 );
283                 names.insert( name );
284         }
285         {
286                 name_t name( names.make_unique( name_t( "func_bleh_", "1" ) ) );
287                 name_check_equal( name, "func_bleh_", 3 );
288                 names.insert( name );
289         }
290         {
291                 name_t name( names.make_unique( name_t( "func_bleh_", "2" ) ) );
292                 name_check_equal( name, "func_bleh_", 5 );
293                 names.insert( name );
294         }
295         {
296                 name_t name( names.make_unique( name_t( "func_bleh_", "3" ) ) );
297                 name_check_equal( name, "func_bleh_", 6 );
298                 names.insert( name );
299         }
300
301         names.erase( name_t( "func_bleh_", "1" ) );
302         names.erase( name_t( "func_bleh_", "2" ) );
303         names.erase( name_t( "func_bleh_", "3" ) );
304         names.erase( name_t( "func_bleh_", "4" ) );
305         names.erase( name_t( "func_bleh_", "5" ) );
306         names.erase( name_t( "func_bleh_", "6" ) );
307
308         ASSERT_MESSAGE( names.empty(), "test failed!" );
309 }
310 public:
311 TestUniqueName(){
312         test_refcount();
313         test_make_unique();
314 }
315 };
316
317 const TestUniqueName g_testuniquename;
318
319 #endif
320
321
322 #endif