common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * common.c
  3. *
  4. * Created on: 2020¦~5¤ë27¤é
  5. * Author: foluswen
  6. */
  7. #include "Module_OcppBackend20.h"
  8. void refreshStartTimer(struct timespec *timer)
  9. {
  10. clock_gettime(CLOCK_MONOTONIC, timer);
  11. }
  12. int getDiffSecNow(struct timespec timer)
  13. {
  14. struct timespec timerNow;
  15. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  16. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  17. }
  18. int getDiffSecBetween(struct timespec start, struct timespec end)
  19. {
  20. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  21. }
  22. void work(char s[]) // Delete space on start & end
  23. {
  24. int i,j;
  25. for(i=0;s[i]==' ';i++); // Search first non space
  26. for(j=0;s[i];)s[j++]=s[i++]; // Delete space in start
  27. for(i--;s[i]==' ';i--)s[i]='\0'; // Delete space in end
  28. }
  29. char* strchr(const char *p, int ch)
  30. {
  31. char c;
  32. c = ch;
  33. for (;; ++p) {
  34. if (*p == c)
  35. return ((char *)p);
  36. if (*p == '\0')
  37. return (NULL);
  38. }
  39. /* NOTREACHED */
  40. return NULL;
  41. }
  42. void splitstring(char *src, const char *separator, char **dest,int *num)
  43. {
  44. char *pNext;
  45. int count = 0;
  46. if (src == NULL || strlen(src) == 0)
  47. return;
  48. if (separator == NULL || strlen(separator) == 0)
  49. return;
  50. pNext = (char *)strtok(src,separator);
  51. while(pNext != NULL)
  52. {
  53. *dest++ = pNext;
  54. ++count;
  55. pNext = (char *)strtok(NULL,separator);
  56. }
  57. *num = count;
  58. }
  59. char* stringtrim( char * s )
  60. {
  61. char * p1 = s;
  62. char * p2 = s;
  63. while(*p1 != '\0')
  64. {
  65. while(*p1 == ' ' || *p1 == '\t' || *p1 == '\"' || *p1 == '\n' || *p1 == '}' || *p1 == '\r')
  66. {
  67. if(*p1 != ',')
  68. {
  69. p1 ++;
  70. }
  71. else
  72. {
  73. break;
  74. }
  75. }
  76. if(*p1 != ',')
  77. {
  78. * p2 ++ = *p1 ++;
  79. //printf("p2=%s\n",p2);
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. *p2 = '\0';
  87. return (s);
  88. }
  89. char* stringtrimspace( char * s )
  90. {
  91. char * p1 = s;
  92. char * p2 = s;
  93. while(*p1 != '\0')
  94. {
  95. while(*p1 == ' ') //while(*p1 == ' ' || *p1 == '\t' || *p1 == '\n' || *p1 == '\r')
  96. {
  97. p1 ++;
  98. }
  99. * p2 ++ = *p1 ++;
  100. //printf("p2=%s\n",p2);
  101. }
  102. *p2 = '\0';
  103. return (s);
  104. }
  105. int DiffTimeb(struct timeb ST, struct timeb ET)
  106. {
  107. //return milli-second
  108. unsigned int StartTime,StopTime;
  109. StartTime=(unsigned int)ST.time;
  110. StopTime=(unsigned int)ET.time;
  111. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  112. }
  113. void trim(char *s)
  114. {
  115. int i=0, j, k, l=0;
  116. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  117. i++;
  118. j = strlen(s)-1;
  119. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  120. j--;
  121. if(i==0 && j==strlen(s)-1) { }
  122. else if(i==0) s[j+1] = '\0';
  123. else {
  124. for(k=i; k<=j; k++) s[l++] = s[k];
  125. s[l] = '\0';
  126. }
  127. }
  128. int mystrcmp(char *p1,char *p2)
  129. {
  130. while(*p1==*p2)
  131. {
  132. if(*p1=='\0' || *p2=='\0')
  133. break;
  134. p1++;
  135. p2++;
  136. }
  137. if(*p1=='\0' && *p2=='\0')
  138. return(PASS);
  139. else
  140. return(FAIL);
  141. }
  142. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  143. {
  144. strncpy(dest, src + start, cnt);
  145. dest[cnt] = 0;
  146. }
  147. int strposs(char *source, char *substr, int idx)
  148. {
  149. char stack[strlen(source)];
  150. int result=0;
  151. int count=0;
  152. while(count<=idx)
  153. {
  154. memset(stack,0,sizeof stack);
  155. strncpy(stack, source+result, strlen(source)-result);
  156. int loc = strcspn(stack, substr);
  157. if(loc>0)
  158. result += (loc+1);
  159. else
  160. result = -1;
  161. count++;
  162. }
  163. return result;
  164. }
  165. void getSubStr(char *dest, char* src, char *split, int idx)
  166. {
  167. int start = (strposs(src,",",idx)+1);
  168. int cnt = (strposs(src,",",idx+1)-2)-(strposs(src,",",idx)+1);
  169. strncpy(dest, src + start, cnt);
  170. dest[cnt] = 0;
  171. }
  172. void split(char **arr, char *str, const char *del)
  173. {
  174. char *s = strtok(str, del);
  175. while(s != NULL)
  176. {
  177. *arr++ = s;
  178. s = strtok(NULL, del);
  179. }
  180. }
  181. int strpos(char *source, char *substr, int skip)
  182. {
  183. char stack[strlen(source)];
  184. strncpy(stack, source+skip, strlen(source)-skip);
  185. char *p = strstr(stack, substr);
  186. if (p)
  187. return p - stack+skip;
  188. return -1;
  189. }
  190. char* strtrim( char * s )
  191. {
  192. char * p1 = s;
  193. char * p2 = s;
  194. while(*p1 != '\0')
  195. {
  196. while(*p1 == '\t' || *p1 == '\n' || *p1 == '\r') //while(*p1 == ' ' || *p1 == '\t' || *p1 == '\n' || *p1 == '\r')
  197. {
  198. p1 ++;
  199. }
  200. * p2 ++ = *p1 ++;
  201. //printf("p2=%s\n",p2);
  202. }
  203. *p2 = '\0';
  204. return (s);
  205. }
  206. char* strtrimc( char * s )
  207. {
  208. char * p1 = s;
  209. char * p2 = s;
  210. while(*p1 != '\0')
  211. {
  212. while(*p1 == ' ' || *p1 == '\t' || *p1 == '\"' || *p1 == '\n' || *p1 == '}' || *p1 == '\r')
  213. {
  214. if(*p1 != ',')
  215. {
  216. p1 ++;
  217. }
  218. else
  219. {
  220. break;
  221. }
  222. }
  223. if(*p1 != ',')
  224. {
  225. * p2 ++ = *p1 ++;
  226. //printf("p2=%s\n",p2);
  227. }
  228. else
  229. {
  230. break;
  231. }
  232. }
  233. *p2 = '\0';
  234. return (s);
  235. }
  236. char* random_uuid(char* buf)
  237. {
  238. uuid_t uuid;
  239. uuid_generate(uuid);
  240. uuid_unparse(uuid, buf);
  241. return 0;
  242. }
  243. //===========================================================
  244. // URL parsing function
  245. //===========================================================
  246. /**
  247. * Parse a non null terminated string into an integer.
  248. *
  249. * str: the string containing the number.
  250. * len: Number of characters to parse.
  251. */
  252. static inline int
  253. natoi(const char *str, size_t len)
  254. {
  255. int i, r = 0;
  256. for (i = 0; i < len; i++) {
  257. r *= 10;
  258. r += str[i] - '0';
  259. }
  260. return r;
  261. }
  262. /**
  263. * Check if a URL is relative (no scheme and hostname).
  264. *
  265. * url: the string containing the URL to check.
  266. *
  267. * Returns 1 if relative, otherwise 0.
  268. */
  269. static inline int
  270. is_relative(const char *url)
  271. {
  272. return (*url == '/') ? 1 : 0;
  273. }
  274. /**
  275. * Parse the scheme of a URL by inserting a null terminator after the scheme.
  276. *
  277. * str: the string containing the URL to parse. Will be modified.
  278. *
  279. * Returns a pointer to the hostname on success, otherwise NULL.
  280. */
  281. static inline char *
  282. parse_scheme(char *str)
  283. {
  284. char *s;
  285. /* If not found or first in string, return error */
  286. s = strchr(str, ':');
  287. if (s == NULL || s == str) {
  288. return NULL;
  289. }
  290. /* If not followed by two slashes, return error */
  291. if (s[1] == '\0' || s[1] != '/' || s[2] == '\0' || s[2] != '/') {
  292. return NULL;
  293. }
  294. *s = '\0'; // Replace ':' with NULL
  295. return s + 3;
  296. }
  297. /**
  298. * Find a character in a string, replace it with '\0' and return the next
  299. * character in the string.
  300. *
  301. * str: the string to search in.
  302. * find: the character to search for.
  303. *
  304. * Returns a pointer to the character after the one to search for. If not
  305. * found, NULL is returned.
  306. */
  307. static inline char *
  308. find_and_terminate(char *str, char find)
  309. {
  310. str = strchr(str, find);
  311. if (NULL == str) {
  312. return NULL;
  313. }
  314. *str = '\0';
  315. return str + 1;
  316. }
  317. /* Yes, the following functions could be implemented as preprocessor macros
  318. instead of inline functions, but I think that this approach will be more
  319. clean in this case. */
  320. static inline char *
  321. find_fragment(char *str)
  322. {
  323. return find_and_terminate(str, '#');
  324. }
  325. static inline char *
  326. find_query(char *str)
  327. {
  328. return find_and_terminate(str, '?');
  329. }
  330. static inline char *
  331. find_path(char *str)
  332. {
  333. return find_and_terminate(str, '/');
  334. }
  335. /**
  336. * Parse a URL string to a struct.
  337. *
  338. * url: pointer to the struct where to store the parsed URL parts.
  339. * u: the string containing the URL to be parsed.
  340. *
  341. * Returns 0 on success, otherwise -1.
  342. */
  343. int
  344. yuarel_parse(struct yuarel *url, char *u)
  345. {
  346. if (NULL == url || NULL == u) {
  347. return -1;
  348. }
  349. memset(url, 0, sizeof (struct yuarel));
  350. /* (Fragment) */
  351. url->fragment = find_fragment(u);
  352. /* (Query) */
  353. url->query = find_query(u);
  354. /* Relative URL? Parse scheme and hostname */
  355. if (!is_relative(u)) {
  356. /* Scheme */
  357. url->scheme = u;
  358. u = parse_scheme(u);
  359. if (u == NULL) {
  360. return -1;
  361. }
  362. /* Host */
  363. if ('\0' == *u) {
  364. return -1;
  365. }
  366. url->host = u;
  367. /* (Path) */
  368. url->path = find_path(u);
  369. /* (Credentials) */
  370. u = strchr(url->host, '@');
  371. if (NULL != u) {
  372. /* Missing credentials? */
  373. if (u == url->host) {
  374. return -1;
  375. }
  376. url->username = url->host;
  377. url->host = u + 1;
  378. *u = '\0';
  379. u = strchr(url->username, ':');
  380. if (NULL == u) {
  381. return -1;
  382. }
  383. url->password = u + 1;
  384. *u = '\0';
  385. }
  386. /* Missing hostname? */
  387. if ('\0' == *url->host) {
  388. return -1;
  389. }
  390. /* (Port) */
  391. u = strchr(url->host, ':');
  392. if (NULL != u && (NULL == url->path || u < url->path)) {
  393. *(u++) = '\0';
  394. if ('\0' == *u) {
  395. return -1;
  396. }
  397. if (url->path) {
  398. url->port = natoi(u, url->path - u - 1);
  399. } else {
  400. url->port = atoi(u);
  401. }
  402. }
  403. /* Missing hostname? */
  404. if ('\0' == *url->host) {
  405. return -1;
  406. }
  407. } else {
  408. /* (Path) */
  409. url->path = find_path(u);
  410. }
  411. return 0;
  412. }
  413. /**
  414. * Split a path into several strings.
  415. *
  416. * No data is copied, the slashed are used as null terminators and then
  417. * pointers to each path part will be stored in **parts. Double slashes will be
  418. * treated as one.
  419. *
  420. * path: the path to split.
  421. * parts: a pointer to an array of (char *) where to store the result.
  422. * max_parts: max number of parts to parse.
  423. */
  424. int
  425. yuarel_split_path(char *path, char **parts, int max_parts)
  426. {
  427. int i = 0;
  428. if (NULL == path || '\0' == *path) {
  429. return -1;
  430. }
  431. do {
  432. /* Forward to after slashes */
  433. while (*path == '/') path++;
  434. if ('\0' == *path) {
  435. break;
  436. }
  437. parts[i++] = path;
  438. path = strchr(path, '/');
  439. if (NULL == path) {
  440. break;
  441. }
  442. *(path++) = '\0';
  443. } while (i < max_parts);
  444. return i;
  445. }
  446. int
  447. yuarel_parse_query(char *query, char delimiter, struct yuarel_param *params, int max_params)
  448. {
  449. int i = 0;
  450. if (NULL == query || '\0' == *query) {
  451. return -1;
  452. }
  453. params[i++].key = query;
  454. while (i < max_params && NULL != (query = strchr(query, delimiter))) {
  455. *query = '\0';
  456. params[i].key = ++query;
  457. params[i].val = NULL;
  458. /* Go back and split previous param */
  459. if (i > 0) {
  460. if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL) {
  461. *(params[i - 1].val)++ = '\0';
  462. }
  463. }
  464. i++;
  465. }
  466. /* Go back and split last param */
  467. if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL) {
  468. *(params[i - 1].val)++ = '\0';
  469. }
  470. return i;
  471. }