00001 // Filename: eggGroupUniquifier.cxx 00002 // Created by: drose (22Feb01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "eggGroupUniquifier.h" 00020 #include "eggGroup.h" 00021 00022 #include <notify.h> 00023 00024 #include <ctype.h> 00025 00026 TypeHandle EggGroupUniquifier::_type_handle; 00027 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: EggGroupUniquifier::Constructor 00031 // Access: Public 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 EggGroupUniquifier:: 00035 EggGroupUniquifier() { 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: EggGroupUniquifier::get_category 00040 // Access: Public 00041 // Description: Returns the category name into which the given node 00042 // should be collected, or the empty string if the 00043 // node's name should be left alone. 00044 //////////////////////////////////////////////////////////////////// 00045 string EggGroupUniquifier:: 00046 get_category(EggNode *node) { 00047 if (node->is_of_type(EggGroup::get_class_type()) && node->has_name()) { 00048 return "group"; 00049 } 00050 00051 return string(); 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: EggGroupUniquifier::filter_name 00056 // Access: Public, Virtual 00057 // Description: Returns the name of the given node, or at least the 00058 // name it should be. This provides a hook to adjust 00059 // the name before attempting to uniquify it, if 00060 // desired, for instance to remove invalid characters. 00061 //////////////////////////////////////////////////////////////////// 00062 string EggGroupUniquifier:: 00063 filter_name(EggNode *node) { 00064 string name = node->get_name(); 00065 nassertr(!name.empty(), string()); 00066 00067 string result; 00068 00069 // First, replace characters not A-Z, a-z, 0-9, or '_' with 00070 // underscore, and remove consecutive underscores. 00071 string::const_iterator pi; 00072 bool last_underscore = false; 00073 for (pi = name.begin(); pi != name.end(); ++pi) { 00074 if (isalnum(*pi)) { 00075 result += *pi; 00076 last_underscore = false; 00077 00078 } else if (!last_underscore) { 00079 result += '_'; 00080 last_underscore = true; 00081 } 00082 } 00083 00084 // Next, ensure the name does not begin with a digit. 00085 nassertr(!result.empty(), string()); 00086 if (isdigit(result[0])) { 00087 result = "_" + result; 00088 } 00089 00090 return result; 00091 } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function: EggGroupUniquifier::generate_name 00095 // Access: Public, Virtual 00096 // Description: Generates a new name for the given node when its 00097 // existing name clashes with some other node. This 00098 // function will be called repeatedly, if necessary, 00099 // until it returns a name that actually is unique. 00100 // 00101 // The category is the string returned by 00102 // get_category(), and index is a uniquely-generated 00103 // number that may be useful for synthesizing the name. 00104 //////////////////////////////////////////////////////////////////// 00105 string EggGroupUniquifier:: 00106 generate_name(EggNode *node, const string &category, int index) { 00107 ostringstream str; 00108 str << node->get_name() << "_group" << index; 00109 return str.str(); 00110 }