Panda3D
 All Classes Functions Variables Enumerations
nameUniquifier.cxx
1 // Filename: nameUniquifier.cxx
2 // Created by: drose (16Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "nameUniquifier.h"
16 
17 #include "pnotify.h"
18 
19 #include <stdio.h>
20 
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: NameUniquifier::Constructor
24 // Access: Public
25 // Description: Creates a new NameUniquifier.
26 //
27 // The separator string is used to separate the original
28 // name (or supplied prefix) and the generated number
29 // when a name must be generated.
30 //
31 // If the original name is empty, the empty string is
32 // used, followed by the generated number.
33 ////////////////////////////////////////////////////////////////////
35 NameUniquifier(const string &separator,
36  const string &empty) :
37  _separator(separator),
38  _empty(empty)
39 {
40  _counter = 0;
41 
42  if (_empty.empty()) {
43  _empty = _separator;
44  }
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: NameUniquifier::Destructor
49 // Access: Public
50 // Description:
51 ////////////////////////////////////////////////////////////////////
52 NameUniquifier::
53 ~NameUniquifier() {
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: NameUniquifier::add_name_body
58 // Access: Private
59 // Description: The actual implementation of the two flavors of
60 // add_name().
61 //
62 // If name is nonempty and so far unique, returns it
63 // unchanged.
64 //
65 // Otherwise, generates and returns a new name according
66 // to the following rules:
67 //
68 // If the prefix is empty, the new name is the
69 // NameUniquifier's "empty" string followed by a number,
70 // or the "separator" string if the "empty" string is
71 // empty.
72 //
73 // If the prefix is nonempty, the new name is the
74 // prefix, followed by the NameUniquifier's "separator"
75 // string, followed by a number.
76 ////////////////////////////////////////////////////////////////////
77 string NameUniquifier::
78 add_name_body(const string &name, const string &prefix) {
79  if (!name.empty()) {
80  if (_names.insert(name).second) {
81  // The name was successfully inserted into the set; therefore,
82  // it's unique. Return it.
83  return name;
84  }
85  }
86 
87  // The name was not successfully inserted; there must be another one
88  // already. Make up a new one.
89 
90  // Keep trying to make up names until we make one that's unique.
91  string temp_name;
92  do {
93  static const int max_len = 16;
94  char num_str[max_len];
95  sprintf(num_str, "%d", ++_counter);
96  nassertr((int)strlen(num_str) <= max_len, "");
97 
98  if (prefix.empty()) {
99  temp_name = _empty + num_str;
100  } else {
101  temp_name = prefix + _separator + num_str;
102  }
103  } while (!_names.insert(temp_name).second);
104 
105  return temp_name;
106 }
107 
NameUniquifier(const string &separator=string(), const string &empty=string())
Creates a new NameUniquifier.