Genivia Home Documentation
address.cpp Source File

updated Wed Mar 2 2016
 
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 
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 = user_input("Country");
145  // Advanced level: use the generated s2a__ISO_country() to convert string to
146  // enum constant
147  if (soap_s2a__ISO_country(soap, s, &a->country) != SOAP_OK)
148  std::cerr << "Not a valid country code" << std::endl;
149  if (*(s = user_input("Phone")))
150  {
151  // Allocate string in engine's data space:
152  a->phone = soap_new_std__string(soap, -1);
153  *a->phone = s;
154  }
155  if (*(s = user_input("Mobile")))
156  {
157  // Allocate string in engine's data space:
158  a->mobile = soap_new_std__string(soap, -1);
159  *a->mobile = s;
160  }
161 
162  // Add contact to address book
163  ab->address.push_back(a);
164 
165  std::cout << std::endl << "Contact information added." << std::endl;
166 
167  // Save updated address book to address.xml
168  fs.open("address.xml", std::ios::out);
169  if (!fs)
170  {
171  std::cerr << "Cannot create address.xml file" << std::endl;
172  exit(1);
173  }
174  soap->os = &fs;
175  if (soap_write__a__address_book(soap, ab) != SOAP_OK)
176  {
177  std::cerr << "Error writing address.xml file" << std::endl;
178  soap_stream_fault(soap, std::cerr);
179  exit(1);
180  }
181  fs.close();
182 
183  // Delete instances
184  soap_destroy(soap);
185  // Delete data
186  soap_end(soap);
187  // Free soap struct engine context
188  soap_free(soap);
189 
190  return 0;
191 }
192 
193 char *user_input(const char *prompt)
194 {
195  static char buf[80];
196  char *s;
197 
198  printf("%-9s> ", prompt);
199  fgets(buf, 80, stdin);
200 
201  // Strip trailing space
202  for (s = buf + strlen(buf) - 1; s > buf; s--)
203  {
204  if (*s > ' ')
205  break;
206  }
207  s[1] = '\0';
208 
209  // Strip leading space
210  for (s = buf; *s; s++)
211  {
212  if (*s > ' ')
213  break;
214  }
215 
216  return s;
217 }
#define soap_read__a__address_book(soap, data)
Definition: addressH.h:179
enum a__ISO_country country
Element country of type "":ISO-country.
Definition: address.h:188
int ID
Attribute ID of type xs:int.
Definition: address.h:196
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:193
int main()
Address book application: reads address.xml, displays its content, prompts the user for new contact...
Definition: address.cpp:81
virtual void soap_default(struct soap *)
#define soap_write__a__address_book(soap, data)
Definition: addressH.h:173
std::string street
Element street of type xs:string.
Definition: address.h:182
std::string * soap_new_std__string(struct soap *soap, int n=-1)
Definition: addressH.h:155
std::string * phone
Element phone of type xs:string.
Definition: address.h:190
_a__address_book * soap_new__a__address_book(struct soap *soap, int n=-1)
Definition: addressH.h:184
An address information item.
Definition: address.h:177
std::vector< a__address * > address
Vector of a__address* with length 0..unbounded.
Definition: address.h:209
a__address * soap_new_a__address(struct soap *soap, int n=-1)
Definition: addressH.h:213
std::string name
Element name of type xs:string.
Definition: address.h:180
SOAP_FMAC3S int SOAP_FMAC4S soap_s2a__ISO_country(struct soap *, const char *, enum a__ISO_country *)
std::string city
Element city of type xs:string.
Definition: address.h:184
SOAP_FMAC3S const char *SOAP_FMAC4S soap_a__ISO_country2s(struct soap *, enum a__ISO_country)
std::string * mobile
Element mobile of type xs:string.
Definition: address.h:192
The root element of the address book schema.
Definition: address.h:206
std::string zip
Element zip of type xs:string.
Definition: address.h:186