Subversion Repositories heating

Rev

Rev 4 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 * 
16
 * Author: Andreas Theofilu <andreas@theosys.at>
17
 */
18
#include <iostream>
19
#include <fstream>
20
#include <string>
21
#include <cstring>
22
#include <cctype>
23
#include <cstddef>
24
#include <ctime>
25
#include <cstdio>
26
#include <cstdlib>
27
#include <algorithm>
28
#include <functional>
29
#include <cctype>
30
#include <locale>
31
#include <unistd.h>
32
#include <syslog.h>
33
#include <sys/stat.h>
34
#include <sys/types.h>
35
#include <sys/utsname.h>
36
#include <fcntl.h>
37
#include "helper.h"
38
 
39
using namespace std;
40
 
41
helper::helper()
42
{
43
	strcpy(pname, "this");
44
	dbg = true;
45
}
46
 
47
char *helper::readLine(int fd, char *buf, int bufLen)
48
{
49
	int i, end;
50
	char ch, *p;
51
 
52
	if (fd <= 0)
53
		return NULL;
54
 
55
	i = end = 0;
56
	p = buf;
57
 
58
	while (read(fd, &ch, 1) > 0)
59
	{
60
		end = 1;
61
 
62
		if (ch == 0x0a)
63
		{
64
			*p = 0;
65
			return buf;
66
		}
67
 
68
		if (ch == 0x0d)	// ignore this!
69
			continue;
70
 
71
		if (i < (bufLen - 1))
72
		{
73
			*p = ch;
74
			p++;
75
			i++;
76
		}
77
	}
78
 
79
	*p = 0;
80
 
81
	if (end)
82
		return buf;
83
	else
84
		return NULL;
85
}
86
 
87
char *helper::trim(char *str)
88
{
89
char *p1, *p2, *p;
90
 
91
	if (!str)
92
		return NULL;
93
 
94
	p = str;
95
	p1 = p2 = NULL;
96
 
97
	while (*p)
98
	{
99
		if (!p1 && *p != ' ')
100
		{
101
			p1 = p;
102
			break;
103
		}
104
 
105
		p++;
106
	}
107
 
108
	p2 = str + (std::char_traits<char>::length(str) - 1);
109
 
110
	while (p2 > str && *p2 == ' ')
111
		p2--;
112
 
113
	if (p2)
114
		*(p2+1) = 0;
115
 
116
	if (p1)
117
	{
118
		std::string buf = std::string(p1);
119
		strcpy(str, buf.c_str());
120
	}
121
 
122
	return str;
123
}
124
 
125
string helper::trim (string str)
126
{
127
	if (str.empty())
128
		return str;
129
 
130
	str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
131
	str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
132
	return str;
133
}
134
 
135
int helper::compare(char *str1, char *str2)
136
{
137
	int len = std::char_traits<char>::length(str1);
138
 
139
	while (len)
140
	{
141
		if (*str1 && *str2 && *str1 == *str2)
142
		{
143
			str1++;
144
			str2++;
145
		}
146
		else
147
		{
148
			if (*str1 > *str2)
149
				return 1;
150
			else
151
				return -1;
152
		}
153
 
154
		len--;
155
	}
156
 
157
	return 0;
158
}
159
 
160
int helper::compcase(const char *str1, const char *str2)
161
{
162
	int len = std::char_traits<char>::length(str1);
163
//	debug("compcase: len=" + itostring(len) + ",str1=" + std::string(str1) + ",str2=" + std::string(str2));
164
 
165
	while (len)
166
	{
167
		if (*str1 && *str2 && std::tolower(*str1) == std::tolower(*str2))
168
		{
169
			str1++;
170
			str2++;
171
		}
172
		else
173
		{
174
			if (std::tolower(*str1) > std::tolower(*str2))
175
				return 1;
176
			else
177
				return -1;
178
		}
179
 
180
		len--;
181
	}
182
 
183
	return 0;
184
}
185
 
186
int helper::compcase (string str1, string str2)
187
{
188
string s1 = str1;
189
string s2 = str2;
190
 
191
	if (str1.empty())
192
		return -1;
193
	else if (str2.empty())
194
		return 1;
195
 
196
	transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
197
	transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
198
	return s1.compare(s2);
199
}
200
 
201
char* helper::findc (char* str, int len, char c)
202
{
203
	if (str == NULL || *str == 0 || !len)
204
		return NULL;
205
 
206
	for (int i = 0; i < len; i++)
207
	{
208
		if (*(str+i) == c)
209
			return str+i;
210
	}
211
 
212
	return NULL;
213
}
214
 
215
int helper::findc (string str, char c)
216
{
217
	return str.find(c);
218
}
219
 
5 andreas 220
vector<string> split(string str, char delimiter)
221
{
222
vector<string> internal;
223
stringstream ss(str);		// Turn the string into a stream.
224
string tok;
225
 
226
	while(getline(ss, tok, delimiter))
227
		internal.push_back(tok);
228
 
229
	return internal;
230
}
231
 
3 andreas 232
void helper::setPName(char *name)
233
{
234
	if (name == NULL || std::char_traits<char>::length(name) < 1)
235
		return;
236
 
237
	strncpy(pname, name, sizeof(pname) - 1);
238
}
239
 
240
string helper::itostring (int i)
241
{
242
	return ToString(i);
243
}
244
 
245
char* helper::assign (string str)
246
{
247
char *p;
248
int len;
249
 
250
	if (str.empty())
251
		return NULL;
252
 
253
	len = strlen(str.c_str());
254
	p = new char[len+1];
255
	memset(p, 0, len+1);
256
	strncpy(p, str.c_str(), len);
257
	return p;
258
}
259
 
4 andreas 260
bool helper::ToBool (char* str)
261
{
262
	if (!strcasestr(str, "true") || !strcasestr(str, "yes") || !strcasestr(str, "1") || !strcasestr(str, "on"))
263
		return true;
264
 
265
	return false;
266
}
267
 
268
bool helper::ToBool (string s)
269
{
270
	if (!compcase(s, "true") || !compcase(s, "yes") || !compcase(s, "1") || !compcase(s, "on"))
271
		return true;
272
 
273
	return false;
274
}
275
 
276
bool helper::ToBool (int i)
277
{
278
	if (i != 0)
279
		return true;
280
 
281
	return false;
282
}
283
 
3 andreas 284
bool helper::isArm()
285
{
286
struct utsname uts;
287
std::string str;
288
 
289
	if (uname(&uts) == -1)
290
	{
291
		syslog(LOG_DAEMON, "Error getting architecture!");
292
		return false;
293
	}
294
 
295
	if (compcase(uts.machine, "armv5tel") == 0)
296
		return true;
297
 
298
	str = std::string(uts.machine);
299
 
300
	if (str.find("arm") != std::string::npos || str.find("ARM") != std::string::npos)
301
		return true;
302
 
303
	return false;
304
}
305
 
306
void helper::debug(std::string msg)
307
{
308
	if (!dbg || msg.empty())
309
		return;
310
#ifdef TRACE
311
	if (!isArm())
312
#endif
313
		syslog(LOG_DEBUG, "%s: %s", pname, msg.c_str());
314
#ifdef TRACE
315
	else
316
	{
317
	std::ofstream df;
318
	time_t t = time(0);
319
	char *ct;
320
	int len;
321
	struct tm *tinfo = localtime(&t);
322
 
323
		df.open("/var/log/prtg_mini.log", std::ios::app);
324
		ct = ctime(&t);
325
		len = strlen(ct);
326
		*(ct + (len-1)) = 0;
327
		df << ct << ": " << msg << std::endl;
328
		df.close();
329
	}
330
#endif
331
}