Skip to content

Commit feb4031

Browse files
patrikjuvonenLpsd
andcommitted
Clean up a for loop and some misc small things
Co-Authored-By: LopSided <[email protected]>
1 parent dd34ad3 commit feb4031

File tree

5 files changed

+51
-107
lines changed

5 files changed

+51
-107
lines changed

Client/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*****************************************************************************
22
*
3-
* PROJECT: Multi Theft Auto v1.0
4-
* (Shared logic for modifications)
3+
* PROJECT: Multi Theft Auto
54
* LICENSE: See LICENSE in the top level directory
6-
* FILE: mods/shared_logic/lua/CLuaMain.cpp
5+
* FILE: mods/deathmatch/logic/lua/CLuaMain.cpp
76
* PURPOSE: Lua main
87
*
8+
* Multi Theft Auto is available from http://www.multitheftauto.com/
9+
*
910
*****************************************************************************/
1011

1112
#include "StdInc.h"
@@ -365,35 +366,30 @@ CXMLFile* CLuaMain::CreateXML(const char* szFilename, bool bUseIDs, bool bReadOn
365366
CXMLNode* CLuaMain::ParseString(const char* strXmlContent)
366367
{
367368
CXMLNode* xmlNode = g_pCore->GetXML()->ParseString(strXmlContent);
368-
369369
return xmlNode;
370370
}
371371

372372
bool CLuaMain::DestroyXML(CXMLFile* pFile)
373373
{
374-
if (m_XMLFiles.empty()) {
374+
if (m_XMLFiles.empty())
375375
return false;
376-
}
377376
m_XMLFiles.remove(pFile);
378377
delete pFile;
379378
return true;
380379
}
381380

382381
bool CLuaMain::DestroyXML(CXMLNode* pRootNode)
383382
{
384-
if (m_XMLFiles.empty()) {
383+
if (m_XMLFiles.empty())
385384
return false;
386-
}
387-
list<CXMLFile*>::iterator iter;
388-
for (iter = m_XMLFiles.begin(); iter != m_XMLFiles.end(); iter++)
385+
for (CXMLFile* pFile : m_XMLFiles)
389386
{
390-
CXMLFile* file = (*iter);
391-
if (file)
387+
if (pFile)
392388
{
393-
if (file->GetRootNode() == pRootNode)
389+
if (pFile->GetRootNode() == pRootNode)
394390
{
395-
delete file;
396-
m_XMLFiles.erase(iter);
391+
m_XMLFiles.remove(pFile);
392+
delete pFile;
397393
break;
398394
}
399395
}
@@ -403,16 +399,10 @@ bool CLuaMain::DestroyXML(CXMLNode* pRootNode)
403399

404400
bool CLuaMain::SaveXML(CXMLNode* pRootNode)
405401
{
406-
for (CXMLFile* file : m_XMLFiles)
407-
{
408-
if (file)
409-
{
410-
if (file->GetRootNode() == pRootNode)
411-
{
412-
return file->Write();
413-
}
414-
}
415-
}
402+
for (CXMLFile* pFile : m_XMLFiles)
403+
if (pFile)
404+
if (pFile->GetRootNode() == pRootNode)
405+
return pFile->Write();
416406
if (m_pResource)
417407
{
418408
list<CResourceConfigItem*>::iterator iter = m_pResource->ConfigIterBegin();
@@ -423,9 +413,7 @@ bool CLuaMain::SaveXML(CXMLNode* pRootNode)
423413
{
424414
CXMLFile* pFile = pConfigItem->GetFile();
425415
if (pFile)
426-
{
427416
return pFile->Write();
428-
}
429417
return false;
430418
}
431419
}

Server/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*****************************************************************************
22
*
3-
* PROJECT: Multi Theft Auto v1.0
3+
* PROJECT: Multi Theft Auto
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: mods/deathmatch/logic/lua/CLuaMain.cpp
66
* PURPOSE: Lua virtual machine container class
@@ -419,36 +419,30 @@ CXMLFile* CLuaMain::CreateXML(const char* szFilename, bool bUseIDs, bool bReadOn
419419
CXMLNode* CLuaMain::ParseString(const char* strXmlContent)
420420
{
421421
CXMLNode* xmlNode = g_pServerInterface->GetXML()->ParseString(strXmlContent);
422-
423422
return xmlNode;
424423
}
425424

426-
427425
bool CLuaMain::DestroyXML(CXMLFile* pFile)
428426
{
429-
if (m_XMLFiles.empty()) {
427+
if (m_XMLFiles.empty())
430428
return false;
431-
}
432429
m_XMLFiles.remove(pFile);
433430
delete pFile;
434431
return true;
435432
}
436433

437434
bool CLuaMain::DestroyXML(CXMLNode* pRootNode)
438435
{
439-
if (m_XMLFiles.empty()) {
436+
if (m_XMLFiles.empty())
440437
return false;
441-
}
442-
list<CXMLFile*>::iterator iter;
443-
for (iter = m_XMLFiles.begin(); iter != m_XMLFiles.end(); ++iter)
438+
for (CXMLFile* pFile : m_XMLFiles)
444439
{
445-
CXMLFile* file = (*iter);
446-
if (file)
440+
if (pFile)
447441
{
448-
if (file->GetRootNode() == pRootNode)
442+
if (pFile->GetRootNode() == pRootNode)
449443
{
450-
m_XMLFiles.erase(iter);
451-
delete file;
444+
m_XMLFiles.remove(pFile);
445+
delete pFile;
452446
break;
453447
}
454448
}
@@ -458,16 +452,10 @@ bool CLuaMain::DestroyXML(CXMLNode* pRootNode)
458452

459453
bool CLuaMain::SaveXML(CXMLNode* pRootNode)
460454
{
461-
for (CXMLFile* file : m_XMLFiles)
462-
{
463-
if (file)
464-
{
465-
if (file->GetRootNode() == pRootNode)
466-
{
467-
return file->Write();
468-
}
469-
}
470-
}
455+
for (CXMLFile* pFile : m_XMLFiles)
456+
if (pFile)
457+
if (pFile->GetRootNode() == pRootNode)
458+
return pFile->Write();
471459
if (m_pResource)
472460
{
473461
list<CResourceFile*>::iterator iter = m_pResource->IterBegin();
@@ -481,9 +469,7 @@ bool CLuaMain::SaveXML(CXMLNode* pRootNode)
481469
{
482470
CXMLFile* pFile = pConfigItem->GetFile();
483471
if (pFile)
484-
{
485472
return pFile->Write();
486-
}
487473
return false;
488474
}
489475
}

Shared/XML/CXMLImpl.cpp

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*****************************************************************************
22
*
3-
* PROJECT: Multi Theft Auto v1.0
3+
* PROJECT: Multi Theft Auto
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: xml/CXMLImpl.cpp
66
* PURPOSE: XML handler class
@@ -32,11 +32,8 @@ CXMLFile* CXMLImpl::CreateXML(const char* szFilename, bool bUseIDs, bool bReadOn
3232
CXMLFile* xmlFile = new CXMLFileImpl(szFilename, bUseIDs, bReadOnly);
3333
if (xmlFile->IsValid())
3434
return xmlFile;
35-
else
36-
{
37-
delete xmlFile;
38-
return NULL;
39-
}
35+
delete xmlFile;
36+
return nullptr;
4037
}
4138

4239
void CXMLImpl::DeleteXML(CXMLFile* pFile)
@@ -46,99 +43,72 @@ void CXMLImpl::DeleteXML(CXMLFile* pFile)
4643

4744
CXMLNode* CXMLImpl::ParseString(const char* strXmlContent)
4845
{
49-
TiXmlElement *xmlDocumentRoot;
50-
CXMLNodeImpl *xmlBaseNode;
51-
CXMLNode *xmlRootNode;
52-
53-
TiXmlDocument* xmlDoc = new TiXmlDocument();
54-
46+
TiXmlDocument* xmlDoc = new TiXmlDocument();
5547
if (xmlDoc)
5648
{
5749
if (xmlDoc->Parse(strXmlContent, 0, TIXML_ENCODING_UTF8))
5850
{
59-
xmlDocumentRoot = xmlDoc->RootElement();
60-
61-
xmlBaseNode = new CXMLNodeImpl(NULL, NULL, *xmlDocumentRoot);
62-
63-
xmlRootNode = CXMLImpl::BuildNode(xmlBaseNode, xmlDocumentRoot);
64-
51+
TiXmlElement* xmlDocumentRoot = xmlDoc->RootElement();
52+
CXMLNodeImpl* xmlBaseNode = new CXMLNodeImpl(nullptr, nullptr, *xmlDocumentRoot);
53+
CXMLNode* xmlRootNode = CXMLImpl::BuildNode(xmlBaseNode, xmlDocumentRoot);
6554
return xmlRootNode;
6655
}
67-
else
68-
return NULL;
69-
}
70-
else
71-
{
72-
return NULL;
7356
}
57+
return nullptr;
7458
}
7559

7660
CXMLNode* CXMLImpl::BuildNode(CXMLNodeImpl* xmlParent, TiXmlNode* xmlNode)
7761
{
78-
TiXmlNode *xmlChild;
79-
TiXmlElement *xmlChildElement;
80-
CXMLNodeImpl *xmlChildNode;
81-
82-
xmlChild = 0;
62+
TiXmlNode* xmlChild = nullptr;
63+
TiXmlElement* xmlChildElement;
64+
CXMLNodeImpl* xmlChildNode;
8365
while (xmlChild = xmlNode->IterateChildren(xmlChild))
8466
{
8567
xmlChildElement = xmlChild->ToElement();
86-
87-
xmlChildNode = new CXMLNodeImpl(NULL, xmlParent, *xmlChildElement);
88-
68+
xmlChildNode = new CXMLNodeImpl(nullptr, xmlParent, *xmlChildElement);
8969
CXMLImpl::BuildNode(xmlChildNode, xmlChildElement);
9070
}
91-
9271
return xmlParent;
9372
}
9473

9574
CXMLNode* CXMLImpl::CreateDummyNode()
9675
{
97-
CXMLNode* xmlNode = new CXMLNodeImpl(NULL, NULL, *new TiXmlElement("dummy_storage"));
76+
CXMLNode* xmlNode = new CXMLNodeImpl(nullptr, nullptr, *new TiXmlElement("dummy_storage"));
9877
if (xmlNode->IsValid())
9978
return xmlNode;
100-
else
101-
{
102-
delete xmlNode;
103-
return NULL;
104-
}
79+
delete xmlNode;
80+
return nullptr;
10581
}
10682

10783
CXMLAttribute* CXMLImpl::GetAttrFromID(unsigned long ulID)
10884
{
10985
// Grab it and verify the type
11086
CXMLCommon* pCommon = CXMLArray::GetEntry(ulID);
11187
if (pCommon && pCommon->GetClassType() == CXML_ATTR)
112-
{
11388
return reinterpret_cast<CXMLAttribute*>(pCommon);
114-
}
11589

11690
// Doesn't exist or bad type
117-
return NULL;
91+
return nullptr;
11892
}
11993

12094
CXMLFile* CXMLImpl::GetFileFromID(unsigned long ulID)
12195
{
12296
// Grab it and verify the type
12397
CXMLCommon* pCommon = CXMLArray::GetEntry(ulID);
12498
if (pCommon && pCommon->GetClassType() == CXML_FILE)
125-
{
12699
return reinterpret_cast<CXMLFile*>(pCommon);
127-
}
128100

129101
// Doesn't exist or bad type
130-
return NULL;
102+
return nullptr;
131103
}
132104

133105
CXMLNode* CXMLImpl::GetNodeFromID(unsigned long ulID)
134106
{
135107
// Grab it and verify the type
136108
CXMLCommon* pCommon = CXMLArray::GetEntry(ulID);
137109
if (pCommon && pCommon->GetClassType() == CXML_NODE)
138-
{
139110
return reinterpret_cast<CXMLNode*>(pCommon);
140-
}
141111

142112
// Doesn't exist or bad type
143-
return NULL;
113+
return nullptr;
144114
}

Shared/XML/CXMLImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*****************************************************************************
22
*
3-
* PROJECT: Multi Theft Auto v1.0
3+
* PROJECT: Multi Theft Auto
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: xml/CXMLImpl.h
66
* PURPOSE: XML handler class

Shared/mods/deathmatch/logic/luadefs/CLuaXMLDefs.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ int CLuaXMLDefs::xmlUnloadFile(lua_State* luaVM)
371371
CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM);
372372
if (luaMain)
373373
{
374-
if (luaMain->DestroyXML(pNode))
375-
{
376-
lua_pushboolean(luaVM, true);
377-
return 1;
378-
}
374+
if (luaMain->DestroyXML(pNode))
375+
{
376+
lua_pushboolean(luaVM, true);
377+
return 1;
378+
}
379379
}
380380
}
381381
else

0 commit comments

Comments
 (0)