1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
7 int osrfMathRun( osrfMethodContext* );
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 );
22 int osrfMathRun( osrfMethodContext* d ) {
24 OSRF_METHOD_VERIFY_CONTEXT(d);
26 jsonObject* x = jsonObjectGetIndex(params, 0);
27 jsonObject* y = jsonObjectGetIndex(params, 1);
31 char* a = jsonObjectToSimpleString(x);
32 char* b = jsonObjectToSimpleString(y);
36 double i = strtod(a, NULL);
37 double j = strtod(b, NULL);
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;
45 jsonObject* resp = jsonNewNumberObject(r);
46 osrfAppRequestRespondComplete( session, request, resp );