Menu

[r1655]: / framework / trunk / src / url_mapper.cpp  Maximize  Restore  History

Download this file

227 lines (200 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/url_mapper.h>
#include <cppcms/cppcms_error.h>
#include <map>
#include <stdlib.h>
namespace cppcms {
struct url_mapper::data
{
struct entry {
std::vector<std::string> parts;
std::vector<int> indexes;
std::vector<std::string> keys;
};
typedef std::map<size_t,entry> by_size_type;
typedef std::map<std::string,by_size_type> by_key_type;
typedef std::map<std::string,std::string> helpers_type;
by_key_type by_key;
helpers_type helpers;
std::string root;
bool map( std::string const key,
std::vector<std::string> const &params,
std::string &output) const
{
by_key_type::const_iterator kp = by_key.find(key);
if(kp == by_key.end())
return false;
by_size_type::const_iterator sp = kp->second.find(params.size());
if(sp == kp->second.end())
return false;
output.clear();
output+=root;
entry const &formatting = sp->second;
for(size_t i=0;i<formatting.parts.size();i++) {
output += formatting.parts[i];
if( i < formatting.indexes.size() ) {
if(formatting.indexes[i]==0) {
std::string const &hkey = formatting.keys[i];
std::map<std::string,std::string>::const_iterator p = helpers.find(hkey);
if(p != helpers.end()) {
output += p->second;
}
}
else {
output+=params.at(formatting.indexes[i] - 1);
}
}
}
return true;
}
};
void url_mapper::assign(std::string const &key,std::string const &url)
{
data::entry e;
std::string::const_iterator prev = url.begin(), p = url.begin();
int max_index = 0;
while(p!=url.end()) {
if(*p=='{') {
e.parts.push_back(std::string(prev,p));
prev = p;
while(p!=url.end()) {
if(*p=='}') {
std::string const hkey(prev+1,p);
prev = p+1;
if(hkey.size()==0) {
throw cppcms_error("cppcms::url_mapper: empty index between {}");
}
bool all_digits = true;
for(unsigned i=0;all_digits && i<hkey.size();i++) {
if(hkey[i] < '0' || '9' <hkey[i])
all_digits = false;
}
if(!all_digits) {
e.indexes.push_back(0);
e.keys.push_back(hkey);
}
else {
int index = atoi(hkey.c_str());
if(index == 0)
throw cppcms_error("cppcms::url_mapper: index 0 is invalid");
max_index = std::max(index,max_index);
e.indexes.push_back(index);
e.keys.resize(e.keys.size()+1);
}
break;
}
else
p++;
}
if(p==url.end())
throw cppcms_error("cppcms::url_mapper: '{' in url without '}'");
p++;
}
else if(*p=='}') {
throw cppcms_error("cppcms::url_mapper: '}' in url without '{'");
}
else
p++;
}
e.parts.push_back(std::string(prev,p));
d->by_key[key][max_index] = e;
}
void url_mapper::set_value(std::string const &key,std::string const &value)
{
d->helpers[key]=value;
}
void url_mapper::clear_value(std::string const &key)
{
d->helpers.erase(key);
}
url_mapper::url_mapper() : d(new url_mapper::data())
{
}
url_mapper::~url_mapper()
{
}
std::string url_mapper::root()
{
return d->root;
}
void url_mapper::root(std::string const &r)
{
d->root = r;
}
std::string url_mapper::real_map(std::string const &key,std::vector<std::string> const &params)
{
std::string result;
if(!d->map(key,params,result)) {
throw cppcms_error("cppcms::url_mapper:invalid key `" + key + "' given");
}
return result;
}
void url_mapper::map( std::ostream &out,
std::string const &key)
{
std::vector<std::string> params;
out << real_map(key,params);
}
void url_mapper::map( std::ostream &out,
std::string const &key,
filters::streamable const &p1)
{
std::vector<std::string> params(1);
params[0] = p1.get(out);
out << real_map(key,params);
}
void url_mapper::map( std::ostream &out,
std::string const &key,
filters::streamable const &p1,
filters::streamable const &p2)
{
std::vector<std::string> params(2);
params[0] = p1.get(out);
params[1] = p2.get(out);
out << real_map(key,params);
}
void url_mapper::map( std::ostream &out,
std::string const &key,
filters::streamable const &p1,
filters::streamable const &p2,
filters::streamable const &p3)
{
std::vector<std::string> params(3);
params[0] = p1.get(out);
params[1] = p2.get(out);
params[2] = p3.get(out);
out << real_map(key,params);
}
void url_mapper::map( std::ostream &out,
std::string const &key,
filters::streamable const &p1,
filters::streamable const &p2,
filters::streamable const &p3,
filters::streamable const &p4)
{
std::vector<std::string> params(4);
params[0] = p1.get(out);
params[1] = p2.get(out);
params[2] = p3.get(out);
params[3] = p4.get(out);
out << real_map(key,params);
}
}
MongoDB Logo MongoDB