Sophie

Sophie

distrib > Fedora > 16 > i386 > by-pkgid > c9cfbd1355daacaf3b34517a6d7b18cc > files > 1374

libdap-doc-3.11.0-2.fc15.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libdap++: util.cc Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">libdap++&#160;<span id="projectnumber">Updated for version 3.8.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('util_8cc.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<h1>util.cc</h1>  </div>
</div>
<div class="contents">
<a href="util_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="comment">// -*- mode: c++; c-basic-offset:4 -*-</span>
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="comment">// This file is part of libdap, A C++ implementation of the OPeNDAP Data</span>
<a name="l00005"></a>00005 <span class="comment">// Access Protocol.</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="comment">// Copyright (c) 2002,2003 OPeNDAP, Inc.</span>
<a name="l00008"></a>00008 <span class="comment">// Author: James Gallagher &lt;jgallagher@opendap.org&gt;</span>
<a name="l00009"></a>00009 <span class="comment">//</span>
<a name="l00010"></a>00010 <span class="comment">// This library is free software; you can redistribute it and/or</span>
<a name="l00011"></a>00011 <span class="comment">// modify it under the terms of the GNU Lesser General Public</span>
<a name="l00012"></a>00012 <span class="comment">// License as published by the Free Software Foundation; either</span>
<a name="l00013"></a>00013 <span class="comment">// version 2.1 of the License, or (at your option) any later version.</span>
<a name="l00014"></a>00014 <span class="comment">//</span>
<a name="l00015"></a>00015 <span class="comment">// This library is distributed in the hope that it will be useful,</span>
<a name="l00016"></a>00016 <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00017"></a>00017 <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<a name="l00018"></a>00018 <span class="comment">// Lesser General Public License for more details.</span>
<a name="l00019"></a>00019 <span class="comment">//</span>
<a name="l00020"></a>00020 <span class="comment">// You should have received a copy of the GNU Lesser General Public</span>
<a name="l00021"></a>00021 <span class="comment">// License along with this library; if not, write to the Free Software</span>
<a name="l00022"></a>00022 <span class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00023"></a>00023 <span class="comment">//</span>
<a name="l00024"></a>00024 <span class="comment">// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.</span>
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="comment">// (c) COPYRIGHT URI/MIT 1994-1999</span>
<a name="l00027"></a>00027 <span class="comment">// Please read the full copyright statement in the file COPYRIGHT_URI.</span>
<a name="l00028"></a>00028 <span class="comment">//</span>
<a name="l00029"></a>00029 <span class="comment">// Authors:</span>
<a name="l00030"></a>00030 <span class="comment">//      jhrg,jimg       James Gallagher &lt;jgallagher@gso.uri.edu&gt;</span>
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 <span class="comment">// Utility functions used by the api.</span>
<a name="l00033"></a>00033 <span class="comment">//</span>
<a name="l00034"></a>00034 <span class="comment">// jhrg 9/21/94</span>
<a name="l00035"></a>00035 
<a name="l00036"></a>00036 <span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span>
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="keyword">static</span> <span class="keywordtype">char</span> rcsid[] <a class="code" href="config_8h.html#a05e109ec893d0b12023d5b0cf4f718c4">not_used</a> =
<a name="l00039"></a>00039     {<span class="stringliteral">&quot;$Id: util.cc 21699 2009-11-05 00:06:01Z jimg $&quot;</span>
<a name="l00040"></a>00040     };
<a name="l00041"></a>00041 
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;cstring&gt;</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="preprocessor">#include &lt;ctype.h&gt;</span>
<a name="l00046"></a>00046 <span class="preprocessor">#ifndef TM_IN_SYS_TIME</span>
<a name="l00047"></a>00047 <span class="preprocessor"></span><span class="preprocessor">#include &lt;time.h&gt;</span>
<a name="l00048"></a>00048 <span class="preprocessor">#else</span>
<a name="l00049"></a>00049 <span class="preprocessor"></span><span class="preprocessor">#include &lt;sys/time.h&gt;</span>
<a name="l00050"></a>00050 <span class="preprocessor">#endif</span>
<a name="l00051"></a>00051 <span class="preprocessor"></span>
<a name="l00052"></a>00052 <span class="preprocessor">#ifndef WIN32</span>
<a name="l00053"></a>00053 <span class="preprocessor"></span><span class="preprocessor">#include &lt;unistd.h&gt;</span>    <span class="comment">// for stat</span>
<a name="l00054"></a>00054 <span class="preprocessor">#else</span>
<a name="l00055"></a>00055 <span class="preprocessor"></span><span class="preprocessor">#include &lt;io.h&gt;</span>
<a name="l00056"></a>00056 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00057"></a>00057 <span class="preprocessor">#include &lt;process.h&gt;</span>
<a name="l00058"></a>00058 <span class="preprocessor">#endif</span>
<a name="l00059"></a>00059 <span class="preprocessor"></span>
<a name="l00060"></a>00060 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00061"></a>00061 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00062"></a>00062 
<a name="l00063"></a>00063 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00064"></a>00064 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00065"></a>00065 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00066"></a>00066 <span class="preprocessor">#include &lt;algorithm&gt;</span>
<a name="l00067"></a>00067 <span class="preprocessor">#include &lt;stdexcept&gt;</span>
<a name="l00068"></a>00068 
<a name="l00069"></a>00069 <span class="preprocessor">#include &quot;<a class="code" href="BaseType_8h.html">BaseType.h</a>&quot;</span>
<a name="l00070"></a>00070 <span class="preprocessor">#include &quot;<a class="code" href="Str_8h.html">Str.h</a>&quot;</span>
<a name="l00071"></a>00071 <span class="preprocessor">#include &quot;<a class="code" href="Url_8h.html">Url.h</a>&quot;</span>
<a name="l00072"></a>00072 <span class="preprocessor">#include &quot;<a class="code" href="Sequence_8h.html">Sequence.h</a>&quot;</span>
<a name="l00073"></a>00073 <span class="preprocessor">#include &quot;<a class="code" href="Error_8h.html">Error.h</a>&quot;</span>
<a name="l00074"></a>00074 <span class="preprocessor">#include &quot;<a class="code" href="parser_8h.html">parser.h</a>&quot;</span>
<a name="l00075"></a>00075 <span class="preprocessor">#include &quot;<a class="code" href="util_8h.html">util.h</a>&quot;</span>
<a name="l00076"></a>00076 <span class="preprocessor">#include &quot;GNURegex.h&quot;</span>
<a name="l00077"></a>00077 <span class="preprocessor">#include &quot;<a class="code" href="debug_8h.html">debug.h</a>&quot;</span>
<a name="l00078"></a>00078 
<a name="l00079"></a>00079 
<a name="l00080"></a>00080 <span class="keyword">using namespace </span>std;
<a name="l00081"></a>00081 
<a name="l00082"></a>00082 <span class="keyword">namespace </span>libdap {
<a name="l00083"></a>00083 
<a name="l00084"></a>00084 <span class="comment">// Remove spaces from the start of a URL and from the start of any constraint</span>
<a name="l00085"></a>00085 <span class="comment">// expression it contains. 4/7/98 jhrg</span>
<a name="l00086"></a>00086 
<a name="l00095"></a>00095 <span class="keywordtype">string</span>
<a name="l00096"></a><a class="code" href="namespacelibdap.html#a4b1fc86f3e9e75f967ad66f07962d406">00096</a> <a class="code" href="namespacelibdap.html#a4b1fc86f3e9e75f967ad66f07962d406">prune_spaces</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;name)
<a name="l00097"></a>00097 {
<a name="l00098"></a>00098     <span class="comment">// If the URL does not even have white space return.</span>
<a name="l00099"></a>00099     <span class="keywordflow">if</span> (name.find_first_of(<span class="charliteral">&#39; &#39;</span>) == name.npos)
<a name="l00100"></a>00100         <span class="keywordflow">return</span> name;
<a name="l00101"></a>00101     <span class="keywordflow">else</span> {
<a name="l00102"></a>00102         <span class="comment">// Strip leading spaces from http://...</span>
<a name="l00103"></a>00103         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = name.find_first_not_of(<span class="charliteral">&#39; &#39;</span>);
<a name="l00104"></a>00104         <span class="keywordtype">string</span> tmp_name = name.substr(i);
<a name="l00105"></a>00105 
<a name="l00106"></a>00106         <span class="comment">// Strip leading spaces from constraint part (following `?&#39;).</span>
<a name="l00107"></a>00107         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j = tmp_name.find(<span class="charliteral">&#39;?&#39;</span>) + 1;
<a name="l00108"></a>00108         i = tmp_name.find_first_not_of(<span class="charliteral">&#39; &#39;</span>, j);
<a name="l00109"></a>00109         tmp_name.erase(j, i - j);
<a name="l00110"></a>00110 
<a name="l00111"></a>00111         <span class="keywordflow">return</span> tmp_name;
<a name="l00112"></a>00112     }
<a name="l00113"></a>00113 }
<a name="l00114"></a>00114 
<a name="l00115"></a>00115 <span class="comment">// Compare elements in a list of (BaseType *)s and return true if there are</span>
<a name="l00116"></a>00116 <span class="comment">// no duplicate elements, otherwise return false.</span>
<a name="l00117"></a>00117 
<a name="l00118"></a>00118 <span class="keywordtype">bool</span>
<a name="l00119"></a><a class="code" href="namespacelibdap.html#afabf64c5d4c1900f1c05ac655895f7ee">00119</a> <a class="code" href="namespacelibdap.html#afabf64c5d4c1900f1c05ac655895f7ee">unique_names</a>(vector&lt;BaseType *&gt; l, <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;var_name,
<a name="l00120"></a>00120              <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;type_name, <span class="keywordtype">string</span> &amp;msg)
<a name="l00121"></a>00121 {
<a name="l00122"></a>00122     <span class="comment">// copy the identifier names to a vector</span>
<a name="l00123"></a>00123     vector&lt;string&gt; names(l.size());
<a name="l00124"></a>00124 
<a name="l00125"></a>00125     <span class="keywordtype">int</span> nelem = 0;
<a name="l00126"></a>00126     <span class="keyword">typedef</span> std::vector&lt;BaseType *&gt;::const_iterator citer ;
<a name="l00127"></a>00127     <span class="keywordflow">for</span> (citer i = l.begin(); i != l.end(); i++) {
<a name="l00128"></a>00128         assert(*i);
<a name="l00129"></a>00129         names[nelem++] = (*i)-&gt;name();
<a name="l00130"></a>00130         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;NAMES[&quot;</span> &lt;&lt; nelem - 1 &lt;&lt; <span class="stringliteral">&quot;]=&quot;</span> &lt;&lt; names[nelem-1] &lt;&lt; endl);
<a name="l00131"></a>00131     }
<a name="l00132"></a>00132 
<a name="l00133"></a>00133     <span class="comment">// sort the array of names</span>
<a name="l00134"></a>00134     sort(names.begin(), names.end());
<a name="l00135"></a>00135 
<a name="l00136"></a>00136 <span class="preprocessor">#ifdef DODS_DEBUG2</span>
<a name="l00137"></a>00137 <span class="preprocessor"></span>    cout &lt;&lt; <span class="stringliteral">&quot;unique:&quot;</span> &lt;&lt; endl;
<a name="l00138"></a>00138     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> ii = 0; ii &lt; nelem; ++ii)
<a name="l00139"></a>00139         cout &lt;&lt; <span class="stringliteral">&quot;NAMES[&quot;</span> &lt;&lt; ii &lt;&lt; <span class="stringliteral">&quot;]=&quot;</span> &lt;&lt; names[ii] &lt;&lt; endl;
<a name="l00140"></a>00140 <span class="preprocessor">#endif</span>
<a name="l00141"></a>00141 <span class="preprocessor"></span>
<a name="l00142"></a>00142     <span class="comment">// sort the array of names</span>
<a name="l00143"></a>00143     sort(names.begin(), names.end());
<a name="l00144"></a>00144 
<a name="l00145"></a>00145 <span class="preprocessor">#ifdef DODS_DEBUG2</span>
<a name="l00146"></a>00146 <span class="preprocessor"></span>    cout &lt;&lt; <span class="stringliteral">&quot;unique:&quot;</span> &lt;&lt; endl;
<a name="l00147"></a>00147     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> ii = 0; ii &lt; nelem; ++ii)
<a name="l00148"></a>00148         cout &lt;&lt; <span class="stringliteral">&quot;NAMES[&quot;</span> &lt;&lt; ii &lt;&lt; <span class="stringliteral">&quot;]=&quot;</span> &lt;&lt; names[ii] &lt;&lt; endl;
<a name="l00149"></a>00149 <span class="preprocessor">#endif</span>
<a name="l00150"></a>00150 <span class="preprocessor"></span>
<a name="l00151"></a>00151     <span class="comment">// look for any instance of consecutive names that are ==</span>
<a name="l00152"></a>00152     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 1; j &lt; nelem; ++j) {
<a name="l00153"></a>00153         <span class="keywordflow">if</span> (names[j-1] == names[j]) {
<a name="l00154"></a>00154             ostringstream oss;
<a name="l00155"></a>00155             oss &lt;&lt; <span class="stringliteral">&quot;The variable `&quot;</span> &lt;&lt; names[j]
<a name="l00156"></a>00156             &lt;&lt; <span class="stringliteral">&quot;&#39; is used more than once in &quot;</span> &lt;&lt; type_name &lt;&lt; <span class="stringliteral">&quot; `&quot;</span>
<a name="l00157"></a>00157             &lt;&lt; var_name &lt;&lt; <span class="stringliteral">&quot;&#39;&quot;</span>;
<a name="l00158"></a>00158             msg = oss.str();
<a name="l00159"></a>00159 
<a name="l00160"></a>00160             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00161"></a>00161         }
<a name="l00162"></a>00162     }
<a name="l00163"></a>00163 
<a name="l00164"></a>00164     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 
<a name="l00167"></a>00167 <span class="keyword">const</span> <span class="keywordtype">char</span> *
<a name="l00168"></a><a class="code" href="namespacelibdap.html#aa410c0b58527efed5753f9f2da1b9775">00168</a> <a class="code" href="namespacelibdap.html#aa410c0b58527efed5753f9f2da1b9775">libdap_root</a>()
<a name="l00169"></a>00169 {
<a name="l00170"></a>00170     <span class="keywordflow">return</span> <a class="code" href="config_8h.html#a10e968b1eac80c4b7d88a9db47137f41">LIBDAP_ROOT</a>;
<a name="l00171"></a>00171 }
<a name="l00172"></a>00172 
<a name="l00173"></a>00173 <span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span>
<a name="l00174"></a>00174     <span class="keyword">const</span> <span class="keywordtype">char</span> *
<a name="l00175"></a><a class="code" href="namespacelibdap.html#a3c2e481465a4567eaf6d1f22d84c0ef9">00175</a>     <a class="code" href="namespacelibdap.html#a3c2e481465a4567eaf6d1f22d84c0ef9">libdap_version</a>()
<a name="l00176"></a>00176 {
<a name="l00177"></a>00177     <span class="keywordflow">return</span> <a class="code" href="config_8h.html#aa326a05d5e30f9e9a4bb0b4469d5d0c0">PACKAGE_VERSION</a>;
<a name="l00178"></a>00178 }
<a name="l00179"></a>00179 
<a name="l00180"></a>00180 <span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span>
<a name="l00181"></a>00181     <span class="keyword">const</span> <span class="keywordtype">char</span> *
<a name="l00182"></a><a class="code" href="namespacelibdap.html#a218c76777bb3b1a6088a5d9f431fd3f0">00182</a>     <a class="code" href="namespacelibdap.html#a218c76777bb3b1a6088a5d9f431fd3f0">libdap_name</a>()
<a name="l00183"></a>00183 {
<a name="l00184"></a>00184     <span class="keywordflow">return</span> <a class="code" href="config_8h.html#a1c0439e4355794c09b64274849eb0279">PACKAGE_NAME</a>;
<a name="l00185"></a>00185 }
<a name="l00186"></a>00186 
<a name="l00187"></a>00187 <span class="comment">// Since Server4 can get compressed responses using Tomcat, bail on this</span>
<a name="l00188"></a>00188 <span class="comment">// software (which complicates building under Win32). It can be turned on</span>
<a name="l00189"></a>00189 <span class="comment">// for use with Server3 in configure.ac.</span>
<a name="l00190"></a>00190 
<a name="l00191"></a>00191 <span class="preprocessor">#if COMPRESSION_FOR_SERVER3</span>
<a name="l00192"></a>00192 <span class="preprocessor"></span>
<a name="l00193"></a>00193 <span class="comment">// Return true if the program deflate exists and is executable by user, group</span>
<a name="l00194"></a>00194 <span class="comment">// and world. If this returns false the caller should assume that server</span>
<a name="l00195"></a>00195 <span class="comment">// filter programs won&#39;t be able to find the deflate program and thus won&#39;t</span>
<a name="l00196"></a>00196 <span class="comment">// be able to compress the return document.</span>
<a name="l00197"></a>00197 <span class="comment">// NB: this works because this function uses the same rules as compressor()</span>
<a name="l00198"></a>00198 <span class="comment">// (which follows) to look for deflate. 2/11/98 jhrg</span>
<a name="l00199"></a>00199 
<a name="l00200"></a>00200 <span class="keywordtype">bool</span>
<a name="l00201"></a>00201 <a class="code" href="namespacelibdap.html#a27871bfdfbe3fda3db0d2d8e5966d771">deflate_exists</a>()
<a name="l00202"></a>00202 {
<a name="l00203"></a>00203     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Entering deflate_exists...&quot;</span>);
<a name="l00204"></a>00204 
<a name="l00205"></a>00205     <span class="keywordtype">int</span> status = <span class="keyword">false</span>;
<a name="l00206"></a>00206     <span class="keyword">struct </span>stat buf;
<a name="l00207"></a>00207 
<a name="l00208"></a>00208 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00209"></a>00209 <span class="preprocessor"></span>    <span class="keywordtype">string</span> <a class="code" href="namespacelibdap.html#aada86e90b40fc8068dbfe9be79f09e55a3b0e7efd18178acc018030f0e69565a5">deflate</a> = (string)<a class="code" href="namespacelibdap.html#aa410c0b58527efed5753f9f2da1b9775">libdap_root</a>() + <span class="stringliteral">&quot;\\bin\\deflate&quot;</span>;
<a name="l00210"></a>00210 <span class="preprocessor">#else</span>
<a name="l00211"></a>00211 <span class="preprocessor"></span>    <span class="keywordtype">string</span> deflate = (string)<a class="code" href="namespacelibdap.html#aa410c0b58527efed5753f9f2da1b9775">libdap_root</a>() + <span class="stringliteral">&quot;/sbin/deflate&quot;</span>;
<a name="l00212"></a>00212 <span class="preprocessor">#endif</span>
<a name="l00213"></a>00213 <span class="preprocessor"></span>
<a name="l00214"></a>00214     <span class="comment">// Check that the file exists...</span>
<a name="l00215"></a>00215     <span class="comment">// First look for deflate using DODS_ROOT (compile-time constant subsumed</span>
<a name="l00216"></a>00216     <span class="comment">// by an environment variable) and if that fails in the CWD which finds</span>
<a name="l00217"></a>00217     <span class="comment">// the program when it is in the same directory as the dispatch script</span>
<a name="l00218"></a>00218     <span class="comment">// and other server components. 2/11/98 jhrg</span>
<a name="l00219"></a>00219     status = (stat(deflate.c_str(), &amp;buf) == 0)
<a name="l00220"></a>00220 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00221"></a>00221 <span class="preprocessor"></span>             || (stat(<span class="stringliteral">&quot;.\\deflate&quot;</span>, &amp;buf) == 0);
<a name="l00222"></a>00222 <span class="preprocessor">#else</span>
<a name="l00223"></a>00223 <span class="preprocessor"></span>             || (stat(<span class="stringliteral">&quot;./deflate&quot;</span>, &amp;buf) == 0);
<a name="l00224"></a>00224 <span class="preprocessor">#endif</span>
<a name="l00225"></a>00225 <span class="preprocessor"></span>
<a name="l00226"></a>00226     <span class="comment">// and that it can be executed.</span>
<a name="l00227"></a>00227 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00228"></a>00228 <span class="preprocessor"></span>    status &amp;= (buf.st_mode &amp; _S_IEXEC);
<a name="l00229"></a>00229 <span class="preprocessor">#else</span>
<a name="l00230"></a>00230 <span class="preprocessor"></span>    status &amp;= buf.st_mode &amp; (S_IXUSR | S_IXGRP | S_IXOTH);
<a name="l00231"></a>00231 <span class="preprocessor">#endif</span>
<a name="l00232"></a>00232 <span class="preprocessor"></span>    <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot; returning &quot;</span> &lt;&lt; (status ? <span class="stringliteral">&quot;true.&quot;</span> : <span class="stringliteral">&quot;false.&quot;</span>) &lt;&lt; endl);
<a name="l00233"></a>00233     <span class="keywordflow">return</span> (status != 0);
<a name="l00234"></a>00234 }
<a name="l00235"></a>00235 
<a name="l00236"></a>00236 FILE *
<a name="l00237"></a>00237 <a class="code" href="namespacelibdap.html#acd1d919406377c736eb8ad51933c2cd0">compressor</a>(FILE *output, <span class="keywordtype">int</span> &amp;childpid)
<a name="l00238"></a>00238 {
<a name="l00239"></a>00239 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00240"></a>00240 <span class="preprocessor"></span>    <span class="comment">//  There is no such thing as a &quot;fork&quot; under win32. This makes it so that</span>
<a name="l00241"></a>00241     <span class="comment">//  we have to juggle handles more aggressively. This code hasn&#39;t been</span>
<a name="l00242"></a>00242     <span class="comment">//  tested and shown to work as of 07/2000.</span>
<a name="l00243"></a>00243     <span class="keywordtype">int</span> pid, data[2];
<a name="l00244"></a>00244     <span class="keywordtype">int</span> hStdIn, hStdOut;
<a name="l00245"></a>00245 
<a name="l00246"></a>00246     <span class="keywordflow">if</span> (_pipe(data, 512, O_BINARY | O_NOINHERIT) &lt; 0) {
<a name="l00247"></a>00247         cerr &lt;&lt; <span class="stringliteral">&quot;Could not create IPC channel for compressor process&quot;</span>
<a name="l00248"></a>00248         &lt;&lt; endl;
<a name="l00249"></a>00249         <span class="keywordflow">return</span> NULL;
<a name="l00250"></a>00250     }
<a name="l00251"></a>00251 
<a name="l00252"></a>00252 
<a name="l00253"></a>00253     <span class="comment">// This sets up for the child process, but it has to be reversed for the</span>
<a name="l00254"></a>00254     <span class="comment">// parent after the spawn takes place.</span>
<a name="l00255"></a>00255 
<a name="l00256"></a>00256     <span class="comment">// Store stdin, stdout so we have something to restore to</span>
<a name="l00257"></a>00257     hStdIn  = _dup(_fileno(stdin));
<a name="l00258"></a>00258     hStdOut = _dup(_fileno(stdout));
<a name="l00259"></a>00259 
<a name="l00260"></a>00260     <span class="comment">// Child is to read from read end of pipe</span>
<a name="l00261"></a>00261     <span class="keywordflow">if</span> (_dup2(data[0], _fileno(stdin)) != 0) {
<a name="l00262"></a>00262         cerr &lt;&lt; <span class="stringliteral">&quot;dup of child stdin failed&quot;</span> &lt;&lt; endl;
<a name="l00263"></a>00263         <span class="keywordflow">return</span> NULL;
<a name="l00264"></a>00264     }
<a name="l00265"></a>00265     <span class="comment">// Child is to write its&#39;s stdout to file</span>
<a name="l00266"></a>00266     <span class="keywordflow">if</span> (_dup2(_fileno(output), _fileno(stdout)) != 0) {
<a name="l00267"></a>00267         cerr &lt;&lt; <span class="stringliteral">&quot;dup of child stdout failed&quot;</span> &lt;&lt; endl;
<a name="l00268"></a>00268         <span class="keywordflow">return</span> NULL;
<a name="l00269"></a>00269     }
<a name="l00270"></a>00270 
<a name="l00271"></a>00271     <span class="comment">// Spawn child process</span>
<a name="l00272"></a>00272     <span class="keywordtype">string</span> deflate = <span class="stringliteral">&quot;deflate.exe&quot;</span>;
<a name="l00273"></a>00273     <span class="keywordflow">if</span> ((pid = _spawnlp(_P_NOWAIT, deflate.c_str(), deflate.c_str(),
<a name="l00274"></a>00274                         <span class="stringliteral">&quot;-c&quot;</span>, <span class="stringliteral">&quot;5&quot;</span>, <span class="stringliteral">&quot;-s&quot;</span>, NULL)) &lt; 0) {
<a name="l00275"></a>00275         cerr &lt;&lt; <span class="stringliteral">&quot;Could not spawn to create compressor process&quot;</span> &lt;&lt; endl;
<a name="l00276"></a>00276         <span class="keywordflow">return</span> NULL;
<a name="l00277"></a>00277     }
<a name="l00278"></a>00278 
<a name="l00279"></a>00279     <span class="comment">// Restore stdin, stdout for parent and close duplicate copies</span>
<a name="l00280"></a>00280     <span class="keywordflow">if</span> (_dup2(hStdIn, _fileno(stdin)) != 0) {
<a name="l00281"></a>00281         cerr &lt;&lt; <span class="stringliteral">&quot;dup of stdin failed&quot;</span> &lt;&lt; endl;
<a name="l00282"></a>00282         <span class="keywordflow">return</span> NULL;
<a name="l00283"></a>00283     }
<a name="l00284"></a>00284     <span class="keywordflow">if</span> (_dup2(hStdOut, _fileno(stdout)) != 0) {
<a name="l00285"></a>00285         cerr &lt;&lt; <span class="stringliteral">&quot;dup of stdout failed&quot;</span> &lt;&lt; endl;
<a name="l00286"></a>00286         <span class="keywordflow">return</span> NULL;
<a name="l00287"></a>00287     }
<a name="l00288"></a>00288     close(hStdIn);
<a name="l00289"></a>00289     close(hStdOut);
<a name="l00290"></a>00290 
<a name="l00291"></a>00291     <span class="comment">// Tell the parent that it reads from the opposite end of the</span>
<a name="l00292"></a>00292     <span class="comment">// place where the child writes.</span>
<a name="l00293"></a>00293     close(data[0]);
<a name="l00294"></a>00294     FILE *input = fdopen(data[1], <span class="stringliteral">&quot;w&quot;</span>);
<a name="l00295"></a>00295     setbuf(input, 0);
<a name="l00296"></a>00296     childpid = pid;
<a name="l00297"></a>00297     <span class="keywordflow">return</span> input;
<a name="l00298"></a>00298 
<a name="l00299"></a>00299 <span class="preprocessor">#else</span>
<a name="l00300"></a>00300 <span class="preprocessor"></span>    FILE *ret_file = NULL ;
<a name="l00301"></a>00301 
<a name="l00302"></a>00302     <span class="keywordtype">int</span> pid, data[2];
<a name="l00303"></a>00303 
<a name="l00304"></a>00304     <span class="keywordflow">if</span> (pipe(data) &lt; 0) {
<a name="l00305"></a>00305         cerr &lt;&lt; <span class="stringliteral">&quot;Could not create IPC channel for compressor process&quot;</span>
<a name="l00306"></a>00306         &lt;&lt; endl;
<a name="l00307"></a>00307         <span class="keywordflow">return</span> NULL;
<a name="l00308"></a>00308     }
<a name="l00309"></a>00309 
<a name="l00310"></a>00310     <span class="keywordflow">if</span> ((pid = fork()) &lt; 0) {
<a name="l00311"></a>00311         cerr &lt;&lt; <span class="stringliteral">&quot;Could not fork to create compressor process&quot;</span> &lt;&lt; endl;
<a name="l00312"></a>00312         <span class="keywordflow">return</span> NULL;
<a name="l00313"></a>00313     }
<a name="l00314"></a>00314 
<a name="l00315"></a>00315     <span class="comment">// The parent process closes the write end of the Pipe, and creates a</span>
<a name="l00316"></a>00316     <span class="comment">// FILE * using fdopen(). The FILE * is used by the calling program to</span>
<a name="l00317"></a>00317     <span class="comment">// access the read end of the Pipe.</span>
<a name="l00318"></a>00318 
<a name="l00319"></a>00319     <span class="keywordflow">if</span> (pid &gt; 0) {   <span class="comment">// Parent, pid is that of the child</span>
<a name="l00320"></a>00320         close(data[0]);
<a name="l00321"></a>00321         ret_file = fdopen(data[1], <span class="stringliteral">&quot;w&quot;</span>);
<a name="l00322"></a>00322         setbuf(ret_file, 0);
<a name="l00323"></a>00323         childpid = pid;
<a name="l00324"></a>00324     }
<a name="l00325"></a>00325     <span class="keywordflow">else</span> {   <span class="comment">// Child</span>
<a name="l00326"></a>00326         close(data[1]);
<a name="l00327"></a>00327         dup2(data[0], 0); <span class="comment">// Read from the pipe...</span>
<a name="l00328"></a>00328         dup2(fileno(output), 1); <span class="comment">// Write to the FILE *output.</span>
<a name="l00329"></a>00329 
<a name="l00330"></a>00330         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Opening compression stream.&quot;</span> &lt;&lt; endl);
<a name="l00331"></a>00331 
<a name="l00332"></a>00332         <span class="comment">// First try to run deflate using DODS_ROOT (the value read from the</span>
<a name="l00333"></a>00333         <span class="comment">// DODS_ROOT environment variable takes precedence over the value set</span>
<a name="l00334"></a>00334         <span class="comment">// at build time. If that fails, try the CWD.</span>
<a name="l00335"></a>00335         <span class="keywordtype">string</span> deflate = (string)<a class="code" href="namespacelibdap.html#aa410c0b58527efed5753f9f2da1b9775">libdap_root</a>() + <span class="stringliteral">&quot;/sbin/deflate&quot;</span>;
<a name="l00336"></a>00336         (void) execl(deflate.c_str(), <span class="stringliteral">&quot;deflate&quot;</span>, <span class="stringliteral">&quot;-c&quot;</span>,  <span class="stringliteral">&quot;5&quot;</span>, <span class="stringliteral">&quot;-s&quot;</span>, NULL);
<a name="l00337"></a>00337         (void) execl(<span class="stringliteral">&quot;./deflate&quot;</span>, <span class="stringliteral">&quot;deflate&quot;</span>, <span class="stringliteral">&quot;-c&quot;</span>,  <span class="stringliteral">&quot;5&quot;</span>, <span class="stringliteral">&quot;-s&quot;</span>, NULL);
<a name="l00338"></a>00338         cerr &lt;&lt; <span class="stringliteral">&quot;Warning: Could not start compressor!&quot;</span> &lt;&lt; endl;
<a name="l00339"></a>00339         cerr &lt;&lt; <span class="stringliteral">&quot;defalte should be in DODS_ROOT/etc or in the CWD!&quot;</span>
<a name="l00340"></a>00340         &lt;&lt; endl;
<a name="l00341"></a>00341         _exit(127);  <span class="comment">// Only here if an error occurred.</span>
<a name="l00342"></a>00342     }
<a name="l00343"></a>00343 
<a name="l00344"></a>00344     <span class="keywordflow">return</span> ret_file ;
<a name="l00345"></a>00345 <span class="preprocessor">#endif</span>
<a name="l00346"></a>00346 <span class="preprocessor"></span>}
<a name="l00347"></a>00347 
<a name="l00348"></a>00348 <span class="preprocessor">#endif // COMPRESSION_FOR_SERVER3</span>
<a name="l00349"></a>00349 <span class="preprocessor"></span>
<a name="l00350"></a>00350 <span class="comment">// This function returns a pointer to the system time formated for an httpd</span>
<a name="l00351"></a>00351 <span class="comment">// log file.</span>
<a name="l00352"></a>00352 
<a name="l00353"></a>00353 <span class="keywordtype">string</span>
<a name="l00354"></a><a class="code" href="namespacelibdap.html#aa865553d4933d667330f305dcb2564b0">00354</a> <a class="code" href="namespacelibdap.html#aa865553d4933d667330f305dcb2564b0">systime</a>()
<a name="l00355"></a>00355 {
<a name="l00356"></a>00356     time_t TimBin;
<a name="l00357"></a>00357 
<a name="l00358"></a>00358     <span class="keywordflow">if</span> (time(&amp;TimBin) == (time_t) - 1)
<a name="l00359"></a>00359         <span class="keywordflow">return</span> string(<span class="stringliteral">&quot;time() error&quot;</span>);
<a name="l00360"></a>00360     <span class="keywordflow">else</span> {
<a name="l00361"></a>00361         <span class="keywordtype">string</span> TimStr = ctime(&amp;TimBin);
<a name="l00362"></a>00362         <span class="keywordflow">return</span> TimStr.substr(0, TimStr.size() - 2); <span class="comment">// remove the \n</span>
<a name="l00363"></a>00363     }
<a name="l00364"></a>00364 }
<a name="l00365"></a>00365 
<a name="l00366"></a>00366 <span class="keywordtype">void</span>
<a name="l00367"></a><a class="code" href="namespacelibdap.html#a7edce266869606fc7bb46124f8d63809">00367</a> <a class="code" href="namespacelibdap.html#a7edce266869606fc7bb46124f8d63809">downcase</a>(<span class="keywordtype">string</span> &amp;s)
<a name="l00368"></a>00368 {
<a name="l00369"></a>00369     <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; s.length(); i++)
<a name="l00370"></a>00370         s[i] = tolower(s[i]);
<a name="l00371"></a>00371 }
<a name="l00372"></a>00372 
<a name="l00373"></a>00373 <span class="keywordtype">bool</span>
<a name="l00374"></a><a class="code" href="namespacelibdap.html#af5f0e11f33c1eb25b831c02d65b2a9ee">00374</a> <a class="code" href="namespacelibdap.html#af5f0e11f33c1eb25b831c02d65b2a9ee">is_quoted</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;s)
<a name="l00375"></a>00375 {
<a name="l00376"></a>00376     <span class="keywordflow">return</span> (!s.empty() &amp;&amp; s[0] == <span class="charliteral">&#39;\&quot;&#39;</span> &amp;&amp; s[s.length()-1] == <span class="charliteral">&#39;\&quot;&#39;</span>);
<a name="l00377"></a>00377 }
<a name="l00378"></a>00378 
<a name="l00379"></a>00379 <span class="keywordtype">string</span>
<a name="l00380"></a><a class="code" href="namespacelibdap.html#a37a24fcba1630ac658ec4c8a902dd92c">00380</a> <a class="code" href="namespacelibdap.html#a37a24fcba1630ac658ec4c8a902dd92c">remove_quotes</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;s)
<a name="l00381"></a>00381 {
<a name="l00382"></a>00382     <span class="keywordflow">if</span> (<a class="code" href="namespacelibdap.html#af5f0e11f33c1eb25b831c02d65b2a9ee">is_quoted</a>(s))
<a name="l00383"></a>00383         <span class="keywordflow">return</span> s.substr(1, s.length() - 2);
<a name="l00384"></a>00384     <span class="keywordflow">else</span>
<a name="l00385"></a>00385         <span class="keywordflow">return</span> s;
<a name="l00386"></a>00386 }
<a name="l00387"></a>00387 
<a name="l00388"></a>00388 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00389"></a>00389 <span class="preprocessor"></span><span class="comment">//  Sometimes need to buffer within an iostream under win32 when</span>
<a name="l00390"></a>00390 <span class="comment">//  we want the output to go to a FILE *.  This is because</span>
<a name="l00391"></a>00391 <span class="comment">//  it&#39;s not possible to associate an ofstream with a FILE *</span>
<a name="l00392"></a>00392 <span class="comment">//  under the Standard ANSI C++ Library spec.  Unix systems</span>
<a name="l00393"></a>00393 <span class="comment">//  don&#39;t follow the spec in this regard.</span>
<a name="l00394"></a>00394 <span class="keywordtype">void</span> flush_stream(iostream ios, FILE *out)
<a name="l00395"></a>00395 {
<a name="l00396"></a>00396     <span class="keywordtype">int</span> nbytes;
<a name="l00397"></a>00397     <span class="keywordtype">char</span> buffer[512];
<a name="l00398"></a>00398 
<a name="l00399"></a>00399     ios.get(buffer, 512, NULL);
<a name="l00400"></a>00400     <span class="keywordflow">while</span> ((nbytes = ios.gcount()) &gt; 0) {
<a name="l00401"></a>00401         fwrite(buffer, 1, nbytes, out);
<a name="l00402"></a>00402         ios.get(buffer, 512, NULL);
<a name="l00403"></a>00403     }
<a name="l00404"></a>00404 
<a name="l00405"></a>00405     <span class="keywordflow">return</span>;
<a name="l00406"></a>00406 }
<a name="l00407"></a>00407 <span class="preprocessor">#endif</span>
<a name="l00408"></a>00408 <span class="preprocessor"></span>
<a name="l00409"></a>00409 <span class="comment">// Jose Garcia</span>
<a name="l00410"></a>00410 <span class="keywordtype">void</span>
<a name="l00411"></a><a class="code" href="namespacelibdap.html#a576580781bca7dcd4b5a84cc06892aff">00411</a> <a class="code" href="namespacelibdap.html#a576580781bca7dcd4b5a84cc06892aff">append_long_to_string</a>(<span class="keywordtype">long</span> val, <span class="keywordtype">int</span> base, <span class="keywordtype">string</span> &amp;str_val)
<a name="l00412"></a>00412 {
<a name="l00413"></a>00413     <span class="comment">// The array digits contains 36 elements which are the</span>
<a name="l00414"></a>00414     <span class="comment">// posible valid digits for out bases in the range</span>
<a name="l00415"></a>00415     <span class="comment">// [2,36]</span>
<a name="l00416"></a>00416     <span class="keywordtype">char</span> digits[] = <span class="stringliteral">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;
<a name="l00417"></a>00417     <span class="comment">// result of val / base</span>
<a name="l00418"></a>00418     ldiv_t r;
<a name="l00419"></a>00419 
<a name="l00420"></a>00420     <span class="keywordflow">if</span> (base &gt; 36 || base &lt; 2) {
<a name="l00421"></a>00421         <span class="comment">// no conversion if wrong base</span>
<a name="l00422"></a>00422         std::invalid_argument ex(<span class="stringliteral">&quot;The parameter base has an invalid value.&quot;</span>);
<a name="l00423"></a>00423         <span class="keywordflow">throw</span> ex;
<a name="l00424"></a>00424     }
<a name="l00425"></a>00425     <span class="keywordflow">if</span> (val &lt; 0)
<a name="l00426"></a>00426         str_val += <span class="charliteral">&#39;-&#39;</span>;
<a name="l00427"></a>00427     r = ldiv(labs(val), base);
<a name="l00428"></a>00428 
<a name="l00429"></a>00429     <span class="comment">// output digits of val/base first</span>
<a name="l00430"></a>00430     <span class="keywordflow">if</span> (r.quot &gt; 0)
<a name="l00431"></a>00431         <a class="code" href="namespacelibdap.html#a576580781bca7dcd4b5a84cc06892aff">append_long_to_string</a>(r.quot, base, str_val);
<a name="l00432"></a>00432 
<a name="l00433"></a>00433     <span class="comment">// output last digit</span>
<a name="l00434"></a>00434 
<a name="l00435"></a>00435     str_val += digits[(int)r.rem];
<a name="l00436"></a>00436 }
<a name="l00437"></a>00437 
<a name="l00438"></a>00438 <span class="comment">// base defaults to 10</span>
<a name="l00439"></a>00439 <span class="keywordtype">string</span>
<a name="l00440"></a><a class="code" href="namespacelibdap.html#a317edea830aab50d2137a5bd313cc86a">00440</a> <a class="code" href="namespacelibdap.html#a317edea830aab50d2137a5bd313cc86a">long_to_string</a>(<span class="keywordtype">long</span> val, <span class="keywordtype">int</span> base)
<a name="l00441"></a>00441 {
<a name="l00442"></a>00442     <span class="keywordtype">string</span> s;
<a name="l00443"></a>00443     <a class="code" href="namespacelibdap.html#a576580781bca7dcd4b5a84cc06892aff">append_long_to_string</a>(val, base, s);
<a name="l00444"></a>00444     <span class="keywordflow">return</span> s;
<a name="l00445"></a>00445 }
<a name="l00446"></a>00446 
<a name="l00447"></a>00447 <span class="comment">// Jose Garcia</span>
<a name="l00448"></a><a class="code" href="namespacelibdap.html#a385e0e3c04caef28126afb481f4867dd">00448</a> <span class="keywordtype">void</span> <a class="code" href="namespacelibdap.html#a385e0e3c04caef28126afb481f4867dd">append_double_to_string</a>(<span class="keyword">const</span> <span class="keywordtype">double</span> &amp;num, <span class="keywordtype">string</span> &amp;str)
<a name="l00449"></a>00449 {
<a name="l00450"></a>00450     <span class="comment">// s having 100 characters should be enough for sprintf to do its job.</span>
<a name="l00451"></a>00451     <span class="comment">// I want to banish all instances of sprintf. 10/5/2001 jhrg</span>
<a name="l00452"></a>00452     ostringstream oss;
<a name="l00453"></a>00453     oss.precision(9);
<a name="l00454"></a>00454     oss &lt;&lt; num;
<a name="l00455"></a>00455     str += oss.str();
<a name="l00456"></a>00456 }
<a name="l00457"></a>00457 
<a name="l00458"></a>00458 <span class="keywordtype">string</span>
<a name="l00459"></a><a class="code" href="namespacelibdap.html#a7c3f898946a0ae498f4ca830731cc474">00459</a> <a class="code" href="namespacelibdap.html#a7c3f898946a0ae498f4ca830731cc474">double_to_string</a>(<span class="keyword">const</span> <span class="keywordtype">double</span> &amp;num)
<a name="l00460"></a>00460 {
<a name="l00461"></a>00461     <span class="keywordtype">string</span> s;
<a name="l00462"></a>00462     <a class="code" href="namespacelibdap.html#a385e0e3c04caef28126afb481f4867dd">append_double_to_string</a>(num, s);
<a name="l00463"></a>00463     <span class="keywordflow">return</span> s;
<a name="l00464"></a>00464 }
<a name="l00465"></a>00465 
<a name="l00466"></a>00466 <span class="comment">// Get the version number of the core software. Defining this means that</span>
<a name="l00467"></a>00467 <span class="comment">// clients of the DAP don&#39;t have to rely on config.h for the version</span>
<a name="l00468"></a>00468 <span class="comment">// number.</span>
<a name="l00469"></a>00469 <span class="keywordtype">string</span>
<a name="l00470"></a><a class="code" href="namespacelibdap.html#affccb78f3a718e01f5fac17b682430d1">00470</a> <a class="code" href="namespacelibdap.html#affccb78f3a718e01f5fac17b682430d1">dap_version</a>()
<a name="l00471"></a>00471 {
<a name="l00472"></a>00472     <span class="keywordflow">return</span> (<span class="keywordtype">string</span>)<span class="stringliteral">&quot;OPeNDAP DAP/&quot;</span> + <a class="code" href="namespacelibdap.html#a3c2e481465a4567eaf6d1f22d84c0ef9">libdap_version</a>() + <span class="stringliteral">&quot;: compiled on &quot;</span> + __DATE__ + <span class="stringliteral">&quot;:&quot;</span> + __TIME__ ;
<a name="l00473"></a>00473 }
<a name="l00474"></a>00474 
<a name="l00475"></a>00475 <span class="comment">// Given a pathname, return the file at the end of the path. This is used</span>
<a name="l00476"></a>00476 <span class="comment">// when reporting errors (maybe other times, too) to keep the server from</span>
<a name="l00477"></a>00477 <span class="comment">// revealing too much about its organization when sending error responses</span>
<a name="l00478"></a>00478 <span class="comment">// back to clients. 10/11/2000 jhrg</span>
<a name="l00479"></a>00479 <span class="comment">// MT-safe. 08/05/02 jhrg</span>
<a name="l00480"></a>00480 
<a name="l00481"></a>00481 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00482"></a>00482 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> path_sep[] =
<a name="l00483"></a>00483     {<span class="stringliteral">&quot;\\&quot;</span>
<a name="l00484"></a>00484     };
<a name="l00485"></a>00485 <span class="preprocessor">#else</span>
<a name="l00486"></a>00486 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> path_sep[] =
<a name="l00487"></a>00487     {<span class="stringliteral">&quot;/&quot;</span>
<a name="l00488"></a>00488     };
<a name="l00489"></a>00489 <span class="preprocessor">#endif</span>
<a name="l00490"></a>00490 <span class="preprocessor"></span>
<a name="l00491"></a>00491 <span class="keywordtype">string</span>
<a name="l00492"></a><a class="code" href="namespacelibdap.html#a38d9163360ace25ebe5dec96b17039ac">00492</a> <a class="code" href="namespacelibdap.html#a38d9163360ace25ebe5dec96b17039ac">path_to_filename</a>(<span class="keywordtype">string</span> path)
<a name="l00493"></a>00493 {
<a name="l00494"></a>00494     string::size_type pos = path.rfind(path_sep);
<a name="l00495"></a>00495 
<a name="l00496"></a>00496     <span class="keywordflow">return</span> (pos == string::npos) ? path : path.substr(++pos);
<a name="l00497"></a>00497 }
<a name="l00498"></a>00498 
<a name="l00503"></a>00503 <span class="keywordtype">string</span>
<a name="l00504"></a><a class="code" href="namespacelibdap.html#aed6073fcecd8d82a616f4d2de9a8e591">00504</a> <a class="code" href="namespacelibdap.html#aed6073fcecd8d82a616f4d2de9a8e591">file_to_string</a>(FILE *fp)
<a name="l00505"></a>00505 {
<a name="l00506"></a>00506     rewind(fp);
<a name="l00507"></a>00507     ostringstream oss;
<a name="l00508"></a>00508     <span class="keywordtype">char</span> c;
<a name="l00509"></a>00509     <span class="keywordflow">while</span> (fread(&amp;c, 1, 1, fp))
<a name="l00510"></a>00510         oss &lt;&lt; c;
<a name="l00511"></a>00511     <span class="keywordflow">return</span> oss.str();
<a name="l00512"></a>00512 }
<a name="l00513"></a>00513 
<a name="l00516"></a>00516 
<a name="l00522"></a>00522 <span class="keywordtype">bool</span>
<a name="l00523"></a><a class="code" href="namespacelibdap.html#a36c4fc80766718669c9790c6cb20c895">00523</a> <a class="code" href="namespacelibdap.html#a36c4fc80766718669c9790c6cb20c895" title="sanitize the size of an array. Test for integer overflow when dynamically allocating an array...">size_ok</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sz, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nelem)
<a name="l00524"></a>00524 {
<a name="l00525"></a>00525     <span class="keywordflow">return</span> (sz &gt; 0 &amp;&amp; nelem &lt; UINT_MAX / sz);
<a name="l00526"></a>00526 }
<a name="l00527"></a>00527 
<a name="l00544"></a>00544 <span class="keywordtype">bool</span>
<a name="l00545"></a><a class="code" href="namespacelibdap.html#a7c34f89476954f3a2fd0eaa1d1e8cd32">00545</a> <a class="code" href="namespacelibdap.html#a7c34f89476954f3a2fd0eaa1d1e8cd32" title="Does the string name a potentailly valid pathname? Test the given pathname to verfiy that it is a val...">pathname_ok</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;path, <span class="keywordtype">bool</span> strict)
<a name="l00546"></a>00546 {
<a name="l00547"></a>00547     <span class="keywordflow">if</span> (path.length() &gt; 255)
<a name="l00548"></a>00548         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00549"></a>00549 
<a name="l00550"></a>00550     Regex name(<span class="stringliteral">&quot;[-0-9A-z_./]+&quot;</span>);
<a name="l00551"></a>00551     <span class="keywordflow">if</span> (!strict)
<a name="l00552"></a>00552         name = <span class="stringliteral">&quot;[:print:]+&quot;</span>;
<a name="l00553"></a>00553 
<a name="l00554"></a>00554     string::size_type len = path.length();
<a name="l00555"></a>00555     <span class="keywordtype">int</span> result = name.match(path.c_str(), len);
<a name="l00556"></a>00556     <span class="comment">// Protect against casting too big an uint to int</span>
<a name="l00557"></a>00557     <span class="comment">// if LEN is bigger than the max int32, the second test can&#39;t work</span>
<a name="l00558"></a>00558     <span class="keywordflow">if</span> (len &gt; INT_MAX || result != static_cast&lt;int&gt;(len))
<a name="l00559"></a>00559         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00560"></a>00560 
<a name="l00561"></a>00561     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00562"></a>00562 }
<a name="l00563"></a>00563 
<a name="l00565"></a>00565 
<a name="l00566"></a>00566 } <span class="comment">// namespace libdap</span>
<a name="l00567"></a>00567 
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="util_8cc.html">util.cc</a>      </li>
      <li class="footer">Generated on Tue Feb 8 2011 for libdap++ by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </li>
    </ul>
  </div>

</body>
</html>