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 21

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 wfjm
// $Id: RparseUrl.cpp 516 2013-05-05 21:24:52Z mueller $
2 19 wfjm
//
3
// Copyright 2013- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4
//
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
// 2013-02-23   492   1.0.1  add static FindScheme(); allow no or empty scheme
17
// 2013-02-03   481   1.0    Initial version, extracted from RlinkPort
18
// ---------------------------------------------------------------------------
19
 
20
/*!
21
  \file
22 21 wfjm
  \version $Id: RparseUrl.cpp 516 2013-05-05 21:24:52Z mueller $
23 19 wfjm
  \brief   Implemenation of RparseUrl.
24
*/
25
 
26
#include <iostream>
27
 
28
#include "RparseUrl.hpp"
29
 
30
#include "RosFill.hpp"
31
#include "RosPrintf.hpp"
32
 
33
using namespace std;
34
 
35
/*!
36
  \class Retro::RparseUrl
37
  \brief FIXME_docs
38
*/
39
 
40
// all method definitions in namespace Retro
41
namespace Retro {
42
 
43
//------------------------------------------+-----------------------------------
44
//! Default constructor
45
 
46
RparseUrl::RparseUrl()
47
  : fUrl(),
48
    fScheme(),
49
    fPath(),
50
    fOptMap()
51
{}
52
 
53
//------------------------------------------+-----------------------------------
54
//! Destructor
55
 
56
RparseUrl::~RparseUrl()
57
{}
58
 
59
//------------------------------------------+-----------------------------------
60
//! FIXME_docs
61
 
62
bool RparseUrl::Set(const std::string& url, const std::string& optlist,
63
                    RerrMsg& emsg)
64
{
65
  fUrl    = url;
66
  fScheme = FindScheme(url);
67
  fPath.clear();
68
  fOptMap.clear();
69
 
70
  size_t pdel = fScheme.length();
71
  if (pdel == 0 && url.length()>0 && url[0] != ':') pdel = -1;
72
  size_t odel = url.find_first_of('?', fScheme.length());
73
 
74
  if (odel == string::npos) {               // no options
75
    if (url.length() > pdel+1) fPath = url.substr(pdel+1);
76
 
77
  } else {                                  // options to process
78
    fPath = url.substr(pdel+1,odel-(pdel+1));
79
    string key;
80
    string val;
81
    bool   hasval = false;
82
 
83
    for (size_t i=odel+1; i<url.length(); i++) {
84
      char c = url[i];
85
      if (c == ';') {
86
        if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
87
        key.clear();
88
        val.clear();
89
        hasval = false;
90
      } else {
91
        if (!hasval) {
92
          if (c == '=') {
93
            hasval = true;
94
          } else
95
            key.push_back(c);
96
        } else {
97
          if (c == '\\') {
98
            if (i+1 >= url.length()) {
99
              emsg.Init("RparseUrl::ParseUrl()",
100 21 wfjm
                        string("invalid trailing \\ in url '") + url + "'");
101 19 wfjm
              return false;
102
            }
103
            i += 1;
104
            switch (url[i]) {
105
              case '\\' : c = '\\'; break;
106
              case ';'  : c = ';';  break;
107
              default   : emsg.Init("RparseUrl::ParseUrl()",
108
                                    string("invalid \\ escape in url '") +
109 21 wfjm
                                    url + "'");
110 19 wfjm
                          return false;
111
            }
112
          }
113
          val.push_back(c);
114
        }
115
      }
116
    }
117
    if (key.length() || hasval) {
118
      if (!AddOpt(key, val, hasval, optlist, emsg)) return false;
119
    }
120
  }
121
 
122
  return true;
123
}
124
 
125
//------------------------------------------+-----------------------------------
126
//! FIXME_docs
127
 
128
void RparseUrl::Clear()
129
{
130
  fUrl.clear();
131
  fScheme.clear();
132
  fPath.clear();
133
  fOptMap.clear();
134
  return;
135
}
136
 
137
//------------------------------------------+-----------------------------------
138
//! FIXME_docs
139
 
140
bool RparseUrl::FindOpt(const std::string& name) const
141
{
142
  omap_cit_t it = fOptMap.find(name);
143
  if (it == fOptMap.end()) return false;
144
  return true;
145
}
146
 
147
//------------------------------------------+-----------------------------------
148
//! FIXME_docs
149
 
150
bool RparseUrl::FindOpt(const std::string& name, std::string& value) const
151
{
152
  omap_cit_t it = fOptMap.find(name);
153
  if (it == fOptMap.end()) return false;
154
 
155
  value = it->second;
156
 
157
  return true;
158
}
159
 
160
//------------------------------------------+-----------------------------------
161
//! FIXME_docs
162
 
163
void RparseUrl::Dump(std::ostream& os, int ind, const char* text) const
164
{
165
  RosFill bl(ind);
166
  os << bl << (text?text:"--") << "RparseUrl @ " << this << endl;
167
 
168
  os << bl << "  fUrl:            " << fUrl << endl;
169
  os << bl << "  fScheme:         " << fScheme << endl;
170
  os << bl << "  fPath:           " << fPath << endl;
171
  os << bl << "  fOptMap:         " << endl;
172
  for (omap_cit_t it=fOptMap.begin(); it!=fOptMap.end(); it++) {
173
    os << bl << "    " << RosPrintf((it->first).c_str(), "-s",8)
174
       << " : " << it->second << endl;
175
  }
176
  return;
177
}
178
 
179
//------------------------------------------+-----------------------------------
180
//! FIXME_docs
181
 
182
std::string RparseUrl::FindScheme(const std::string& url,
183
                                  const std::string& def)
184
{
185
  size_t pdel = url.find_first_of(':');
186
  if (pdel == string::npos) {               // no : found
187
    return def;
188
  }
189
 
190
  size_t odel = url.find_first_of('?');
191
  if (odel != string::npos && odel < pdel) { // : after ?
192
    return def;
193
  }
194
 
195
  return url.substr(0, pdel);
196
}
197
 
198
//------------------------------------------+-----------------------------------
199
//! FIXME_docs
200
 
201
bool RparseUrl::AddOpt(const std::string& key, const std::string& val,
202
                       bool hasval, const std::string& optlist, RerrMsg& emsg)
203
{
204
  string lkey = "|";
205
  lkey += key;
206
  if (hasval) lkey += "=";
207
  lkey += "|";
208
  if (optlist.find(lkey) == string::npos) {
209
    emsg.Init("RparseUrl::AddOpt()",
210 21 wfjm
              string("invalid field name '") + lkey + "'; allowed: '" +
211
              optlist + "'");
212 19 wfjm
    return false;
213
  }
214
 
215
  fOptMap.insert(omap_val_t(key, hasval ? val : "1"));
216
  return true;
217
}
218
 
219
} // end namespace Retro

powered by: WebSVN 2.1.0

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