00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "URL.h"
00039 #include "ClearSilverError.h"
00040 #include "ClearSilverException.h"
00041
00042 #include <ClearSilver/cgi/cgi.h>
00043
00044 using namespace std;
00045
00046 #if !HAVE_UNSIGNED_URL
00047 #include "c_string.h"
00048 #endif
00049
00050 #if HAVE_UNSIGNED_URL
00051
00052 template <class Destination,class Source>
00053 class convert
00054 {
00055 Destination * s_;
00056
00057 private:
00058 convert (const convert&);
00059 convert& operator = (const convert&);
00060
00061 public:
00062 convert (const Source * s);
00063 ~convert ();
00064 operator Destination * () const;
00065 Destination * operator () () const;
00066 };
00067 #endif // HAVE_UNSIGNED_URL
00068
00069 namespace ClearSilver
00070 {
00071
00072 URL::URL () : url_() {}
00073 URL::URL (const char * s) : url_()
00074 {
00075 #if HAVE_UNSIGNED_URL
00076 unsigned char * url;
00077 ClearSilverError error
00078 (cgi_url_escape (convert<unsigned char,char>(s), &url));
00079 if (error)
00080 {
00081 string message;
00082 message += "ClearSilver::URL::URL (const char*): ";
00083 ClearSilverException e (message);
00084 e += error;
00085 throw e;
00086 }
00087 convert<char,unsigned char> c (url);
00088 url_ = c();
00089 #else // HAVE_UNSIGNED_URL
00090 char * url;
00091 cgi_url_escape (c_string(s), &url);
00092 url_ = url;
00093 #endif // HAVE_UNSIGNED_URL
00094 }
00095 URL::URL (const string& s) : url_()
00096 {
00097 #if HAVE_UNSIGNED_URL
00098 unsigned char * url;
00099 ClearSilverError error
00100 (cgi_url_escape (convert<unsigned char,char>(s.c_str()), &url));
00101 if (error)
00102 {
00103 string message;
00104 message += "ClearSilver::URL::URL (const string&): ";
00105 ClearSilverException e (message);
00106 e += error;
00107 throw e;
00108 }
00109 convert<char,unsigned char> c (url);
00110 url_ = c();
00111 #else // HAVE_UNSIGNED_URL
00112 char * url;
00113 cgi_url_escape (c_string(s), &url);
00114 url_ = url;
00115 #endif // HAVE_UNSIGNED_URL
00116 }
00117 URL::URL (const URL& u) : url_(u.url_) {}
00118 URL::~URL () {}
00119
00120
00121 URL&
00122 URL::operator = (const URL& u)
00123 {
00124 url_ = u.url_;
00125 return *this;
00126 }
00127
00128
00129
00130 URL::operator string () const
00131 {
00132 #if HAVE_UNSIGNED_URL
00133 convert<unsigned char,char> c (url_.c_str());
00134 cgi_url_unescape (c());
00135 return string (convert<char,unsigned char>(c()));
00136 #else // HAVE_UNSIGNED_URL
00137 return string (cgi_url_unescape (c_string(url_.c_str())));
00138 #endif
00139 }
00140
00141
00142 string
00143 URL::operator () () const { return url_; }
00144 };
00145
00146
00147 #if HAVE_UNSIGNED_URL
00148 template <typename Destination,typename Source>
00149 inline
00150 convert<Destination,Source>::convert (const Source * s) : s_()
00151 {
00152 size_t len = 0;
00153 for (const Source * j = s; *j; ++j)
00154 ++len;
00155 s_ = static_cast<Destination*>(calloc(len+1, sizeof(Destination)));
00156 Destination * i = s_;
00157 while (*s)
00158 *i++ = *s++;
00159 }
00160
00161 template <typename Destination,typename Source>
00162 inline
00163 convert<Destination,Source>::~convert () { free(s_); }
00164
00165 template <typename Destination,typename Source>
00166 inline
00167 convert<Destination,Source>::operator Destination * () const { return s_; }
00168
00169 template <typename Destination,typename Source>
00170 inline
00171 Destination *
00172 convert<Destination,Source>::operator () () const { return s_; }
00173 #endif // HAVE_UNSIGNED_URL