auxiliar.cpp 946 Bytes
//
// Created by Imanol on 28-may-16.
//

#include "auxiliar.h"

void timer_handler (int signum)
{
    if(signum == SIGVTALRM)
    {
        cout << servicename << " CRITICAL - timeout occurred" << endl;
        exit(2);
    }
}

int str2int(string str)
{
    int num;
    stringstream sstream;
    sstream << str;
    if(!(sstream >> num))
    {
        throw integerConversionException("Integer conversion error");
    }
    return num;
}

string int2str(int x)
{
    string str;
    stringstream sstream;
    sstream << x;
    sstream >> str;
    return str;
}

int exec(string cmd, string *output)
{
    *output = "";
    FILE* pipe = popen(cmd.c_str(), "r");
    if (!pipe)
    {
        cout << "Error opening child process" << endl;
        exit(3);
    }
    char buffer[128];
    while(!feof(pipe))
    {
        if(fgets(buffer, 128, pipe) != NULL)
        {
            *output += buffer;
        }
    }
    return pclose(pipe)/256;
}