-
Notifications
You must be signed in to change notification settings - Fork 1
/
plan.cpp
144 lines (128 loc) · 3.75 KB
/
plan.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
** Copyright (C) 2022 Johannes Lorenz <mail_umleitung@web.de>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <fstream>
#include <limits>
#include <regex>
#include <iostream> // TODO
#include <sys/stat.h>
#include <unistd.h>
#include "plan.h"
plan_t::plan_t()
{
}
void plan_t::read_from(const std::string &fname)
{
{
std::string line;
std::size_t cur_caption;
std::ifstream cfg_file(fname);
if(!cfg_file.good())
{
throw std::runtime_error("Cannot open file: " + fname);
}
const char* x = "";
std::regex
re_blankline (R"XXX(^\s*$)XXX",
std::regex::optimize),
re_break (R"XXX(^\s*\\(n)?break\s*([0-9]+(\.[0-9]+)?)\s*$)XXX",
std::regex::optimize),
re_follow (R"XXX(^\s*\\follow\s*([0-9]+)\s*$)XXX",
std::regex::optimize),
re_caption (R"XXX(^\s*#\s*(\S+)\s*$)XXX",
std::regex::optimize),
re_words (R"XXX(^(\s*(([^:]+):)?([0-9]+)|\s*\\br\s*([0-9]+(\.[0-9]+)?))+\s*$)XXX",
std::regex::optimize),
re_word (R"XXX(^\s*((([^:]+):)?([0-9]+)|\s*\\br\s*([0-9]+(\.[0-9]+)?)))XXX",
std::regex::optimize);
std::string last_prefix;
for(std::size_t line_no = 1; std::getline(cfg_file, line); ++line_no)
{
std::smatch match;
if(std::regex_match(line, match, re_blankline))
{
}
else if(std::regex_match(line, match, re_break))
{
if(match.length(1))
def_nbreak = std::stof(match.str(2));
else
def_break = std::stof(match.str(2));
}
else if(std::regex_match(line, match, re_follow))
{
def_follow = std::stof(match.str(1));
}
else if(std::regex_match(line, match, re_caption))
{
last_prefix = match.str(1);
if(last_prefix[last_prefix.length()-1] != '/')
{
struct stat st;
if (lstat(last_prefix.c_str(), &st) == 0 && S_ISDIR(st.st_mode))
{
last_prefix += '/';
}
}
}
else if(std::regex_match(line, match, re_words))
{
std::string::const_iterator next( line.cbegin() );
while ( regex_search( next, line.cend(), match, re_word) )
{
if(match.length(4))
{
std::string path = last_prefix + match.str(3);
struct stat st;
if (lstat(path.c_str(), &st) == 0 &&
S_ISREG(st.st_mode) &&
0 == access(path.c_str(), R_OK))
{
std::size_t cur_file;
auto itr = file_dict.find(path);
if(itr == file_dict.end())
{
files.push_back(path);
cur_file = files.size() - 1;
file_dict.emplace(path, cur_file);
}
else
cur_file = itr->second;
words.emplace_back(cur_file, match.str(4));
}
else
throw std::runtime_error("File not found: " + path);
}
else
words.emplace_back(std::numeric_limits<decltype(cur_caption)>::max(),
match.str(5));
next = match.suffix().first;
}
}
else
throw std::runtime_error("Invalid line " + std::to_string(line_no) + ": " + line);
}
}
for(const auto& pr : words)
{
std::string file = (pr.first == std::numeric_limits<std::size_t>::max())
? "(break)"
: files.at(pr.first);
const std::string& word = pr.second;
std::cout << file << ", " << word << std::endl;
}
}