Helper.cc 4 KB
/**
 * Helper.cc
 *
 *  Created on: Dec 13, 2012
 *      Author: AKKA IS
 */

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <regex.h>
#include <sys/stat.h>
#include <cerrno>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/wait.h>


#include <iostream>
#include <sstream>
#include <fstream>

#include <boost/regex.hpp>

#include "Helper.hh"

namespace AMDA {
namespace Helpers {

/**
 * @brief get files in rootDir with pattern
 * @return the number of match file
 * @param rootDir path of entries search
 * @param files return's list of match files
 * @param pattern regexec pattern
 */
int Helper::getMatchFiles(const char* rootDir, std::vector<std::string> &files,
		const char* pattern) {
	DIR *dp;
	errno = 0;
	if ((dp = opendir(rootDir)) == NULL) {
		return errno;
	} else {
		struct dirent *dirp;
		while ((dirp = readdir(dp)) != NULL) {
			if (format(dirp->d_name, pattern)) {
				files.push_back(std::string(dirp->d_name));
			}
		}

		closedir(dp);
	}
	return 0;
}


/**
 * @brief return true if subject match format
 */
bool Helper::format(const char* subject, const char* format) {
	bool ok = false;
	regex_t re;

	if (regcomp(&re, format, REG_EXTENDED | REG_NOSUB) != 0) {
		/* Report error. */
	} else {
		int status = regexec(&re, subject, (size_t) 0, NULL, 0);
		regfree(&re);
		if (status != 0) {
			/* Report error. */
		} else {
			ok = true;
		}
	}

	return ok;
}

int  Helper::mkdir(const char* path) {
	errno = 0;
	DIR *dir = opendir(path);
	if(dir == NULL) {
		errno = 0;
		if (::mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP) != 0) {
		    return errno;
		}
	} else {
		closedir(dir);
	}
    return 0;

}

std::string Helper::_pid;

const std::string &Helper::getPID() {
	if(_pid == "") {
		std::stringstream buf;
		 buf << getpid();
		 _pid = buf.str();
	}
  return _pid;
}

time_t Helper::dateOfFile(const char* path) {
	struct stat buf;
	if(lstat(path, &buf) == 0) {
		return buf.st_mtime;
	}
	return 0;
}



#define SIZE_LINE 128
bool Helper::SystemCommand(std::string command, log4cxx::LoggerPtr pLogger) {
	bool anError = false;
	std::string fullCommand = command + " 2>&1";
	std::string  outputCommand;
	LOG4CXX_INFO(pLogger, fullCommand)
	FILE *processOutput = popen(fullCommand.c_str(), "r");
	char line[SIZE_LINE + 1];
	while(fgets(line, SIZE_LINE, processOutput) != NULL) {
		outputCommand += line;
	}
	int status = pclose(processOutput);

	if (!WIFEXITED(status) ) {
		anError = true;
		LOG4CXX_ERROR(pLogger, "Compilation: " << outputCommand);
		if (WIFSIGNALED(status)) {
			LOG4CXX_ERROR(pLogger, "Compilation: " << "End cause by signal " << WTERMSIG(status));
		}
		if (WCOREDUMP(status)) {
			LOG4CXX_ERROR(pLogger, "Compilation: " << " a core dump is generated");
		}
	}
	else if(WEXITSTATUS(status) != 0) {
		anError = true;
		LOG4CXX_ERROR(pLogger, "Compilation: " << outputCommand);
		LOG4CXX_ERROR(pLogger, "Compilation: " << "End with an error " << status);
	}

	return anError;
}

/*
 * @brief Load a CSV file
 */
std::vector<std::vector<std::string> > Helper::loadCSVFile(const char* filePath, char colSeparator) {
	std::vector<std::vector<std::string> > result;

	std::ifstream infile(filePath);

	std::string pattern = std::string(1,colSeparator);
	pattern += "(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
	boost::regex fieldsregx(pattern);

	std::string line;
	while (std::getline(infile, line)) {
		line.erase( std::remove(line.begin(), line.end(), '\r'), line.end() );

		// Split line to tokens

		boost::sregex_token_iterator ti(line.begin(), line.end(), fieldsregx, -1);
		boost::sregex_token_iterator end2;

		std::vector<std::string> row;
		while (ti != end2) {
			std::string token = ti->str();
			++ti;
			if (token.front() == '"') {
				token.erase(0,1);
			}
			if (token.back() == '"') {
				token.erase(token.size()-1);
			}
			
			row.push_back(token);
		}
		if (line.back() == ';') {
			// last character was a separator
			row.push_back("");
		}
		result.push_back(row);
	}
	return result;
}



} /* namespace Parameters */
} /* namespace AMDA */