auxiliar.cpp 1.19 KB
//
// 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;
}

void split(const string &s, const char* delim, vector<string> & v)
{
    char *dup = strdup(s.c_str());
    char *token = strtok(dup, delim);
    while(token != NULL)
    {
        v.push_back(string(token));
        token = strtok(NULL, delim);
    }
    free(dup);
}

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;
}