OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 {
57  explicit C4StrValAdapt(T &rValue, C4InVal::ValidationOption eValType) : rValue(rValue), eValType(eValType) { }
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
bool operator==(const D &nValue) const
C4StrValAdapt< T > & operator=(const D &nValue)
void CopyValidated(const StdStrBuf &sFromVal)
C4StrValAdapt< T > mkStrValAdapt(T &&rValue, C4InVal::ValidationOption eValType)
#define _MAX_PATH
const unsigned int C4MaxLongName
C4StrValAdapt(T &rValue, C4InVal::ValidationOption eValType)
ValidatedStdCopyStrBufBase(const char *szCopy)
const unsigned int C4MaxComment
virtual ~ValidatedStdCopyStrBufBase()=default
bool ValidateInt(int32_t &riVal, int32_t iMinVal, int32_t iMaxVal)
const unsigned int C4MaxName
void Value(const T &rStruct)
Definition: StdCompiler.h:161
void CompileFunc(StdCompiler *pComp)
bool operator==(const D &nValue) const
bool ValidateString(char *szString, ValidationOption eOption, size_t iMaxSize)
virtual bool isDeserializer()
Definition: StdCompiler.h:53
bool ValidateFilename(char *szFilename, size_t iMaxSize=_MAX_PATH)
C4InVal::ValidationOption eValType
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:458
virtual bool Validate()=0
ValidatedStdCopyStrBuf< V > & operator=(const D &nValue)
void Copy()
Definition: StdBuf.h:467
void CompileFunc(StdCompiler *pComp, int iRawType=0)
void CopyValidated(const char *szFromVal)
ValidatedStdCopyStrBuf(const char *szCopy)