Addressbook: /Users/engelen/Projects/gsoap/samples/databinding/address.cpp Source File
Main Page | Class List | File List | Class Members | File Members | Related Pages

/Users/engelen/Projects/gsoap/samples/databinding/address.cpp

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

Generated on Mon Nov 9 14:54:59 2009 for Addressbook by doxygen 1.3.8