Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Commands.h
Go to the documentation of this file.
1 /* \file Commands.h
2  * \brief Functions used for parsing server commands.
3  */
4 
5 #pragma once
6 
7 #include <string>
8 #include <vector>
9 #include "Halo/HaloStreams.h"
10 #include "Halo/Player.h"
11 
12 // stupid enum warning
13 #pragma warning( disable : 4482)
15 {
18 };
19 
20 namespace commands
21 {
22  e_command_result ProcessCommand(const std::string& command,
23  COutStream& out, halo::s_player* exec_player=NULL);
24 
25  // Used for parsing user input in server commands.
26  // If an error occurs the Read functions throw and exception which
27  // is caught in ProcessCommand.
28  class CArgParser
29  {
30  private:
31  const std::vector<std::string>& args;
32  size_t start_index, index;
33  std::string function; // command being executed (ie sv_players)
34 
35  enum e_arg_types
36  {
37  kNone,
38  kString,
39  kStringOneOf,
40  kInteger,
41  kUnsignedInteger,
42  kDouble,
43  kBoolean,
44  kPlayer,
45  kPlayerOrHash
46  };
47  static const char* k_arg_names[];
48 
49  template <class T>
50  T ReadNumber(e_arg_types type, T min, T max);
51 
52  bool InVector(const std::vector<std::string>& opts,
53  const std::string& to_check);
54 
55  void RaiseError(e_arg_types expected, double min=0, double max=0,
56  const std::vector<std::string>* opts = 0);
57  void HasData() { if (args.size() <= index) RaiseError(kNone); }
58 
59  public:
60  // args should remain for duration of this object
61  explicit CArgParser(const std::vector<std::string>& args,
62  const std::string& function, size_t start_index);
63  size_t size() const { return args.size() - start_index;}
64 
65  std::string ReadString(size_t min=0, size_t max=0);
66  std::wstring ReadWideString(size_t min=0, size_t max=0);
67  // if ignore case == true, all opts should be lowercase (not enforced)
68  std::string ReadStringOneOf(const std::vector<std::string>& opts, bool ignorecase=false);
69  std::wstring ReadWideStringOneOf(const std::vector<std::string>& opts, bool ignorecase=false);
70 
71  int ReadInt(int min=INT_MIN, int max=INT_MAX);
72  unsigned int ReadUInt(unsigned int min=0, unsigned int max=UINT_MAX);
73  double ReadDouble(double min=-DBL_MAX, double max=DBL_MAX);
74  float ReadFloat(float min=-FLT_MAX, float max=FLT_MAX);
75  bool ReadBool(); // accepts true/false, 1/0
77  std::string ReadPlayerHash();
78  std::string ReadPlayerOrHash();
79  };
80 }