Blame view

src/helpers/Helper.cc 4 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 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>
c1f4db8e   Benjamin Renard   Add process to te...
24
25
26
#include <fstream>

#include <boost/regex.hpp>
fbe3c2bb   Benjamin Renard   First commit
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

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

c1f4db8e   Benjamin Renard   Add process to te...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * @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;
04d84599   Benjamin Renard   Parser: Add suppo...
175
176
177
178
179
180
181
			if (token.front() == '"') {
				token.erase(0,1);
			}
			if (token.back() == '"') {
				token.erase(token.size()-1);
			}
			
c1f4db8e   Benjamin Renard   Add process to te...
182
183
184
185
186
187
188
189
190
191
192
193
			row.push_back(token);
		}
		if (line.back() == ';') {
			// last character was a separator
			row.push_back("");
		}
		result.push_back(row);
	}
	return result;
}


fbe3c2bb   Benjamin Renard   First commit
194
195
196

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