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 osrfLogInit(MODULENAME);
17 osrfAppRegisterMethod(
22 "[ num1, num2 ]", 2 );
24 osrfAppRegisterMethod(
28 "Subtracts two numbers",
29 "[ num1, num2 ]", 2 );
31 osrfAppRegisterMethod(
35 "Multiplies two numbers",
36 "[ num1, num2 ]", 2 );
38 osrfAppRegisterMethod(
42 "Divides two numbers",
43 "[ num1, num2 ]", 2 );
48 int osrfAppChildInit() {
52 int osrfMathRun( osrfMethodContext* ctx ) {
54 OSRF_METHOD_VERIFY_CONTEXT(ctx);
56 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
57 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
61 char* a = jsonObjectToSimpleString(x);
62 char* b = jsonObjectToSimpleString(y);
66 double i = strtod(a, NULL);
67 double j = strtod(b, NULL);
70 if(!strcmp(ctx->method->name, "add")) r = i + j;
71 if(!strcmp(ctx->method->name, "sub")) r = i - j;
72 if(!strcmp(ctx->method->name, "mult")) r = i * j;
73 if(!strcmp(ctx->method->name, "div")) r = i / j;
75 jsonObject* resp = jsonNewNumberObject(r);
76 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );