common.c 9.9 KB

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