OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtools/] [RparseUrl.cpp] - Blame information for rev 33

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 31 wfjm
// $Id: RparseUrl.cpp 686 2015-06-04 21:08:08Z mueller $
2 19 wfjm
//
3 31 wfjm
// Copyright 2013-2015 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 19 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 31 wfjm
// 2015-06-04   686   1.0.2  Set(): add check that optlist is enclosed by '|'
17 19 wfjm
// 2013-02-23   492   1.0.1  add static FindScheme(); allow no or empty scheme
18
// 2013-02-03   481   1.0    Initial version, extracted from RlinkPort
19
// ---------------------------------------------------------------------------
20
 
21
/*!
22
  \file
23 31 wfjm
  \version $Id: RparseUrl.cpp 686 2015-06-04 21:08:08Z mueller $
24 19 wfjm
  \brief   Implemenation of RparseUrl.
25
*/
26
 
27
#include <iostream>
28
 
29
#include "RparseUrl.hpp"
30
 
31
#include "RosFill.hpp"
32
#include "RosPrintf.hpp"
33
 
34
using namespace std;
35
 
36
/*!
37
  \class Retro::RparseUrl
38
  \brief FIXME_docs
39
*/
40
 
41
// all method definitions in namespace Retro
42
namespace Retro {
43
 
44
//------------------------------------------+-----------------------------------
45
//! Default constructor
46
 
47
RparseUrl::RparseUrl()
48
  : fUrl(),
49
    fScheme(),
50
    fPath(),
51
    fOptMap()
52
{}
53
 
54
//------------------------------------------+-----------------------------------
55
//! Destructor
56
 
57
RparseUrl::~RparseUrl()
58
{}
59
 
60
//------------------------------------------+-----------------------------------
61
//! FIXME_docs
62
 
63
bool RparseUrl::Set(const std::string& url, const std::string& optlist,
64
                    RerrMsg& emsg)
65
{
66
  fUrl    = url;
67
  fScheme = FindScheme(url);
68
  fPath.clear();
69
  fOptMap.clear();
70
 
71 31 wfjm
  // check that optlist is empty or starts and ends with '|'
72
  if (optlist.length() > 0 &&
73
        (optlist.length()<2 ||
74
         optlist[0]!='|' || optlist[optlist.length()-1]!='|') ) {
75
    emsg.Init("RparseUrl::Set()", string("optlist \"") + optlist +
76
                                         "\" not enclosed in '|'");
77
    return false;
78
  }
79
 
80 19 wfjm
  size_t pdel = fScheme.length();
81
  if (pdel == 0 && url.length()>0 && url[0] != ':') pdel = -1;
82
  size_t odel = url.find_first_of('?', fScheme.length());
83
 
84
  if (odel == string::npos) {               // no options
85
    if (url.length() > pdel+1) fPath = url.substr(pdel+1);
86
 
87
  } else {                                  // options to process
88
    fPath = url.substr(pdel+1,odel-(pdel+1));
89
    string key;
90
    string val;
91
    bool   hasval = false;
92
 
93
    for (size_t i=odel+1; i<url.length(); i++) {
94
      char c = url[i];
95
      if (c == ';') {
96
        if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
97
        key.clear();
98
        val.clear();
99
        hasval = false;
100
      } else {
101
        if (!hasval) {
102
          if (c == '=') {
103
            hasval = true;
104
          } else
105
            key.push_back(c);
106
        } else {
107
          if (c == '\\') {
108
            if (i+1 >= url.length()) {
109 31 wfjm
              emsg.Init("RparseUrl::Set()",
110 21 wfjm
                        string("invalid trailing \\ in url '") + url + "'");
111 19 wfjm
              return false;
112
            }
113
            i += 1;
114
            switch (url[i]) {
115
              case '\\' : c = '\\'; break;
116
              case ';'  : c = ';';  break;
117 31 wfjm
              default   : emsg.Init("RparseUrl::Set()",
118 19 wfjm
                                    string("invalid \\ escape in url '") +
119 21 wfjm
                                    url + "'");
120 19 wfjm
                          return false;
121
            }
122
          }
123
          val.push_back(c);
124
        }
125
      }
126
    }
127
    if (key.length() || hasval) {
128
      if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
129
    }
130
  }
131
 
132
  return true;
133
}
134
 
135
//------------------------------------------+-----------------------------------
136
//! FIXME_docs
137
 
138
void RparseUrl::Clear()
139
{
140
  fUrl.clear();
141
  fScheme.clear();
142
  fPath.clear();
143
  fOptMap.clear();
144
  return;
145
}
146
 
147
//------------------------------------------+-----------------------------------
148
//! FIXME_docs
149
 
150
bool RparseUrl::FindOpt(const std::string& name) const
151
{
152
  omap_cit_t it = fOptMap.find(name);
153
  if (it == fOptMap.end()) return false;
154
  return true;
155
}
156
 
157
//------------------------------------------+-----------------------------------
158
//! FIXME_docs
159
 
160
bool RparseUrl::FindOpt(const std::string& name, std::string& value) const
161
{
162
  omap_cit_t it = fOptMap.find(name);
163
  if (it == fOptMap.end()) return false;
164
 
165
  value = it->second;
166
 
167
  return true;
168
}
169
 
170
//------------------------------------------+-----------------------------------
171
//! FIXME_docs
172
 
173
void RparseUrl::Dump(std::ostream& os, int ind, const char* text) const
174
{
175
  RosFill bl(ind);
176
  os << bl << (text?text:"--") << "RparseUrl @ " << this << endl;
177
 
178
  os << bl << "  fUrl:            " << fUrl << endl;
179
  os << bl << "  fScheme:         " << fScheme << endl;
180
  os << bl << "  fPath:           " << fPath << endl;
181
  os << bl << "  fOptMap:         " << endl;
182
  for (omap_cit_t it=fOptMap.begin(); it!=fOptMap.end(); it++) {
183
    os << bl << "    " << RosPrintf((it->first).c_str(), "-s",8)
184
       << " : " << it->second << endl;
185
  }
186
  return;
187
}
188
 
189
//------------------------------------------+-----------------------------------
190
//! FIXME_docs
191
 
192
std::string RparseUrl::FindScheme(const std::string& url,
193
                                  const std::string& def)
194
{
195
  size_t pdel = url.find_first_of(':');
196
  if (pdel == string::npos) {               // no : found
197
    return def;
198
  }
199
 
200
  size_t odel = url.find_first_of('?');
201
  if (odel != string::npos && odel < pdel) { // : after ?
202
    return def;
203
  }
204
 
205
  return url.substr(0, pdel);
206
}
207
 
208
//------------------------------------------+-----------------------------------
209
//! FIXME_docs
210
 
211
bool RparseUrl::AddOpt(const std::string& key, const std::string& val,
212
                       bool hasval, const std::string& optlist, RerrMsg& emsg)
213
{
214
  string lkey = "|";
215
  lkey += key;
216
  if (hasval) lkey += "=";
217
  lkey += "|";
218
  if (optlist.find(lkey) == string::npos) {
219
    emsg.Init("RparseUrl::AddOpt()",
220 21 wfjm
              string("invalid field name '") + lkey + "'; allowed: '" +
221
              optlist + "'");
222 19 wfjm
    return false;
223
  }
224
 
225
  fOptMap.insert(omap_val_t(key, hasval ? val : "1"));
226
  return true;
227
}
228
 
229
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.