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

View File

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

View File

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

View File

@ -1424,14 +1424,13 @@ void AddTriangleModels( entity_t *eparent ){
/* get shader remappings */
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" */
if ( !strEmptyOrNull( ep->key ) && !strEmptyOrNull( ep->value ) &&
striEqualPrefix( ep->key, "_remap" ) ) {
if ( striEqualPrefix( ep.key.c_str(), "_remap" ) ) {
/* create new remapping */
remap_t remap;
strcpy( remap.from, ep->value );
strcpy( remap.from, ep.value.c_str() );
/* split the string */
char *split = strchr( remap.from, ';' );

View File

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

View File

@ -323,10 +323,6 @@ brushType_t;
#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 */
#define ANGLE_UP -1
#define ANGLE_DOWN -2
@ -1099,8 +1095,7 @@ metaTriangle_t;
typedef struct epair_s
{
struct epair_s *next;
char *key, *value;
CopiedString key, value;
}
epair_t;
@ -1112,7 +1107,7 @@ typedef struct
parseMesh_t *patches;
int mapEntityNum, firstDrawSurf;
int firstBrush, numBrushes; /* only valid during BSP compile */
epair_t *epairs;
std::list<epair_t> epairs;
vec3_t originbrush_origin;
}
entity_t;
@ -1885,7 +1880,7 @@ void PartialLoadBSPFile( const char *filename );
void WriteBSPFile( const char *filename );
void PrintBSPFileSizes( void );
epair_t *ParseEPair( void );
void ParseEPair( std::list<epair_t>& epairs );
void ParseEntities( void );
void UnparseEntities( void );
void PrintEntity( const entity_t *ent );

View File

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