hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Funcs.cpp
Go to the documentation of this file.
1 #include <ctype.h>
2 #include <dirent.h>
3 #include <makro.h>
4 #include <MemCpy.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <time.h>
11 #include <unistd.h>
12 
13 const char *wkdayArray[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Set", "Sun"};
14 const char *monthArray[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
15 
16 int strRcmp(const char *str, const char *str1)
17 {
18  int ret = (str ? strlen(str) : 0) - (str1 ? strlen(str1) : 0);
19  if(ret == 0 && str && str1)
20  {
21  unsigned int i = 0;
22  while(str[i] != 0)
23  {
24  if(static_cast<char>(tolower(str[i])) == static_cast<char>(tolower(str1[i])))
25 /*
26  if(!(str[i] == str1[i] ||
27  str[i] == ((str1[i] >= 'a' && str1[i] <= 'z') ? (str1[i] - 0x20) : (str1[i])) ||
28  str[i] == ((str1[i] >= 'A' && str1[i] <= 'Z') ? (str1[i] + 0x20) : (str1[i]))))
29 */
30  {
31  if(str[i] >= str1[i])
32  {
33  ret = 1;
34  }
35  else
36  {
37  ret = -1;
38  }
39  break;
40  }
41  i++;
42  }
43  }
44  return ret;
45 }
46 
47 const char *strRstr(const char *str, const char *str1)
48 {
49  const char *ret = NULL;
50  int i = 0;
51  int y = 0;
52  while(str[i] != 0)
53  {
54  y = 0;
55  while(static_cast<char>(tolower(str[i])) == static_cast<char>(tolower(str1[y])))
56 /*
57  while(str[i] == str1[y] ||
58  str[i] == ((str1[y] >= 'a' && str1[y] <= 'z') ? (str1[y] - 0x20) : (str1[y])) ||
59  str[i] == ((str1[y] >= 'A' && str1[y] <= 'Z') ? (str1[y] + 0x20) : (str1[y])))
60 */
61  {
62  i++;
63  y++;
64  if(str1[y] == 0)
65  {
66  ret = str + i - y;
67  break;
68  }
69  else if(str[i] == 0)
70  {
71  break;
72  }
73  }
74  if(str[i] != 0)
75  {
76  i++;
77  }
78  }
79  return ret;
80 }
81 
82 bool beganRstr(const char *str, const char *str1)
83 {
84  bool ret = false;
85  if(str && str1)
86  {
87  int i = 0;
88  ret = true;
89  while(str[i] != 0 && str1[i] != 0)
90  {
91  if(static_cast<char>(tolower(str[i])) != static_cast<char>(tolower(str1[i])))
92 /*
93  if(!(str[i] == str1[i] ||
94  str[i] == ((str1[i] >= 'a' && str1[i] <= 'z') ? (str1[i] - 0x20) : (str1[i])) ||
95  str[i] == ((str1[i] >= 'A' && str1[i] <= 'Z') ? (str1[i] + 0x20) : (str1[i]))))
96 */
97  {
98  ret= false;
99  break;
100  }
101  i++;
102  }
103  if((str[i] == 0) && (str1[i] != 0))
104  {
105  ret = false;
106  }
107  }
108  return ret;
109 }
110 
111 bool beganStr(const char *str, const char *str1)
112 {
113  bool ret = false;
114  if(str && str1)
115  {
116  int i = 0;
117  ret = true;
118  while(str[i] != 0 && str1[i] != 0)
119  {
120  if(str[i] != str1[i])
121  {
122  ret= false;
123  break;
124  }
125  i++;
126  }
127  if((str[i] == 0) && (str1[i] != 0))
128  {
129  ret = false;
130  }
131  }
132  return ret;
133 }
134 
135 char **breakBuffByString(const char *buff, char *strSeparate, unsigned int &stringsCount)
136 {
137  char **ret = NULL;
138  stringsCount = 0;
139  if(buff)
140  {
141  unsigned int len = strlen(buff);
142  if(strSeparate)
143  {
144  unsigned int localLen = strlen(strSeparate);
145  unsigned int count = 0;
146  char *baseBuff = NULL;
147  char *find = const_cast<char *>(buff);
148  while((find = strstr(find, strSeparate)) != NULL)
149  {
150  if(find == buff)
151  {
152  find += localLen;
153  if(find[0] == 0)
154  {
155  break;
156  }
157  continue;
158  }
159  find += localLen;
160  if(find[0] == 0)
161  {
162  count++;
163  break;
164  }
165  count++;
166  }
167 // cout++;
168  if(!find)
169  {
170  count++;
171  }
172  if(count > 0)
173  {
174  ret = new char *[count];
175  stringsCount = count;
176  for(unsigned int i = 0; i < count; i++)
177  {
178  ret[i] = new char[len + 1];
179  ret[i][0] = 0;
180  }
181  baseBuff = find = const_cast<char *>(buff);
182  count = 0;
183  while((find = strstr(find, strSeparate)) != NULL)
184  {
185  if(find == buff)
186  {
187  find += localLen;
188  baseBuff = find;
189  continue;
190  }
191  memcpy_arjanv_interleave(ret[count], baseBuff, find - baseBuff);
192  ret[count][find - baseBuff] = 0;
193  find += localLen;
194  baseBuff = find;
195  count++;
196  }
197  if(baseBuff < buff + len)
198  {
199  memcpy_arjanv_interleave(ret[count], baseBuff, buff + len + 1 - baseBuff);
200  }
201  }
202  }
203  else
204  {
205  ret = new char *[1];
206  ret[0] = new char [len + 1];
207  memcpy_arjanv_interleave(ret[0], buff, len + 1);
208  }
209  }
210  return ret;
211 }
212 
213 char **breakBuffBySymbol(const char *buff, char symbol, unsigned int &stringsCount)
214 {
215  stringsCount = 0;
216  char **ret = NULL;
217  if(buff)
218  {
219  int y = 0;
220  int j = 0;
221  int k = 0;
222  int size = 0;
223  while(buff[y] != 0x00)
224  {
225  if(buff[y] == symbol)
226  {
227  if(y > 0 && buff[y - 1] != symbol)
228  {
229  stringsCount++;
230  }
231  }
232  y++;
233  }
234  if(y > 0 && buff[y] != symbol)
235  {
236  stringsCount++;
237  }
238  else
239  {
240  return ret;
241  }
242  size = y;
243  ret = new char* [stringsCount];
244  for(unsigned int i = 0; i < stringsCount; i++)
245  {
246  ret[i] = new char[size + 1];
247  }
248  y = 0;
249  while(buff[y] != 0x00 )
250  {
251  if(buff[y] == symbol)
252  {
253  if(y > 0 && buff[y - 1] != symbol)
254  {
255  ret[j][k] = 0x00;
256  k = 0;
257  j++;
258  }
259  y++;
260  continue;
261  }
262  ret[j][k] = buff[y];
263  k++;
264  y++;
265  }
266  ret[j][k] = 0x00;
267  }
268  return ret;
269 }
270 
271 void breakBeginSubStr(char **massStrings, unsigned int stringsCount, const char *subStr)
272 {
273  if(massStrings && stringsCount > 0 && subStr)
274  {
275  for(unsigned int i = 0; i < stringsCount; i++)
276  {
277  if(massStrings[i] && (strstr(massStrings[i], subStr) == massStrings[i]) && strcmp(massStrings[i], subStr) != 0)
278  {
279  memmove(massStrings[i], massStrings[i] + strlen(subStr), strlen(massStrings[i]) - strlen(subStr) + 1);
280  }
281  }
282  }
283 }
284 
285 const char *strRepl(const char *baseStr, const char **replFrom, const char **replTo, unsigned int replCount, bool chg)
286 {
287  char *ret = NULL;
288  if(baseStr && replFrom && replTo && replCount)
289  {
290  unsigned int size = strlen(baseStr);
291  unsigned int len = size;
292  unsigned int *replSizeFrom = new unsigned int[replCount];
293  unsigned int *replSizeTo = new unsigned int[replCount];
294  char *buff = NULL;
295  for(unsigned int i = 0; i < replCount; i++)
296  {
297  buff = const_cast<char *>(baseStr);
298  if(replFrom[i] && replTo[i])
299  {
300  replSizeFrom[i] = strlen(replFrom[i]);
301  replSizeTo[i] = strlen(replTo[i]);
302  while(buff)
303  {
304  buff = strstr(buff, replFrom[i]);
305  if(buff)
306  {
307  size += replSizeTo[i];
308  buff += replSizeFrom[i];
309  }
310  }
311  }
312  }
313  if(size > 0U)
314  {
315  char *buff1 = new char[size + 1];
316  char *buff2 = new char[size + 1];
317  unsigned int num1 = 0;
318  unsigned int num2 = 0;
319  memcpy_arjanv_interleave(buff1, baseStr, len + 1);
320  char *tmpBuff = NULL;
321  for(unsigned int i = 0; i < replCount; i++)
322  {
323  num1 = 0;
324  num2 = 0;
325  tmpBuff = buff1;
326  if(replFrom[i] && replTo[i])
327  {
328  while(tmpBuff)
329  {
330  tmpBuff = strstr(tmpBuff, replFrom[i]);
331  if(tmpBuff)
332  {
333  strncpy(buff2 + num2, buff1 + num1, tmpBuff - (buff1 + num1));
334  num2 += tmpBuff - (buff1 + num1);
335  buff2[num2] = 0;
336  if(replTo[i])
337  {
338  strcat(buff2, replTo[i]);
339  num2 += replSizeTo[i];
340  }
341  tmpBuff += replSizeFrom[i];
342  num1 = tmpBuff - buff1;
343  }
344  }
345  }
346  if(num1 > 0)
347  {
348  strncpy(buff2 + num2, buff1 + num1, strlen(buff1) - num1);
349  buff2[num2 + strlen(buff1) - num1] = 0;
350  tmpBuff = buff1;
351  buff1 = buff2;
352  buff2 = tmpBuff;
353  }
354  }
355  DELL_BUFF(buff2)
356  ret = buff1;
357  }
358  DELL_BUFF(replSizeFrom)
359  DELL_BUFF(replSizeTo)
360  }
361  if(!ret && chg)
362  {
363  ret = const_cast<char *>(baseStr);
364  }
365  return ret;
366 }
367 
368 const char *getFileExtension(const char *str)
369 {
370  char *ret = NULL;
371  if(str)
372  {
373  unsigned int i = 0;
374  unsigned int y = 0;
375  unsigned int size = strlen(str);
376  if(strstr(str, "https://") == str)
377  {
378  str += 8;
379  }
380  else if(strstr(str, "http://") == str)
381  {
382  str += 7;
383  }
384  while(str[i] != '/' && str[i] != 0)
385  {
386  i++;
387  }
388  if(str[i] == '/')
389  {
390  while(str[i] != '.' && str[i] != 0)
391  {
392  i++;
393  }
394  if(str[i] == '.')
395  {
396  i++;
397  ret = new char[size + 1];
398  while(str[i] != '?' && str[i] != 0)
399  {
400  ret[y] = str[i];
401  i++;
402  y++;
403  }
404  if(y > 0)
405  {
406  ret[y] = 0;
407  }
408  else
409  {
410  DELL_BUFF(ret)
411  }
412  }
413  }
414  }
415  return ret;
416 }
417 
418 void recurseRemove(const char *fName)
419 {
420  struct stat sStat;
421  char localFname[1024];
422  unsigned int fNameLen = strlen(fName);
423  unsigned int additionChar = 0;
424  if(stat(fName, &sStat) == 0)
425  {
426  if(sStat.st_mode & S_IFDIR)
427  {
428  DIR *fDir = opendir(fName);
429  if (fDir)
430  {
431  struct dirent *dirEntr = NULL;
432  while((dirEntr = readdir(fDir)) != NULL)
433  {
434  additionChar = 0;
435  if(strcmp(dirEntr->d_name, ".") != 0 && strcmp(dirEntr->d_name, "..") != 0)
436  {
437  if((fNameLen + strlen(dirEntr->d_name)) < 1022)
438  {
439  memcpy(localFname, fName, fNameLen);
440  if(localFname[fNameLen - 1] != '/')
441  {
442  localFname[fNameLen] = '/';
443  additionChar = 1;
444  }
445  memcpy(localFname + fNameLen + additionChar, dirEntr->d_name, strlen(dirEntr->d_name) + 1);
446  recurseRemove(localFname);
447  }
448  }
449  }
450  closedir(fDir);
451  }
452  }
453  remove(fName);
454  }
455 }
456 
457 void createGMTString(char *ret, const char *previousStr, time_t timeVal)
458 {
459  if(timeVal > 0 && ret)
460  {
461  struct tm *localTime = gmtime(&timeVal);
462  if(localTime)
463  {
464  sprintf(ret, "%s%s, %02d %s %4d %02d:%02d:%02d GMT", (previousStr ? previousStr : ""),
465  wkdayArray[localTime->tm_wday ? (localTime->tm_wday - 1) : 6], localTime->tm_mday, monthArray[localTime->tm_mon],
466  localTime->tm_year + 1900, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
467  }
468  }
469 }
470 
471 const char *createGMTString(const char *previousStr, time_t timeVal)
472 {
473  char *ret = NULL;
474  if(timeVal > 0)
475  {
476  struct tm *localTime = gmtime(&timeVal);
477  if(localTime)
478  {
479  ret = new char[(previousStr ? strlen(previousStr) : 0 ) + 256];
480  sprintf(ret, "%s%s, %02d %s %4d %02d:%02d:%02d GMT", (previousStr ? previousStr : ""),
481  wkdayArray[localTime->tm_wday ? (localTime->tm_wday - 1) : 6], localTime->tm_mday, monthArray[localTime->tm_mon],
482  localTime->tm_year + 1900, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
483  }
484  }
485  return ret;
486 }