Gorgon Game Engine
Input.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <string>
5 #include <vector>
6 #include <iomanip>
7 #include <fstream>
8 #include "../String.h"
9 #include "../Scripting.h"
10 
11 namespace Gorgon { namespace Scripting {
12 
13  class InputProvider {
14  public:
15  enum Dialect {
19  Binary
20  };
21 
23 
25  Dialect GetDialect() const {
26  return dialect;
27  }
28 
31  this->dialect=dialect;
32  checkdialect();
33  }
34 
35  std::string GetName() const { return name; }
36 
37 
42  virtual bool ReadLine(std::string &, bool newline) = 0;
43 
45  virtual bool IsInteractive() const { return false; }
46 
47  //virtual int ReadBinary(std::vector<Byte> &buffer) = 0;
48 
50  virtual void Reset() = 0;
51 
52  protected:
53  std::string name;
54 
55  virtual void checkdialect() { }
56 
58  };
59 
61  class ConsoleInput : public InputProvider {
62  public:
63 
66  prompt(prompt) {
67  name="#Console";
68  }
69 
70  void SetPrompt(const std::string &prompt) {
71  this->prompt=prompt;
72  }
73 
74  virtual bool ReadLine(std::string &input, bool newline) override final {
75  line++;
76  std::cout<<std::setw(3)<<line<<prompt;
77 
78  return bool(std::getline(std::cin, input));
79  }
80 
81  virtual void Reset() override {
82  line=0;
83  }
84 
85  virtual bool IsInteractive() const override { return true; }
86 
87 
88  protected:
89  virtual void checkdialect() override {
92  throw std::runtime_error("Cannot accept binary code from the console");
93  }
94  }
95 
96  private:
97  std::string prompt;
98  int line=0;
99  };
100 
102  class StreamInput : public InputProvider {
103  public:
104  StreamInput(std::istream &stream, Dialect dialect, const std::string &name="") : InputProvider(dialect), stream(stream) {
105  this->name=name;
106  }
107 
108  virtual bool ReadLine(std::string &input, bool) override final {
109  return bool(std::getline(stream,input));
110  }
111 
112  virtual void Reset() override {
113  stream.seekg(0);
114  }
115 
116  private:
117  std::istream &stream;
118  };
119 
121  class FileInput : public StreamInput {
122  public:
123  FileInput(const std::string &filename) : StreamInput(file, InputProvider::Programming) {
124  auto loc=filename.find_last_of('.');
125  std::string ext="";
126  if(loc!=filename.npos)
127  ext=filename.substr(loc);
128 
129  name=filename;
130 
131  //determine dialect from the extension
132  if(ext.length()>=3 && ext.substr(0,3)=="gsb") {
134  }
135  else if(ext.length()>=3 && ext.substr(0,3)=="gsc") {
137  }
138  else if(ext.length()>=3 && ext.substr(0,3)=="gsi") {
140  }
141  else { // generally *.gs*
143  }
144 
145  file.open(filename, dialect==InputProvider::Binary ? std::ios::out | std::ios::binary : std::ios::out);
146  if(!file.is_open()) {
147  throw std::runtime_error("Cannot open file");
148  }
149  }
150 
151  private:
152  std::ifstream file;
153  };
154 
155 } }
Gorgon::Scripting::ConsoleInput::IsInteractive
virtual bool IsInteractive() const override
Returns if this input provider allows interaction.
Definition: Input.h:85
Gorgon::Scripting::InputProvider::Reset
virtual void Reset()=0
Resets the input to the beginning.
Gorgon::Scripting::InputProvider::name
std::string name
Definition: Input.h:53
Gorgon::Scripting::InputProvider::ReadLine
virtual bool ReadLine(std::string &, bool newline)=0
This method should read a single physical line from the source.
Gorgon::Scripting::InputProvider::checkdialect
virtual void checkdialect()
Definition: Input.h:55
Gorgon::Scripting::StreamInput::Reset
virtual void Reset() override
Resets the input to the beginning.
Definition: Input.h:112
Gorgon::Scripting::ConsoleInput::checkdialect
virtual void checkdialect() override
Definition: Input.h:89
Gorgon::Scripting::StreamInput::StreamInput
StreamInput(std::istream &stream, Dialect dialect, const std::string &name="")
Definition: Input.h:104
Gorgon::Scripting::ConsoleInput::Reset
virtual void Reset() override
Resets the input to the beginning.
Definition: Input.h:81
Gorgon::Scripting::FileInput
Reads lines from a file.
Definition: Input.h:121
Gorgon::Scripting::InputProvider::GetDialect
Dialect GetDialect() const
Returns the current dialect of the input.
Definition: Input.h:25
Gorgon::Scripting::ConsoleInput::SetPrompt
void SetPrompt(const std::string &prompt)
Definition: Input.h:70
Gorgon::Scripting::ConsoleInput
Reads lines from the console.
Definition: Input.h:61
Gorgon::Scripting::InputProvider::IsInteractive
virtual bool IsInteractive() const
Returns if this input provider allows interaction.
Definition: Input.h:45
Gorgon::Scripting::InputProvider::Dialect
Dialect
Definition: Input.h:15
Gorgon::Scripting::StreamInput::ReadLine
virtual bool ReadLine(std::string &input, bool) override final
This method should read a single physical line from the source.
Definition: Input.h:108
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Scripting::ConsoleInput::ConsoleInput
ConsoleInput(Dialect dialect=InputProvider::Console, const std::string &prompt="> ")
Initializes console input. line number will be appended at the start of the prompt.
Definition: Input.h:65
Gorgon::Scripting::InputProvider::SetDialect
void SetDialect(Dialect dialect)
Changes the current dialect of the input.
Definition: Input.h:30
Gorgon::Scripting::InputProvider
Definition: Input.h:13
Gorgon::Scripting::InputProvider::Console
@ Console
Definition: Input.h:16
Gorgon::Scripting::InputProvider::Binary
@ Binary
Definition: Input.h:19
Gorgon::Scripting::InputProvider::Programming
@ Programming
Definition: Input.h:17
Gorgon::Scripting::InputProvider::dialect
Dialect dialect
Definition: Input.h:57
Gorgon::Scripting::InputProvider::Intermediate
@ Intermediate
Definition: Input.h:18
Gorgon::Scripting::ConsoleInput::ReadLine
virtual bool ReadLine(std::string &input, bool newline) override final
This method should read a single physical line from the source.
Definition: Input.h:74
Gorgon::Scripting::InputProvider::InputProvider
InputProvider(Dialect dialect)
Definition: Input.h:22
Gorgon::Scripting::StreamInput
Reads lines from a stream.
Definition: Input.h:102
Gorgon::Scripting::InputProvider::GetName
std::string GetName() const
Definition: Input.h:35
Gorgon::Scripting::FileInput::FileInput
FileInput(const std::string &filename)
Definition: Input.h:123