entitiesdef.c 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 "parse.h"
#include "entitiesdef.h"
Include dependency graph for entitiesdef.c:

Go to the source code of this file.

Defines

#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
#define ED_TEST_RETURN_ERROR(condition,...)
 test a condition, write an error message and exit the current function with ED_ERROR
#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
#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

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
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.
static int ED_Type2Constant (const char *strType)
 converts a string representation of a type (eg V_FLOAT) to the appropriate internal constant integer
static const char * ED_Constant2Type (int constInt)
 converts an internal constant integer to a string representation of a type (eg V_FLOAT)
static int ED_GetIntVectorFromString (const char *str, int v[], const int n)
 parses an int array from a string
static int ED_GetFloatVectorFromString (const char *str, float v[], const int n)
 parses an float array from a string
int ED_GetDefaultFloat (float *defaultBuf, const int n, const entityKeyDef_t *kd)
 gets the default value for the key
int ED_GetDefaultInt (int *defaultBuf, const int n, const entityKeyDef_t *kd)
 gets the default value for the key
int ED_GetIntVector (const entityKeyDef_t *kd, int v[], const int n)
 parses a value from the definition
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
static int ED_CheckRange (const entityKeyDef_t *keyDef, const int floatOrInt, const int index, int_float_tu parsedNumber)
 check a value against the range for the key
static int ED_CheckNumericType (const entityKeyDef_t *keyDef, const char *value, const int floatOrInt)
 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.
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.
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.
int ED_GetDefaultString (char *defaultBuf, const size_t n, const entityKeyDef_t *kd)
 gets the default value for the key
static int ED_ParseType (entityKeyDef_t *kd, const char *parsedToken)
 takes a type string (eg "V_FLOAT 6") and configures entity def
static int ED_Block2Constant (const char *blockName)
 converts a block name (eg "optional") to an constant (eg ED_OPTIONAL).
static const char * ED_Constant2Block (int constInt)
 converts an internal constant integer to a string representation of a type (eg V_FLOAT)
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.
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.
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
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
const entityDef_tED_GetEntityDef (const char *classname)
 searches for the parsed entity def by classname
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.c.


Define Documentation

#define ED_MAX_ERR_LEN   512

Definition at line 39 of file entitiesdef.c.

#define ED_MAX_KEYS_PER_ENT   32

Definition at line 37 of file entitiesdef.c.

Referenced by ED_PairParsed(), and ED_ParseEntities().

#define ED_MAX_TOKEN_LEN   512

Definition at line 38 of file entitiesdef.c.

Referenced by ED_ParseEntities().

#define ED_PASS_ERROR ( function_call   ) 
Value:
if ((function_call) == ED_ERROR) { \
        return ED_ERROR; \
    }

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 72 of file entitiesdef.c.

Referenced by ED_CheckNumericType(), ED_GetDefaultFloat(), ED_GetDefaultInt(), 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; \
    }

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 83 of file entitiesdef.c.

Referenced by ED_CheckDefaultTypes(), and ED_CheckNumericType().

#define ED_RETURN_ERROR ( ...   ) 
Value:
{ \
        snprintf(lastErr, sizeof(lastErr), __VA_ARGS__); \
        return ED_ERROR; \
    }

write an error message and exit the current function returning ED_ERROR

See also:
ED_TEST_RETURN_ERROR

Definition at line 51 of file entitiesdef.c.

Referenced by 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; \
    }

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

Definition at line 61 of file entitiesdef.c.

Referenced by ED_AllocEntityDef(), ED_AllocRange(), ED_CheckKey(), ED_CheckNumber(), ED_CheckNumericType(), ED_CheckRange(), ED_GetDefaultFloat(), ED_GetDefaultInt(), ED_GetDefaultString(), ED_GetFloatVectorFromString(), 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 94 of file entitiesdef.c.

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

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 505 of file entitiesdef.c.

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

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 402 of file entitiesdef.c.

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 707 of file entitiesdef.c.

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 417 of file entitiesdef.c.

References ED_CheckNumericType(), ED_KEY_TYPE, ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, 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:
insistPositive if 1, then tests for the number being greater than or equal to zero.
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 280 of file entitiesdef.c.

References ED_Constant2Type(), ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, 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  floatOrInt 
) [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:
floatOrInt one of ED_TYPE_FLOAT or ED_TYPE_INT
Returns:
ED_OK or ED_ERROR (call ED_GetLastError)

Definition at line 366 of file entitiesdef.c.

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_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  floatOrInt,
const int  index,
int_float_tu  parsedNumber 
) [static]

check a value against the range for the key

Parameters:
index the index of the number being checked in the value. eg angles "90 180", 90 is at 0, 180 is at 1.
Note:
checks lastCheckedInt or lastCheckedFloat against the range in the supplied keyDef.
Returns:
ED_ERROR or ED_OK

Definition at line 316 of file entitiesdef.c.

References entityKeyRange_s::continuous, ED_OK, ED_RETURN_ERROR, ED_TEST_RETURN_ERROR, 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 NULL if the integer is not recognised.

Definition at line 528 of file entitiesdef.c.

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 NULL if the integer is not recognised.

Definition at line 155 of file entitiesdef.c.

References 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 NULL if it is not found

Definition at line 120 of file entitiesdef.c.

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

Referenced by ED_PairParsed().

void ED_Free ( void   ) 
int ED_GetDefaultFloat ( float *  defaultBuf,
const int  n,
const entityKeyDef_t kd 
)

gets the default value for the key

Parameters:
[in] defaultBuf the calling function is responsible for ensuring this buffer has enough space
n the number of floats the defaultBuf can hold
kd the key definition to get the default for.
See also:
ED_GetKeyDef can be used to find kd.
Returns:
ED_OK or ED_ERROR (no default, or n is too small).
Note:
the defualt int array is tested on parsing

Definition at line 230 of file entitiesdef.c.

References entityKeyDef_s::defaultVal, ED_GetFloatVectorFromString(), ED_OK, ED_PASS_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_FLOAT, and entityKeyDef_s::flags.

int ED_GetDefaultInt ( int *  defaultBuf,
const int  n,
const entityKeyDef_t kd 
)

gets the default value for the key

Parameters:
[in] defaultBuf the calling function is responsible for ensuring this buffer has enough space
n the number of ints the defaultBuf can hold
kd the key definition to get the default for.
See also:
ED_GetKeyDef can be used to find kd.
Returns:
ED_OK or ED_ERROR (no default, or n is too small).
Note:
the defualt int array is tested on parsing

Definition at line 248 of file entitiesdef.c.

References entityKeyDef_s::defaultVal, ED_GetIntVectorFromString(), ED_OK, ED_PASS_ERROR, ED_TEST_RETURN_ERROR, ED_TYPE_INT, and entityKeyDef_s::flags.

int ED_GetDefaultString ( char *  defaultBuf,
const size_t  n,
const entityKeyDef_t kd 
)

gets the default value for the key

Parameters:
[in] defaultBuf the calling function is responsible for ensuring this buffer has enough space
n the number of chars the defualtBuf can hold
kd the key definition to get the default for. E
See also:
D_GetKeyDef can be used to find kd.
Returns:
ED_OK or ED_ERROR (no default, or n is too small).

Definition at line 442 of file entitiesdef.c.

References entityKeyDef_s::defaultVal, ED_OK, and ED_TEST_RETURN_ERROR.

const entityDef_t* ED_GetEntityDef ( const char *  classname  ) 

searches for the parsed entity def by classname

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

Definition at line 875 of file entitiesdef.c.

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

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

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

parses an float array from a string

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

Definition at line 204 of file entitiesdef.c.

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

Referenced by ED_GetDefaultFloat().

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

parses a value from the definition

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

Definition at line 265 of file entitiesdef.c.

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:
str the string to parse
[out] v the array of int to put the answer in it must have enough space for n elements.
n the number of elements expected in the vector
Returns:
ED_ERROR or ED_OK
See also:
ED_GetLastError.

Definition at line 179 of file entitiesdef.c.

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

Referenced by ED_GetDefaultInt(), and ED_GetIntVector().

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

searches for the parsed key def

Parameters:
abstract send abstract to find an abstract key with this name
Returns:
NULL if the entity def or key def is not found. call ED_GetLastError to get a relevant message.

Definition at line 837 of file entitiesdef.c.

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:
abstract send a nonzero value if the abstract (radiant - not in any block) version of the key is required
Returns:
NULL if the entity def or key def is not found. call ED_GetLastError to get a relevant message.

Definition at line 849 of file entitiesdef.c.

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

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

const char* ED_GetLastError ( void   ) 

Definition at line 827 of file entitiesdef.c.

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 798 of file entitiesdef.c.

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

Referenced by Check_InitEntityDefs().

static int ED_ParseEntities ( const char **  data_p  )  [static]
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 457 of file entitiesdef.c.

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, 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 726 of file entitiesdef.c.

References entityDef_s::classname, 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_FLOAT, ED_TYPE_INT, ED_TYPE_STRING, entityKeyRange_s::fArr, entityKeyDef_s::flags, i, entityKeyRange_s::iArr, int(), entityDef_s::keyDefs, entityKeyDef_s::name, entityKeyRange_s::numElements, entityDef_s::numKeyDefs, entityKeyDef_s::numRanges, entityKeyDef_s::ranges, entityKeyRange_s::str, and entityKeyDef_s::vLen.

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.c.

References ED_RETURN_ERROR, ED_TYPE_FLOAT, ED_TYPE_INT, and ED_TYPE_STRING.

Referenced by ED_ParseType().


Variable Documentation

entityDef_t entityDefs[ED_MAX_DEFS+1]

Definition at line 45 of file entitiesdef.c.

Referenced by Check_Stats().

char lastErr[ED_MAX_ERR_LEN] [static]

for storing last error message

Definition at line 41 of file entitiesdef.c.

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 42 of file entitiesdef.c.

Definition at line 44 of file entitiesdef.c.

Referenced by Check_InitEntityDefs(), Check_Stats(), ED_Free(), ED_Parse(), and ED_ParseEntities().


Generated by  doxygen 1.6.2