use std::list<epair_t>

This commit is contained in:
Garux 2021-01-23 17:25:47 +03:00
parent 36b63e1d2c
commit 54c38610bf
9 changed files with 46 additions and 113 deletions

View File

@ -302,11 +302,10 @@ int pk3BSPMain( int argc, char **argv ){
} }
/* Ent keys */ /* Ent keys */
epair_t *ep; for ( const auto& ep : entities[0].epairs )
for ( ep = entities[0].epairs; ep != NULL; ep = ep->next )
{ {
if ( striEqualPrefix( ep->key, "vertexremapshader" ) ) { if ( striEqualPrefix( ep.key.c_str(), "vertexremapshader" ) ) {
sscanf( ep->value, "%*[^;] %*[;] %s", str ); // textures/remap/from;textures/remap/to sscanf( ep.value.c_str(), "%*[^;] %*[;] %s", str ); // textures/remap/from;textures/remap/to
res2list( pk3Shaders, str ); res2list( pk3Shaders, str );
} }
} }
@ -977,11 +976,10 @@ int repackBSPMain( int argc, char **argv ){
} }
/* Ent keys */ /* Ent keys */
epair_t *ep; for ( const auto& ep : entities[0].epairs )
for ( ep = entities[0].epairs; ep != NULL; ep = ep->next )
{ {
if ( striEqualPrefix( ep->key, "vertexremapshader" ) ) { if ( striEqualPrefix( ep.key.c_str(), "vertexremapshader" ) ) {
sscanf( ep->value, "%*[^;] %*[;] %s", str ); // textures/remap/from;textures/remap/to sscanf( ep.value.c_str(), "%*[^;] %*[;] %s", str ); // textures/remap/from;textures/remap/to
res2list( pk3Shaders, str ); res2list( pk3Shaders, str );
} }
} }
@ -1074,14 +1072,6 @@ int repackBSPMain( int argc, char **argv ){
//allocatedBSPBrushes = 0; //allocatedBSPBrushes = 0;
} }
*/ { */ {
for ( const auto& e : entities ){
ep = e.epairs;
while( ep != NULL){
epair_t *ep2free = ep;
ep = ep->next;
free( ep2free );
}
}
entities.clear(); entities.clear();
//Sys_Printf( "freed entities\n" ); //Sys_Printf( "freed entities\n" );
numBSPEntities = 0; numBSPEntities = 0;

View File

@ -523,30 +523,20 @@ void StripTrailing( char *e ){
parses a single quoted "key" "value" pair into an epair struct parses a single quoted "key" "value" pair into an epair struct
*/ */
epair_t *ParseEPair( void ){ void ParseEPair( std::list<epair_t>& epairs ){
/* allocate and clear new epair */
epair_t *e = safe_calloc( sizeof( epair_t ) );
/* handle key */ /* handle key */
if ( strlen( token ) >= ( MAX_KEY - 1 ) ) { /* strip trailing spaces that sometimes get accidentally added in the editor */
Error( "ParseEPair: token too long" ); StripTrailing( token );
} epair_t ep;
ep.key = token;
e->key = copystring( token );
GetToken( false );
/* handle value */ /* handle value */
if ( strlen( token ) >= MAX_VALUE - 1 ) { GetToken( false );
Error( "ParseEpar: token too long" ); StripTrailing( token );
} ep.value = token;
e->value = copystring( token );
/* strip trailing spaces that sometimes get accidentally added in the editor */ if( !ep.key.empty() && !ep.value.empty() )
StripTrailing( e->key ); epairs.emplace_back( ep );
StripTrailing( e->value );
/* return it */
return e;
} }
@ -557,9 +547,6 @@ epair_t *ParseEPair( void ){
*/ */
bool ParseEntity( void ){ bool ParseEntity( void ){
epair_t *e;
/* dummy check */ /* dummy check */
if ( !GetToken( true ) ) { if ( !GetToken( true ) ) {
return false; return false;
@ -581,9 +568,7 @@ bool ParseEntity( void ){
if ( strEqual( token, "}" ) ) { if ( strEqual( token, "}" ) ) {
break; break;
} }
e = ParseEPair(); ParseEPair( mapEnt->epairs );
e->next = mapEnt->epairs;
mapEnt->epairs = e;
} }
/* return to sender */ /* return to sender */
@ -677,9 +662,8 @@ void UnparseEntities( void ){
entity_t *e = &entities[ i ]; entity_t *e = &entities[ i ];
/* get epair */ /* get epair */
if ( e->epairs == NULL ) { if ( e->epairs.empty() ) {
continue; /* ent got removed */ continue; /* ent got removed */
} }
/* ydnar: certain entities get stripped from bsp file */ /* ydnar: certain entities get stripped from bsp file */
const char *classname = ent_classname( e ); const char *classname = ent_classname( e );
@ -694,12 +678,12 @@ void UnparseEntities( void ){
end += 2; end += 2;
/* walk epair list */ /* walk epair list */
for ( epair_t *ep = e->epairs; ep != NULL; ep = ep->next ) for ( const auto& ep : e->epairs )
{ {
/* copy and clean */ /* copy and clean */
strcpy( key, ep->key ); strcpy( key, ep.key.c_str() );
StripTrailing( key ); StripTrailing( key );
strcpy( value, ep->value ); strcpy( value, ep.value.c_str() );
StripTrailing( value ); StripTrailing( value );
/* add to buffer */ /* add to buffer */
@ -730,13 +714,9 @@ void UnparseEntities( void ){
*/ */
void PrintEntity( const entity_t *ent ){ void PrintEntity( const entity_t *ent ){
epair_t *ep;
Sys_Printf( "------- entity %p -------\n", ent ); Sys_Printf( "------- entity %p -------\n", ent );
for ( ep = ent->epairs; ep != NULL; ep = ep->next ) for ( const auto& ep : ent->epairs )
Sys_Printf( "%s = %s\n", ep->key, ep->value ); Sys_Printf( "%s = %s\n", ep.key.c_str(), ep.value.c_str() );
} }
@ -747,25 +727,17 @@ void PrintEntity( const entity_t *ent ){
*/ */
void SetKeyValue( entity_t *ent, const char *key, const char *value ){ void SetKeyValue( entity_t *ent, const char *key, const char *value ){
epair_t *ep;
/* check for existing epair */ /* check for existing epair */
for ( ep = ent->epairs; ep != NULL; ep = ep->next ) for ( auto& ep : ent->epairs )
{ {
if ( EPAIR_EQUAL( ep->key, key ) ) { if ( EPAIR_EQUAL( ep.key.c_str(), key ) ) {
free( ep->value ); ep.value = value;
ep->value = copystring( value );
return; return;
} }
} }
/* create new epair */ /* create new epair */
ep = safe_malloc( sizeof( *ep ) ); ent->epairs.emplace_back( epair_t{ key, value } );
ep->next = ent->epairs;
ent->epairs = ep;
ep->key = copystring( key );
ep->value = copystring( value );
} }
@ -781,10 +753,10 @@ const char *ValueForKey( const entity_t *ent, const char *key ){
} }
/* walk epair list */ /* walk epair list */
for ( epair_t *ep = ent->epairs; ep != NULL; ep = ep->next ) for ( const auto& ep : ent->epairs )
{ {
if ( EPAIR_EQUAL( ep->key, key ) ) { if ( EPAIR_EQUAL( ep.key.c_str(), key ) ) {
return ep->value; return ep.value.c_str();
} }
} }

View File

@ -967,29 +967,26 @@ static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origi
*/ */
static void ConvertEPairs( FILE *f, entity_t *e, bool skip_origin ){ static void ConvertEPairs( FILE *f, entity_t *e, bool skip_origin ){
epair_t *ep;
/* walk epairs */ /* walk epairs */
for ( ep = e->epairs; ep != NULL; ep = ep->next ) for ( const auto& ep : e->epairs )
{ {
/* ignore empty keys/values */ /* ignore empty keys/values */
if ( strEmpty( ep->key ) || strEmpty( ep->value ) ) { if ( ep.key.empty() || ep.value.empty() ) {
continue; continue;
} }
/* ignore model keys with * prefixed values */ /* ignore model keys with * prefixed values */
if ( striEqual( ep->key, "model" ) && ep->value[ 0 ] == '*' ) { if ( striEqual( ep.key.c_str(), "model" ) && ep.value.c_str()[ 0 ] == '*' ) {
continue; continue;
} }
/* ignore origin keys if skip_origin is set */ /* ignore origin keys if skip_origin is set */
if ( skip_origin && striEqual( ep->key, "origin" ) ) { if ( skip_origin && striEqual( ep.key.c_str(), "origin" ) ) {
continue; continue;
} }
/* emit the epair */ /* emit the epair */
fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value ); fprintf( f, "\t\"%s\" \"%s\"\n", ep.key.c_str(), ep.value.c_str() );
} }
} }

View File

@ -428,7 +428,7 @@ void ProcessDecals( void ){
/* any patches? */ /* any patches? */
if ( e.patches == NULL ) { if ( e.patches == NULL ) {
Sys_Warning( "Decal entity without any patch meshes, ignoring.\n" ); Sys_Warning( "Decal entity without any patch meshes, ignoring.\n" );
e.epairs = NULL; /* fixme: leak! */ e.epairs.clear();
continue; continue;
} }

View File

@ -1664,7 +1664,6 @@ void LoadEntityIndexMap( entity_t *e ){
*/ */
static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){
epair_t *ep;
brush_t *brush; brush_t *brush;
parseMesh_t *patch; parseMesh_t *patch;
@ -1737,13 +1736,7 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){
else else
{ {
/* parse a key / value pair */ /* parse a key / value pair */
ep = ParseEPair(); ParseEPair( mapEnt->epairs );
/* ydnar: 2002-07-06 fixed wolf bug with empty epairs */
if ( !strEmpty( ep->key ) && !strEmpty( ep->value ) ) {
ep->next = mapEnt->epairs;
mapEnt->epairs = ep;
}
} }
} }

View File

@ -1424,14 +1424,13 @@ void AddTriangleModels( entity_t *eparent ){
/* get shader remappings */ /* get shader remappings */
std::list<remap_t> remaps; std::list<remap_t> remaps;
for ( epair_t *ep = e->epairs; ep != NULL; ep = ep->next ) for ( const auto& ep : e->epairs )
{ {
/* look for keys prefixed with "_remap" */ /* look for keys prefixed with "_remap" */
if ( !strEmptyOrNull( ep->key ) && !strEmptyOrNull( ep->value ) && if ( striEqualPrefix( ep.key.c_str(), "_remap" ) ) {
striEqualPrefix( ep->key, "_remap" ) ) {
/* create new remapping */ /* create new remapping */
remap_t remap; remap_t remap;
strcpy( remap.from, ep->value ); strcpy( remap.from, ep.value.c_str() );
/* split the string */ /* split the string */
char *split = strchr( remap.from, ';' ); char *split = strchr( remap.from, ';' );

View File

@ -223,7 +223,6 @@ void ParsePatch( bool onlyLights ){
parseMesh_t *pm; parseMesh_t *pm;
mesh_t m; mesh_t m;
bspDrawVert_t *verts; bspDrawVert_t *verts;
epair_t *ep;
vec4_t delta, delta2, delta3; vec4_t delta, delta2, delta3;
bool degenerate; bool degenerate;
float longestCurve; float longestCurve;
@ -268,10 +267,8 @@ void ParsePatch( bool onlyLights ){
// if brush primitives format, we may have some epairs to ignore here // if brush primitives format, we may have some epairs to ignore here
GetToken( true ); GetToken( true );
if ( !strEqual( token, "}" ) && ( g_brushType == BPRIMIT_BP || g_brushType == BPRIMIT_UNDEFINED ) ) { if ( !strEqual( token, "}" ) && ( g_brushType == BPRIMIT_BP || g_brushType == BPRIMIT_UNDEFINED ) ) {
ep = ParseEPair(); std::list<epair_t> dummy;
free( ep->key ); ParseEPair( dummy );
free( ep->value );
free( ep );
} }
else{ else{
UnGetToken(); UnGetToken();

View File

@ -323,10 +323,6 @@ brushType_t;
#define MAX_MAP_ADVERTISEMENTS 30 #define MAX_MAP_ADVERTISEMENTS 30
/* key / value pair sizes in the entities lump */
#define MAX_KEY 32
#define MAX_VALUE 1024
/* the editor uses these predefined yaw angles to orient entities up or down */ /* the editor uses these predefined yaw angles to orient entities up or down */
#define ANGLE_UP -1 #define ANGLE_UP -1
#define ANGLE_DOWN -2 #define ANGLE_DOWN -2
@ -1099,8 +1095,7 @@ metaTriangle_t;
typedef struct epair_s typedef struct epair_s
{ {
struct epair_s *next; CopiedString key, value;
char *key, *value;
} }
epair_t; epair_t;
@ -1112,7 +1107,7 @@ typedef struct
parseMesh_t *patches; parseMesh_t *patches;
int mapEntityNum, firstDrawSurf; int mapEntityNum, firstDrawSurf;
int firstBrush, numBrushes; /* only valid during BSP compile */ int firstBrush, numBrushes; /* only valid during BSP compile */
epair_t *epairs; std::list<epair_t> epairs;
vec3_t originbrush_origin; vec3_t originbrush_origin;
} }
entity_t; entity_t;
@ -1885,7 +1880,7 @@ void PartialLoadBSPFile( const char *filename );
void WriteBSPFile( const char *filename ); void WriteBSPFile( const char *filename );
void PrintBSPFileSizes( void ); void PrintBSPFileSizes( void );
epair_t *ParseEPair( void ); void ParseEPair( std::list<epair_t>& epairs );
void ParseEntities( void ); void ParseEntities( void );
void UnparseEntities( void ); void UnparseEntities( void );
void PrintEntity( const entity_t *ent ); void PrintEntity( const entity_t *ent );

View File

@ -284,7 +284,6 @@ void SetModelNumbers( void ){
void SetLightStyles( void ){ void SetLightStyles( void ){
int j, numStyles; int j, numStyles;
entity_t *e; entity_t *e;
epair_t *ep, *next;
char value[ 10 ]; char value[ 10 ];
char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ]; char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
int lightStyles[ MAX_SWITCHED_LIGHTS ]; int lightStyles[ MAX_SWITCHED_LIGHTS ];
@ -310,16 +309,7 @@ void SetLightStyles( void ){
if ( !ENT_READKV( &t, e, "targetname" ) ) { if ( !ENT_READKV( &t, e, "targetname" ) ) {
/* ydnar: strip the light from the BSP file */ /* ydnar: strip the light from the BSP file */
if ( !keepLights ) { if ( !keepLights ) {
ep = e->epairs; e->epairs.clear();
while ( ep != NULL )
{
next = ep->next;
free( ep->key );
free( ep->value );
free( ep );
ep = next;
}
e->epairs = NULL;
numStrippedLights++; numStrippedLights++;
} }