| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvParams.cc |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <fstream> |
| 30 |
#include <iostream> |
| 31 |
#include <sstream> |
| 32 |
|
| 33 |
#include "ApvlvCmds.h" |
| 34 |
#include "ApvlvParams.h" |
| 35 |
#include "ApvlvUtil.h" |
| 36 |
|
| 37 |
namespace apvlv |
| 38 |
{ |
| 39 |
using namespace std; |
| 40 |
|
| 41 |
ApvlvParams::ApvlvParams () |
| 42 |
{ |
| 43 |
push ("inverted", "no"); |
| 44 |
push ("fullscreen", "no"); |
| 45 |
push ("zoom", "fitwidth"); |
| 46 |
push ("continuous", "yes"); |
| 47 |
push ("autoscrollpage", "yes"); |
| 48 |
push ("autoscrolldoc", "yes"); |
| 49 |
push ("noinfo", "no"); |
| 50 |
push ("width", "800"); |
| 51 |
push ("height", "600"); |
| 52 |
push ("fix_width", "0"); |
| 53 |
push ("fix_height", "0"); |
| 54 |
push ("background", ""); |
| 55 |
push ("warpscan", "yes"); |
| 56 |
push ("commandtimeout", "1000"); |
| 57 |
#ifdef WIN32 |
| 58 |
push ("defaultdir", "C:\\"); |
| 59 |
#else |
| 60 |
push ("defaultdir", "/tmp"); |
| 61 |
#endif |
| 62 |
push ("guioptions", "mTsS"); |
| 63 |
push ("autoreload", "3"); |
| 64 |
push ("thread_count", "auto"); |
| 65 |
push ("lok_path", "/usr/lib64/libreoffice/program"); |
| 66 |
|
| 67 |
push (".pdf:engine", "MuPDF"); |
| 68 |
push (".epub:engine", "Web"); |
| 69 |
push (".fb2:engine", "Web"); |
| 70 |
push (".txt:engine", "MuPDF"); |
| 71 |
|
| 72 |
push ("ocr:lang", "eng+chi_sim"); |
| 73 |
} |
| 74 |
|
| 75 |
ApvlvParams::~ApvlvParams () = default; |
| 76 |
|
| 77 |
bool |
| 78 |
ApvlvParams::loadFile (const std::string &filename) |
| 79 |
{ |
| 80 |
string str; |
| 81 |
fstream os (filename, ios::in); |
| 82 |
|
| 83 |
if (!os.is_open ()) |
| 84 |
{ |
| 85 |
qWarning () << "Open configure file " << filename << " error"; |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
while ((getline (os, str))) |
| 90 |
{ |
| 91 |
string argu; |
| 92 |
string data; |
| 93 |
string crap; |
| 94 |
stringstream is (str); |
| 95 |
|
| 96 |
is >> crap; |
| 97 |
if (crap[0] == '\"' || crap.empty ()) |
| 98 |
{ |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if (crap == "set") |
| 103 |
{ |
| 104 |
is >> argu; |
| 105 |
size_t off = argu.find ('='); |
| 106 |
if (off == string::npos) |
| 107 |
{ |
| 108 |
is >> crap >> data; |
| 109 |
if (crap == "=") |
| 110 |
{ |
| 111 |
push (argu, data); |
| 112 |
continue; |
| 113 |
} |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
argu[off] = ' '; |
| 118 |
stringstream ass{ argu }; |
| 119 |
ass >> argu >> data; |
| 120 |
push (argu, data); |
| 121 |
continue; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// like "map n next-page" |
| 126 |
else if (crap == "map") |
| 127 |
{ |
| 128 |
is >> argu; |
| 129 |
|
| 130 |
if (argu.empty ()) |
| 131 |
{ |
| 132 |
qWarning () << "map command not complete"; |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
getline (is, data); |
| 137 |
|
| 138 |
while (!data.empty () && isspace (data[0])) |
| 139 |
data.erase (0, 1); |
| 140 |
|
| 141 |
if (!argu.empty () && !data.empty ()) |
| 142 |
{ |
| 143 |
ApvlvCmds::buildCommandMap (argu, data); |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
qWarning () << "Syntax error: map: " << str; |
| 148 |
} |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
qWarning () << "Unknown rc command: " << crap << ": " << str; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
bool |
| 160 |
ApvlvParams::push (string_view ch, string_view str) |
| 161 |
{ |
| 162 |
mParamMap[string (ch)] = str; |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
string |
| 167 |
ApvlvParams::getGroupStringOrDefault (std::string_view entry, |
| 168 |
std::string_view key, |
| 169 |
const std::string &defs) |
| 170 |
{ |
| 171 |
auto itr = std::ranges::find_if ( |
| 172 |
mParamMap, [entry, key] (const pair<string, string> &p) -> bool { |
| 173 |
if (p.first.find (':') == string::npos) |
| 174 |
return false; |
| 175 |
else |
| 176 |
{ |
| 177 |
auto pos = p.first.find (':'); |
| 178 |
auto pentry = p.first.substr (0, pos); |
| 179 |
auto pkey = p.first.substr (pos + 1); |
| 180 |
return pentry == entry && pkey == key; |
| 181 |
} |
| 182 |
}); |
| 183 |
if (itr != mParamMap.cend ()) |
| 184 |
{ |
| 185 |
return itr->second; |
| 186 |
} |
| 187 |
return defs; |
| 188 |
} |
| 189 |
|
| 190 |
string |
| 191 |
ApvlvParams::getStringOrDefault (string_view key, const string &defs) |
| 192 |
{ |
| 193 |
auto itr = std::ranges::find_if ( |
| 194 |
mParamMap, [key] (const pair<string, string> &p) -> bool { |
| 195 |
return p.first == key; |
| 196 |
}); |
| 197 |
if (itr != mParamMap.cend ()) |
| 198 |
{ |
| 199 |
return itr->second; |
| 200 |
} |
| 201 |
return defs; |
| 202 |
} |
| 203 |
|
| 204 |
int |
| 205 |
ApvlvParams::getIntOrDefault (string_view key, int defi) |
| 206 |
{ |
| 207 |
auto values = getStringOrDefault (key, ""); |
| 208 |
if (values.empty ()) |
| 209 |
return defi; |
| 210 |
|
| 211 |
return int (strtol (values.c_str (), nullptr, 10)); |
| 212 |
} |
| 213 |
|
| 214 |
bool |
| 215 |
ApvlvParams::getBoolOrDefault (string_view key, bool defb) |
| 216 |
{ |
| 217 |
auto values = getStringOrDefault (key, ""); |
| 218 |
if (values.empty ()) |
| 219 |
return defb; |
| 220 |
|
| 221 |
if (values == "true" || values == "yes" || values == "on" || values == "1") |
| 222 |
{ |
| 223 |
return true; |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// Local Variables: |
| 233 |
// mode: c++ |
| 234 |
// End: |