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/StdBuf.h"
22 #include "platform/StdFile.h"
23 #include "lib/StdAdaptors.h"
24 
25 const unsigned int C4MaxName = 30; // player names, etc.
26 const unsigned int C4MaxLongName = 120; // scenario titles, etc. - may include markup
27 const unsigned int C4MaxComment = 256; // network game and player comments
28 
29 namespace C4InVal
30 {
31  // validation options
33  {
34  VAL_Filename, // regular filenames only (Sven2.ocp)
35  VAL_SubPathFilename, // filenames and optional subpath (Spieler\Sven2.ocp)
36  VAL_FullPath, // full filename paths (C:\Clonk\Sven2.ocp; ..\..\..\..\AutoExec.bat)
37  VAL_NameAllowEmpty, // stuff like player names (Sven2). No markup. Max. C4MaxName characters. Spaces trimmed.
38  VAL_NameNoEmpty, // same as above, but empty string not allowed
39  VAL_NameExAllowEmpty,// stuff like Clonk names (Joki the {{WIPF}}). Markup allowed. Max. C4MaxLongName characters. Spaces trimmed.
40  VAL_NameExNoEmpty, // same as above, but empty string not allowed
41  VAL_IRCName, // nickname for IRC. a-z, A-Z, _^{[]} only; 0-9|- inbetween
42  VAL_IRCPass, // password for IRC
43  VAL_IRCChannel, // IRC channel name
44  VAL_Comment // comment - just a length limit
45  };
46  // input validation functions: Validate input by changing it to an allowed value if possible
47  // issues warnings in log and returns true if such an action is performed
48  bool ValidateString(char *szString, ValidationOption eOption, size_t iMaxSize);
49  bool ValidateString(StdStrBuf &rsString, ValidationOption eOption);
50  bool ValidateInt(int32_t &riVal, int32_t iMinVal, int32_t iMaxVal);
51 
52  inline bool ValidateFilename(char *szFilename, size_t iMaxSize=_MAX_PATH) { return ValidateString(szFilename, VAL_Filename, iMaxSize); }
53 }
54 
55 // Validation adapter: Call ValidateString on string after compiling it
56 template <class T> struct C4StrValAdapt
57 {
59  explicit C4StrValAdapt(T &rValue, C4InVal::ValidationOption eValType) : rValue(rValue), eValType(eValType) { }
60  inline void CompileFunc(StdCompiler *pComp)
61  {
62  pComp->Value(rValue);
63  if (pComp->isDeserializer()) C4InVal::ValidateString(rValue.GetObj(), eValType); // works on Par adapt only :(
64  }
65  template <class D> inline bool operator == (const D &nValue) const { return rValue == nValue; }
66  template <class D> inline C4StrValAdapt<T> &operator = (const D &nValue) { rValue = nValue; return *this; }
67 };
68 template <class T> inline C4StrValAdapt<T> mkStrValAdapt(T &&rValue, C4InVal::ValidationOption eValType) { return C4StrValAdapt<T>(rValue, eValType); }
69 
70 // StdStrBuf that validates on compilation
72 {
73  ValidatedStdCopyStrBufBase(const char *szCopy) : StdCopyStrBuf(szCopy) {}
75 
76  inline void CompileFunc(StdCompiler *pComp, int iRawType = 0)
77  {
78  pComp->Value(mkParAdapt(static_cast<StdCopyStrBuf &>(*this), iRawType));
79  if (pComp->isDeserializer()) Validate();
80  }
81 
82  virtual bool Validate() = 0;
83 
84  void CopyValidated(const char *szFromVal)
85  {
86  Copy(szFromVal);
87  Validate();
88  }
89  void CopyValidated(const StdStrBuf &sFromVal)
90  {
91  Copy(sFromVal);
92  Validate();
93  }
94 
96 };
97 
98 template <int V> struct ValidatedStdCopyStrBuf : public ValidatedStdCopyStrBufBase
99 {
100  ValidatedStdCopyStrBuf(const char *szCopy) : ValidatedStdCopyStrBufBase(szCopy) {}
102 
103  virtual bool Validate()
104  {
106  }
107 
108  template <class D> inline bool operator == (const D &nValue) const { return static_cast<const StdCopyStrBuf &>(*this) == nValue; }
109  template <class D> inline ValidatedStdCopyStrBuf<V> &operator = (const D &nValue) { static_cast<StdCopyStrBuf &>(*this) = nValue; return *this; }
110 };
111 
112 #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
bool ValidateInt(int32_t &riVal, int32_t iMinVal, int32_t iMaxVal)
const unsigned int C4MaxName
void Value(const T &rStruct)
Definition: StdCompiler.h:171
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:63
bool ValidateFilename(char *szFilename, size_t iMaxSize=_MAX_PATH)
C4InVal::ValidationOption eValType
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:456
virtual bool Validate()=0
ValidatedStdCopyStrBuf< V > & operator=(const D &nValue)
void Copy()
Definition: StdBuf.h:475
void CompileFunc(StdCompiler *pComp, int iRawType=0)
void CopyValidated(const char *szFromVal)
ValidatedStdCopyStrBuf(const char *szCopy)