Newer
Older
/*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 "Config.hpp"
#include "util.hpp"
if (!colIndex2Name.empty())
util::myThrow("a mcd has already been read for this Config");
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);
}
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);
nbColumns = colIndex2Name.size();
void Config::readRawInput(std::string_view rawFilename)
{
std::FILE * file = std::fopen(rawFilename.data(), "r");
if (not file)
util::myThrow(fmt::format("Cannot open file '{}'", rawFilename));
while (not std::feof(file))
rawInput.push_back(std::fgetc(file));
std::fclose(file);
rawInputUtf8 = util::splitAsUtf8(rawInput);
}
void Config::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);
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));
addLine();
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);
}
Config::Config(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename)
{
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);
void Config::print(FILE * dest)
for (unsigned int line = 0; line < getNbLines(); line++)
for (int i = 0; i < nbColumns-1; i++)
fmt::print(dest, "{}{}", getLastNotEmpty(i, line).get(), i < nbColumns-2 ? "\t" : "\n");
if (getLastNotEmpty(EOSColName, line) == EOSSymbol1)
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
void Config::addLine()
{
lines.resize(lines.size() + nbColumns*(nbHypothesesMax+1));
}
boost::flyweight<std::string> & Config::get(const std::string & colName, int lineIndex, int hypothesisIndex)
{
return get(colName2Index[colName], lineIndex, hypothesisIndex);
}
boost::flyweight<std::string> & Config::get(int colIndex, int lineIndex, int hypothesisIndex)
{
return lines[lineIndex * nbColumns * (nbHypothesesMax+1) + colIndex * (nbHypothesesMax+1) + hypothesisIndex];
}
boost::flyweight<std::string> & Config::getLastNotEmpty(int colIndex, int lineIndex)
{
int baseIndex = lineIndex * nbColumns * (nbHypothesesMax+1) + colIndex * (nbHypothesesMax+1);
for (int i = nbHypothesesMax; i > 0; --i)
if (!lines[baseIndex+i].get().empty())
return lines[baseIndex+i];
return lines[baseIndex];
}
boost::flyweight<std::string> & Config::getLastNotEmpty(const std::string & colName, int lineIndex)
{
return getLastNotEmpty(colName2Index[colName], lineIndex);
}
std::size_t Config::getNbLines() const
return lines.size() / (nbColumns * (nbHypothesesMax+1));