ConstHDFNode.cc

00001 /*
00002  * $Id: ConstHDFNode.cc,v 1.5 2006/03/29 05:35:58 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 "ConstHDFNode.h"
00039 
00040 #include <algorithm>
00041 #include <stdexcept>
00042 #include <string.h>
00043 #include <syslog.h>
00044 #include <boost/lexical_cast.hpp>
00045 
00046 #define CGI ClearSilverCGI
00047 #define HDF ClearSilverHDF
00048 #include <ClearSilver/ClearSilver.h>
00049 #undef CGI
00050 #undef HDF
00051 
00052 using namespace std;
00053 
00054 static string hex(void*);
00055 
00056                                 // constructors
00057 ClearSilver::ConstHDFNode::ConstHDFNode ()
00058   : is_iterator_(false) {}
00059 
00060 ClearSilver::ConstHDFNode::ConstHDFNode (const ConstHDFNode& n)
00061   : is_iterator_(n.is_iterator_) {}
00062 
00063 ClearSilver::ConstHDFNode::~ConstHDFNode () throw()
00064 {}
00065 
00066                                 // assignment
00067 #if 0                           // XXX
00068 ClearSilver::ConstHDFNode&
00069 ClearSilver::ConstHDFNode::operator = (const ConstHDFNode& n)
00070 {
00071   ConstHDFNode n0 (n);
00072   swap (n0);
00073   return *this;
00074 }
00075 #endif
00076 
00077                                 // STL inspectors
00078 bool
00079 ClearSilver::ConstHDFNode::empty () const
00080 {
00081   return node().empty();
00082 }
00083 
00084                                 // conversions
00085                                 // is this a valid node?
00086 ClearSilver::ConstHDFNode::operator bool () const
00087 {
00088   return !node().empty();
00089 }
00090 
00091                                 // convert to the value of the node.
00092 ClearSilver::ConstHDFNode::operator std::string () const
00093 {
00094   return node().value();
00095 }
00096 
00097                                 // node accessors
00098                                 // name of the node.
00099 std::string
00100 ClearSilver::ConstHDFNode::name () const
00101 {
00102   return node().name();
00103 }
00104 
00105 // value of the node.
00106 std::string
00107 ClearSilver::ConstHDFNode::value () const
00108 {
00109   return node().value();
00110 }
00111 
00112                                 // HDF dataset operations
00113 std::string
00114 ClearSilver::ConstHDFNode::get_value () const
00115 {
00116   return node().value();
00117 }
00118 
00119 std::string
00120 ClearSilver::ConstHDFNode::get_value (const char* key) const
00121 {
00122   return get_value(std::string(key));
00123 }
00124 
00125 std::string
00126 ClearSilver::ConstHDFNode::get_value (const std::string& key) const
00127 {
00128   return get_value(key, std::string());
00129 }
00130 
00131 std::string
00132 ClearSilver::ConstHDFNode::get_value (const char* key,
00133                                       const char* default_value) const
00134 {
00135   return get_value(std::string(key), std::string(default_value));
00136 }
00137 
00138 std::string
00139 ClearSilver::ConstHDFNode::get_value (const std::string& key,
00140                          const std::string& default_value) const
00141 {
00142   return node().get_value(key, default_value);
00143 }
00144     
00145 void
00146 ClearSilver::ConstHDFNode::write_file (const char * filename) const
00147 {
00148   write_file(std::string(filename));
00149 }
00150 
00151 void
00152 ClearSilver::ConstHDFNode::write_file (const std::string& filename) const
00153 {
00154   node().write_file(filename);
00155 }
00156 
00157 void
00158 ClearSilver::ConstHDFNode::dump () const
00159 {
00160   dump("<BR>");
00161 }
00162 
00163 void
00164 ClearSilver::ConstHDFNode::dump (const char* prefix) const
00165 {
00166   dump(std::string(prefix));
00167 }
00168 
00169 void
00170 ClearSilver::ConstHDFNode::dump (const std::string& prefix) const
00171 {
00172   node().dump(prefix);
00173 }
00174 
00175                                 // boolean constants
00176 std::string
00177 ClearSilver::ConstHDFNode::Boolean (bool b)
00178 {
00179   return ClearSilverNode::Boolean(b);
00180 }
00181 
00182 std::string
00183 ClearSilver::ConstHDFNode::True ()
00184 {
00185   return ClearSilverNode::True();
00186 }
00187 
00188 std::string
00189 ClearSilver::ConstHDFNode::False ()
00190 {
00191   return ClearSilverNode::False();
00192 }
00193 
00194                                 // is this a dataset?
00195 bool
00196 ClearSilver::ConstHDFNode::is_dataset () const
00197 {
00198   return nodes().empty();
00199 }
00200 
00201                                 // is this a node within a dataset?
00202 bool
00203 ClearSilver::ConstHDFNode::is_reference () const
00204 {
00205   return !nodes().empty() && !is_iterator_;
00206 }
00207 
00208 bool
00209 ClearSilver::ConstHDFNode::is_iterator () const
00210 {
00211   return !nodes().empty() && is_iterator_;
00212 }
00213 
00214                                 // swap contents with another object.
00215 void
00216 ClearSilver::ConstHDFNode::swap (ConstHDFNode& n) throw()
00217 {
00218   std::swap (is_iterator_, n.is_iterator_);
00219 }
00220 
00221                                 // debug flag
00222 bool&
00223 ClearSilver::ConstHDFNode::debug ()
00224 {
00225   static bool debug_ = false;
00226   return debug_;
00227 }
00228 
00229 ClearSilver::Nodes
00230 ClearSilver::ConstHDFNode::find_node (const string& key) const
00231 {
00232   if (key.empty())
00233     {
00234       string message;
00235       message += "ClearSilver::ConstHDFNode::find_node ";
00236       message += "(const string&) const: ";
00237       message += "empty key";
00238       throw invalid_argument(message);
00239     }
00240   if (*key.rbegin() == '.')
00241     {
00242       string message;
00243       message += "ClearSilver::ConstHDFNode::find_node ";
00244       message += "(const string&) const: ";
00245       message += "'" + key + "' ends in a period (.)";
00246       throw invalid_argument(message);
00247     }
00248   if (empty())
00249     {
00250       string message;
00251       message += "ClearSilver::ConstHDFNode::find_node ";
00252       message += "(const string&) const: ";
00253       message += "empty dataset";
00254       throw logic_error(message);
00255     }
00256 
00257   if (debug())
00258     {
00259       string message;
00260       message += string("ConstHDFNode::find_node (const string& key) const: ");
00261       message += "key=" + key;
00262       syslog (LOG_DEBUG, "%s", message.c_str());
00263     }
00264   Nodes n (nodes());
00265   ClearSilverNode node (this->node());
00266   if (debug())
00267     {
00268       string message;
00269       message += string("ConstHDFNode::find_node (const string& key) const: ");
00270       message += string("node=") + hex(node.get());
00271       if (node)
00272         {
00273           message += ", ";
00274           if (node.get()->name)
00275             message += string("name=") + node.get()->name + ", ";
00276           if (node.get()->value)
00277             message += string("value=") + node.get()->value + ", ";
00278           message += string("top=") + hex(node.get()->top) + ", ";
00279           message += string("child=") + hex(node.get()->child);
00280         }
00281       syslog (LOG_DEBUG, "%s", message.c_str());
00282     }
00283   string::size_type begin (0);
00284   while (1)                     // for each path component
00285     {
00286       string::size_type end(key.find(".", begin));
00287       string k (key.substr(begin, end == string::npos?string::npos:end-begin));
00288       if (debug())
00289         {
00290           string message;
00291           message
00292             += string("ConstHDFNode::find_node (const string& key) const: ");
00293           message += string("range: [") + boost::lexical_cast<string>(begin)
00294             + "," + boost::lexical_cast<string>(end) + "], ";
00295           message += string("key(0,end)=") + key.substr(0,end) + ", ";
00296           message += string("key(begin,end)=") + k;
00297           syslog (LOG_DEBUG, "%s", message.c_str());
00298         }
00299       node = node.get_obj(k);
00300       if (debug())
00301         {
00302           string message;
00303           message
00304             += string("ConstHDFNode::find_node (const string& key) const: ");
00305           message += string("node=") + hex(node.get());
00306           if (node)
00307             {
00308               message += ", ";
00309               if (node.get()->name)
00310                 message += string("name=") + node.get()->name + ", ";
00311               if (node.get()->value)
00312                 message += string("value=") + node.get()->value + ", ";
00313               message += string("top=") + hex(node.get()->top) + ", ";
00314               message += string("child=") + hex(node.get()->child);
00315             }
00316           syslog (LOG_DEBUG, "%s", message.c_str());
00317         }
00318       if (!node)
00319         {
00320           n = nodes();
00321           n.push(string(), ClearSilverNode());
00322           break;
00323         }
00324       n.push(k, node);
00325       if (end == string::npos)
00326         break;
00327       begin = ++end;
00328     }
00329   if (debug())
00330     {
00331       string message;
00332       message += string("ConstHDFNode::find_node (const string& key) const: ")
00333         + "done";
00334       syslog (LOG_DEBUG, "%s", message.c_str());
00335       n.print();
00336     }
00337   return n;
00338 }
00339 
00340                                 // access the iterator flag.
00341 bool&
00342 ClearSilver::ConstHDFNode::is_iterator ()
00343 {
00344   return is_iterator_;
00345 }
00346 
00347 string
00348 hex (void* p)
00349 {
00350   ostringstream s;
00351   s << std::hex << p;
00352   return s.str();
00353 }
00354 

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