Survey Common
#include "SurveyCommon.h"
 
using namespace std;
 
void sendInt(CommunicatingSocket *sock, uint32_t val) throw(SocketException) {
    val = htonl(val);
    sock->send(&val, sizeof(val));
}
 
void sendString(CommunicatingSocket *sock, const string &str) throw(SocketException) {
    sendInt(sock, str.length());
    sock->send(str.c_str(), str.length());
}
 
uint32_t recvInt(CommunicatingSocket *sock) throw(runtime_error) {
    uint32_t val;
    if (sock->recvFully(&val, sizeof(val)) != sizeof(val))
        throw runtime_error("Socket closed while reading int");
 
    return ntohl(val);
}
 
string recvString(CommunicatingSocket *sock) throw(runtime_error) {
    uint32_t len = recvInt(sock);
    char *buffer = new char[len + 1];
    if (sock->recvFully(buffer, len) != len) {
        delete [] buffer;
        throw runtime_error("Socket closed while reading string");
    }
 
    buffer[len] = '\0';
    string result(buffer);
    delete [] buffer;
    return result;
}
 
bool readSurvey(istream &stream, std::vector<Question> &qList) {
    int count = 0;
    stream >> count;
    stream.ignore();
    qList = vector<Question>(count);
 
    for (unsigned int q = 0; q < qList.size(); q++) {
        getline(stream, qList[q].qText);
 
        count = 0;
        stream >> count;
        stream.ignore();
 
        qList[q].rList = vector<string>(count);
        for (unsigned int r = 0; r < qList[q].rList.size(); r++)
            getline(stream, qList[q].rList[r]);
    }
 
    return stream;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License