OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 (int32_t cnt=0; cnt<C4MaxNameList; cnt++)
67  if (Name[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 }
StdStringAdapt mkStringAdapt(char *szString, int iMaxLength, StdCompiler::RawCompileType eRawType=StdCompiler::RCT_Escaped)
Definition: StdAdaptors.h:188
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:129
bool Add(const char *szName, int32_t iCount=0)
Definition: C4NameList.cpp:50
bool IsEmpty()
Definition: C4NameList.cpp:64
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:129
const int C4MaxNameList
Definition: C4NameList.h:23
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
const unsigned int C4MaxName
void Value(const T &rStruct)
Definition: StdCompiler.h:171
std::enable_if< std::is_nothrow_default_constructible< T >::value >::type InplaceReconstruct(T *obj)
Definition: Standard.h:35
virtual bool isDeserializer()
Definition: StdCompiler.h:63
bool Set(const char *szName, int32_t iCount)
Definition: C4NameList.cpp:28
void CompileFunc(StdCompiler *pComp, bool fValues=true)
Definition: C4NameList.cpp:72
int32_t Count[C4MaxNameList]
Definition: C4NameList.h:33
StdDefaultAdapt< T, D > mkDefaultAdapt(T &&rValue, const D &rDefault)
Definition: StdAdaptors.h:65
char Name[C4MaxNameList][C4MaxName+1]
Definition: C4NameList.h:32
void Clear()
Definition: C4NameList.cpp:23