Debian Codegraph @

src/ApvlvInfo.cc

raw
1 /*
2 * This file is part of the apvlv package
3 * Copyright (C) <2008> <Alf>
4 *
5 * Contact: Alf <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22 /* @CPPFILE ApvlvInfo.cc
23 *
24 * Author: Alf <[email protected]>
25 */
26
27 #include <algorithm>
28 #include <cstdlib>
29 #include <cstring>
30 #include <fstream>
31 #include <ranges>
32 #include <sstream>
33
34 #include "ApvlvInfo.h"
35 #include "ApvlvParams.h"
36
37 namespace apvlv
38 {
39 using namespace std;
40
41 void
42 ApvlvInfo::loadFile (std::string_view file)
43 {
44 mFileName = file;
45
46 ifstream is (mFileName, ios::in);
47 if (is.is_open ())
48 {
49 string line;
50
51 while (getline (is, line))
52 {
53 auto p = line.c_str ();
54
55 if (*p != '\'' /* the ' */
56 || !isdigit (*(p + 1))) /* the digit */
57 {
58 continue;
59 }
60
61 addPosition (p);
62 }
63
64 is.close ();
65 }
66 }
67
68 bool
69 ApvlvInfo::update ()
70 {
71 ofstream os (mFileName, ios::out);
72 if (!os.is_open ())
73 {
74 return false;
75 }
76
77 int i = 0;
78 for (const auto &infofile : mInfoFiles)
79 {
80 os << "'" << i++ << "\t";
81 os << infofile.page << ':' << infofile.skip << "\t";
82 os << infofile.rate << "\t";
83 os << infofile.file << endl;
84 }
85
86 os.close ();
87 return true;
88 }
89
90 std::optional<InfoFile *>
91 ApvlvInfo::lastFile ()
92 {
93 if (mInfoFiles.empty ())
94 return nullopt;
95 else
96 return &*(mInfoFiles.rbegin ());
97 }
98
99 optional<InfoFile *>
100 ApvlvInfo::file (const string &filename)
101 {
102 auto itr = std::ranges::find_if (
103 std::views::reverse (mInfoFiles),
104 [filename] (auto const &infofile) { return infofile.file == filename; });
105 if (itr != mInfoFiles.rend ())
106 {
107 return &(*itr);
108 }
109
110 return nullopt;
111 }
112
113 bool
114 ApvlvInfo::updateFile (int page, int skip, double rate, const string &filename)
115 {
116 InfoFile infofile{ page, skip, rate, filename };
117 auto optinfofile = file (filename);
118 if (optinfofile)
119 {
120 *optinfofile.value () = infofile;
121 }
122 else
123 {
124 mInfoFiles.push_back (infofile);
125 if (mInfoFiles.size () > mMaxInfo)
126 mInfoFiles.pop_front ();
127 }
128
129 return update ();
130 }
131
132 ApvlvInfo::ApvlvInfo ()
133 {
134 mMaxInfo = ApvlvParams::instance ()->getIntOrDefault ("max_info",
135 DEFAULT_MAX_INFO);
136 }
137
138 bool
139 ApvlvInfo::addPosition (const char *str)
140 {
141 const char *p;
142 const char *s;
143
144 p = strchr (str + 2, '\t'); /* Skip the ' and the digit */
145 if (p == nullptr)
146 {
147 return false;
148 }
149
150 while (*p != '\0' && !isdigit (*p))
151 {
152 p++;
153 }
154 int page = int (strtol (p, nullptr, 10));
155 int skip;
156
157 s = strchr (p, ':');
158 for (; s && p < s; ++p)
159 {
160 if (!isdigit (*p))
161 {
162 break;
163 }
164 }
165 if (p == s)
166 {
167 ++p;
168 skip = int (strtol (p, nullptr, 10));
169 }
170 else
171 {
172 skip = 0;
173 }
174
175 p = strchr (p, '\t');
176 if (p == nullptr)
177 {
178 return false;
179 }
180
181 while (*p != '\0' && !isdigit (*p))
182 {
183 p++;
184 }
185 double rate = strtod (p, nullptr);
186
187 p = strchr (p, '\t');
188 if (p == nullptr)
189 {
190 return false;
191 }
192
193 while (*p != '\0' && isspace (*p))
194 {
195 p++;
196 }
197 if (*p == '\0')
198 {
199 return false;
200 }
201
202 auto fp = InfoFile{ page, skip, rate, p };
203 mInfoFiles.emplace_back (fp);
204 return true;
205 }
206 };
207
208 // Local Variables:
209 // mode: c++
210 // End: