blob: b8142ac6b66dea3bfabe4e752b05970b7bdf2f6d (
plain) (
blame)
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
44
45
46
47
48
49
50
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "fileconfigreader.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/config/common/misc.h>
#include <vespa/config/common/configvalue.h>
#include <vespa/vespalib/util/exceptions.h>
#include <fstream>
#include <sstream>
namespace config {
template <typename ConfigType>
FileConfigReader<ConfigType>::FileConfigReader(const vespalib::string & fileName)
: _fileName(fileName)
{
}
template <typename ConfigType>
std::unique_ptr<ConfigType>
FileConfigReader<ConfigType>::read(const ConfigFormatter & formatter)
{
ConfigDataBuffer buffer;
std::ifstream file(_fileName.c_str());
if (!file.is_open())
throw ConfigReadException("error: unable to read file '%s'", _fileName.c_str());
std::stringstream buf;
buf << file.rdbuf();
buffer.setEncodedString(buf.str());
formatter.decode(buffer);
return std::make_unique<ConfigType>(buffer);
}
template <typename ConfigType>
std::unique_ptr<ConfigType>
FileConfigReader<ConfigType>::read()
{
StringVector lines;
std::ifstream f(_fileName.c_str());
if (f.fail())
throw vespalib::IllegalArgumentException(std::string("Unable to open file ") + _fileName);
std::string line;
for (std::getline(f, line); f; std::getline(f, line)) {
lines.push_back(line);
}
return std::make_unique<ConfigType>(ConfigValue(std::move(lines)));
}
} // namespace config
|