GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/FileParser/src/openFileStream.cpp Lines: 44 44 100.0 %
Date: 2023-10-11 10:52:07 Branches: 42 42 100.0 %

Line Branch Exec Source
1
2
/***************************************
3
	Auteur : Pierre Aubert
4
	Mail : pierre.aubert@lapp.in2p3.fr
5
	Licence : CeCILL-C
6
****************************************/
7
8
#include <iostream>
9
#include "string_filename.h"
10
#include "convertToString.h"
11
#include "openFileStream.h"
12
13
14
///Open a ofstream and says if there is a problem
15
/**	@param fs : ofstream to open
16
 * 	@param fileName : name of the file in witch to write
17
 * 	@return true on success, false otherwise
18
*/
19
93
bool openFileStream(std::ofstream & fs, const std::string & fileName){
20
93
	fs.open(fileName.c_str());
21
93
	if(!fs.is_open()){
22
2
		std::cerr << "openFileStream : can't open file '" << fileName << "'" << std::endl;
23
2
		return false;
24
91
	}else return true;
25
}
26
27
///Open a ofstream and says if there is a problem
28
/**	@param fs : ifstream to open
29
 * 	@param fileName : name of the file in witch to write
30
 * 	@return true on success, false otherwise
31
*/
32
93
bool openFileStream(std::ifstream & fs, const std::string & fileName){
33
93
	fs.open(fileName.c_str());
34
93
	if(!fs.is_open()){
35
2
		std::cerr << "openFileStream : can't open file '" << fileName << "'" << std::endl;
36
2
		return false;
37
91
	}else return true;
38
}
39
40
///Open a vector of ofstream
41
/**	@param[out] fs : vector of ofstream to be opened
42
 * 	@param fileName : basic name of the file to be used (the file name will have a file index)
43
 * 	@param nbFile : number of file to open
44
 * 	@return true on success, false otherwise
45
*/
46
20
bool openFileStream(PVecOFStream & fs, const std::string & fileName, size_t nbFile){
47
20
	if(nbFile == 0lu){return true;}		//There is no problem in oppening 0 file
48
36
	std::string baseFileName(eraseExtension(fileName)), extention(getExtention(fileName));
49

18
	if(extention != ""){extention = "." + extention;}
50
18
	fs.resize(nbFile);
51
18
	bool b(true);
52
18
	size_t i(0lu);
53
108
	for(PVecOFStream::iterator it(fs.begin()); it != fs.end(); ++it){
54

180
		std::string tmpFileName(baseFileName + "_" + convertToString(i) + extention);
55
90
		b &= openFileStream(*it, tmpFileName);
56
90
		++i;
57
	}
58
18
	return b;
59
}
60
61
///Open a vector of ifstream
62
/**	@param[out] fs : vector of ifstream to be opened
63
 * 	@param fileName : basic name of the file to be used (the file name will have a file index)
64
 * 	@param nbFile : number of file to open
65
 * 	@return true on success, false otherwise
66
*/
67
20
bool openFileStream(PVecIFStream & fs, const std::string & fileName, size_t nbFile){
68
20
	if(nbFile == 0lu){return true;}		//There is no problem in oppening 0 file
69
36
	std::string baseFileName(eraseExtension(fileName)), extention(getExtention(fileName));
70

18
	if(extention != ""){extention = "." + extention;}
71
18
	fs.resize(nbFile);
72
18
	bool b(true);
73
18
	size_t i(0lu);
74
108
	for(PVecIFStream::iterator it(fs.begin()); it != fs.end(); ++it){
75

180
		std::string tmpFileName(baseFileName + "_" + convertToString(i) + extention);
76
90
		b &= openFileStream(*it, tmpFileName);
77
90
		++i;
78
	}
79
18
	return b;
80
}
81
82
///Close a vector of ofstream
83
/**	@param[out] fs : vector of ofstream to be closed
84
*/
85
20
void closeFileStream(PVecOFStream & fs){
86
110
	for(PVecOFStream::iterator it(fs.begin()); it != fs.end(); ++it){
87
90
		it->close();
88
	}
89
20
}
90
91
///Close a vector of ifstream
92
/**	@param[out] fs : vector of ifstream to be closed
93
*/
94
20
void closeFileStream(PVecIFStream & fs){
95
110
	for(PVecIFStream::iterator it(fs.begin()); it != fs.end(); ++it){
96
90
		it->close();
97
	}
98
20
}
99