Torque3D Documentation / _generateds / editorFunctions.cpp

editorFunctions.cpp

Engine/source/gui/editor/editorFunctions.cpp

More...

Public Functions

bool
validateObjectName(const char * data, const SimObject * object)

Detailed Description

Public Functions

validateObjectName(const char * data, const SimObject * object)

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#include "platform/platform.h"
25#include "gui/editor/editorFunctions.h"
26
27#include "console/simObject.h"
28
29
30bool validateObjectName( const char *data, const SimObject *object )
31{
32   if( !data || !data[ 0 ] )
33      return true;
34
35   bool isValidId = true;
36   if( !dIsalpha( data[ 0 ] ) && data[ 0 ] != '_' )
37      isValidId = false;
38   else
39   {
40      for( U32 i = 1; data[ i ] != '\0'; ++ i )
41      {
42         if( !dIsalnum( data[ i ] ) && data[ i ] != '_' )
43         {
44            isValidId = false;
45            break;
46         }
47      }
48   }
49
50   if( !isValidId )
51   {
52      Platform::AlertOK( "Invalid Object Name", avar( "'%s' is not a valid "
53         "object name.  Please choose a name that begins with a letter or "
54         "underscore and is otherwise comprised exclusively of letters, "
55         "digits, and/or underscores.", data ) );
56      return false;
57   }
58
59   SimObject *pTemp = NULL;
60   if ( Sim::findObject( data, pTemp ) && pTemp != object )
61   {
62      const char* filename = pTemp->getFilename();
63      if ( !filename || !filename[0] )
64         filename = "an unknown file";
65
66      Platform::AlertOK( "Invalid Object Name", avar( "Object names must be unique, "
67         "and there is an existing %s object with the name '%s' (defined in %s).  "
68         "Please choose another name.", pTemp->getClassName(), data, filename ) );
69      return false;
70   }
71
72   if ( AbstractClassRep::findClassRep( data ) )
73   {
74      Platform::AlertOK( "Invalid Object Name", avar( "'%s' is the name of an "
75         "existing TorqueScript class.  Please choose another name.", data ) );
76      return false;
77   }
78
79   return true;
80}
81