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

Generated on Sat Nov 7 13:49:55 2009 for Addressbook by doxygen 1.3.8