]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/c-apps/osrf_dbmath.c
07cf0f653d319aef497596011b3f4bb51ccb3ae6
[Evergreen.git] / OpenSRF / 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 #include "opensrf/osrf_log.h"
5
6 int osrfAppInitialize();
7 int osrfAppChildInit();
8 int osrfMathRun( osrfMethodContext* );
9
10
11 int osrfAppInitialize() {
12         osrfLogInit("opensrf.dbmath");
13         osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
14         osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
15         osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
16         osrfAppRegisterMethod( "opensrf.dbmath", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
17         return 0;
18 }
19
20 int osrfAppChildInit() {
21         return 0;
22 }
23
24 int osrfMathRun( osrfMethodContext* ctx ) {
25
26         OSRF_METHOD_VERIFY_CONTEXT(ctx);        
27
28         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
29         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
30
31         if( x && y ) {
32
33                 char* a = jsonObjectToSimpleString(x);
34                 char* b = jsonObjectToSimpleString(y);
35
36                 if( a && b ) {
37
38                         double i = strtod(a, NULL);
39                         double j = strtod(b, NULL);
40                         double r = 0;
41
42                         if(!strcmp(ctx->method->name, "add"))   r = i + j;
43                         if(!strcmp(ctx->method->name, "sub"))   r = i - j;
44                         if(!strcmp(ctx->method->name, "mult"))  r = i * j;
45                         if(!strcmp(ctx->method->name, "div"))   r = i / j;
46
47                         jsonObject* resp = jsonNewNumberObject(r);
48                         osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
49                         jsonObjectFree(resp);
50
51                         free(a); free(b);
52                         return 0;
53                 }
54         }
55
56         return -1;
57 }
58
59
60