OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
MD5Checksum Class Reference

#include <checksum_model.h>

Public Member Functions

 MD5Checksum ()
void parse (std::string filenam)
int count ()
void member (int N, char *checksum_title, int *len_title, char *checksum, int *len_checksum)
std::list< std::string > get_checksums ()
void print ()

Private Member Functions

void remove_carriage_return (std::string &line)
std::string separator ()
std::string get_path (const std::string &filepath)
void new_checksum (std::string title, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
void process_checksum (std::string line, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
void end_checksum (std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
void finalize_checksum (std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
int file_read (std::string filename, std::string deck_directory, int level, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)

Private Attributes

std::list< std::tuple< int, std::string, md5_state_t, std::string > > md5_states
int debug =0

Detailed Description

Definition at line 40 of file checksum_model.h.

Constructor & Destructor Documentation

◆ MD5Checksum()

MD5Checksum::MD5Checksum ( )

Definition at line 232 of file checksum_model.cpp.

234 {};

Member Function Documentation

◆ count()

int MD5Checksum::count ( )

Definition at line 252 of file checksum_model.cpp.

252 {
253 // --------------------------------------------------------------------------------------------------------
254 return md5_states.size();
255 }
std::list< std::tuple< int, std::string, md5_state_t, std::string > > md5_states

◆ end_checksum()

void MD5Checksum::end_checksum ( std::list< std::tuple< int, std::string, md5_state_t, std::string > > * md5_states_tmp)
private

Definition at line 107 of file checksum_model.cpp.

107 {
108 // --------------------------------------------------------------------------------------------------------------------
109 int state=-1;
110 for (auto& item : *md5_states_tmp ) {
111 state=get<0>(item);
112 if (state == 1) {
113 get<0>(item) = 0; // Set Active flag to 0
114
115 // Finish the MD5 checksum
116 md5_state_t state = get<2>(item);
117 unsigned char md5[16];
118 md5_finish (&state, md5);
119
120 // Add MD5 in hexadecimal format in tuplet
121 ostringstream formatted_line;
122 for (int i = 0; i < 16; ++i) {
123 formatted_line << hex << setw(2) << setfill('0') << static_cast<int>(md5[i]);
124 }
125 get<3>(item)=formatted_line.str();
126 break;
127 }
128 }
129 };

◆ file_read()

int MD5Checksum::file_read ( std::string filename,
std::string deck_directory,
int level,
std::list< std::tuple< int, std::string, md5_state_t, std::string > > * md5_states_tmp )
private

Definition at line 169 of file checksum_model.cpp.

169 {
170 // -----------------------------------------------------------------------------------------------------------------------------
171 string chksum_start=( "/CHECKSUM/START");
172 string chksum_end=( "/CHECKSUM/END");
173 string chksum_include=( "#include ");
174 fstream new_file;
175 new_file.open(filename, ios::in);
176
177 // Stop after 15 levels of recursion
178 if (level > 15) return 0;
179
180 if ( !new_file.is_open() ) {
181 return -1;
182 }
183
184 string line;
185 while (getline(new_file, line)) {
186
187 remove_carriage_return(line); // Remove carriage return characters
188 // Search for /CHECKSUM/START keyword
189 if (line == chksum_start) {
190 string title;
191 getline(new_file, title);
192 remove_carriage_return(title); // Remove carriage return characters
193 new_checksum(title,md5_states_tmp);
194 continue;
195 }
196 // Search for /CHECKSUM/END keyword
197 if (line == chksum_end) {
198 end_checksum(md5_states_tmp);
199 continue;
200 }
201
202 string comp=line.substr(0,chksum_include.length());
203 if (comp == chksum_include) {
204 // Process include files
205 string include_file = line.substr(chksum_include.length());
206 if (deck_directory.length() > 0){
207 include_file = deck_directory+separator()+include_file; // Get the path of the file
208 }
209 if (debug){
210 cout << "Include file: " << include_file << endl;
211 }
212 // include_file.erase( remove(include_file.rbegin(), include_file.end(), ' '), include_file.end());
213 // debug cout << "Include file: " << include_file << endl;
214 file_read(include_file,deck_directory, level + 1,md5_states_tmp);
215 continue;
216 }
217
218 if (line[0] == '#') {
219 // Skip comment lines
220 continue;
221 }
222 process_checksum(line,md5_states_tmp);
223
224 }
225 new_file.close();
226 return 0;
227 }
void end_checksum(std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
std::string separator()
void process_checksum(std::string line, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
int file_read(std::string filename, std::string deck_directory, int level, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
void new_checksum(std::string title, std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)
void remove_carriage_return(std::string &line)
int comp(int a, int b)

◆ finalize_checksum()

void MD5Checksum::finalize_checksum ( std::list< std::tuple< int, std::string, md5_state_t, std::string > > * md5_states_tmp)
private

Definition at line 137 of file checksum_model.cpp.

137 {
138 // --------------------------------------------------------------------------------------------------------------------
139 int state=-1;
140 for (auto& item : *md5_states_tmp ) {
141 state=get<0>(item);
142 if (state == 1) {
143 get<0>(item) = 0; // Set Active flag to 0
144
145 // Finish the MD5 checksum
146 md5_state_t state = get<2>(item);
147 unsigned char md5[16];
148 md5_finish (&state, md5);
149
150 // Add MD5 in hexadecimal format in tuplet
151 ostringstream formatted_line;
152 for (int i = 0; i < 16; ++i) {
153 formatted_line << hex << setw(2) << setfill('0') << static_cast<int>(md5[i]);
154 }
155 get<3>(item)=formatted_line.str();
156 }
157 }
158 };

◆ get_checksums()

list< string > MD5Checksum::get_checksums ( )

Definition at line 303 of file checksum_model.cpp.

303 {
304 // --------------------------------------------------------------------------------------------------------
305 list<string> checksums;
306 for (const auto& item : md5_states){
307 if (get<0>(item) == 0){
308 string chksum_item=get<1>(item)+"_"+get<3>(item);
309 checksums.push_back(chksum_item);
310 }
311 }
312 return checksums;
313 }

◆ get_path()

std::string MD5Checksum::get_path ( const std::string & filepath)
private

Definition at line 50 of file checksum_model.cpp.

50 {
51 // Find the last occurrence of the path separator
52#ifdef _WIN64
53 size_t pos = filepath.find_last_of("/\\");
54 if (pos == std::string::npos) {
55 pos = filepath.find_last_of("/");
56 }
57#else
58 size_t pos = filepath.find_last_of("/");
59#endif
60 if (pos != std::string::npos) {
61 // Extract the substring up to the last separator
62 return filepath.substr(0, pos);
63 }
64 // If no separator is found, return an empty string
65 return "";
66 }

◆ member()

void MD5Checksum::member ( int N,
char * checksum_title,
int * len_title,
char * checksum,
int * len_checksum )

Definition at line 268 of file checksum_model.cpp.

268 {
269 // --------------------------------------------------------------------------------------------------------
270 int count= md5_states.size();
271 if (N > count) {
272 cout << "Error: N=" << N << " is greater than the number of checksums " << count << endl;
273 checksum[0]='\0';
274 checksum_title[0]='\0';
275 *len_checksum=0;
276 *len_title=0;
277 }else{
278 auto it = md5_states.begin();
279 advance(it, N-1); // Move iterator to the N-th element (0-based index)
280
281 // Copy checksum_title
282 int size_title = get<1>(*it).size();
283 get<1>(*it).copy(checksum_title ,size_title);
284 checksum_title[size_title]='\0';
285 *len_title=size_title;
286
287 // Copy checksum
288 int size_checksum = get<3>(*it).size();
289 get<3>(*it).copy(checksum ,size_checksum);
290 checksum[size_checksum]='\0';
291 *len_checksum=size_checksum;
292
293 // cout << "Member " << *N << " Checksum : " << get<1>(*it) << " " << get<3>(*it) << endl;
294 }
295 }
#define N

◆ new_checksum()

void MD5Checksum::new_checksum ( std::string title,
std::list< std::tuple< int, std::string, md5_state_t, std::string > > * md5_states_tmp )
private

Definition at line 69 of file checksum_model.cpp.

69 {
70 // ------------------------------------------------------------------------------------------------------------------------
71 // CHECKSUM/START was met, create a new checksum state
72 // input:
73 // title : title of the checksum
74 // input / output:
75 // md5_states_tmp : list of checksums computed in the file
76 // ------------------------------------------------------------------------------------------------------------------------
77 md5_state_t new_md5;
78 md5_init(&new_md5);
79 string md5_digest(16,'0');
80 md5_states_tmp->push_front(make_tuple(1,title,new_md5,md5_digest));
81 };

◆ parse()

void MD5Checksum::parse ( std::string filenam)

Definition at line 236 of file checksum_model.cpp.

236 {
237 list<tuple<int,string, md5_state_t, string>> md5_states_tmp;
238 string deck_directory = get_path(filenam); // Get the directory of the file
239 file_read(filenam,deck_directory,0,&md5_states_tmp);
240 finalize_checksum(&md5_states_tmp); // Finalize all active checksums
241 // intvert the list to have it in deck order
242 for (const auto& item : md5_states_tmp){
243 md5_states.push_front(item); // Add the checksums to the main list
244 }
245
246 md5_states_tmp.clear(); // Clear the temporary list
247 };
std::string get_path(const std::string &filepath)
void finalize_checksum(std::list< std::tuple< int, std::string, md5_state_t, std::string > > *md5_states_tmp)

◆ print()

void MD5Checksum::print ( )

Definition at line 318 of file checksum_model.cpp.

318 {
319 // ------------------------
320 cout << "Checksum list " << endl;
321 cout << "==============" << endl;
322 for (const auto& item : md5_states){
323 if (get<0>(item) == 0){
324 cout << "Checksum : " << get<1>(item) << " " << get<3>(item) << endl;
325 }
326 }
327 }

◆ process_checksum()

void MD5Checksum::process_checksum ( std::string line,
std::list< std::tuple< int, std::string, md5_state_t, std::string > > * md5_states_tmp )
private

Definition at line 90 of file checksum_model.cpp.

90 {
91 // ------------------------------------------------------------------------------------------------------------------------
92 for (auto& item : *md5_states_tmp ) {
93 if (get<0>(item) == 1){
94 md5_state_t state = get<2>(item);
95 md5_append( &state, (const md5_byte_t *) line.c_str(), line.length() );
96 get<2>(item) = state;
97 }
98 }
99 };

◆ remove_carriage_return()

void MD5Checksum::remove_carriage_return ( std::string & line)
private

Definition at line 30 of file checksum_model.cpp.

30 {
31 line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
32 }

◆ separator()

string MD5Checksum::separator ( )
private

Definition at line 39 of file checksum_model.cpp.

39 {
40 // -----------------------------------------------------------------------------------
41 #ifdef _WIN64
42 return "\\"; // Windows separator
43 #else
44 return "/"; // Unix separator
45 #endif
46 }

Field Documentation

◆ debug

int MD5Checksum::debug =0
private

Definition at line 48 of file checksum_model.h.

◆ md5_states

std::list<std::tuple<int,std::string, md5_state_t, std::string> > MD5Checksum::md5_states
private

Definition at line 41 of file checksum_model.h.


The documentation for this class was generated from the following files: