]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/c-apps/osrf_dbmath.c
Finished adding doxygen-style comments to document the app session
[OpenSRF.git] / src / c-apps / osrf_dbmath.c
1 #include <opensrf/osrf_app_session.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_json.h>
4 #include <opensrf/log.h>
5
6 #define MODULENAME "opensrf.dbmath"
7
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
11
12
13 int osrfAppInitialize() {
14
15         osrfAppRegisterMethod( 
16                         MODULENAME, 
17                         "add", 
18                         "osrfMathRun", 
19                         "Addss two numbers", 2, 0 );
20
21         osrfAppRegisterMethod( 
22                         MODULENAME, 
23                         "sub", 
24                         "osrfMathRun", 
25                         "Subtracts two numbers", 2, 0 );
26
27         osrfAppRegisterMethod( 
28                         MODULENAME, 
29                         "mult", 
30                         "osrfMathRun", 
31                         "Multiplies two numbers", 2, 0 );
32
33         osrfAppRegisterMethod( 
34                         MODULENAME, 
35                         "div", 
36                         "osrfMathRun", 
37                         "Divides two numbers", 2, 0 );
38
39         return 0;
40 }
41
42 int osrfAppChildInit() {
43         return 0;
44 }
45
46 int osrfMathRun( osrfMethodContext* ctx ) {
47         if( osrfMethodVerifyContext( ctx ) ) {
48                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
49                 return -1;
50         }
51
52         const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
53         const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
54
55         if( x && y ) {
56
57                 char* a = jsonObjectToSimpleString(x);
58                 char* b = jsonObjectToSimpleString(y);
59
60                 if( a && b ) {
61
62                         double i = strtod(a, NULL);
63                         double j = strtod(b, NULL);
64                         double r = 0;
65
66                         if(!strcmp(ctx->method->name, "add"))   r = i + j;
67                         if(!strcmp(ctx->method->name, "sub"))   r = i - j;
68                         if(!strcmp(ctx->method->name, "mult"))  r = i * j;
69                         if(!strcmp(ctx->method->name, "div"))   r = i / j;
70
71                         jsonObject* resp = jsonNewNumberObject(r);
72                         osrfAppRespondComplete( ctx, resp );
73                         jsonObjectFree(resp);
74
75                         free(a); free(b);
76                         return 0;
77                 }
78                 else {
79                         if(a) free(a);
80                         if(b) free(b);
81                 }
82         }
83
84         return -1;
85 }
86
87
88