<!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 Page</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related 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 <iostream></span> 00091 <span class="preprocessor">#include <fstream></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->is = &fs; 00124 <span class="keywordflow">if</span> (soap_read__a__address_book(soap, ab) != SOAP_OK) 00125 { 00126 std::cerr << <span class="stringliteral">"Error reading address.xml file"</span> << 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 << std::endl << <span class="stringliteral">"ADDRESS BOOK - An Example XML Data Binding Application"</span> << std::endl << std::endl; 00135 <span class="keywordflow">for</span> (std::vector<a__address*>::const_iterator i = ab-><a class="code" href="class__a____address__book.html#o0">address</a>.begin(); i != ab-><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 << <span class="stringliteral">"Address entry "</span> << (*i)->ID << std::endl; 00140 std::cout << <span class="stringliteral">"Name: "</span> << (*i)->name << std::endl; 00141 std::cout << <span class="stringliteral">"Street: "</span> << (*i)->street << std::endl; 00142 std::cout << <span class="stringliteral">"City: "</span> << (*i)->city << std::endl; 00143 std::cout << <span class="stringliteral">"Zip: "</span> << (*i)->zip << 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 << <span class="stringliteral">"Country: "</span> << soap_a__ISO_country2s(soap, (*i)->country) << 00148 std::endl; 00149 <span class="keywordflow">if</span> ((*i)->phone) 00150 std::cout << <span class="stringliteral">"Phone: "</span> << *(*i)->phone << std::endl; 00151 <span class="keywordflow">if</span> ((*i)->mobile) 00152 std::cout << <span class="stringliteral">"Mobile: "</span> << *(*i)->mobile << 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)->dob) 00155 std::cout << <span class="stringliteral">"DOB: "</span> << soap_dateTime2s(soap, *(*i)->dob) << std::endl; 00156 std::cout << <span class="stringliteral">"---------"</span> << 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->soap_default(soap); 00164 00165 a-><a class="code" href="classa____address.html#o8">ID</a> = ab-><a class="code" href="class__a____address__book.html#o0">address</a>.size() + 1; 00166 00167 std::cout << <span class="stringliteral">"Enter a new contact:"</span> << std::endl; 00168 a-><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-><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-><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-><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, &a-><a class="code" href="classa____address.html#o4">country</a>) != SOAP_OK) 00176 std::cerr << <span class="stringliteral">"Not a valid country code"</span> << 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-><a class="code" href="classa____address.html#o5">phone</a> = soap_new_std__string(soap, -1); 00181 *a-><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-><a class="code" href="classa____address.html#o6">mobile</a> = soap_new_std__string(soap, -1); 00187 *a-><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-><a class="code" href="class__a____address__book.html#o0">address</a>.push_back(a); 00192 00193 std::cout << std::endl << <span class="stringliteral">"Contact information added."</span> << 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 << <span class="stringliteral">"Cannot create address.xml file"</span> << std::endl; 00200 exit(1); 00201 } 00202 soap->os = &fs; 00203 <span class="keywordflow">if</span> (soap_write__a__address_book(soap, ab) != SOAP_OK) 00204 { 00205 std::cerr << <span class="stringliteral">"Error writing address.xml file"</span> << 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> "</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 > buf; s--) 00231 { 00232 <span class="keywordflow">if</span> (*s > <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 > <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>