w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RparseUrl.cpp
Go to the documentation of this file.
1// $Id: RparseUrl.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-07 1161 1.2 add DirName,FileName,FileStem,FileType
8// 2018-11-16 1070 1.1.1 use auto; use emplace,make_pair; use range loop
9// 2017-04-15 875 1.1 add Set() with default scheme handling
10// 2015-06-04 686 1.0.2 Set(): add check that optlist is enclosed by '|'
11// 2013-02-23 492 1.0.1 add static FindScheme(); allow no or empty scheme
12// 2013-02-03 481 1.0 Initial version, extracted from RlinkPort
13// ---------------------------------------------------------------------------
14
19#include <iostream>
20
21#include "RparseUrl.hpp"
22
23#include "RosFill.hpp"
24#include "RosPrintf.hpp"
25
26using namespace std;
27
33// all method definitions in namespace Retro
34namespace Retro {
35
36//------------------------------------------+-----------------------------------
38
40 : fUrl(),
41 fScheme(),
42 fPath(),
43 fOptMap()
44{}
45
46//------------------------------------------+-----------------------------------
48
50{}
51
52//------------------------------------------+-----------------------------------
54
55bool RparseUrl::Set(const std::string& url, const std::string& optlist,
56 RerrMsg& emsg)
57{
58 fUrl = url;
59 fScheme = FindScheme(url);
60 fPath.clear();
61 fOptMap.clear();
62
63 // check that optlist is empty or starts and ends with '|'
64 if (optlist.length() > 0 &&
65 (optlist.length()<2 ||
66 optlist[0]!='|' || optlist[optlist.length()-1]!='|') ) {
67 emsg.Init("RparseUrl::Set()", string("optlist \"") + optlist +
68 "\" not enclosed in '|'");
69 return false;
70 }
71
72 size_t pdel = fScheme.length();
73 if (pdel == 0 && url.length()>0 && url[0] != ':') pdel = -1;
74 size_t odel = url.find_first_of('?', fScheme.length());
75
76 if (odel == string::npos) { // no options
77 if (url.length() > pdel+1) fPath = url.substr(pdel+1);
78
79 } else { // options to process
80 fPath = url.substr(pdel+1,odel-(pdel+1));
81 string key;
82 string val;
83 bool hasval = false;
84
85 for (size_t i=odel+1; i<url.length(); i++) {
86 char c = url[i];
87 if (c == ';') {
88 if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
89 key.clear();
90 val.clear();
91 hasval = false;
92 } else {
93 if (!hasval) {
94 if (c == '=') {
95 hasval = true;
96 } else
97 key.push_back(c);
98 } else {
99 if (c == '\\') {
100 if (i+1 >= url.length()) {
101 emsg.Init("RparseUrl::Set()",
102 string("invalid trailing \\ in url '") + url + "'");
103 return false;
104 }
105 i += 1;
106 switch (url[i]) {
107 case '\\' : c = '\\'; break;
108 case ';' : c = ';'; break;
109 default : emsg.Init("RparseUrl::Set()",
110 string("invalid \\ escape in url '") +
111 url + "'");
112 return false;
113 }
114 }
115 val.push_back(c);
116 }
117 }
118 }
119 if (key.length() || hasval) {
120 if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
121 }
122 }
123
124 return true;
125}
126
127//------------------------------------------+-----------------------------------
129
130bool RparseUrl::Set(const std::string& url, const std::string& optlist,
131 const std::string& scheme, RerrMsg& emsg)
132{
133 if (FindScheme(url).length() == 0 && scheme.length() > 0) {
134 string url1 = scheme + string(":") + url;
135 return Set(url1, optlist, emsg);
136 }
137
138 return Set(url, optlist, emsg);
139}
140
141//------------------------------------------+-----------------------------------
143
145{
146 fUrl.clear();
147 fScheme.clear();
148 fPath.clear();
149 fOptMap.clear();
150 return;
151}
152
153//------------------------------------------+-----------------------------------
155
156std::string RparseUrl::DirName() const
157{
158 size_t ddel = fPath.find_last_of('/');
159 return (ddel == string::npos) ? "." : fPath.substr(0,ddel);
160}
161
162//------------------------------------------+-----------------------------------
164
165std::string RparseUrl::FileName() const
166{
167 size_t ddel = fPath.find_last_of('/');
168 return (ddel != string::npos && ddel+1 <= fPath.length()) ?
169 fPath.substr(ddel+1) : fPath;
170}
171
172//------------------------------------------+-----------------------------------
174
175std::string RparseUrl::FileStem() const
176{
177 string fname = FileName();
178 size_t ddel = fname.find_last_of('.');
179 return (ddel == string::npos) ? "" : fname.substr(0,ddel);
180}
181
182//------------------------------------------+-----------------------------------
184
185std::string RparseUrl::FileType() const
186{
187 string fname = FileName();
188 size_t ddel = fname.find_last_of('.');
189 return (ddel != string::npos && ddel+1 <= fname.length()) ?
190 fname.substr(ddel+1) : "";
191}
192
193//------------------------------------------+-----------------------------------
195
196bool RparseUrl::FindOpt(const std::string& name) const
197{
198 auto it = fOptMap.find(name);
199 if (it == fOptMap.end()) return false;
200 return true;
201}
202
203//------------------------------------------+-----------------------------------
205
206bool RparseUrl::FindOpt(const std::string& name, std::string& value) const
207{
208 auto it = fOptMap.find(name);
209 if (it == fOptMap.end()) return false;
210
211 value = it->second;
212
213 return true;
214}
215
216//------------------------------------------+-----------------------------------
218
219void RparseUrl::Dump(std::ostream& os, int ind, const char* text) const
220{
221 RosFill bl(ind);
222 os << bl << (text?text:"--") << "RparseUrl @ " << this << endl;
223
224 os << bl << " fUrl: " << fUrl << endl;
225 os << bl << " fScheme: " << fScheme << endl;
226 os << bl << " fPath: " << fPath << endl;
227 os << bl << " fOptMap: " << endl;
228 for (auto& o: fOptMap) {
229 os << bl << " " << RosPrintf(o.first.c_str(), "-s",8)
230 << " : " << o.second << endl;
231 }
232 return;
233}
234
235//------------------------------------------+-----------------------------------
237
238std::string RparseUrl::FindScheme(const std::string& url,
239 const std::string& def)
240{
241 size_t pdel = url.find_first_of(':');
242 if (pdel == string::npos) { // no : found
243 return def;
244 }
245
246 size_t odel = url.find_first_of('?');
247 if (odel != string::npos && odel < pdel) { // : after ?
248 return def;
249 }
250
251 return url.substr(0, pdel);
252}
253
254//------------------------------------------+-----------------------------------
256
257bool RparseUrl::AddOpt(const std::string& key, const std::string& val,
258 bool hasval, const std::string& optlist, RerrMsg& emsg)
259{
260 string lkey = "|";
261 lkey += key;
262 if (hasval) lkey += "=";
263 lkey += "|";
264 if (optlist.find(lkey) == string::npos) {
265 emsg.Init("RparseUrl::AddOpt()",
266 string("invalid field name '") + lkey + "'; allowed: '" +
267 optlist + "'");
268 return false;
269 }
270
271 fOptMap.emplace(make_pair(key, hasval ? val : "1"));
272 return true;
273}
274
275} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
static std::string FindScheme(const std::string &url, const std::string &def="")
FIXME_docs.
Definition: RparseUrl.cpp:238
omap_t fOptMap
option map
Definition: RparseUrl.hpp:68
bool FindOpt(const std::string &name) const
FIXME_docs.
Definition: RparseUrl.cpp:196
bool Set(const std::string &url, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:55
std::string FileName() const
FIXME_docs.
Definition: RparseUrl.cpp:165
bool AddOpt(const std::string &key, const std::string &val, bool hasval, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:257
std::string fPath
url path part
Definition: RparseUrl.hpp:67
RparseUrl()
Default constructor.
Definition: RparseUrl.cpp:39
std::string FileType() const
FIXME_docs.
Definition: RparseUrl.cpp:185
std::string fUrl
full url given with open
Definition: RparseUrl.hpp:65
std::string DirName() const
FIXME_docs.
Definition: RparseUrl.cpp:156
std::string FileStem() const
FIXME_docs.
Definition: RparseUrl.cpp:175
virtual ~RparseUrl()
Destructor.
Definition: RparseUrl.cpp:49
std::string fScheme
url scheme part
Definition: RparseUrl.hpp:66
virtual void Dump(std::ostream &os, int ind=0, const char *text=0) const
FIXME_docs.
Definition: RparseUrl.cpp:219
void Clear()
FIXME_docs.
Definition: RparseUrl.cpp:144
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47