00001
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
#include <iostream>
00080
#include <fstream>
00081
00082
#include "addressH.h"
00083
#include "a.nsmap"
00084
00092
char *
user_input(
const char *prompt);
00093
00098 int main()
00099 {
00100
00101
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
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
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
00134
00135
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
00143
if ((*i)->dob)
00144 std::cout <<
"DOB: " << soap_dateTime2s(soap, *(*i)->dob) << std::endl;
00145 std::cout <<
"---------" << std::endl;
00146 }
00147 }
00148
00149
00150
a__address *a = soap_new_a__address(soap, -1);
00151
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
00163
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
00169 a->
phone = soap_new_std__string(soap, -1);
00170 *a->
phone = s;
00171 }
00172
if (*(s =
user_input(
"Mobile")))
00173 {
00174
00175 a->
mobile = soap_new_std__string(soap, -1);
00176 *a->
mobile = s;
00177 }
00178
00179
00180 ab->
address.push_back(a);
00181
00182 std::cout << std::endl <<
"Contact information added." << std::endl;
00183
00184
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
00201 soap_destroy(soap);
00202
00203 soap_end(soap);
00204
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
00219
for (s = buf + strlen(buf) - 1; s > buf; s--)
00220 {
00221
if (*s >
' ')
00222
break;
00223 }
00224 s[1] =
'\0';
00225
00226
00227
for (s = buf; *s; s++)
00228 {
00229
if (*s >
' ')
00230
break;
00231 }
00232
00233
return s;
00234 }