-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdf.cc
99 lines (83 loc) · 2.2 KB
/
hdf.cc
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
51
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
// Copyright (C) 2011 Petr Machata
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
#include <wordexp.h>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <util/neo_err.h>
#include "hdf.hh"
#include "main.hh"
hdf::hdf ()
{
handle_neoerr (hdf_init (&_m_hdf));
}
hdf::~hdf ()
{
hdf_destroy (&_m_hdf);
}
hdf::operator HDF* ()
{
return _m_hdf;
}
HDF *
hdf::node (char const *name)
{
HDF *node;
handle_neoerr (hdf_get_node (*this, name, &node));
return node;
}
namespace
{
bool
get_home (boost::filesystem::path &ret)
{
wordexp_t we;
int err = wordexp ("~", &we, WRDE_NOCMD);
if (err == 0)
{
assert (we.we_wordc > 0);
ret = we.we_wordv[0];
}
wordfree (&we);
if (err != 0)
std::cerr << "Unknown user home directory." << std::endl;
return err == 0;
}
NEOERR *
try_read_config (hdf &self, boost::filesystem::path dir)
{
dir /= "lemmatizer.hdf";
NEOERR *ret = hdf_read_file (self, dir.string ().c_str ());
if (ret == NULL)
std::cerr << boost::format ("Config loaded from %s\n") % dir;
return ret;
}
}
default_hdf::default_hdf ()
{
// Look for config file in current directory, then in user's home
// directory and then in system-wide /etc/ directory.
if (try_read_config (*this, boost::filesystem::current_path ()) == NULL)
return;
boost::filesystem::path pth;
NEOERR *err;
if (get_home (pth)
&& (err = try_read_config (*this, pth / ".lemmatizer")) == NULL)
return;
if ((err = try_read_config (*this, "/etc/")) == NULL)
return;
handle_neoerr (err);
}