Skip to content

Commit 913a5c3

Browse files
authored
Merge pull request #5 from rbergen/cpp_strings
Transition to C++ strings and I/O
2 parents 925028c + 20bb7d8 commit 913a5c3

27 files changed

+827
-685
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15.0)
22

33
project(
44
R136
5-
VERSION 3.3.0)
5+
VERSION 3.3.1)
66

77
set(CURSES_NEED_NCURSES 1)
88
set(CURSES_NEED_WIDE 1)

R136.vcxproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<ClCompile>
9191
<WarningLevel>Level3</WarningLevel>
9292
<SDLCheck>true</SDLCheck>
93-
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;PDC_WIDE;PDC_WIDE;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<PreprocessorDefinitions>PDC_WIDE;PDC_WIDE;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9494
<ConformanceMode>true</ConformanceMode>
9595
<LanguageStandard_C>stdc11</LanguageStandard_C>
9696
<ShowIncludes>
@@ -107,7 +107,7 @@
107107
<FunctionLevelLinking>true</FunctionLevelLinking>
108108
<IntrinsicFunctions>true</IntrinsicFunctions>
109109
<SDLCheck>true</SDLCheck>
110-
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;PDC_WIDE;PDC_WIDE;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
110+
<PreprocessorDefinitions>PDC_WIDE;PDC_WIDE;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
111111
<ConformanceMode>true</ConformanceMode>
112112
<LanguageStandard_C>stdc11</LanguageStandard_C>
113113
<ShowIncludes>
@@ -124,7 +124,7 @@
124124
<ClCompile>
125125
<WarningLevel>Level3</WarningLevel>
126126
<SDLCheck>true</SDLCheck>
127-
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;PDC_WIDE;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127+
<PreprocessorDefinitions>PDC_WIDE;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
128128
<ConformanceMode>true</ConformanceMode>
129129
<LanguageStandard_C>stdc11</LanguageStandard_C>
130130
<ShowIncludes>
@@ -141,7 +141,7 @@
141141
<FunctionLevelLinking>true</FunctionLevelLinking>
142142
<IntrinsicFunctions>true</IntrinsicFunctions>
143143
<SDLCheck>true</SDLCheck>
144-
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;PDC_WIDE;PDC_WIDE;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
144+
<PreprocessorDefinitions>PDC_WIDE;PDC_WIDE;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
145145
<ConformanceMode>true</ConformanceMode>
146146
<LanguageStandard_C>stdc11</LanguageStandard_C>
147147
<ShowIncludes>
@@ -166,8 +166,10 @@
166166
<ClInclude Include="include\inlines\console.h" />
167167
<ClInclude Include="include\inlines\r136.h" />
168168
<ClInclude Include="include\main.h" />
169+
<ClInclude Include="include\parser.h" />
169170
<ClInclude Include="include\r136.h" />
170171
<ClInclude Include="include\status.h" />
172+
<ClInclude Include="include\templates\console.h" />
171173
<ClInclude Include="include\templates\r136.h" />
172174
<ClInclude Include="include\types\animates.h" />
173175
<ClInclude Include="include\types\console.h" />
@@ -198,6 +200,7 @@
198200
<ClCompile Include="src\init.cpp" />
199201
<ClCompile Include="src\intro.cpp" />
200202
<ClCompile Include="src\loadsave.cpp" />
203+
<ClCompile Include="src\parser.cpp" />
201204
<ClCompile Include="src\r136.cpp" />
202205
<ClCompile Include="src\random.cpp" />
203206
<ClCompile Include="src\status.cpp" />

R136.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
<ClInclude Include="include\animates.h">
7272
<Filter>Header Files</Filter>
7373
</ClInclude>
74+
<ClInclude Include="include\templates\console.h">
75+
<Filter>Header Files\templates</Filter>
76+
</ClInclude>
77+
<ClInclude Include="include\parser.h">
78+
<Filter>Header Files</Filter>
79+
</ClInclude>
7480
</ItemGroup>
7581
<ItemGroup>
7682
<ClCompile Include="src\console.cpp">
@@ -157,6 +163,9 @@
157163
<ClCompile Include="src\animates\voices.cpp">
158164
<Filter>Source Files\animates</Filter>
159165
</ClCompile>
166+
<ClCompile Include="src\parser.cpp">
167+
<Filter>Source Files</Filter>
168+
</ClCompile>
160169
</ItemGroup>
161170
<ItemGroup>
162171
<Text Include="CMakeLists.txt" />

include/act.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
#include "types/r136.h"
44

5-
bool parse_owned_item_command_param(CoreData& core, ParseData& parse_data, const char* command, const char* parseString);
6-
7-
bool check_found_item(ParseData& parse_data, ItemID item, const char* itemname, const char* undefinedItemFormatString);
8-
9-
ItemID find_owned_item(CoreData& core, const char* item_name);
10-
11-
bool is_room_lit(Status& status);
12-
13-
ItemID find_laying_item(CoreData& core, const char* item_name);
14-
15-
void parse_input(CoreData& core, char* inpstr, ParseData& parse_data);
16-
17-
void parse_combine_parameters(CoreData& core, ParseData& parse_data, const char* currentMatch);
18-
195
bool perform_command(CoreData& core);
206

217
void use(CoreData& core, ItemID item_id);

include/inlines/console.h

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,54 @@ inline void Window::refresh()
148148
wrefresh(wnd);
149149
}
150150

151-
inline void Window::print(char c)
151+
inline int Window::print(char c)
152152
{
153-
waddch(wnd, c);
153+
return waddch(wnd, c);
154154
}
155155

156-
inline int Window::write(const wchar_t* text)
156+
inline int Window::print(const string& text)
157157
{
158-
return waddwstr(wnd, text);
158+
return waddstr(wnd, text.c_str());
159+
}
160+
161+
inline int Window::print(const wstring& text)
162+
{
163+
return waddwstr(wnd, text.c_str());
164+
}
165+
166+
inline int Window::print(const string& format, const string& value)
167+
{
168+
return print(replace(format, string("{0}"), value));
169+
}
170+
171+
inline int Window::print(const wstring& format, const wstring& value)
172+
{
173+
return print(replace(format, wstring(L"{0}"), value));
159174
}
160175

161176
inline InputWindow::InputWindow(WINDOW* wnd) : Window(wnd, true, Color::bold) {}
162177

178+
inline void InputWindow::print_error(const string& format, const string& value)
179+
{
180+
print_error(replace(format, string("{0}"), value));
181+
}
182+
183+
inline void InputWindow::print_error(const wstring& format, const wstring& value)
184+
{
185+
print_error(replace(format, wstring(L"{0}"), value));
186+
}
187+
188+
inline void InputWindow::print_error(const string& text)
189+
{
190+
print_error_template(text);
191+
}
192+
193+
inline void InputWindow::print_error(const wstring& text)
194+
{
195+
print_error_template(text);
196+
}
197+
198+
163199
inline Console::Console() :
164200
banner_window(nullptr),
165201
fullscreen_window(nullptr),

include/parser.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "types/r136.h"
2+
3+
struct ParseData
4+
{
5+
Command command;
6+
ItemID item1;
7+
ItemID item2;
8+
bool parse_error;
9+
};
10+
11+
class Parser {
12+
static string dont_own_item_format_string;
13+
static string commands[];
14+
15+
ParseData parse_data;
16+
17+
void parse_combine_parameters(CoreData& core, ParseData& parse_data, const string& param_string);
18+
bool parse_owned_item_command_param(CoreData& core, ParseData& parse_data, const string& command, const string& parse_string);
19+
bool check_found_item(ParseData& parse_data, ItemID item, const string& item_name, const string undefined_item_format_string);
20+
ItemID find_owned_item(CoreData& core, const string& item_name);
21+
ItemID find_laying_item(CoreData& core, string& item_name);
22+
23+
public:
24+
Parser();
25+
26+
ParseData& parse_input(CoreData& core, string& input_string);
27+
};

include/r136.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ extern const char* saved_status_path;
1010
int get_random_number(int max);
1111
AnimateStatus get_random_status(AnimateStatus lowest, AnimateStatus highest);
1212
void run_intro();
13-
bool save_status(CoreData& core);
14-
bool load_status(CoreData& core);
13+
bool save_game(CoreData& core);
14+
bool load_game(CoreData& core);
1515
void initialize(CoreData& core);
1616
bool perform_command(CoreData& core);
1717
void force_exit(void);
18+
bool is_room_lit(CoreData& core);
1819
void show_room_status(CoreData& core);
1920
bool progress_animate_status(CoreData& core);
2021
void sleep_ms(int name);
@@ -25,5 +26,6 @@ AnimateStatus& operator++(AnimateStatus& status);
2526
extern Console console;
2627

2728
#include "templates/r136.h"
29+
#include "templates/console.h"
2830
#include "inlines/r136.h"
2931
#include "inlines/console.h"

include/templates/console.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "types/console.h"
2+
3+
template<class TChar>
4+
const std::basic_string<TChar> Window::replace(const std::basic_string<TChar>& format, const std::basic_string<TChar>& tag, const std::basic_string<TChar>& value)
5+
{
6+
size_t search_pos = 0;
7+
size_t tag_pos = string::npos;
8+
9+
std::basic_string<TChar> result;
10+
bool found = false;
11+
12+
while (true)
13+
{
14+
tag_pos = format.find(tag, search_pos);
15+
if (tag_pos == std::basic_string<TChar>::npos)
16+
return found ? result : format;
17+
18+
if (!found)
19+
{
20+
found = true;
21+
result = std::basic_string<TChar>(format);
22+
}
23+
24+
result.replace(tag_pos, 3, value);
25+
search_pos = tag_pos + value.size();
26+
}
27+
}
28+
29+
template<class TChar>
30+
void InputWindow::print_error_template(const std::basic_string<TChar>& text)
31+
{
32+
set_position(0, 0);
33+
clear_line();
34+
35+
set_color(Color::error);
36+
37+
print("< ");
38+
39+
print(text);
40+
41+
unset_color(Color::error);
42+
43+
refresh();
44+
wait_for_key();
45+
}
46+

include/types/console.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class Window
5252
{
5353
friend class Console;
5454

55-
int scanf(int check_input, int length, const char* allowed_characters, const char* format_string, ...);
56-
5755
protected:
5856
WINDOW* wnd;
5957
Color standard_color;
@@ -64,6 +62,9 @@ class Window
6462
Window(WINDOW* wnd, Color standard_color);
6563
Window(WINDOW* wnd, bool enable_keypad, Color standard_color);
6664

65+
template<class TChar>
66+
const std::basic_string<TChar> replace(const std::basic_string<TChar>& format, const std::basic_string<TChar>& tag, const std::basic_string<TChar>& value);
67+
6768
void resize(int height, int width);
6869
void move(int y, int x, int height, int width);
6970
void set_color(Color color);
@@ -74,7 +75,7 @@ class Window
7475
void set_position(int y, int x);
7576
void clear_line();
7677
void set_scrollable(bool enable);
77-
int get_string_input(const char* allowed_characters, char* input, int input_y, int input_x, int force_case, int enable_escape, int enable_directionals);
78+
int get_string_input(const string& allowed_characters, string& input, int input_y, int input_x, int force_case, int enable_escape, int enable_directionals);
7879

7980
public:
8081
void clear();
@@ -83,15 +84,20 @@ class Window
8384
void unset_attribute(chtype attr);
8485
void refresh();
8586
void get_size(int& y, int& x);
86-
void print_centered(const char* str);
87-
int print(const char* format, ...);
88-
void print(char c);
89-
int write(const wchar_t* text);
90-
void write_block(int y, int x, Color color, const wchar_t** block, int rowcount);
91-
void write_block(int y, int x, Color color, const wchar_t** block, int topy, int leftx, int bottomy, int rightx);
92-
void write(int y, int x, Color color, const wchar_t* text);
87+
void print_centered(const string& str);
88+
89+
int print(const string& format, const string& value);
90+
int print(const wstring& format, const wstring& value);
91+
92+
int print(char c);
93+
int print(const string& text);
94+
int print(const wstring& text);
95+
96+
void print(int y, int x, Color color, const wstring* block, int rowcount);
97+
void print(int y, int x, Color color, const wstring* block, int topy, int leftx, int bottomy, int rightx);
98+
void print(int y, int x, Color color, const wstring& text);
9399
void wait_for_key(bool prompt = false);
94-
int get_char_input(const char* allowed);
100+
int get_char_input(const string& allowed);
95101
};
96102

97103
class InputWindow : protected Window
@@ -100,9 +106,18 @@ class InputWindow : protected Window
100106

101107
InputWindow(WINDOW* wnd);
102108

109+
template<class TChar>
110+
void print_error_template(const std::basic_string<TChar>& text);
111+
103112
public:
104-
void get_string_input(char* input, int max_length);
105-
void print_error(const char* format, ...);
113+
void get_string_input(string& input);
114+
115+
void print_error(const string& format, const string& value);
116+
void print_error(const wstring& format, const wstring& value);
117+
118+
void print_error(const string& text);
119+
void print_error(const wstring& text);
120+
106121
};
107122

108123
class Console

0 commit comments

Comments
 (0)