Skip to content

Commit 1d40016

Browse files
committed
v2
1 parent 94df984 commit 1d40016

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Header_File_constructor.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace arg_info {
5555
bool open_help = false; //-h or --help : Display help
5656
std::string relocate_path; //-r or --relocate : Relocate the input file path in "#line" to the specified path
5757
bool relocate_path_was_an_url = false;
58+
bool format_line_beginning = false; //-f or --format : Format the line beginning
59+
bool using_std_out = false; //-s or --std-out : Output to standard output
5860
} // namespace arg_info
5961

6062
//路径转义为合法的C++字符串
@@ -87,7 +89,9 @@ void process_file(std::filesystem::path in_file, std::istream& in, std::ostream&
8789
while(std::getline(in, line)) {
8890
line_num++;
8991
//get line begin of this line
90-
std::string line_begin_of_this_line = line_begin;
92+
std::string line_begin_of_this_line;
93+
if(arg_info::format_line_beginning)
94+
line_begin_of_this_line = line_begin;
9195
{
9296
auto pos = line.find_first_not_of(" \t");
9397
if(pos != std::string::npos) {
@@ -208,12 +212,18 @@ void process_file(std::filesystem::path in_file, std::istream& in, std::ostream&
208212
void process_file(std::string in_file_name, std::string out_file_name, std::filesystem::path root_path_for_skip) {
209213
std::cout << "process file: " << in_file_name << std::endl;
210214
std::ifstream in_file(in_file_name);
211-
std::ofstream out_file(out_file_name, std::ios_base::binary);
215+
std::ofstream out_file;
216+
std::ostream* out_stream = &out_file;
217+
if(arg_info::using_std_out)
218+
out_stream = &std::cout;
219+
else
220+
out_file.open(out_file_name, std::ios_base::binary);
212221
std::filesystem::path include_path = std::filesystem::path(in_file_name).parent_path();
213-
if(in_file.is_open() && out_file.is_open()) {
222+
if(in_file.is_open() && (arg_info::using_std_out || out_file.is_open())) {
214223
process_file(in_file_name, in_file, out_file, "", include_path, root_path_for_skip);
215224
in_file.close();
216-
out_file.close();
225+
if(!arg_info::using_std_out)
226+
out_file.close();
217227
}
218228
else {
219229
std::cerr << "can't open file" << std::endl;
@@ -236,6 +246,11 @@ void print_help() {
236246
std::cout << " Display help" << std::endl;
237247
std::cout << " -r, --relocate" << std::endl;
238248
std::cout << " Relocate the input file path in \"#line\" to the specified path" << std::endl;
249+
std::cout << " -f, --format" << std::endl;
250+
std::cout << " Format the line beginning" << std::endl;
251+
std::cout << " This will result in a better looking output file, but the number of columns in the compilation warning will not match the source file." << std::endl;
252+
std::cout << " -s, --std-out" << std::endl;
253+
std::cout << " Output to standard output" << std::endl;
239254
std::cout << "if in_file is a directory, out_file must be a directory or not exist," << std::endl;
240255
std::cout << "and all superficial files in in_file will be processed." << std::endl;
241256
}
@@ -255,6 +270,12 @@ int main(size_t argc, char* _argv[]) {
255270
else if(arg == "-h" || arg == "--help") {
256271
arg_info::open_help = true;
257272
}
273+
else if(arg == "-f" || arg == "--format") {
274+
arg_info::format_line_beginning = true;
275+
}
276+
else if(arg == "-s" || arg == "--std-out") {
277+
arg_info::using_std_out = true;
278+
}
258279
else if(arg == "-r" || arg == "--relocate") {
259280
if(i + 1 < argv.size()) {
260281
arg_info::relocate_path = argv[i + 1];

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Options:
1313
Display help
1414
-r, --relocate
1515
Relocate the input file path in "#line" to the specified path
16+
-f, --format
17+
Format the line beginning
18+
This will result in a better looking output file, but the number of columns in the compilation warning will not match the source file.
19+
-s, --std-out
20+
Output to standard output
1621
if in_file is a directory, out_file must be a directory or not exist,
1722
and all superficial files in in_file will be processed.
1823
```

0 commit comments

Comments
 (0)