20
20
#include < regex>
21
21
#include < sstream>
22
22
#include < string>
23
+ #include < utility>
23
24
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
+ */
24
47
auto getCurrentDateTime () -> std::string {
25
48
auto now = std::chrono::system_clock::now ();
26
49
std::time_t const time = std::chrono::system_clock::to_time_t (now);
@@ -32,10 +55,10 @@ auto getCurrentDateTime() -> std::string {
32
55
}
33
56
34
57
/* *
35
- * Rewrite the Device line to remove the sof path
58
+ * @brief Rewrite the Device line to remove the sof path
36
59
* @param[in/out] svf_data Serial Vector File as String
37
60
*/
38
- void extract_device_name (std::string &svf_data) {
61
+ void clear_sof_path (std::string &svf_data) {
39
62
std::regex const device_info_regex (" (!Device #1:) (.*) - (.*)" );
40
63
std::smatch match;
41
64
@@ -45,25 +68,6 @@ void extract_device_name(std::string &svf_data) {
45
68
}
46
69
}
47
70
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 !\n SIR 10 TDI (203);\n RUNTEST 128 TCK;\n SDR 23 TDI (000000);\n SIR 10 TDI (3F2);\n RUNTEST 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
-
67
71
/* *
68
72
* @brief CLI main application
69
73
* @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) {
72
76
*/
73
77
auto main (int num_args, char **args) -> int {
74
78
// 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;
77
81
return EXIT_FAILURE;
78
82
}
79
83
80
84
// 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);
82
87
83
88
// Check if the file was opened successfully
84
89
if (!infile.is_open ()) {
85
90
std::cerr << " Error: Could not open file." << std::endl;
86
91
return EXIT_FAILURE;
87
92
}
88
93
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
+ }
91
113
92
114
// Close the input file
93
115
infile.close ();
94
116
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
+ }
98
129
99
130
// Open the output file for writing
100
- std::ofstream outfile (args[ 2 ] );
131
+ std::ofstream outfile (output_filename );
101
132
102
133
// Check if the output file was opened successfully
103
134
if (!outfile.is_open ()) {
0 commit comments