Genivia Home Documentation
address.cpp Source File

updated Fri Aug 16 2024 by Robert van Engelen
 
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.
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  {
169  a->dob = soap_new_dateTime(soap);
170  if (soap_s2dateTime(soap, s, a->dob) != SOAP_OK)
171  std::cerr << "Not a valid ISO 8601 date time (ignored)" << std::endl;
172  }
173  soap->error = SOAP_OK;
174 
175  // Add contact to address book
176  ab->address.push_back(a);
177 
178  std::cout << std::endl << "Contact information added." << std::endl;
179 
180  // Save updated address book to address.xml
181  fs.open("address.xml", std::ios::out);
182  if (!fs)
183  {
184  std::cerr << "Cannot create address.xml file" << std::endl;
185  exit(1);
186  }
187  soap->os = &fs;
188  if (soap_write__a__address_book(soap, ab) != SOAP_OK)
189  {
190  std::cerr << "Error writing address.xml file" << std::endl;
191  soap_stream_fault(soap, std::cerr);
192  exit(1);
193  }
194  fs.close();
195 
196  // Delete instances
197  soap_destroy(soap);
198  // Delete data
199  soap_end(soap);
200  // Free soap struct engine context
201  soap_free(soap);
202 
203  return 0;
204 }
205 
206 char *user_input(const char *prompt)
207 {
208  static char buf[80];
209  char *s;
210 
211  printf("%-9s> ", prompt);
212  fgets(buf, 80, stdin);
213 
214  // Strip trailing space
215  for (s = buf + strlen(buf) - 1; s > buf; s--)
216  {
217  if (*s > ' ')
218  break;
219  }
220  s[1] = '\0';
221 
222  // Strip leading space
223  for (s = buf; *s; s++)
224  {
225  if (*s > ' ')
226  break;
227  }
228 
229  return s;
230 }
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:206
int main()
Address book application: reads address.xml, displays its content, prompts the user for new contact...
Definition: address.cpp:81