-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
110 lines (95 loc) · 2.64 KB
/
common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <stdlib.h>
#include "common.hpp"
#include "global_defines.hpp"
using namespace cwrap;
namespace
{
inline bool char_match(const char& ch, const std::string& s)
{
for(std::string::const_iterator it = s.begin(); it != s.end(); ++it)
{
if(ch == *it)
{
return true;
}
}
return false;
}
}
namespace common
{
bool is_letter(const char& ch)
{
return char_match(ch, LETTERS);
}
bool is_number(const char& ch)
{
return char_match(ch, NUMBERS);
}
bool is_special(const char& ch)
{
return char_match(ch, SPECIALS);
}
bool is_char(const char& ch)
{
return (is_letter(ch) || is_number(ch) || is_special(ch));
}
namespace input
{
std::string get_user_string(const std::string& message)
{
std::string input;
char ch;
while(true)
{
cls();
for(short x = 0; x < 10; x++) cout<< endl;
cout<< ((message.size() > 0) ? message : ">> ")<< input;
ch = gkey();
switch(is_char(ch))
{
case true:
{
input += ch;
}
break;
case false:
{
switch(ch)
{
case char(KEY_BACKSPACE):
{
if(input.size() > 0)
{
input.resize((input.size() - 1));
}
}
break;
case char(ESCAPE_KEY):
{
input.erase();
return GSTRING_CANCEL;
}
break;
case char(ENTER_KEY):
{
return input;
}
break;
default:
{
}
break;
}
}
break;
default:
break;
}
}
return GSTRING_CANCEL;
}
}
}