Initial commit
This commit is contained in:
62
include/util/input.hpp
Normal file
62
include/util/input.hpp
Normal file
@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <istream>
|
||||
#include <streambuf>
|
||||
|
||||
|
||||
namespace fsh::util
|
||||
{
|
||||
|
||||
/**
|
||||
* Applies some settings to terminals to fix some inconsistencies between
|
||||
* windows and linux terminals
|
||||
*
|
||||
* * **Windows** : it enables using ansii characters
|
||||
* * **Linux** : it enables [Noncanonical Mode](https://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html)
|
||||
*/
|
||||
void prepTerminal();
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
|
||||
class CursorIStreamBuffer : public std::streambuf {
|
||||
public:
|
||||
enum EscapeSequence {
|
||||
FAILED,
|
||||
POSSIBLE,
|
||||
LEFT_ARROW,
|
||||
RIGHT_ARROW
|
||||
};
|
||||
|
||||
CursorIStreamBuffer() : valid(0), read_chars(0), cursor_pos(0), buffer() {
|
||||
setg(buffer,buffer,buffer);
|
||||
}
|
||||
protected:
|
||||
int underflow() override;
|
||||
// int uflow() override;
|
||||
// std::streamsize xsgetn(char* s, std::streamsize n) override;
|
||||
|
||||
private:
|
||||
int valid;
|
||||
EscapeSequence testForEscapeCodes(char chr);
|
||||
|
||||
int readInputToNewLine();
|
||||
std::string input;
|
||||
int read_chars;
|
||||
int cursor_pos;
|
||||
char buffer[32];
|
||||
};
|
||||
|
||||
class CursorIStream : public std::istream {
|
||||
public:
|
||||
CursorIStream() : std::istream(&buffer) {}
|
||||
private:
|
||||
CursorIStreamBuffer buffer;
|
||||
};
|
||||
|
||||
extern CursorIStream cin;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
}
|
||||
55
include/util/text.hpp
Normal file
55
include/util/text.hpp
Normal file
@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace fsh::util {
|
||||
|
||||
|
||||
extern const char* tokens[];
|
||||
|
||||
template <class T>
|
||||
static inline std::optional<std::string> mk_optional(T t){
|
||||
if(t){
|
||||
return t->gvalue();
|
||||
}
|
||||
return std::optional<std::string>();
|
||||
}
|
||||
|
||||
static inline bool contains(char x, std::string text) {
|
||||
for (const auto& chr : text){
|
||||
if(chr == x) return 1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
||||
static inline std::ifstream input(const std::string& x) {
|
||||
std::ifstream in(x, std::ios::in);
|
||||
if(!in) {
|
||||
throw std::runtime_error("Can't open file");
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
static inline std::ofstream output(const std::string& x) {
|
||||
std::ofstream out(x, std::ios::out);
|
||||
if(!out.is_open()) {
|
||||
throw std::runtime_error("Can't open file");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
static inline std::ofstream output_append(const std::string& x) {
|
||||
std::ofstream out(x, std::ios::app | std::ios::out);
|
||||
if(!out.is_open()) {
|
||||
throw std::runtime_error("Can't open file");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user