Subversion Repositories heating

Rev

Rev 4 | Go to most recent revision | Details | 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
 
220
void helper::setPName(char *name)
221
{
222
	if (name == NULL || std::char_traits<char>::length(name) < 1)
223
		return;
224
 
225
	strncpy(pname, name, sizeof(pname) - 1);
226
}
227
 
228
string helper::itostring (int i)
229
{
230
	return ToString(i);
231
}
232
 
233
char* helper::assign (string str)
234
{
235
char *p;
236
int len;
237
 
238
	if (str.empty())
239
		return NULL;
240
 
241
	len = strlen(str.c_str());
242
	p = new char[len+1];
243
	memset(p, 0, len+1);
244
	strncpy(p, str.c_str(), len);
245
	return p;
246
}
247
 
248
bool helper::isArm()
249
{
250
struct utsname uts;
251
std::string str;
252
 
253
	if (uname(&uts) == -1)
254
	{
255
		syslog(LOG_DAEMON, "Error getting architecture!");
256
		return false;
257
	}
258
 
259
	if (compcase(uts.machine, "armv5tel") == 0)
260
		return true;
261
 
262
	str = std::string(uts.machine);
263
 
264
	if (str.find("arm") != std::string::npos || str.find("ARM") != std::string::npos)
265
		return true;
266
 
267
	return false;
268
}
269
 
270
void helper::debug(std::string msg)
271
{
272
	if (!dbg || msg.empty())
273
		return;
274
#ifdef TRACE
275
	if (!isArm())
276
#endif
277
		syslog(LOG_DEBUG, "%s: %s", pname, msg.c_str());
278
#ifdef TRACE
279
	else
280
	{
281
	std::ofstream df;
282
	time_t t = time(0);
283
	char *ct;
284
	int len;
285
	struct tm *tinfo = localtime(&t);
286
 
287
		df.open("/var/log/prtg_mini.log", std::ios::app);
288
		ct = ctime(&t);
289
		len = strlen(ct);
290
		*(ct + (len-1)) = 0;
291
		df << ct << ": " << msg << std::endl;
292
		df.close();
293
	}
294
#endif
295
}