OpenClonk
C4InputValidation.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2007-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // user input validation functions
17 
18 #ifndef INC_C4InputValidation
19 #define INC_C4InputValidation
20 
21 #include "lib/StdAdaptors.h"
22 
23 const unsigned int C4MaxName = 30; // player names, etc.
24 const unsigned int C4MaxLongName = 120; // scenario titles, etc. - may include markup
25 const unsigned int C4MaxComment = 256; // network game and player comments
26 
27 namespace C4InVal
28 {
29  // validation options
31  {
32  VAL_Filename, // regular filenames only (Sven2.ocp)
33  VAL_SubPathFilename, // filenames and optional subpath (Spieler\Sven2.ocp)
34  VAL_FullPath, // full filename paths (C:\Clonk\Sven2.ocp; ..\..\..\..\AutoExec.bat)
35  VAL_NameAllowEmpty, // stuff like player names (Sven2). No markup. Max. C4MaxName characters. Spaces trimmed.
36  VAL_NameNoEmpty, // same as above, but empty string not allowed
37  VAL_NameExAllowEmpty,// stuff like Clonk names (Joki the {{WIPF}}). Markup allowed. Max. C4MaxLongName characters. Spaces trimmed.
38  VAL_NameExNoEmpty, // same as above, but empty string not allowed
39  VAL_IRCName, // nickname for IRC. a-z, A-Z, _^{[]} only; 0-9|- inbetween
40  VAL_IRCPass, // password for IRC
41  VAL_IRCChannel, // IRC channel name
42  VAL_Comment // comment - just a length limit
43  };
44  // input validation functions: Validate input by changing it to an allowed value if possible
45  // issues warnings in log and returns true if such an action is performed
46  bool ValidateString(char *szString, ValidationOption eOption, size_t iMaxSize);
47  bool ValidateString(StdStrBuf &rsString, ValidationOption eOption);
48  bool ValidateInt(int32_t &riVal, int32_t iMinVal, int32_t iMaxVal);
49 
50  inline bool ValidateFilename(char *szFilename, size_t iMaxSize=_MAX_PATH) { return ValidateString(szFilename, VAL_Filename, iMaxSize); }
51 }
52 
53 // Validation adapter: Call ValidateString on string after compiling it
54 template <class T> struct C4StrValAdapt
55 {
58  inline void CompileFunc(StdCompiler *pComp)
59  {
60  pComp->Value(rValue);
61  if (pComp->isDeserializer()) C4InVal::ValidateString(rValue.GetObj(), eValType); // works on Par adapt only :(
62  }
63  template <class D> inline bool operator == (const D &nValue) const { return rValue == nValue; }
64  template <class D> inline C4StrValAdapt<T> &operator = (const D &nValue) { rValue = nValue; return *this; }
65 };
66 template <class T> inline C4StrValAdapt<T> mkStrValAdapt(T &&rValue, C4InVal::ValidationOption eValType) { return C4StrValAdapt<T>(rValue, eValType); }
67 
68 // StdStrBuf that validates on compilation
70 {
71  ValidatedStdCopyStrBufBase(const char *szCopy) : StdCopyStrBuf(szCopy) {}
73 
74  inline void CompileFunc(StdCompiler *pComp, int iRawType = 0)
75  {
76  pComp->Value(mkParAdapt(static_cast<StdCopyStrBuf &>(*this), iRawType));
77  if (pComp->isDeserializer()) Validate();
78  }
79 
80  virtual bool Validate() = 0;
81 
82  void CopyValidated(const char *szFromVal)
83  {
84  Copy(szFromVal);
85  Validate();
86  }
87  void CopyValidated(const StdStrBuf &sFromVal)
88  {
89  Copy(sFromVal);
90  Validate();
91  }
92 
93  virtual ~ValidatedStdCopyStrBufBase() = default;
94 };
95 
96 template <int V> struct ValidatedStdCopyStrBuf : public ValidatedStdCopyStrBufBase
97 {
98  ValidatedStdCopyStrBuf(const char *szCopy) : ValidatedStdCopyStrBufBase(szCopy) {}
100 
101  bool Validate() override
102  {
104  }
105 
106  template <class D> inline bool operator == (const D &nValue) const { return static_cast<const StdCopyStrBuf &>(*this) == nValue; }
107  template <class D> inline ValidatedStdCopyStrBuf<V> &operator = (const D &nValue) { static_cast<StdCopyStrBuf &>(*this) = nValue; return *this; }
108 };
109 
110 #endif // INC_C4InputValidation
const unsigned int C4MaxComment
C4StrValAdapt< T > mkStrValAdapt(T &&rValue, C4InVal::ValidationOption eValType)
const unsigned int C4MaxLongName
const unsigned int C4MaxName
#define _MAX_PATH
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:490
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
void Copy()
Definition: StdBuf.h:467
bool ValidateString(char *szString, ValidationOption eOption, size_t iMaxSize)
@ VAL_NameExAllowEmpty
bool ValidateInt(int32_t &riVal, int32_t iMinVal, int32_t iMaxVal)
bool ValidateFilename(char *szFilename, size_t iMaxSize=_MAX_PATH)
C4InVal::ValidationOption eValType
bool operator==(const D &nValue) const
void CompileFunc(StdCompiler *pComp)
C4StrValAdapt< T > & operator=(const D &nValue)
C4StrValAdapt(T &rValue, C4InVal::ValidationOption eValType)
void CopyValidated(const char *szFromVal)
virtual bool Validate()=0
virtual ~ValidatedStdCopyStrBufBase()=default
void CompileFunc(StdCompiler *pComp, int iRawType=0)
void CopyValidated(const StdStrBuf &sFromVal)
ValidatedStdCopyStrBufBase(const char *szCopy)
ValidatedStdCopyStrBuf< V > & operator=(const D &nValue)
ValidatedStdCopyStrBuf(const char *szCopy)
bool operator==(const D &nValue) const