#include <exception>
#include <iostream>
#include <string>
+#include <unistd.h>
#include <util_json.h>
+
using std::cin;
using std::cout;
using std::endl;
int main(int argc, char **argv) {
/* Variables */
- string input, method;
+ string method, output;
const char* c_str;
+ char *input;
JSONObject json;
JSONObject *result;
json_error_t jsonErr;
GSSCommand *cmd;
+ int len;
+ ssize_t readTotal, readThisRound, readRemaining;
+ std::streamsize threeTwoBits = 32 / sizeof(string::size_type);
/* Error checking */
{
try
{
- cout << "Give me some JSON: ";
- getline(cin, input);
+ len = 0;
+ readThisRound = 0;
+ while(0 == readThisRound)
+ readThisRound = fread(&len, 4, 1, stdin);
- c_str = input.c_str();
- JSONObject json = JSONObject::load(c_str, 0, &jsonErr);
+ input = new char[len + 1];
+ readTotal = readThisRound = 0;
+ while (readTotal < len)
+ {
+ readRemaining = len - readTotal;
+ readThisRound = fread( &(input[readTotal]), 1, readRemaining, stdin );
+ if (-1 == readThisRound)
+ readTotal = len;
+ else
+ readTotal += readThisRound;
+ }
+ input[len] = '\0';
+
+ JSONObject json = JSONObject::load(input, 0, &jsonErr);
+ delete[] input;
// Oh, how I wish I could simply use: switch(json.get("method"))
c_str = json.get("method").string();
JSONObject response;
response.set("method", "unknown");
response.set("error_message", "Did not find a valid method to execute.");
- cout << response.dump() << endl;
+ output = response.dump();
+ len = output.length();
+ cout.write((char *)&len, threeTwoBits);
+ cout << output << endl;
continue;
}
result = cmd->toJSON();
delete cmd;
- cout << result->dump( JSON_INDENT(4) ) << endl;
+ output = result->dump();
+ len = output.length();
+
+ cout.write((char *)&len, threeTwoBits);
+ cout << output;
+ cout.flush();
}
- catch ( std::bad_alloc )
+ catch ( std::bad_alloc& e )
{
JSONObject response;
JSONObject error;
response.set("method", "unknown");
response.set("error_message", "Could not parse the input JSON");
+ response.set("original message", input);
error.set("text", jsonErr.text);
error.set("source", jsonErr.source);
error.set("line", jsonErr.line);
error.set("column", jsonErr.column);
error.set("position", jsonErr.position);
response.set("error", error);
- cout << response.dump() << endl;
+ output = response.dump();
+ len = output.length();
+ cout.write((char *)&len, threeTwoBits);
+ cout << output << endl;
}
} while(1);