Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e5b501e96823201f44cb057859a8bf79 > files > 208

gsoap-2.8.67-2.mga7.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Addressbook: /Users/engelen/Projects/gsoap/samples/databinding/address.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.8 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>/Users/engelen/Projects/gsoap/samples/databinding/address.cpp</h1><a href="address_8cpp.html">Go to the documentation of this file.</a><pre class="fragment"><div>00001 
00061 <span class="comment">/*</span>
00062 <span class="comment">--------------------------------------------------------------------------------</span>
00063 <span class="comment">gSOAP XML Web services tools</span>
00064 <span class="comment">Copyright (C) 2001-2009, Robert van Engelen, Genivia, Inc. All Rights Reserved.</span>
00065 <span class="comment">This software is released under one of the following two licenses:</span>
00066 <span class="comment">GPL or Genivia's license for commercial use.</span>
00067 <span class="comment">--------------------------------------------------------------------------------</span>
00068 <span class="comment">GPL license.</span>
00069 <span class="comment"></span>
00070 <span class="comment">This program is free software; you can redistribute it and/or modify it under</span>
00071 <span class="comment">the terms of the GNU General Public License as published by the Free Software</span>
00072 <span class="comment">Foundation; either version 2 of the License, or (at your option) any later</span>
00073 <span class="comment">version.</span>
00074 <span class="comment"></span>
00075 <span class="comment">This program is distributed in the hope that it will be useful, but WITHOUT ANY</span>
00076 <span class="comment">WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A</span>
00077 <span class="comment">PARTICULAR PURPOSE. See the GNU General Public License for more details.</span>
00078 <span class="comment"></span>
00079 <span class="comment">You should have received a copy of the GNU General Public License along with</span>
00080 <span class="comment">this program; if not, write to the Free Software Foundation, Inc., 59 Temple</span>
00081 <span class="comment">Place, Suite 330, Boston, MA 02111-1307 USA</span>
00082 <span class="comment"></span>
00083 <span class="comment">Author contact information:</span>
00084 <span class="comment">engelen@genivia.com / engelen@acm.org</span>
00085 <span class="comment">--------------------------------------------------------------------------------</span>
00086 <span class="comment">A commercial use license is available from Genivia, Inc., contact@genivia.com</span>
00087 <span class="comment">--------------------------------------------------------------------------------</span>
00088 <span class="comment">*/</span>
00089 
00090 <span class="preprocessor">#include &lt;iostream&gt;</span>
00091 <span class="preprocessor">#include &lt;fstream&gt;</span>
00092 
00093 <span class="preprocessor">#include "addressH.h"</span>   <span class="comment">// generated, also includes stdsoap2.h</span>
00094 <span class="preprocessor">#include "a.nsmap"</span>      <span class="comment">// generated</span>
00095 
00103 <span class="keywordtype">char</span> *<a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *prompt);
00104 
<a name="l00109"></a><a class="code" href="address_8cpp.html#a1">00109</a> <span class="keywordtype">int</span> <a class="code" href="address_8cpp.html#a1">main</a>()
00110 {
00111   <span class="comment">// New soap struct engine context</span>
00112   <span class="comment">// Use strict validation and indented canonicalized output</span>
00113   <span class="keyword">struct </span>soap *soap = soap_new1(SOAP_XML_STRICT | SOAP_XML_INDENT | SOAP_XML_CANONICAL);
00114 
00115   <a class="code" href="class__a____address__book.html">_a__address_book</a> *ab = soap_new__a__address_book(soap, -1);
00116 
00117   std::fstream fs;
00118 
00119   <span class="comment">// Read the address book from address.xml (defined by address.xsd)</span>
00120   fs.open(<span class="stringliteral">"address.xml"</span>, std::ios::in);
00121   <span class="keywordflow">if</span> (fs)
00122   {
00123     soap-&gt;is = &amp;fs;
00124     <span class="keywordflow">if</span> (soap_read__a__address_book(soap, ab) != SOAP_OK)
00125     { 
00126       std::cerr &lt;&lt; <span class="stringliteral">"Error reading address.xml file"</span> &lt;&lt; std::endl;
00127       soap_stream_fault(soap, std::cerr);
00128       exit(1);
00129     }
00130     fs.close();
00131   }
00132 
00133   <span class="comment">// Display the address book content</span>
00134   std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">"ADDRESS BOOK - An Example XML Data Binding Application"</span> &lt;&lt; std::endl &lt;&lt; std::endl;
00135   <span class="keywordflow">for</span> (std::vector&lt;a__address*&gt;::const_iterator i = ab-&gt;<a class="code" href="class__a____address__book.html#o0">address</a>.begin(); i != ab-&gt;<a class="code" href="class__a____address__book.html#o0">address</a>.end(); ++i)
00136   {
00137     <span class="keywordflow">if</span> (*i)
00138     {
00139       std::cout &lt;&lt; <span class="stringliteral">"Address entry "</span> &lt;&lt; (*i)-&gt;ID &lt;&lt; std::endl;
00140       std::cout &lt;&lt; <span class="stringliteral">"Name:    "</span> &lt;&lt; (*i)-&gt;name &lt;&lt; std::endl;
00141       std::cout &lt;&lt; <span class="stringliteral">"Street:  "</span> &lt;&lt; (*i)-&gt;street &lt;&lt; std::endl;
00142       std::cout &lt;&lt; <span class="stringliteral">"City:    "</span> &lt;&lt; (*i)-&gt;city &lt;&lt; std::endl;
00143       std::cout &lt;&lt; <span class="stringliteral">"Zip:     "</span> &lt;&lt; (*i)-&gt;zip &lt;&lt; std::endl;
00144       <span class="comment">// Advanced level: we use the soapcpp2-generated soap_a__ISO_country2s()</span>
00145       <span class="comment">// function to convert enum a__ISO_country values to strings. The strings</span>
00146       <span class="comment">// are allocated in the gSOAP engine and deleted with soap_end()</span>
00147       std::cout &lt;&lt; <span class="stringliteral">"Country: "</span> &lt;&lt; soap_a__ISO_country2s(soap, (*i)-&gt;country) &lt;&lt;
00148       std::endl;
00149       <span class="keywordflow">if</span> ((*i)-&gt;phone)
00150         std::cout &lt;&lt; <span class="stringliteral">"Phone:   "</span> &lt;&lt; *(*i)-&gt;phone &lt;&lt; std::endl;
00151       <span class="keywordflow">if</span> ((*i)-&gt;mobile)
00152         std::cout &lt;&lt; <span class="stringliteral">"Mobile:  "</span> &lt;&lt; *(*i)-&gt;mobile &lt;&lt; std::endl;
00153       <span class="comment">// Advanced level: use the soap_dateTime2s() from the stdsoap2.cpp engine</span>
00154       <span class="keywordflow">if</span> ((*i)-&gt;dob)
00155         std::cout &lt;&lt; <span class="stringliteral">"DOB:     "</span> &lt;&lt; soap_dateTime2s(soap, *(*i)-&gt;dob) &lt;&lt; std::endl;
00156       std::cout &lt;&lt; <span class="stringliteral">"---------"</span> &lt;&lt; std::endl;
00157     }
00158   }
00159 
00160   <span class="comment">// Allocate a new address in the gSOAP engine's data space</span>
00161   <a class="code" href="classa____address.html">a__address</a> *a = soap_new_a__address(soap, -1);
00162   <span class="comment">// Set object's default values (soap_default is generated)</span>
00163   a-&gt;soap_default(soap);
00164 
00165   a-&gt;<a class="code" href="classa____address.html#o8">ID</a> = ab-&gt;<a class="code" href="class__a____address__book.html#o0">address</a>.size() + 1;
00166 
00167   std::cout &lt;&lt; <span class="stringliteral">"Enter a new contact:"</span> &lt;&lt; std::endl;
00168   a-&gt;<a class="code" href="classa____address.html#o0">name</a> = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Name"</span>);
00169   a-&gt;<a class="code" href="classa____address.html#o1">street</a> = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Street"</span>);
00170   a-&gt;<a class="code" href="classa____address.html#o2">city</a> = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"City"</span>);
00171   a-&gt;<a class="code" href="classa____address.html#o3">zip</a> = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Zip"</span>);
00172   <span class="keywordtype">char</span> *s = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Country"</span>);
00173   <span class="comment">// Advanced level: use the generated s2a__ISO_country() to convert string to</span>
00174   <span class="comment">// enum constant</span>
00175   <span class="keywordflow">if</span> (soap_s2a__ISO_country(soap, s, &amp;a-&gt;<a class="code" href="classa____address.html#o4">country</a>) != SOAP_OK)
00176     std::cerr &lt;&lt; <span class="stringliteral">"Not a valid country code"</span> &lt;&lt; std::endl;
00177   <span class="keywordflow">if</span> (*(s = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Phone"</span>)))
00178   {
00179     <span class="comment">// Allocate string in engine's data space:</span>
00180     a-&gt;<a class="code" href="classa____address.html#o5">phone</a> = soap_new_std__string(soap, -1);
00181     *a-&gt;<a class="code" href="classa____address.html#o5">phone</a> = s;
00182   } 
00183   <span class="keywordflow">if</span> (*(s = <a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="stringliteral">"Mobile"</span>)))
00184   {
00185     <span class="comment">// Allocate string in engine's data space:</span>
00186     a-&gt;<a class="code" href="classa____address.html#o6">mobile</a> = soap_new_std__string(soap, -1);
00187     *a-&gt;<a class="code" href="classa____address.html#o6">mobile</a> = s;
00188   } 
00189 
00190   <span class="comment">// Add contact to address book</span>
00191   ab-&gt;<a class="code" href="class__a____address__book.html#o0">address</a>.push_back(a);
00192 
00193   std::cout &lt;&lt; std::endl &lt;&lt; <span class="stringliteral">"Contact information added."</span> &lt;&lt; std::endl;
00194 
00195   <span class="comment">// Save updated address book to address.xml</span>
00196   fs.open(<span class="stringliteral">"address.xml"</span>, std::ios::out);
00197   <span class="keywordflow">if</span> (!fs)
00198   {
00199     std::cerr &lt;&lt; <span class="stringliteral">"Cannot create address.xml file"</span> &lt;&lt; std::endl;
00200     exit(1);
00201   }
00202   soap-&gt;os = &amp;fs;
00203   <span class="keywordflow">if</span> (soap_write__a__address_book(soap, ab) != SOAP_OK)
00204   {
00205     std::cerr &lt;&lt; <span class="stringliteral">"Error writing address.xml file"</span> &lt;&lt; std::endl;
00206     soap_stream_fault(soap, std::cerr);
00207     exit(1);
00208   }
00209   fs.close();
00210 
00211   <span class="comment">// Delete instances</span>
00212   soap_destroy(soap);
00213   <span class="comment">// Delete data</span>
00214   soap_end(soap);
00215   <span class="comment">// Free soap struct engine context</span>
00216   soap_free(soap);
00217 
00218   <span class="keywordflow">return</span> 0;
00219 }
00220 
<a name="l00221"></a><a class="code" href="address_8cpp.html#a0">00221</a> <span class="keywordtype">char</span> *<a class="code" href="address_8cpp.html#a0">user_input</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *prompt)
00222 {
00223   <span class="keyword">static</span> <span class="keywordtype">char</span> buf[80];
00224   <span class="keywordtype">char</span> *s;
00225 
00226   printf(<span class="stringliteral">"%-9s&gt; "</span>, prompt);
00227   fgets(buf, 80, stdin);
00228 
00229   <span class="comment">// Strip trailing space</span>
00230   <span class="keywordflow">for</span> (s = buf + strlen(buf) - 1; s &gt; buf; s--)
00231   {
00232     <span class="keywordflow">if</span> (*s &gt; <span class="charliteral">' '</span>)
00233       <span class="keywordflow">break</span>;
00234   }
00235   s[1] = <span class="charliteral">'\0'</span>;
00236 
00237   <span class="comment">// Strip leading space</span>
00238   <span class="keywordflow">for</span> (s = buf; *s; s++)
00239   {
00240     <span class="keywordflow">if</span> (*s &gt; <span class="charliteral">' '</span>)
00241       <span class="keywordflow">break</span>;
00242   }
00243 
00244   <span class="keywordflow">return</span> s;
00245 }
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Mon Nov 9 14:54:59 2009 for Addressbook by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.8 </small></address>
</body>
</html>