Added the test for bug #641.
[yaz-moved-to-github.git] / zoom / zoomtst9.c
1 /* $Id: zoomtst9.c,v 1.3 2006-08-24 13:19:44 adam Exp $  */
2
3 /** \file zoomtst9.c
4     \brief Extended Service Update
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <yaz/wrbuf.h>
10
11 #include <yaz/nmem.h>
12 #include <yaz/xmalloc.h>
13 #include <yaz/zoom.h>
14
15 static void usage()
16 {
17     fprintf(stderr, "usage:\n"
18             "zoomtst9 target [insert|delete|replace|update] id1 rec1 "
19             "id2 rec2 ..\n");
20
21     fprintf(stderr, "\nThis program illustrates the usage of"
22             " extended services Update from ZOOM.\n");
23     fprintf(stderr, "\nid "
24             "is optional opaque record Id and is omitted if empty.\n");
25     fprintf(stderr, "\nrec "
26             "is optional record data and is omitted if empty.\n");
27     exit (1);
28 }
29
30 int main(int argc, char **argv)
31 {
32     ZOOM_connection z;
33     ZOOM_options o = ZOOM_options_create ();
34     int error;
35     const char *errmsg, *addinfo;
36     
37     if (argc < 3)
38         usage();
39
40     z = ZOOM_connection_create (o);
41     
42     /* connect and init */
43     ZOOM_connection_connect (z, argv[1], 0);
44     
45     if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
46     {
47         fprintf(stderr, "%s error: %s (%d) %s\n",
48                 ZOOM_connection_option_get(z, "host"),
49                 errmsg, error, addinfo);
50     }
51     else
52     {
53         ZOOM_package pkg = ZOOM_connection_package(z, 0);
54         const char *cmd = argv[2];
55         int i;
56
57         if (!strcmp(cmd, "insert"))
58             ZOOM_package_option_set(pkg, "action", "recordInsert");
59         else if (!strcmp(cmd, "update"))
60             ZOOM_package_option_set(pkg, "action", "specialUpdate");
61         else if (!strcmp(cmd, "replace"))
62             ZOOM_package_option_set(pkg, "action", "recordReplace");
63         else if (!strcmp(cmd, "delete"))
64             ZOOM_package_option_set(pkg, "action", "recordDelete");
65         else
66         {
67             fprintf(stderr, "Bad action %s\n", cmd);
68             usage();
69         }
70
71         i = 3;
72         while (i < argc-1)
73         {
74             ZOOM_package_option_set(pkg, "recordIdOpaque",
75                                     argv[i][0] ? argv[i] : 0);
76             i++;
77             if (!strcmp(argv[i], "-"))
78             {
79                 /* For -, read record buffer from stdin */
80                 WRBUF w = wrbuf_alloc();
81                 int ch;
82                 while ((ch = getchar()) != EOF)
83                     wrbuf_putc(w, ch);
84                 wrbuf_putc(w, '\0');
85                 ZOOM_package_option_set(pkg, "record", wrbuf_buf(w));
86             }
87             else
88             {
89                 ZOOM_package_option_set(pkg, "record",
90                                         argv[i][0] ? argv[i] : 0);
91             }
92             i++;
93             ZOOM_package_send(pkg, "update"); /* Update EXT service */
94
95             if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
96             {
97                 fprintf(stderr, "%s error: %s (%d) %s\n",
98                         ZOOM_connection_option_get(z, "host"),
99                         errmsg, error, addinfo);
100             }
101         }
102     }
103     ZOOM_connection_destroy (z);
104     ZOOM_options_destroy(o);
105     exit (0);
106 }
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114