initial load of upstream version 1.06.32
[xmlrpc-c] / examples / cpp / meerkat-app-list.cpp
1 // List recently-released Linux applications. (Written in C++.)
2 // For more details about O'Reilly's excellent Meerkat news service, see:
3 // http://www.oreillynet.com/pub/a/rss/2000/11/14/meerkat_xmlrpc.html */
4
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8
9 using namespace std;
10
11 #include <xmlrpc-c/oldcppwrapper.hpp>
12
13 #define NAME           "XML-RPC C++ Meerkat Query Demo"
14 #define VERSION        "0.1"
15 #define MEERKAT_URL    "http://www.oreillynet.com/meerkat/xml-rpc/server.php"
16 #define SOFTWARE_LINUX (6)
17
18 static void
19 list_apps(unsigned int const hours) {
20
21     // Build our time_period parameter.
22     ostringstream time_period_stream;
23     time_period_stream << hours << "HOUR";
24     string const time_period(time_period_stream.str());
25
26     // Assemble our meerkat query recipe.
27     XmlRpcValue recipe = XmlRpcValue::makeStruct();
28     recipe.structSetValue("category", XmlRpcValue::makeInt(SOFTWARE_LINUX));
29     recipe.structSetValue("time_period", XmlRpcValue::makeString(time_period));
30     recipe.structSetValue("descriptions", XmlRpcValue::makeInt(76));
31
32     // Build our parameter array.
33     XmlRpcValue param_array(XmlRpcValue::makeArray());
34     param_array.arrayAppendItem(recipe);
35
36     // Create a client pointing to Meerkat.
37     XmlRpcClient meerkat(MEERKAT_URL);
38
39     // Perform the query.
40     XmlRpcValue const apps(meerkat.call("meerkat.getItems", param_array));
41
42     // Print our results.
43     size_t const app_count(apps.arraySize());
44     bool first;
45     size_t i;
46     for (i = 0, first=false; i < app_count; ++i, first = false) {
47         XmlRpcValue const app(apps.arrayGetItem(i));
48
49         // Get some information about our application.
50         string const title(app.structGetValue("title").getString());
51         string const link(app.structGetValue("link").getString());
52         string const description(
53             app.structGetValue("description").getString());
54     
55         // Print a separator line if necessary.
56         if (!first)
57             cout << endl;
58
59         // Print this application entry.
60         if (description.size() > 0)
61             cout << title << endl << description << endl << link << endl;
62         else
63             cout << title << endl << description << endl << link << endl;
64     }
65 }
66
67
68
69 int
70 main(int argc, char **argv) {
71     int retval;
72     unsigned int hours;
73
74     // Parse our command-line arguments.
75     if (argc == 1) {
76         hours = 25;
77     } else if (argc == 2) {
78         hours = atoi(argv[1]);
79     }
80     if (hours == 0) {
81         cerr << "Hours must be positive" << endl;
82         exit(1);
83     }
84     if (hours > 49) {
85         cerr << "It's not nice to ask for > 49 hours at once." << endl;
86         exit(1);    
87     }
88
89     // Start up our client library.
90     XmlRpcClient::Initialize(NAME, VERSION);
91
92     // Call our implementation, and watch out for faults.
93     try {
94         list_apps(hours);
95         retval = 0;
96     } catch (XmlRpcFault& fault) {
97         cerr << argv[0] << ": XML-RPC fault #" << fault.getFaultCode()
98              << ": " << fault.getFaultString() << endl;
99         retval = 1;
100     }
101
102     // Shut down our client library.
103     XmlRpcClient::Terminate();
104
105     return retval;
106 }