|  | Home | Documentation | address.cpp Source File updated Fri Jan 12 2018 by Robert van Engelen |

- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

address.cpp
Go to the documentation of this file.
1
31 /*
32 --------------------------------------------------------------------------------
33 gSOAP XML Web services tools
34 Copyright (C) 2000-2015, Robert van Engelen, Genivia, Inc. All Rights Reserved.
35 This software is released under one of the following two licenses:
36 GPL or Genivia's license for commercial use.
37 --------------------------------------------------------------------------------
38 GPL license.
39
40 This program is free software; you can redistribute it and/or modify it under
41 the terms of the GNU General Public License as published by the Free Software
42 Foundation; either version 2 of the License, or (at your option) any later
43 version.
44
45 This program is distributed in the hope that it will be useful, but WITHOUT ANY
46 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
47 PARTICULAR PURPOSE. See the GNU General Public License for more details.
48
49 You should have received a copy of the GNU General Public License along with
50 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
51 Place, Suite 330, Boston, MA 02111-1307 USA
52
53 Author contact information:
54 engelen@genivia.com / engelen@acm.org
55 --------------------------------------------------------------------------------
56 A commercial use license is available from Genivia, Inc., contact@genivia.com
57 --------------------------------------------------------------------------------
58 */
59
60 int main();
61
62 #include <iostream>
63 #include <fstream>
64
65 #include "addressH.h" // generated, also includes stdsoap2.h
66 #include "a.nsmap" // generated
67
75 char *user_input(const char *prompt);
76
81 int 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
87 _a__address_book *ab = soap_new__a__address_book(soap, -1);
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
203 char *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 }
a__address::country
enum a__ISO_country country
Element country of type "":ISO-country.
Definition: address.h:188
a__address::ID
int ID
Attribute ID of type xs:int.
Definition: address.h:196
user_input
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
main
int main()
Address book application: reads address.xml, displays its content, prompts the user for new contact...
Definition: address.cpp:81
a__address::soap_default
virtual void soap_default(struct soap *)
(Re)set members to default values
a__address::street
std::string street
Element street of type xs:string.
Definition: address.h:182
soap_read__a__address_book
int soap_read__a__address_book(struct soap *soap, _a__address_book *p)
Definition: addressH.h:478
soap_new_std__string
std::string * soap_new_std__string(struct soap *soap, int n=-1)
Definition: addressH.h:343
a__address::phone
std::string * phone
Element phone of type xs:string.
Definition: address.h:190
soap_new__a__address_book
_a__address_book * soap_new__a__address_book(struct soap *soap, int n=-1)
Definition: addressH.h:426
a__address
An address information item.
Definition: address.h:177
_a__address_book::address
std::vector< a__address * > address
Vector of a__address* with length 0..unbounded.
Definition: address.h:209
soap_new_a__address
a__address * soap_new_a__address(struct soap *soap, int n=-1)
Definition: addressH.h:509
a__address::name
std::string name
Element name of type xs:string.
Definition: address.h:180
soap_s2a__ISO_country
SOAP_FMAC3S int SOAP_FMAC4S soap_s2a__ISO_country(struct soap *, const char *, enum a__ISO_country *)
a__address::city
std::string city
Element city of type xs:string.
Definition: address.h:184
soap_a__ISO_country2s
SOAP_FMAC3S const char *SOAP_FMAC4S soap_a__ISO_country2s(struct soap *, enum a__ISO_country)
a__address::mobile
std::string * mobile
Element mobile of type xs:string.
Definition: address.h:192
soap_write__a__address_book
int soap_write__a__address_book(struct soap *soap, _a__address_book const *p)
Definition: addressH.h:453
_a__address_book
The root element of the address book schema.
Definition: address.h:206
a__address::dob
time_t * dob
Element dob of type xs:dateTime.
Definition: address.h:194
a__address::zip
std::string zip
Element zip of type xs:string.
Definition: address.h:186
addressH.h