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