1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/osrf_log.h"
6 #define MODULENAME "opensrf.dbmath"
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
13 int osrfAppInitialize() {
15 osrfAppRegisterMethod(
20 "[ num1, num2 ]", 2, 0 );
22 osrfAppRegisterMethod(
26 "Subtracts two numbers",
27 "[ num1, num2 ]", 2, 0 );
29 osrfAppRegisterMethod(
33 "Multiplies two numbers",
34 "[ num1, num2 ]", 2, 0 );
36 osrfAppRegisterMethod(
40 "Divides two numbers",
41 "[ num1, num2 ]", 2, 0 );
46 int osrfAppChildInit() {
50 int osrfMathRun( osrfMethodContext* ctx ) {
52 OSRF_METHOD_VERIFY_CONTEXT(ctx);
54 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
55 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
59 char* a = jsonObjectToSimpleString(x);
60 char* b = jsonObjectToSimpleString(y);
64 double i = strtod(a, NULL);
65 double j = strtod(b, NULL);
68 if(!strcmp(ctx->method->name, "add")) r = i + j;
69 if(!strcmp(ctx->method->name, "sub")) r = i - j;
70 if(!strcmp(ctx->method->name, "mult")) r = i * j;
71 if(!strcmp(ctx->method->name, "div")) r = i / j;
73 jsonObject* resp = jsonNewNumberObject(r);
74 osrfAppRespondComplete( ctx, resp );