URL.cc

00001 /* -*- c++ -*-
00002  * $Id: URL.cc,v 1.12 2006/03/27 19:17:47 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2005,2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // exception-safe string conversions
00052 template <class Destination,class Source>
00053 class convert
00054 {
00055   Destination * s_;
00056 
00057 private:                        // enforce reference semantics
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                                 // constructors
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                                 // assignment
00121 URL&
00122 URL::operator = (const URL& u)
00123 {
00124   url_ = u.url_;
00125   return *this;
00126 }
00127 
00128                                 // conversions
00129                                 // unescape the URL
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 // HAVE_UNSIGNED_URL
00139 }
00140 
00141                                 // inspectors
00142 string
00143 URL::operator () () const { return url_; }
00144 };                              // namespace ClearSilver
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

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5