GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: program/main_split.cpp Lines: 47 70 67.1 %
Date: 2024-12-09 15:30:52 Branches: 70 154 45.5 %

Line Branch Exec Source
1
/***************************************
2
	Auteur : Pierre Aubert
3
	Mail : pierre.aubert@lapp.in2p3.fr
4
	Licence : CeCILL-C
5
****************************************/
6
7
#include "string_utils.h"
8
#include "convertToString.h"
9
#include "OptionParser.h"
10
11
#include "phoenix_mock.h"
12
13
///Create the OptionParser of this program
14
/**	@return OptionParser of this program
15
*/
16
2
OptionParser createOptionParser(){
17
4
	OptionParser parser(true, __PROGRAM_VERSION__);
18
2
	parser.setExampleLongOption("phoenix_mock_split --input=file.pmock --output=split.pmock");
19
2
	parser.setExampleShortOption("phoenix_mock_split -i file2.mock file2.pmock -o split.pmock");
20
21

2
	parser.addOption("input", "i", OptionType::FILENAME, true, "List of input mock to be split");
22
23
4
	std::string defaultOutputFile("./split.pmock");
24

2
	parser.addOption("output", "o", defaultOutputFile, "Name of the output split mock file");
25
26
2
	size_t defaultOffset(0lu), defaultSizePart(1lu), defaultNbPart(0lu);
27

2
	parser.addOption("offset", "f", defaultOffset, "Offset of the first message to be extracted in a split mock");
28

2
	parser.addOption("sizepart", "s", defaultSizePart, "Size of each split part (number of messages in each part to be split)");
29

2
	parser.addOption("nbpart", "n", defaultNbPart, "Number of output mock to be created (0 means automatic by respect to --offset and --sizepart)");
30
4
	return parser;
31
}
32
33
///Make the output file name
34
/**	@param baseFileName : base name of the file
35
 * 	@param indexFile : index of hte file
36
 * 	@param extentionFile : extention of the file
37
 * 	@return corresponding output file
38
*/
39
10
std::string phoenix_mockMakeOutputFile(const std::string & baseFileName, size_t indexFile, const std::string & extentionFile){
40
10
	std::string extention(extentionFile);
41
10
	if(extention == ""){
42
		extention = "pmock";
43
	}
44

30
	return baseFileName + "_" + convertToString(indexFile) + "." + extention;
45
}
46
47
///Merge mock files
48
/**	@param vecInputFile : vector of input files to be merged
49
 * 	@param outputFile : output file name to be saved
50
 * 	@param offsetPart : offset of the first message to be extracted in a split mock
51
 * 	@param sizePart : size of each split part (number of messages in each part to be split)
52
 * 	@param nbPart : number of output mock to be created (0 means automatic by respect to --offset and --sizepart)
53
 * 	@return true on success, false otherwise
54
*/
55
2
bool splitMock(const std::vector<std::string> & vecInputFile, const std::string & outputFile,
56
		size_t offsetPart, size_t sizePart, size_t nbPart)
57
{
58

2
	if(sizePart == 0lu && nbPart == 0lu){
59
		std::cerr << "Error sizePart cannot be 0 id nbPart == 0, aborting split" << std::endl;
60
		return false;
61
	}
62
4
	std::string outputExtension(getExtention(outputFile)), baseOutputFile(eraseExtension(outputFile));
63
2
	bool b(true);
64

4
	for(std::vector<std::string>::const_iterator itFile(vecInputFile.begin()); itFile != vecInputFile.end() && b; ++itFile){
65
4
		std::vector<std::vector<char> > vecTmpFile;
66
2
		if(data_load(*itFile, vecTmpFile)){
67
1
			size_t nbMessageIn(vecTmpFile.size());
68
1
			if(sizePart == 0lu){
69
				if(offsetPart <= nbMessageIn){
70
					sizePart = (nbMessageIn - offsetPart)/nbPart;
71
					if(sizePart == 0lu){
72
						std::cerr << "splitMock : cannot split nbMessageIn = " << nbMessageIn << ", into nbPart = "<<nbPart<<" of size sizePart = "<<sizePart<<" and offset offsetPart = " << offsetPart << std::endl;
73
						b = false;
74
					}else{
75
						for(size_t i(0lu); i < nbPart; ++i){
76
							std::vector<std::vector<char> > vecMessage;
77
							splitVecMessage(vecMessage, vecTmpFile, offsetPart + i*sizePart, sizePart);
78
							b &= data_save(phoenix_mockMakeOutputFile(baseOutputFile, i, outputExtension), vecMessage);
79
						}
80
					}
81
				}else{
82
					std::vector<std::vector<char> > vecMessage;
83
// 					splitVecMessage(vecMessage, vecTmpFile, offsetPart, sizePart);
84
					b &= data_save(phoenix_mockMakeOutputFile(baseOutputFile, 0lu, outputExtension), vecMessage);
85
				}
86
87
1
			}else if(nbPart == 0lu){
88
1
				if((sizePart + offsetPart) <= nbMessageIn){
89
1
					nbPart = (nbMessageIn-offsetPart)/sizePart;
90
11
					for(size_t i(0lu); i < nbPart; ++i){
91
10
						std::vector<std::vector<char> > vecMessage;
92
10
						splitVecMessage(vecMessage, vecTmpFile, offsetPart + i*sizePart, sizePart);
93
10
						b &= data_save(phoenix_mockMakeOutputFile(baseOutputFile, i, outputExtension), vecMessage);
94
					}
95
				}else{
96
					std::vector<std::vector<char> > vecMessage;
97
// 					splitVecMessage(vecMessage, vecTmpFile, offsetPart, sizePart);
98
					b &= data_save(phoenix_mockMakeOutputFile(baseOutputFile, 0lu, outputExtension), vecMessage);
99
				}
100
			}else{
101
				if((sizePart*nbPart + offsetPart) <= nbMessageIn){
102
					for(size_t i(0lu); i < nbPart; ++i){
103
						std::vector<std::vector<char> > vecMessage;
104
						splitVecMessage(vecMessage, vecTmpFile, offsetPart + i*sizePart, sizePart);
105
						b &= data_save(phoenix_mockMakeOutputFile(baseOutputFile, i, outputExtension), vecMessage);
106
					}
107
				}else{
108
					std::cerr << "splitMock : cannot split nbMessageIn = " << nbMessageIn << ", into nbPart = "<<nbPart<<" of size sizePart = "<<sizePart<<" and offset offsetPart = " << offsetPart << std::endl;
109
					b = false;
110
				}
111
			}
112
		}else{
113
1
			b = false;
114
		}
115
	}
116
2
	return b;
117
}
118
119
2
int main(int argc, char** argv){
120
4
	OptionParser parser = createOptionParser();
121
2
	parser.parseArgument(argc, argv);
122
123
2
	const OptionMode & defaultMode = parser.getDefaultMode();
124
4
	std::vector<std::string> vecInputFile;
125
2
	std::string outputFile("");
126
2
	defaultMode.getValue(vecInputFile, "input");
127
2
	defaultMode.getValue(outputFile, "output");
128
129
2
	size_t offsetPart(0lu), sizePart(1lu), nbPart(0lu);
130
2
	defaultMode.getValue(offsetPart, "offset");
131
2
	defaultMode.getValue(sizePart, "sizepart");
132
2
	defaultMode.getValue(nbPart, "nbpart");
133
134
2
	return splitMock(vecInputFile, outputFile, offsetPart, sizePart, nbPart) - 1;
135
}
136
137