Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*Copyright (c) 2019 Alexis Nasr && Franck Dary
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
#include "BaseConfig.hpp"
#include "util.hpp"
void BaseConfig::readMCD(std::string_view mcdFilename)
{
if (!colIndex2Name.empty())
util::myThrow("a mcd has already been read for this BaseConfig");
std::FILE * file = std::fopen(mcdFilename.data(), "r");
if (not file)
util::myThrow(fmt::format("Cannot open file '{}'", mcdFilename));
char lineBuffer[1024];
while (std::fscanf(file, "%1023[^\n]\n", lineBuffer) == 1)
{
colIndex2Name.emplace_back(lineBuffer);
colName2Index.emplace(lineBuffer, colIndex2Name.size()-1);
}
std::fclose(file);
if (colName2Index.count(EOSColName))
util::myThrow(fmt::format("mcd '{}' must not contain column '{}'", mcdFilename, EOSColName));
colIndex2Name.emplace_back(EOSColName);
colName2Index.emplace(EOSColName, colIndex2Name.size()-1);
}
void BaseConfig::readRawInput(std::string_view rawFilename)
{
std::FILE * file = std::fopen(rawFilename.data(), "r");
if (not file)
util::myThrow(fmt::format("Cannot open file '{}'", rawFilename));
std::string rawInputTemp;
while (not std::feof(file))
rawInputTemp.push_back(std::fgetc(file));
std::fclose(file);
rawInputUtf8 = util::splitAsUtf8(rawInputTemp);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}
void BaseConfig::readTSVInput(std::string_view tsvFilename)
{
std::FILE * file = std::fopen(tsvFilename.data(), "r");
if (not file)
util::myThrow(fmt::format("Cannot open file '{}'", tsvFilename));
char lineBuffer[100000];
int inputLineIndex = 0;
bool inputHasBeenRead = false;
int usualNbCol = -1;
while (!std::feof(file))
{
if (lineBuffer != std::fgets(lineBuffer, 100000, file))
break;
std::string_view line(lineBuffer);
inputLineIndex++;
if (line.size() < 3)
{
if (!inputHasBeenRead)
continue;
get(EOSColName, getNbLines()-1, 0) = EOSSymbol1;
continue;
}
else if (line[0] == '#')
continue;
if (line.back() == '\n')
line.remove_suffix(1);
inputHasBeenRead = true;
auto splited = util::split(line, '\t');
if (usualNbCol == -1)
usualNbCol = splited.size();
if ((int)splited.size() != usualNbCol)
util::myThrow(fmt::format("in file {} line {} is invalid, it shoud have {} columns", tsvFilename, line, usualNbCol));
addLines(1);
get(EOSColName, getNbLines()-1, 0) = EOSSymbol0;
for (unsigned int i = 0; i < splited.size(); i++)
if (i < colIndex2Name.size())
get(i, getNbLines()-1, 0) = std::string(splited[i]);
}
std::fclose(file);
}
BaseConfig::BaseConfig(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename) : Config(rawInputUtf8)
{
if (tsvFilename.empty() and rawFilename.empty())
util::myThrow("tsvFilename and rawFilenames can't be both empty");
if (mcdFilename.empty())
util::myThrow("mcdFilename can't be empty");
readMCD(mcdFilename);
if (not rawFilename.empty())
readRawInput(rawFilename);
if (not tsvFilename.empty())
readTSVInput(tsvFilename);
}
std::size_t BaseConfig::getNbColumns() const
{
return colIndex2Name.size();
}
std::size_t BaseConfig::getColIndex(const std::string & colName) const
{
return colName2Index.at(colName);
}
bool BaseConfig::hasColIndex(const std::string & colName) const
{
return colName2Index.count(colName);
}
const std::string & BaseConfig::getColName(int colIndex) const
{
return colIndex2Name[colIndex];
}
std::size_t BaseConfig::getFirstLineIndex() const
{
return 0;
}