OpenClonk
C4NameList.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* A static list of strings and integer values, i.e. for material amounts */
19 
20 #include "C4Include.h"
21 #include "lib/C4NameList.h"
22 
24 {
25  InplaceReconstruct(this);
26 }
27 
28 bool C4NameList::Set(const char *szName, int32_t iCount)
29 {
30  int32_t cnt;
31  // Find existing name, set count
32  for (cnt=0; cnt<C4MaxNameList; cnt++)
33  if (SEqual(Name[cnt],szName))
34  {
35  Count[cnt]=iCount;
36  return true;
37  }
38  // Find empty spot, set name and count
39  for (cnt=0; cnt<C4MaxNameList; cnt++)
40  if (Name[cnt][0]==0)
41  {
42  SCopy(szName,Name[cnt],C4MaxName);
43  Count[cnt]=iCount;
44  return true;
45  }
46  // No empty spots
47  return false;
48 }
49 
50 bool C4NameList::Add(const char *szName, int32_t iCount)
51 {
52  // Find empty spot, set name and count
53  for (int32_t cnt=0; cnt<C4MaxNameList; cnt++)
54  if (!Name[cnt][0])
55  {
56  SCopy(szName,Name[cnt],C4MaxName);
57  Count[cnt]=iCount;
58  return true;
59  }
60  // No empty spots
61  return false;
62 }
63 
65 {
66  for (auto & cnt : Name)
67  if (cnt[0])
68  return false;
69  return true;
70 }
71 
72 void C4NameList::CompileFunc(StdCompiler *pComp, bool fValues)
73 {
74  bool deserializing = pComp->isDeserializer();
75  for (int32_t cnt=0; cnt<C4MaxNameList; cnt++)
76  if (deserializing || Name[cnt][0])
77  {
78  if (cnt) pComp->Separator(StdCompiler::SEP_SEP2);
79  // Name
81  // Value
82  if (fValues)
83  {
85  pComp->Value(mkDefaultAdapt(Count[cnt], 0));
86  }
87  }
88 }
const unsigned int C4MaxName
const int C4MaxNameList
Definition: C4NameList.h:23
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
std::enable_if< std::is_nothrow_default_constructible< T >::value >::type InplaceReconstruct(T *obj)
Definition: Standard.h:35
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
StdStringAdapt mkStringAdapt(char *szString, int iMaxLength, StdCompiler::RawCompileType eRawType=StdCompiler::RCT_Escaped)
Definition: StdAdaptors.h:187
StdDefaultAdapt< T, D > mkDefaultAdapt(T &&rValue, const D &rDefault)
Definition: StdAdaptors.h:64
void Clear()
Definition: C4NameList.cpp:23
char Name[C4MaxNameList][C4MaxName+1]
Definition: C4NameList.h:31
bool IsEmpty()
Definition: C4NameList.cpp:64
void CompileFunc(StdCompiler *pComp, bool fValues=true)
Definition: C4NameList.cpp:72
bool Add(const char *szName, int32_t iCount=0)
Definition: C4NameList.cpp:50
int32_t Count[C4MaxNameList]
Definition: C4NameList.h:32
bool Set(const char *szName, int32_t iCount)
Definition: C4NameList.cpp:28
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:119
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53