Skip to content

Commit 1932f81

Browse files
committed
fix(main): clear all DSM commands, output filename is now optional
1 parent afc7d05 commit 1932f81

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

src/main.cpp

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,30 @@
2020
#include <regex>
2121
#include <sstream>
2222
#include <string>
23+
#include <utility>
2324

25+
/**
26+
* @brief Split a filename into its base name and extension.
27+
* @param[in] filename The filename to split.
28+
* @return A tuple containing the base name and extension of the filename.
29+
*/
30+
auto split_filename(const std::string &filename) -> std::pair<std::string, std::string> {
31+
size_t const dot_pos = filename.find_last_of('.');
32+
if (dot_pos == std::string::npos) {
33+
// if the dot is not found, return the whole filename as the first element
34+
return std::make_pair(filename, "");
35+
} else {
36+
// split the filename into two substrings based on the dot position
37+
std::string const name = filename.substr(0, dot_pos);
38+
std::string const ext = filename.substr(dot_pos + 1);
39+
return std::make_pair(name, ext);
40+
}
41+
}
42+
43+
/**
44+
* @brief Get the Current Date and Time
45+
* @return a string in the following format: Tue Apr 12 23:36:31 2023.
46+
*/
2447
auto getCurrentDateTime() -> std::string {
2548
auto now = std::chrono::system_clock::now();
2649
std::time_t const time = std::chrono::system_clock::to_time_t(now);
@@ -32,10 +55,10 @@ auto getCurrentDateTime() -> std::string {
3255
}
3356

3457
/**
35-
* Rewrite the Device line to remove the sof path
58+
* @brief Rewrite the Device line to remove the sof path
3659
* @param[in/out] svf_data Serial Vector File as String
3760
*/
38-
void extract_device_name(std::string &svf_data) {
61+
void clear_sof_path(std::string &svf_data) {
3962
std::regex const device_info_regex("(!Device #1:) (.*) - (.*)");
4063
std::smatch match;
4164

@@ -45,25 +68,6 @@ void extract_device_name(std::string &svf_data) {
4568
}
4669
}
4770

48-
/**
49-
* @brief Remove the MAX 10 DSM Clear Commands
50-
* @param[in/out] svf_data Serial Vector File as String
51-
*/
52-
void remove_dsm_clear(std::string &svf_data) {
53-
// Define the string to search and replace
54-
std::string const search_string = "!\n!Max 10 DSM Clear\n!\nSIR 10 TDI (203);\nRUNTEST 128 TCK;\nSDR 23 TDI (000000);\nSIR 10 TDI (3F2);\nRUNTEST 8750003 TCK;";
55-
std::string const replace_string = "!\n!Max 10 DSM Clear (REMOVED)";
56-
57-
// Find the position of the search string in the input string
58-
size_t pos = svf_data.find(search_string);
59-
60-
// Replace all occurrences of the search string with the replace string
61-
while (pos != std::string::npos) {
62-
svf_data.replace(pos, search_string.length(), replace_string);
63-
pos = svf_data.find(search_string, pos + replace_string.length());
64-
}
65-
}
66-
6771
/**
6872
* @brief CLI main application
6973
* @param[in] num_args number of arguments being passed into the program from the command line
@@ -72,32 +76,59 @@ void remove_dsm_clear(std::string &svf_data) {
7276
*/
7377
auto main(int num_args, char **args) -> int {
7478
// Check if the number of parameters are correct
75-
if (num_args != 3) {
76-
std::cerr << "Usage: " << args[0] << " <input.svf> <output.svf>" << std::endl;
79+
if (num_args < 2) {
80+
std::cerr << "Usage: " << args[0] << " input_filename [output_filename]" << std::endl;
7781
return EXIT_FAILURE;
7882
}
7983

8084
// Open the input file for reading
81-
std::ifstream infile(args[1], std::ios::binary);
85+
std::string const input_filename = args[1];
86+
std::ifstream infile(input_filename, std::ios::binary);
8287

8388
// Check if the file was opened successfully
8489
if (!infile.is_open()) {
8590
std::cerr << "Error: Could not open file." << std::endl;
8691
return EXIT_FAILURE;
8792
}
8893

89-
// Read the contents of the file into a string
90-
std::string svf_data((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
94+
// Read the input file line by line into a string
95+
std::string svf_data;
96+
std::string line;
97+
bool skip_lines = false; // flag to skip lines
98+
while (getline(infile, line)) {
99+
// check if the current line matches the starting string
100+
if (line == "!Max 10 DSM Clear") {
101+
skip_lines = true; // set the flag to skip lines
102+
}
103+
// check if the current line matches the ending string
104+
if (line == "!Max 10 Disable ISP") {
105+
skip_lines = false; // unset the flag to stop skipping lines
106+
}
107+
// if the flag is set, skip the current line
108+
if (skip_lines) {
109+
continue;
110+
}
111+
svf_data += line + "\n";
112+
}
91113

92114
// Close the input file
93115
infile.close();
94116

95-
// Replace the specified string
96-
remove_dsm_clear(svf_data);
97-
extract_device_name(svf_data);
117+
// Clear the .sof path reference
118+
clear_sof_path(svf_data);
119+
120+
// Handle output filename
121+
std::string output_filename;
122+
if (num_args >= 3) {
123+
output_filename = args[2];
124+
} else {
125+
// if the output filename is not provided, set it to input_filename_nodsm.ext
126+
std::pair<std::string, std::string> const file_ext = split_filename(input_filename);
127+
output_filename = file_ext.first + "_nodsm." + file_ext.second;
128+
}
98129

99130
// Open the output file for writing
100-
std::ofstream outfile(args[2]);
131+
std::ofstream outfile(output_filename);
101132

102133
// Check if the output file was opened successfully
103134
if (!outfile.is_open()) {

0 commit comments

Comments
 (0)