Genivia Home Documentation
address.cpp Source File

updated Mon Jan 22 2024 by Robert van Engelen
 
address.cpp
Go to the documentation of this file.
1
31/*
32--------------------------------------------------------------------------------
33gSOAP XML Web services tools
34Copyright (C) 2000-2015, Robert van Engelen, Genivia, Inc. All Rights Reserved.
35This software is released under one of the following two licenses:
36GPL.
37--------------------------------------------------------------------------------
38GPL license.
39
40This program is free software; you can redistribute it and/or modify it under
41the terms of the GNU General Public License as published by the Free Software
42Foundation; either version 2 of the License, or (at your option) any later
43version.
44
45This program is distributed in the hope that it will be useful, but WITHOUT ANY
46WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
47PARTICULAR PURPOSE. See the GNU General Public License for more details.
48
49You should have received a copy of the GNU General Public License along with
50this program; if not, write to the Free Software Foundation, Inc., 59 Temple
51Place, Suite 330, Boston, MA 02111-1307 USA
52
53Author contact information:
54engelen@genivia.com / engelen@acm.org
55--------------------------------------------------------------------------------
56A commercial use license is available from Genivia, Inc., contact@genivia.com
57--------------------------------------------------------------------------------
58*/
59
60int main();
61
62#include <iostream>
63#include <fstream>
64
65#include "addressH.h" // generated, also includes stdsoap2.h
66#include "a.nsmap" // generated
67
75char *user_input(const char *prompt);
76
81int main()
82{
83 // New soap struct engine context
84 // Use strict validation and indented canonicalized output
85 struct soap *soap = soap_new1(SOAP_XML_STRICT | SOAP_XML_INDENT | SOAP_XML_NOTYPE);
86
88
89 std::fstream fs;
90
91 // Read the address book from address.xml (defined by address.xsd)
92 fs.open("address.xml", std::ios::in);
93 if (fs)
94 {
95 soap->is = &fs;
96 if (soap_read__a__address_book(soap, ab) != SOAP_OK)
97 {
98 std::cerr << "Error reading address.xml file" << std::endl;
99 soap_stream_fault(soap, std::cerr);
100 exit(1);
101 }
102 fs.close();
103 }
104
105 // Display the address book content
106 std::cout << std::endl << "ADDRESS BOOK - An Example XML Data Binding Application" << std::endl << std::endl;
107 for (std::vector<a__address*>::const_iterator i = ab->address.begin(); i != ab->address.end(); ++i)
108 {
109 if (*i)
110 {
111 std::cout << "Address entry " << (*i)->ID << std::endl;
112 std::cout << "Name: " << (*i)->name << std::endl;
113 std::cout << "Street: " << (*i)->street << std::endl;
114 std::cout << "City: " << (*i)->city << std::endl;
115 std::cout << "Zip: " << (*i)->zip << std::endl;
116 // Advanced level: we use the soapcpp2-generated soap_a__ISO_country2s()
117 // function to convert enum a__ISO_country values to strings. The strings
118 // are allocated in the gSOAP engine and deleted with soap_end()
119 std::cout << "Country: " << soap_a__ISO_country2s(soap, (*i)->country) <<
120 std::endl;
121 if ((*i)->phone)
122 std::cout << "Phone: " << *(*i)->phone << std::endl;
123 if ((*i)->mobile)
124 std::cout << "Mobile: " << *(*i)->mobile << std::endl;
125 // Advanced level: use the soap_dateTime2s() from the stdsoap2.cpp engine
126 if ((*i)->dob)
127 std::cout << "DOB: " << soap_dateTime2s(soap, *(*i)->dob) << std::endl;
128 std::cout << "---------" << std::endl;
129 }
130 }
131
132 // Allocate a new address in the gSOAP engine's data space
133 a__address *a = soap_new_a__address(soap, -1);
134 // Set object's default values (soap_default is generated)
135 a->soap_default(soap);
136
137 a->ID = ab->address.size() + 1;
138
139 std::cout << "Enter a new contact:" << std::endl;
140 a->name = user_input("Name");
141 a->street = user_input("Street");
142 a->city = user_input("City");
143 a->zip = user_input("Zip");
144 char *s;
145 do {
146 s = user_input("Country");
147 // Advanced level: use the generated s2a__ISO_country() to convert string to
148 // enum constant
149 soap->error = SOAP_OK;
150 if (soap_s2a__ISO_country(soap, s, &a->country) != SOAP_OK)
151 std::cerr << "Not a valid country code" << std::endl;
152 }
153 while (soap->error);
154 if (*(s = user_input("Phone")))
155 {
156 // Allocate string in engine's data space:
157 a->phone = soap_new_std__string(soap, -1);
158 *a->phone = s;
159 }
160 if (*(s = user_input("Mobile")))
161 {
162 // Allocate string in engine's data space:
163 a->mobile = soap_new_std__string(soap, -1);
164 *a->mobile = s;
165 }
166 // change soap_s2dateTime to soap_xsd__dateTime when overriding the default xsd:dateTime to time_t mapping
167 if (*(s = user_input("DOB")))
168 if (soap_s2dateTime(soap, s, a->dob) != SOAP_OK)
169 std::cerr << "Not a valid ISO 8601 date time (ignored)" << std::endl;
170 soap->error = SOAP_OK;
171
172 // Add contact to address book
173 ab->address.push_back(a);
174
175 std::cout << std::endl << "Contact information added." << std::endl;
176
177 // Save updated address book to address.xml
178 fs.open("address.xml", std::ios::out);
179 if (!fs)
180 {
181 std::cerr << "Cannot create address.xml file" << std::endl;
182 exit(1);
183 }
184 soap->os = &fs;
185 if (soap_write__a__address_book(soap, ab) != SOAP_OK)
186 {
187 std::cerr << "Error writing address.xml file" << std::endl;
188 soap_stream_fault(soap, std::cerr);
189 exit(1);
190 }
191 fs.close();
192
193 // Delete instances
194 soap_destroy(soap);
195 // Delete data
196 soap_end(soap);
197 // Free soap struct engine context
198 soap_free(soap);
199
200 return 0;
201}
202
203char *user_input(const char *prompt)
204{
205 static char buf[80];
206 char *s;
207
208 printf("%-9s> ", prompt);
209 fgets(buf, 80, stdin);
210
211 // Strip trailing space
212 for (s = buf + strlen(buf) - 1; s > buf; s--)
213 {
214 if (*s > ' ')
215 break;
216 }
217 s[1] = '\0';
218
219 // Strip leading space
220 for (s = buf; *s; s++)
221 {
222 if (*s > ' ')
223 break;
224 }
225
226 return s;
227}
char * user_input(const char *prompt)
A quick-and-dirty user input function: reads string from stdin (up to 80 chars), trims leading and tr...
Definition address.cpp:203
int main()
Address book application: reads address.xml, displays its content, prompts the user for new contact,...
Definition address.cpp:81
SOAP_FMAC3S const char *SOAP_FMAC4S soap_a__ISO_country2s(struct soap *, enum a__ISO_country)
SOAP_FMAC3S int SOAP_FMAC4S soap_s2a__ISO_country(struct soap *, const char *, enum a__ISO_country *)
int soap_write__a__address_book(struct soap *soap, _a__address_book const *p)
Definition addressH.h:493
_a__address_book * soap_new__a__address_book(struct soap *soap, int n=-1)
Definition addressH.h:466
a__address * soap_new_a__address(struct soap *soap, int n=-1)
Definition addressH.h:557
std::string * soap_new_std__string(struct soap *soap, int n=-1)
Definition addressH.h:375
int soap_read__a__address_book(struct soap *soap, _a__address_book *p)
Definition addressH.h:526
Top-level root element "urn:address-book-example":address-book.
Definition address.h:264
std::vector< a__address * > address
Vector of a__address* of length 0..unbounded.
Definition address.h:266
"urn:address-book-example":address is a complexType.
Definition address.h:222
virtual void soap_default(struct soap *)
(Re)set members to default values
std::string * phone
Element "phone" of type xs:string.
Definition address.h:234
std::string * mobile
Element "mobile" of type xs:string.
Definition address.h:236
std::string zip
Element "zip" of type xs:string.
Definition address.h:230
std::string street
Element "street" of type xs:string.
Definition address.h:226
enum a__ISO_country country
Element "country" of type "":ISO-country.
Definition address.h:232
time_t * dob
Element "dob" of type xs:dateTime.
Definition address.h:238
int ID
Attribute "ID" of type xs:int.
Definition address.h:240
std::string city
Element "city" of type xs:string.
Definition address.h:228
std::string name
Element "name" of type xs:string.
Definition address.h:224