Skip to content

Commit 25fda1f

Browse files
committed
fix tabs/spaces, remove unused struct
1 parent 0ab9400 commit 25fda1f

File tree

7 files changed

+998
-1018
lines changed

7 files changed

+998
-1018
lines changed

logger.hpp

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
#include "utils.hpp"
1111

12-
enum class log_level : std::uint32_t
13-
{
12+
enum class log_level : std::uint32_t {
1413
LOG_FATAL,
1514
LOG_ERROR,
1615
LOG_WARN,
@@ -20,37 +19,33 @@ enum class log_level : std::uint32_t
2019
LOG_NOPREFIX, // keep this last
2120
};
2221

23-
class logger
24-
{
22+
class logger {
2523
public:
2624

27-
logger( const std::wstring_view& title_name = {} )
28-
{
25+
logger(const std::wstring_view &title_name = {}) {
26+
2927
AllocConsole();
30-
AttachConsole( GetCurrentProcessId() );
28+
AttachConsole(GetCurrentProcessId());
3129

32-
if ( !title_name.empty() )
33-
{
34-
SetConsoleTitle( title_name.data() );
30+
if (!title_name.empty()) {
31+
SetConsoleTitle(title_name.data());
3532
}
3633

37-
FILE* in = nullptr;
38-
FILE* out = nullptr;
34+
FILE *in = nullptr;
35+
FILE *out = nullptr;
3936

40-
freopen_s( &in, "conin$", "r", stdin );
41-
freopen_s( &out, "conout$", "w", stdout );
42-
freopen_s( &out, "conout$", "w", stderr );
37+
freopen_s(&in, "conin$", "r", stdin);
38+
freopen_s(&out, "conout$", "w", stdout);
39+
freopen_s(&out, "conout$", "w", stderr);
4340

44-
console_handle = GetStdHandle( STD_OUTPUT_HANDLE );
41+
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
4542
}
4643

47-
~logger()
48-
{
44+
~logger() {
4945
FreeConsole();
5046
}
5147

52-
enum class console_colors : std::uint8_t
53-
{
48+
enum class console_colors : std::uint8_t {
5449
BLACK,
5550
DARK_BLUE,
5651
DARK_GREEN,
@@ -69,8 +64,8 @@ class logger
6964
WHITE,
7065
};
7166

72-
struct log_type_info_t
73-
{
67+
struct log_type_info_t {
68+
7469
std::string prefix{};
7570
console_colors fg = console_colors::WHITE;
7671
console_colors bg = console_colors::BLACK;
@@ -79,92 +74,90 @@ class logger
7974
};
8075

8176
template<typename ... arg>
82-
void print_colored( console_colors fg, console_colors bg, bool newline, std::string_view fmt, arg ... args )
83-
{
84-
std::unique_lock< decltype( m )> lock( m );
77+
void print_colored(console_colors fg, console_colors bg, bool newline, std::string_view fmt, arg ... args) {
8578

86-
const auto txt = utils::format_string( fmt, args... );
79+
std::unique_lock< decltype(m)> lock(m);
8780

88-
set_console_color( fg, bg );
81+
const auto txt = utils::format_string(fmt, args...);
82+
83+
set_console_color(fg, bg);
8984

9085
std::cout << txt.c_str();
9186

92-
set_console_color( console_colors::WHITE, console_colors::BLACK );
87+
set_console_color(console_colors::WHITE, console_colors::BLACK);
9388

9489
if (newline) {
9590
std::cout << std::endl;
9691
}
9792
}
9893

9994
template< typename ... arg >
100-
void print( log_level level, std::string_view fmt, arg ... args ) {
101-
std::unique_lock< decltype( m )> lock( m );
95+
void print(log_level level, std::string_view fmt, arg ... args) {
96+
97+
std::unique_lock< decltype(m)> lock(m);
10298

10399
const auto &info = console_type_info[level];
104-
const auto txt = utils::format_string( fmt, args... );
100+
const auto txt = utils::format_string(fmt, args...);
105101

106-
set_console_color( info.fg, info.bg );
102+
set_console_color(info.fg, info.bg);
107103

108-
if ( level < log_level::LOG_NOPREFIX ) {
104+
if (level < log_level::LOG_NOPREFIX) {
109105
std::cout << info.prefix;
110106
}
111107

112108
std::cout << txt.c_str();
113109

114-
set_console_color( console_colors::WHITE, console_colors::BLACK );
110+
set_console_color(console_colors::WHITE, console_colors::BLACK);
115111

116112
std::cout << std::endl;
117113
}
118114

119115
template< typename ... arg >
120-
void print_with_func( log_level level, std::string_view func_name, std::string_view fmt, arg ... args )
121-
{
122-
std::unique_lock< decltype( m )> lock( m );
116+
void print_with_func(log_level level, std::string_view func_name, std::string_view fmt, arg ... args) {
123117

124-
const auto &info = console_type_info[ level ];
125-
const auto txt = utils::format_string( fmt, args... );
118+
std::unique_lock< decltype(m)> lock(m);
126119

127-
set_console_color( info.fg, info.bg );
120+
const auto &info = console_type_info[level];
121+
const auto txt = utils::format_string(fmt, args...);
122+
123+
set_console_color(info.fg, info.bg);
128124

129-
if ( level < log_level::LOG_NOPREFIX )
130-
{
125+
if (level < log_level::LOG_NOPREFIX) {
131126
std::cout << info.prefix;
132127
}
133128

134129
std::cout << "[ " << func_name.data() << " ] " << txt.c_str();
135130

136-
set_console_color( console_colors::WHITE, console_colors::BLACK );
131+
set_console_color(console_colors::WHITE, console_colors::BLACK);
137132

138133
std::cout << std::endl;
139134
}
140135

141136
private:
142-
inline bool set_console_color( const console_colors fg, const console_colors bg )
143-
{
144-
if ( console_handle != INVALID_HANDLE_VALUE )
145-
{
146-
WORD color = (uint8_t)fg | ( (uint8_t)bg << 4 );
147-
return SetConsoleTextAttribute( console_handle, color );
137+
inline bool set_console_color(const console_colors fg, const console_colors bg) {
138+
139+
if (console_handle != INVALID_HANDLE_VALUE) {
140+
WORD color = (uint8_t)fg | ((uint8_t)bg << 4);
141+
return SetConsoleTextAttribute(console_handle, color);
148142
}
149143

150144
return false;
151145
}
152146

153-
std::unordered_map< log_level, log_type_info_t > console_type_info =
154-
{
155-
{ log_level::LOG_FATAL, { "[ ! ] ", console_colors::RED, console_colors::WHITE } },
156-
{ log_level::LOG_ERROR, { "[ - ] ", console_colors::RED, console_colors::BLACK } },
157-
{ log_level::LOG_WARN, { "[ # ] ", console_colors::BLACK, console_colors::YELLOW } },
158-
{ log_level::LOG_OK, { "[ + ] ", console_colors::GREEN, console_colors::BLACK } },
159-
{ log_level::LOG_INFO, { "[ ~ ] ", console_colors::WHITE, console_colors::BLACK } },
160-
{ log_level::LOG_DEBUG, { " ", console_colors::DARK_GRAY, console_colors::BLACK } },
147+
std::unordered_map< log_level, log_type_info_t > console_type_info = {
148+
{log_level::LOG_FATAL, {"[ ! ] ", console_colors::RED, console_colors::WHITE}},
149+
{log_level::LOG_ERROR, {"[ - ] ", console_colors::RED, console_colors::BLACK}},
150+
{log_level::LOG_WARN, {"[ # ] ", console_colors::BLACK, console_colors::YELLOW}},
151+
{log_level::LOG_OK, {"[ + ] ", console_colors::GREEN, console_colors::BLACK}},
152+
{log_level::LOG_INFO, {"[ ~ ] ", console_colors::WHITE, console_colors::BLACK}},
153+
{log_level::LOG_DEBUG, {" ", console_colors::DARK_GRAY, console_colors::BLACK}},
161154
};
162155

163156
std::mutex m;
164157
HANDLE console_handle = INVALID_HANDLE_VALUE;
165158
};
166159

167-
inline auto g_logger = std::make_unique< logger >( L"~ rpgmaker scraper by nit ~" );
160+
inline auto g_logger = std::make_unique< logger >(L"~ rpgmaker scraper by nit ~");
168161
#define log_colored_nnl(fg, bg, ...) g_logger->print_colored(fg, bg, false, __VA_ARGS__)
169162
#define log_colored( fg, bg, ... ) g_logger->print_colored( fg, bg, true, __VA_ARGS__ )
170163
#define _log(log_type, ...) g_logger->print( log_type, __VA_ARGS__ )

main.cpp

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,71 @@
55

66
int main(int argc, const char *argv[]) {
77

8-
constexpr uint32_t expected_minimum_argc = 3;
9-
10-
constexpr const char* search_type_variables = "-v";
11-
constexpr const char* search_type_switches = "-s";
12-
13-
const auto print_usage = []() {
14-
log_err(R"(Usage: RPGMakerScraper -v 143 test_output.txt)");
15-
};
16-
17-
// check the argument count
18-
if (argc < expected_minimum_argc) {
19-
print_usage();
20-
return 1;
21-
}
22-
23-
const std::string search_type{ argv[1] };
24-
const std::string id_str{ argv[2] };
25-
26-
const bool output_to_file = argc == 4;
27-
28-
// check search types
29-
if (search_type != search_type_variables) {
30-
log_err(R"(invalid search type. Only '-v' for variables is supported at the moment.")");
31-
print_usage();
32-
return 1;
33-
}
34-
35-
// search variable ids
36-
if (search_type == search_type_variables) {
37-
// make sure this id is actually a number
38-
if (!std::all_of(id_str.begin(), id_str.end(), isdigit)) {
39-
log_err(R"(invalid variable id. Please provide a number.")");
40-
print_usage();
41-
return 1;
42-
}
43-
44-
const uint32_t variable_id = static_cast<uint32_t>(std::stoul(id_str));
45-
46-
try {
47-
RPGMakerScraper scraper{ ScrapeMode::VARIABLES, variable_id };
48-
scraper.scrape_variables();
49-
50-
if (output_to_file) {
51-
log_info(R"(writing results to %s...)", argv[3]);
52-
const std::string file_name{ argv[3] };
53-
std::ofstream file(file_name, std::ios_base::out);
54-
55-
if (!file.is_open() || !file.good()) {
56-
throw std::invalid_argument(R"(unable to create output file)");
57-
}
58-
59-
file << scraper;
60-
file.close();
61-
62-
log_ok(R"(results wrote successfully.)");
63-
}
64-
65-
} catch (const std::exception &e) {
66-
log_err(R"(exception caught: %s)", e.what());
67-
}
68-
}
69-
70-
log_nopre("\n");
71-
log_ok(R"(press any key to close the program...)");
72-
73-
std::cin.get();
74-
return 0;
8+
constexpr uint32_t expected_minimum_argc = 3;
9+
10+
constexpr const char *search_type_variables = "-v";
11+
constexpr const char *search_type_switches = "-s";
12+
13+
const auto print_usage = []() {
14+
log_err(R"(Usage: RPGMakerScraper -v 143 test_output.txt)");
15+
};
16+
17+
// check the argument count
18+
if (argc < expected_minimum_argc) {
19+
print_usage();
20+
return 1;
21+
}
22+
23+
const std::string search_type{argv[1]};
24+
const std::string id_str{argv[2]};
25+
26+
const bool output_to_file = argc == 4;
27+
28+
// check search types
29+
if (search_type != search_type_variables) {
30+
log_err(R"(invalid search type. Only '-v' for variables is supported at the moment.")");
31+
print_usage();
32+
return 1;
33+
}
34+
35+
// search variable ids
36+
if (search_type == search_type_variables) {
37+
// make sure this id is actually a number
38+
if (!std::all_of(id_str.begin(), id_str.end(), isdigit)) {
39+
log_err(R"(invalid variable id. Please provide a number.")");
40+
print_usage();
41+
return 1;
42+
}
43+
44+
const uint32_t variable_id = static_cast<uint32_t>(std::stoul(id_str));
45+
46+
try {
47+
RPGMakerScraper scraper{ScrapeMode::VARIABLES, variable_id};
48+
scraper.scrape_variables();
49+
50+
if (output_to_file) {
51+
log_info(R"(writing results to %s...)", argv[3]);
52+
const std::string file_name{argv[3]};
53+
std::ofstream file(file_name, std::ios_base::out);
54+
55+
if (!file.is_open() || !file.good()) {
56+
throw std::invalid_argument(R"(unable to create output file)");
57+
}
58+
59+
file << scraper;
60+
file.close();
61+
62+
log_ok(R"(results wrote successfully.)");
63+
}
64+
65+
} catch (const std::exception &e) {
66+
log_err(R"(exception caught: %s)", e.what());
67+
}
68+
}
69+
70+
log_nopre("\n");
71+
log_ok(R"(press any key to close the program...)");
72+
73+
std::cin.get();
74+
return 0;
7575
}

0 commit comments

Comments
 (0)