]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/c-apps/osrf_dbmath.c
added some more api wrappers
[OpenSRF.git] / src / c-apps / osrf_dbmath.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4
5 int initialize();
6 int childInit();
7 int osrfMathRun( osrfMethodDispatcher* );
8
9
10 int initialize() {
11         osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
12         osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
13         osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
14         osrfAppRegisterMethod( "opensrf.dbmath", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
15         return 0;
16 }
17
18 int childInit() {
19         return 0;
20 }
21
22 int osrfMathRun( osrfMethodDispatcher* d ) {
23
24         OSRF_METHOD_VERIFY_DISPATCHER(d);       
25
26         jsonObject* x = jsonObjectGetIndex(params, 0);
27         jsonObject* y = jsonObjectGetIndex(params, 1);
28
29         if( x && y ) {
30
31                 char* a = jsonObjectToSimpleString(x);
32                 char* b = jsonObjectToSimpleString(y);
33
34                 if( a && b ) {
35
36                         double i = strtod(a, NULL);
37                         double j = strtod(b, NULL);
38                         double r = 0;
39
40                         if(!strcmp(method->name, "add"))                r = i + j;
41                         if(!strcmp(method->name, "sub"))                r = i - j;
42                         if(!strcmp(method->name, "mult"))       r = i * j;
43                         if(!strcmp(method->name, "div"))                r = i / j;
44
45                         jsonObject* resp = jsonNewNumberObject(r);
46                         osrfAppRequestRespond( session, request, resp );
47                         jsonObjectFree(resp);
48
49                         free(a); free(b);
50                         return 0;
51                 }
52         }
53
54         return -1;
55 }
56
57
58