UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
entitiesdef.cpp File Reference

Handles definition of entities, parsing them from entities.ufo. More...

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include "shared.h"
#include "parse.h"
#include "entitiesdef.h"

Go to the source code of this file.

Macros

#define ED_MAX_KEYS_PER_ENT   32
 
#define ED_MAX_TOKEN_LEN   512
 
#define ED_MAX_ERR_LEN   512
 
#define ED_RETURN_ERROR(...)
 write an error message and exit the current function returning ED_ERROR More...
 
#define ED_TEST_RETURN_ERROR(condition,...)
 test a condition, write an error message and exit the current function with ED_ERROR More...
 
#define ED_PASS_ERROR(function_call)
 if the function returns ED_ERROR, then the function that the macro is in also returns ED_ERROR. Note that the called function is expected to set lastErr, probably by using the ED_TEST_RETURN_ERROR macro More...
 
#define ED_PASS_ERROR_EXTRAMSG(function_call,...)
 if the function returns ED_ERROR, then the function that the macro is in also returns ED_ERROR. Note that the called function is expected to set lastErr, probably by using the ED_TEST_RETURN_ERROR macro. this macro then appends extra detail to the message to give context More...
 

Functions

static int ED_AllocEntityDef (entityKeyDef_t *newKeyDefs, int numKeyDefs, int entityIndex)
 allocate space for key defs etc, pointers for which are stored in the entityDef_t More...
 
static entityKeyDef_tED_FindKeyDefInArray (entityKeyDef_t keyDefs[], int numDefs, const char *name, int parseMode)
 search for an existing keyDef to add a new parsed pair info to. More...
 
static int ED_Type2Constant (const char *strType)
 converts a string representation of a type (eg V_FLOAT) to the appropriate internal constant integer More...
 
static const char * ED_Constant2Type (int constInt)
 converts an internal constant integer to a string representation of a type (eg V_FLOAT) More...
 
static int ED_GetIntVectorFromString (const char *str, int v[], const int n)
 parses an int array from a string More...
 
int ED_GetIntVector (const entityKeyDef_t *kd, int v[], const int n)
 parses a value from the definition More...
 
static int ED_CheckNumber (const char *value, const int floatOrInt, const int insistPositive, int_float_tu *parsedNumber)
 checks that a string represents a single number More...
 
static int ED_CheckRange (const entityKeyDef_t *keyDef, const int type, const int index, int_float_tu parsedNumber)
 check a value against the range for the key More...
 
static int ED_CheckNumericType (const entityKeyDef_t *keyDef, const char *value, const int type)
 tests if a value string matches the type for this key. this includes each element of a numeric array. Also checks value against range def, if one exists. More...
 
int ED_Check (const char *classname, const char *key, const char *value)
 tests if a value string matches the type for this key. Also checks the value against the range, if one was defined. More...
 
int ED_CheckKey (const entityKeyDef_t *kd, const char *value)
 as ED_Check, but where the entity and key are known, so takes different arguments. More...
 
static int ED_ParseType (entityKeyDef_t *kd, const char *parsedToken)
 takes a type string (eg "V_FLOAT 6") and configures entity def More...
 
static int ED_Block2Constant (const char *blockName)
 converts a block name (eg "optional") to an constant (eg ED_OPTIONAL). More...
 
static const char * ED_Constant2Block (int constInt)
 converts an internal constant integer to a string representation of a type (eg V_FLOAT) More...
 
static int ED_AllocRange (entityKeyDef_t *kd, const char *rangeStr)
 
static int ED_PairParsed (entityKeyDef_t keyDefsBuf[], int *numKeyDefsSoFar_p, const char *newName, const char *newVal, const int mode)
 
static int ED_ParseEntities (const char **data_p)
 
static int ED_CheckDefaultTypes (void)
 checks if the default block entries meet the type and range definitions. More...
 
static int ED_ProcessRanges (void)
 finish parsing ranges. Could not be done earlier as would not have necessarily known types and defaults. parses values in ranges into ints or floats and tests ranges against types and defaults against ranges. More...
 
int ED_Parse (const char *data_p)
 
const char * ED_GetLastError (void)
 
const entityKeyDef_tED_GetKeyDef (const char *classname, const char *keyname, const int abstract)
 searches for the parsed key def More...
 
const entityKeyDef_tED_GetKeyDefEntity (const entityDef_t *ed, const char *keyname, const int abstract)
 searches for the parsed key def, when the entity def is known More...
 
const entityDef_tED_GetEntityDef (const char *classname)
 searches for the parsed entity def by classname More...
 
void ED_Free (void)
 

Variables

static char lastErr [ED_MAX_ERR_LEN]
 
static char lastErrExtra [ED_MAX_ERR_LEN]
 
int numEntityDefs
 
entityDef_t entityDefs [ED_MAX_DEFS+1]
 

Detailed Description

Handles definition of entities, parsing them from entities.ufo.

Definition in file entitiesdef.cpp.

Macro Definition Documentation

#define ED_MAX_ERR_LEN   512

Definition at line 40 of file entitiesdef.cpp.

#define ED_MAX_KEYS_PER_ENT   32

Definition at line 38 of file entitiesdef.cpp.

Referenced by ED_PairParsed(), and ED_ParseEntities().

#define ED_MAX_TOKEN_LEN   512

Definition at line 39 of file entitiesdef.cpp.

Referenced by ED_ParseEntities().

#define ED_PASS_ERROR (   function_call)
Value:
if ((function_call) == ED_ERROR) { \
return ED_ERROR; \
}
#define ED_ERROR
Definition: entitiesdef.h:32

if the function returns ED_ERROR, then the function that the macro is in also returns ED_ERROR. Note that the called function is expected to set lastErr, probably by using the ED_TEST_RETURN_ERROR macro

Definition at line 73 of file entitiesdef.cpp.

Referenced by ED_CheckNumericType(), ED_GetIntVector(), ED_PairParsed(), ED_Parse(), ED_ParseEntities(), ED_ParseType(), and ED_ProcessRanges().

#define ED_PASS_ERROR_EXTRAMSG (   function_call,
  ... 
)
Value:
if ((function_call) == ED_ERROR) { \
snprintf(lastErrExtra, sizeof(lastErr), __VA_ARGS__); \
strncat(lastErr, lastErrExtra, sizeof(lastErr) - strlen(lastErr) -1); \
return ED_ERROR; \
}
static char lastErrExtra[ED_MAX_ERR_LEN]
Definition: entitiesdef.cpp:43
#define ED_ERROR
Definition: entitiesdef.h:32
static char lastErr[ED_MAX_ERR_LEN]
Definition: entitiesdef.cpp:42

if the function returns ED_ERROR, then the function that the macro is in also returns ED_ERROR. Note that the called function is expected to set lastErr, probably by using the ED_TEST_RETURN_ERROR macro. this macro then appends extra detail to the message to give context

Definition at line 84 of file entitiesdef.cpp.

Referenced by ED_CheckDefaultTypes(), and ED_CheckNumericType().

#define ED_RETURN_ERROR (   ...)
Value:
{ \
snprintf(lastErr, sizeof(lastErr), __VA_ARGS__); \
return ED_ERROR; \
}
#define ED_ERROR
Definition: entitiesdef.h:32
static char lastErr[ED_MAX_ERR_LEN]
Definition: entitiesdef.cpp:42

write an error message and exit the current function returning ED_ERROR

See also
ED_TEST_RETURN_ERROR

Definition at line 52 of file entitiesdef.cpp.

Referenced by ED_AllocRange(), ED_Block2Constant(), ED_CheckKey(), ED_CheckNumber(), ED_CheckRange(), ED_PairParsed(), ED_ParseEntities(), ED_ProcessRanges(), and ED_Type2Constant().

#define ED_TEST_RETURN_ERROR (   condition,
  ... 
)
Value:
if (condition) { \
snprintf(lastErr, sizeof(lastErr), __VA_ARGS__); \
return ED_ERROR; \
}
#define ED_ERROR
Definition: entitiesdef.h:32
static char lastErr[ED_MAX_ERR_LEN]
Definition: entitiesdef.cpp:42

test a condition, write an error message and exit the current function with ED_ERROR

Definition at line 62 of file entitiesdef.cpp.

Referenced by ED_AllocEntityDef(), ED_CheckKey(), ED_CheckNumber(), ED_CheckNumericType(), ED_CheckRange(), ED_GetIntVectorFromString(), ED_PairParsed(), ED_Parse(), ED_ParseEntities(), ED_ParseType(), and ED_ProcessRanges().

Function Documentation

static int ED_AllocEntityDef ( entityKeyDef_t newKeyDefs,
int  numKeyDefs,
int  entityIndex 
)
static

allocate space for key defs etc, pointers for which are stored in the entityDef_t

Returns
ED_ERROR or ED_OK.

Definition at line 95 of file entitiesdef.cpp.

References entityDef_s::classname, entityKeyDef_s::desc, ED_OK, ED_TEST_RETURN_ERROR, entityDef_s::keyDefs, entityDef_s::numKeyDefs, and OBJZERO.

Referenced by ED_ParseEntities().

static int ED_AllocRange ( entityKeyDef_t kd,
const char *  rangeStr 
)
static
static int ED_Block2Constant ( const char *  blockName)
static

converts a block name (eg "optional") to an constant (eg ED_OPTIONAL).

Returns
the parse mode or ED_ERROR

Definition at line 442 of file entitiesdef.cpp.

References ED_ABSTRACT, ED_DEFAULT, ED_MANDATORY, ED_MODE_TYPE, ED_OPTIONAL, ED_RANGE, ED_RETURN_ERROR, and Q_streq.

Referenced by ED_ParseEntities().

int ED_Check ( const char *  classname,
const char *  key,
const char *  value 
)

tests if a value string matches the type for this key. Also checks the value against the range, if one was defined.

Returns
ED_OK or ED_ERROR
See also
ED_GetLastError
Note
abstract (radiant) keys may not have types. keys used here must be declared in entities.ufo in an optional or mandatory block.

Definition at line 357 of file entitiesdef.cpp.

References ED_CheckKey(), ED_ERROR, and ED_GetKeyDef().

static int ED_CheckDefaultTypes ( void  )
static

checks if the default block entries meet the type and range definitions.

Returns
ED_ERROR or ED_OK
See also
CheckLastError

Definition at line 649 of file entitiesdef.cpp.

References entityKeyDef_s::defaultVal, ED_CheckKey(), ED_OK, ED_PASS_ERROR_EXTRAMSG, entityDef_s::keyDefs, entityKeyDef_s::name, and entityDef_s::numKeyDefs.

Referenced by ED_Parse().

int ED_CheckKey ( const entityKeyDef_t kd,
const char *  value 
)

as ED_Check, but where the entity and key are known, so takes different arguments.

Returns
ED_ERROR or ED_OK
See also
ED_GetLastError

Definition at line 372 of file entitiesdef.cpp.

References ED_CheckNumericType(), ED_KEY_TYPE, ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, ED_TYPE_STRING, and entityKeyDef_s::flags.

Referenced by CheckEntities(), ED_Check(), and ED_CheckDefaultTypes().

static int ED_CheckNumber ( const char *  value,
const int  floatOrInt,
const int  insistPositive,
int_float_tu parsedNumber 
)
static

checks that a string represents a single number

Parameters
valueThe string to parse from
floatOrIntone of ED_TYPE_FLOAT or ED_TYPE_INT
insistPositiveif 1, then tests for the number being greater than or equal to zero.
parsedNumberThe result as a union
See also
ED_CheckNumericType
Note
disallows hex, inf, NaN, numbers with junk on the end (eg -0123junk)
Returns
ED_OK or ED_ERROR
See also
ED_GetLastError
Note
the parsed numbers are stored for later use in lastCheckedInt and lastCheckedFloat

Definition at line 226 of file entitiesdef.cpp.

References ED_Constant2Type(), ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, int_float_u::f, int_float_u::i, and int().

Referenced by ED_CheckNumericType(), ED_ParseType(), and ED_ProcessRanges().

static int ED_CheckNumericType ( const entityKeyDef_t keyDef,
const char *  value,
const int  type 
)
static

tests if a value string matches the type for this key. this includes each element of a numeric array. Also checks value against range def, if one exists.

Parameters
keyDefThe descriptor of the key/value pair
valueThe string to parse from
typeone of ED_TYPE_FLOAT, ED_TYPE_INT or ED_TYPE_BOOL
Returns
ED_OK or ED_ERROR (call ED_GetLastError)

Definition at line 321 of file entitiesdef.cpp.

References Com_Parse(), ED_CheckNumber(), ED_CheckRange(), ED_INSIST_POSITIVE, ED_OK, ED_PASS_ERROR, ED_PASS_ERROR_EXTRAMSG, ED_TEST_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, entityKeyDef_s::flags, i, entityKeyDef_s::name, and entityKeyDef_s::vLen.

Referenced by ED_CheckKey().

static int ED_CheckRange ( const entityKeyDef_t keyDef,
const int  type,
const int  index,
int_float_tu  parsedNumber 
)
static

check a value against the range for the key

Parameters
keyDefThe descriptor of the key/value pair
typeeither ED_TYPE_FLOAT, ED_TYPE_INT or ED_TYPE_BOOL
indexthe index of the number being checked in the value. eg angles "90 180", 90 is at 0, 180 is at 1.
parsedNumberThe value to check
Note
checks lastCheckedInt or lastCheckedFloat against the range in the supplied keyDef.
Returns
ED_ERROR or ED_OK

Definition at line 269 of file entitiesdef.cpp.

References entityKeyRange_s::continuous, ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, int_float_u::f, entityKeyRange_s::fArr, int_float_u::i, entityKeyRange_s::iArr, entityKeyDef_s::name, entityKeyRange_s::numElements, entityKeyDef_s::numRanges, entityKeyDef_s::ranges, and entityKeyRange_s::str.

Referenced by ED_CheckNumericType().

static const char* ED_Constant2Block ( int  constInt)
static

converts an internal constant integer to a string representation of a type (eg V_FLOAT)

Returns
the string, or nullptr if the integer is not recognised.

Definition at line 465 of file entitiesdef.cpp.

References ED_ABSTRACT, ED_DEFAULT, ED_MANDATORY, ED_MODE_TYPE, ED_OPTIONAL, ED_RANGE, and lastErr.

Referenced by ED_PairParsed().

static const char* ED_Constant2Type ( int  constInt)
static

converts an internal constant integer to a string representation of a type (eg V_FLOAT)

Returns
the string, or nullptr if the integer is not recognised.

Definition at line 157 of file entitiesdef.cpp.

References ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, ED_TYPE_STRING, and lastErr.

Referenced by ED_CheckNumber().

static entityKeyDef_t* ED_FindKeyDefInArray ( entityKeyDef_t  keyDefs[],
int  numDefs,
const char *  name,
int  parseMode 
)
static

search for an existing keyDef to add a new parsed pair info to.

Returns
a pointer to the entity def or nullptr if it is not found

Definition at line 121 of file entitiesdef.cpp.

References ED_ABSTRACT, entityKeyDef_s::flags, i, entityKeyDef_s::name, and Q_streq.

Referenced by ED_PairParsed().

const entityDef_t* ED_GetEntityDef ( const char *  classname)

searches for the parsed entity def by classname

Returns
nullptr if the entity def is not found. call ED_GetLastError to get a relevant message.

Definition at line 815 of file entitiesdef.cpp.

References entityDef_s::classname, lastErr, entityDef_s::numKeyDefs, and Q_streq.

Referenced by CheckEntities(), ED_GetKeyDef(), and ED_ParseEntities().

int ED_GetIntVector ( const entityKeyDef_t kd,
int  v[],
const int  n 
)

parses a value from the definition

Parameters
kdthe key definition to parse from
[out]vthe array of int to put the answer in it must have enough space for n elements.
nthe number of elements expected in the vector
Returns
ED_ERROR or ED_OK
See also
ED_GetLastError.

Definition at line 208 of file entitiesdef.cpp.

References entityKeyDef_s::desc, ED_GetIntVectorFromString(), ED_OK, and ED_PASS_ERROR.

Referenced by Check_InfoStartAligned().

static int ED_GetIntVectorFromString ( const char *  str,
int  v[],
const int  n 
)
static

parses an int array from a string

Parameters
strthe string to parse
[out]vthe array of int to put the answer in it must have enough space for n elements.
nthe number of elements expected in the vector
Returns
ED_ERROR or ED_OK
See also
ED_GetLastError.

Definition at line 183 of file entitiesdef.cpp.

References Com_Parse(), ED_OK, ED_TEST_RETURN_ERROR, and i.

Referenced by ED_GetIntVector().

const entityKeyDef_t* ED_GetKeyDef ( const char *  classname,
const char *  keyname,
const int  abstract 
)

searches for the parsed key def

Parameters
classnameThe name, eg. "func_door"
keynameThe key we are looking for
abstractsend abstract to find an abstract key with this name
Returns
nullptr if the entity def or key def is not found. call ED_GetLastError to get a relevant message.

Definition at line 775 of file entitiesdef.cpp.

References ED_GetEntityDef(), and ED_GetKeyDefEntity().

Referenced by ED_Check().

const entityKeyDef_t* ED_GetKeyDefEntity ( const entityDef_t ed,
const char *  keyname,
const int  abstract 
)

searches for the parsed key def, when the entity def is known

Parameters
edThe list of keys for the entity
keynameThe key we are looking for
abstractsend a nonzero value if the abstract (radiant - not in any block) version of the key is required
Returns
nullptr if the entity def or key def is not found. call ED_GetLastError to get a relevant message.

Definition at line 789 of file entitiesdef.cpp.

References entityDef_s::classname, ED_ABSTRACT, entityKeyDef_s::flags, entityDef_s::keyDefs, lastErr, entityKeyDef_s::name, and Q_streq.

Referenced by Check_InfoStartAligned(), CheckEntities(), and ED_GetKeyDef().

const char* ED_GetLastError ( void  )

Definition at line 763 of file entitiesdef.cpp.

References lastErr.

Referenced by Check_InfoStartAligned(), Check_InitEntityDefs(), and CheckEntities().

static int ED_PairParsed ( entityKeyDef_t  keyDefsBuf[],
int numKeyDefsSoFar_p,
const char *  newName,
const char *  newVal,
const int  mode 
)
static
int ED_Parse ( const char *  data_p)

parses entity definitions from entities.ufo

Returns
ED_OK or ED_ERR
See also
ED_GetLastErr

Definition at line 738 of file entitiesdef.cpp.

References ED_CheckDefaultTypes(), ED_OK, ED_ParseEntities(), ED_PASS_ERROR, ED_ProcessRanges(), ED_TEST_RETURN_ERROR, lastErr, numEntityDefs, and OBJZERO.

Referenced by Check_InitEntityDefs(), and TEST_F().

static int ED_ParseType ( entityKeyDef_t kd,
const char *  parsedToken 
)
static

takes a type string (eg "V_FLOAT 6") and configures entity def

Returns
ED_ERROR or ED_OK

Definition at line 394 of file entitiesdef.cpp.

References Com_Parse(), ED_CheckNumber(), ED_ERROR, ED_INSIST_POSITIVE, ED_OK, ED_PASS_ERROR, ED_TEST_RETURN_ERROR, ED_Type2Constant(), ED_TYPE_INT, ED_TYPE_STRING, entityKeyDef_s::flags, entityKeyDef_s::name, Q_streq, type, and entityKeyDef_s::vLen.

Referenced by ED_PairParsed().

static int ED_ProcessRanges ( void  )
static

finish parsing ranges. Could not be done earlier as would not have necessarily known types and defaults. parses values in ranges into ints or floats and tests ranges against types and defaults against ranges.

Returns
ED_ERROR or ED_OK

Definition at line 668 of file entitiesdef.cpp.

References Com_Parse(), entityKeyRange_s::continuous, ED_CheckNumber(), ED_INSIST_POSITIVE, ED_KEY_TYPE, ED_OK, ED_PASS_ERROR, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, ED_TYPE_STRING, entityKeyRange_s::fArr, i, entityKeyRange_s::iArr, int(), entityKeyDef_s::name, entityKeyRange_s::numElements, entityDef_s::numKeyDefs, Q_streq, and entityKeyRange_s::str.

Referenced by ED_Parse().

static int ED_Type2Constant ( const char *  strType)
static

converts a string representation of a type (eg V_FLOAT) to the appropriate internal constant integer

Returns
the constant, or ED_ERROR if strType is not recognised

Definition at line 138 of file entitiesdef.cpp.

References ED_RETURN_ERROR, ED_TYPE_BOOL, ED_TYPE_FLOAT, ED_TYPE_INT, ED_TYPE_STRING, and Q_streq.

Referenced by ED_ParseType().

Variable Documentation

entityDef_t entityDefs[ED_MAX_DEFS+1]

Definition at line 46 of file entitiesdef.cpp.

Referenced by Check_Stats(), and TEST_F().

char lastErr[ED_MAX_ERR_LEN]
static

for storing last error message

Definition at line 42 of file entitiesdef.cpp.

Referenced by ED_Constant2Block(), ED_Constant2Type(), ED_GetEntityDef(), ED_GetKeyDefEntity(), ED_GetLastError(), and ED_Parse().

char lastErrExtra[ED_MAX_ERR_LEN]
static

temporary storage for extra information to be added to lastErr

Definition at line 43 of file entitiesdef.cpp.

int numEntityDefs