PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/soar/1.0/common/c/src/basic/misc.c

http://arronwork.googlecode.com/
C | 718 lines | 616 code | 89 blank | 13 comment | 143 complexity | b581985aeec4962bc847fe7c7ad98be1 MD5 | raw file
  1. #include <basic/errcode.h>
  2. #include <basic/misc.h>
  3. /*==========*/
  4. /* ???? */
  5. /*==========*/
  6. static char *_hex_char="0123456789ABCDEF";
  7. static unsigned char _char_offset(char c)
  8. {/*{{{*/
  9. unsigned char b;
  10. for(b=0; b<16; b++)
  11. if(_hex_char[b] == c) return(b);
  12. return('\xff');
  13. }/*}}}*/
  14. char *expand_to_str(unsigned char *hex, int size, char *str)
  15. {/*{{{*/
  16. char *strtmp;
  17. int strsize;
  18. int i;
  19. unsigned char h;
  20. if(!hex || size < 0 || !str)
  21. return(NULL);
  22. strsize=size*2;
  23. ALLOC_MULTI_RETERR(strtmp, char, strsize+1, NULL);
  24. for(i=0; i<size; i++)
  25. {
  26. h=hex[i];
  27. strtmp[i*2]=_hex_char[h >> 4];
  28. strtmp[i*2+1]=_hex_char[h & 0x0F];
  29. }
  30. strncpy(str, strtmp, strsize);
  31. str[strsize]=ZERO;
  32. free(strtmp);
  33. return(str);
  34. }/*}}}*/
  35. unsigned char *collapse_from_str(char *str, int size, unsigned char *hex)
  36. {/*{{{*/
  37. unsigned char *hextmp;
  38. int hexsize;
  39. int i;
  40. unsigned char high,low;
  41. if(!str || size < 0 || size & 0x01 || !hex)
  42. return(NULL);
  43. hexsize=size/2;
  44. ALLOC_MULTI_RETERR(hextmp, unsigned char, hexsize+1, NULL);
  45. for(i=0; i<size; i+=2)
  46. {
  47. high=_char_offset(str[i]);
  48. low=_char_offset(str[i+1]);
  49. if(high == 0xff || low == 0xff)
  50. {
  51. free(hextmp);
  52. return(NULL);
  53. }
  54. hextmp[i/2]=high<<4 | low;
  55. }
  56. memcpy(hex, hextmp, hexsize);
  57. free(hextmp);
  58. return(hex);
  59. }/*}}}*/
  60. char *trim_head_space(char *str)
  61. {/*{{{*/
  62. if(str)
  63. {
  64. int i, j;
  65. int len;
  66. len=strlen(str);
  67. i=0;
  68. while(str[i] == SPACE && i < len) i++;
  69. for(j=i; j<len; j++)
  70. str[j-i]=str[j];
  71. str[j-i]=ZERO;
  72. }
  73. return(str);
  74. }/*}}}*/
  75. char *trim_tail_space(char *str)
  76. {/*{{{*/
  77. if(str)
  78. {
  79. int i;
  80. if(str[0] == ZERO) return(0);
  81. for(i=strlen(str)-1; i>=0; i--)
  82. if(str[i] != SPACE) break;
  83. str[i+1]=ZERO;
  84. }
  85. return(str);
  86. }/*}}}*/
  87. char *trim_tail_space_notnull(char *str)
  88. {/*{{{*/
  89. if(str)
  90. {
  91. trim_tail_space(str);
  92. if(str[0] == ZERO)
  93. strcpy(str, SPACES);
  94. }
  95. return(str);
  96. }/*}}}*/
  97. char *trim_redundant_space(char *str)
  98. {/*{{{*/
  99. if(str)
  100. {
  101. int i;
  102. int str_size, str_real_size;
  103. enum { NOT_FOUND, FOUND } found;
  104. str_size=strlen(str);
  105. trim_head_space(str);
  106. trim_tail_space(str);
  107. found=NOT_FOUND;
  108. str_real_size=0;
  109. for(i=0; i<str_size; i++)
  110. {
  111. if(str[i]==SPACE && found==NOT_FOUND)
  112. {
  113. found=FOUND;
  114. str[str_real_size++]=str[i];
  115. continue;
  116. }
  117. if(str[i]==SPACE && found==FOUND)
  118. { continue; }
  119. found=NOT_FOUND;
  120. str[str_real_size++]=str[i];
  121. }
  122. str[str_real_size]=ZERO;
  123. }
  124. return(str);
  125. }/*}}}*/
  126. char *trim_space(char *str)
  127. {/*{{{*/
  128. trim_head_space(str);
  129. trim_tail_space(str);
  130. return(str);
  131. }/*}}}*/
  132. char *pad_space(char *str, int buflen)
  133. {/*{{{*/
  134. if(buflen <= 0) return(NULL);
  135. if(str)
  136. {
  137. int str_len;
  138. if((str_len=strlen(str)) < buflen)
  139. {
  140. memset(str+str_len, SPACE, buflen-str_len);
  141. str[buflen]=ZERO;
  142. }
  143. }
  144. return(str);
  145. }/*}}}*/
  146. char *expand_tab(char *str, int buflen, int unit)
  147. {/*{{{*/
  148. int slen;
  149. char *tmpstr;
  150. int src_index, dest_index;
  151. if(!str) return(NULL);
  152. if(unit <= 0 || unit > 8) unit=4;
  153. if(buflen > 0)
  154. {
  155. int tabcount;
  156. slen=0; tabcount=0;
  157. while(str[slen] != ZERO)
  158. {
  159. if(str[slen] == TAB) tabcount++;
  160. slen++;
  161. }
  162. if(slen+tabcount*(unit-1) > buflen-1)
  163. return(NULL);
  164. }
  165. else
  166. slen=strlen(str);
  167. ALLOC_MULTI_RETERR(tmpstr, char, slen+1, NULL);
  168. strcpy(tmpstr, str);
  169. for(src_index=0, dest_index=0; src_index<slen; src_index++)
  170. {
  171. if(tmpstr[src_index] == TAB)
  172. {
  173. memset(str+dest_index, SPACE, unit);
  174. dest_index+=unit;
  175. }
  176. else
  177. {
  178. str[dest_index]=tmpstr[src_index];
  179. dest_index++;
  180. }
  181. }
  182. str[dest_index]=ZERO;
  183. free(tmpstr);
  184. return(str);
  185. }/*}}}*/
  186. unsigned int get_hash(unsigned char *data, int len)
  187. {/*{{{*/
  188. unsigned int hash;
  189. int i;
  190. if(!data || len <= 0) return(0);
  191. hash=0x3e9a168c;
  192. for(i=0; i<len; i++)
  193. hash=(hash << 5) + hash + *data++;
  194. return(hash);
  195. }/*}}}*/
  196. int wrap_size(unsigned char *msg_in, int *msg_size,
  197. unsigned char *msg_out)
  198. {/*{{{*/
  199. char buf[10];
  200. if(!msg_in ||
  201. !msg_size || *msg_size < 0 ||
  202. *msg_size > MSG_SIZE-MSG_SIZE_SIZE ||
  203. !msg_out)
  204. return(RET_ERR_PARA);
  205. sprintf(buf, "%06X", *msg_size);
  206. memmove(msg_out+MSG_SIZE_SIZE, msg_in, *msg_size);
  207. memcpy(msg_out, buf, SHORT_NUMBER_SIZE);
  208. (*msg_size)+=MSG_SIZE_SIZE;
  209. msg_out[*msg_size]=ZERO;
  210. return(0);
  211. }/*}}}*/
  212. int unwrap_size(unsigned char *msg_in, int *msg_size,
  213. unsigned char *msg_out)
  214. {/*{{{*/
  215. char buf[10];
  216. if(!msg_in
  217. || !msg_size
  218. || *msg_size > MSG_SIZE || *msg_size < MSG_SIZE_SIZE
  219. || !msg_out)
  220. return(RET_ERR_PARA);
  221. memcpy(buf, msg_in, MSG_SIZE_SIZE);
  222. buf[MSG_SIZE_SIZE]=ZERO;
  223. *msg_size=strtol(buf, NULL, 16);
  224. memmove(msg_out, msg_in+MSG_SIZE_SIZE, *msg_size);
  225. msg_out[*msg_size]=ZERO;
  226. return(0);
  227. }/*}}}*/
  228. int split_str(char *str, char delim, char ***arrp)
  229. {/*{{{*/
  230. char *in;
  231. char **arr;
  232. int str_size;
  233. char delimiter[2];
  234. char *itemp, *currp;
  235. int item_num;
  236. int item_alloc_num;
  237. if(!arrp)
  238. return(RET_ERR_PARA);
  239. *arrp=(char **)NULL;
  240. if(!str || (str_size=strlen(str)) <= 0 || str_size > MSG_SIZE)
  241. return(RET_ERR_PARA);
  242. if(!(in=strdup(str)))
  243. return(RET_ERR_ALLOC);
  244. if(delim == ZERO)
  245. {
  246. int len=strlen(in);
  247. while(len--)
  248. if(in[len] == TAB) in[len]=SPACE;
  249. delim=SPACE;
  250. }
  251. if(delim == SPACE)
  252. trim_redundant_space(in);
  253. delimiter[0]=delim; delimiter[1]=ZERO;
  254. item_num=item_alloc_num=0;
  255. arr=(char **)NULL;
  256. if((itemp=strtok_r(in, delimiter, &currp)))
  257. {
  258. do
  259. {
  260. #define _EXPAND_NUM 5
  261. if(item_num == item_alloc_num)
  262. EXPAND_ARRAY(arr, char *, _EXPAND_NUM,
  263. item_alloc_num, OUT_ARRAY);
  264. if(!(arr[item_num]=strdup(itemp)))
  265. goto OUT_ARRAY;
  266. item_num++;
  267. } while((itemp=strtok_r(NULL, delimiter, &currp)));
  268. }
  269. free(in);
  270. *arrp=arr;
  271. return(item_num);
  272. OUT_ARRAY:
  273. FREE_PTR_ARRAY(arr, item_num);
  274. free(in);
  275. return(RET_ERR_ALLOC);
  276. }/*}}}*/
  277. char *str_toupper(char *str)
  278. {/*{{{*/
  279. if(str)
  280. {
  281. int c;
  282. int len;
  283. len=strlen(str);
  284. while(len--)
  285. {
  286. c=toupper(str[len]);
  287. str[len]=c;
  288. }
  289. }
  290. return(str);
  291. }/*}}}*/
  292. char *str_tolower(char *str)
  293. {/*{{{*/
  294. if(str)
  295. {
  296. int c;
  297. int len;
  298. len=strlen(str);
  299. while(len--)
  300. {
  301. c=tolower(str[len]);
  302. str[len]=c;
  303. }
  304. }
  305. return(str);
  306. }/*}}}*/
  307. char *replace_str(char *src, char *old, char *rep, char *dest)
  308. {/*{{{*/
  309. int len;
  310. int count;
  311. int src_size, old_size, rep_size, dest_size;
  312. char *srcp, *oldp, *destp, *allocp;
  313. if(!src) return(NULL);
  314. src_size=strlen(src);
  315. if(!old || !rep)
  316. {
  317. if(!dest)
  318. ALLOC_MULTI_RETERR(dest, char, src_size+1, NULL);
  319. if(src != dest)
  320. strcpy(dest, src);
  321. return(dest);
  322. }
  323. old_size=strlen(old);
  324. rep_size=strlen(rep);
  325. if(rep_size > old_size)
  326. {
  327. count=0;
  328. oldp=srcp=src;
  329. while((oldp=strstr(srcp, old)))
  330. {
  331. srcp=oldp+old_size;
  332. count++;
  333. }
  334. dest_size=src_size+count*(rep_size-old_size);
  335. }
  336. else
  337. dest_size=src_size;
  338. if(dest == src) // ??????????????
  339. {
  340. ALLOC_MULTI_RETERR(allocp, char, dest_size+1, NULL);
  341. }
  342. else
  343. {
  344. if(!dest) // ???
  345. ALLOC_MULTI_RETERR(allocp, char, dest_size+1, NULL);
  346. else
  347. allocp=dest;
  348. }
  349. oldp=srcp=src;
  350. destp=allocp;
  351. while((oldp=strstr(srcp, old)))
  352. {
  353. len=oldp-srcp;
  354. memcpy(destp, srcp, len);
  355. destp+=len;
  356. memcpy(destp, rep, rep_size);
  357. destp+=rep_size;
  358. srcp=oldp+old_size;
  359. }
  360. len=src+src_size-srcp;
  361. memcpy(destp, srcp, len);
  362. destp+=len;
  363. *destp=ZERO;
  364. if(dest == src) // ??????????????
  365. {
  366. strcpy(dest, allocp);
  367. free(allocp);
  368. }
  369. else
  370. {
  371. if(!dest) // ???
  372. dest=allocp;
  373. }
  374. return(dest);
  375. }/*}}}*/
  376. int str_is_dec(char *str)
  377. {/*{{{*/
  378. if(str)
  379. {
  380. int i;
  381. int len=strlen(str);
  382. for(i=0; i<len; i++)
  383. if(!isdigit(str[i])) return(0);
  384. return(1);
  385. }
  386. return(0);
  387. }/*}}}*/
  388. int str_is_hex(char *str)
  389. {/*{{{*/
  390. if(str)
  391. {
  392. int i;
  393. for(i=0; i<strlen(str); i++)
  394. if(!isxdigit(str[i])) return(0);
  395. return(1);
  396. }
  397. return(0);
  398. }/*}}}*/
  399. int dec_to_hex(int dec)
  400. {/*{{{*/
  401. char buf[20];
  402. sprintf(buf, "%d", dec);
  403. return(strtol(buf, NULL, 16));
  404. }/*}}}*/
  405. int pack_size(int org_size, int unit)
  406. {/*{{{*/
  407. int res;
  408. if(org_size <= 0)
  409. return(0);
  410. if(unit <= 1)
  411. return(org_size);
  412. res=org_size % unit;
  413. return(res ? org_size+unit-res : org_size);
  414. }/*}}}*/
  415. /*======*/
  416. /* ?? */
  417. /*======*/
  418. unsigned short get_crc_16(unsigned char *p, int count)
  419. {/*{{{*/
  420. static unsigned short a_crc_16[16]=
  421. {
  422. 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
  423. 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
  424. };
  425. unsigned short r, crc = 0;
  426. while(count--)
  427. {
  428. r=a_crc_16[crc & 0xF];
  429. crc=(crc>>4) & 0x0FFF;
  430. crc=crc^r^a_crc_16[*p & 0xF];
  431. r=a_crc_16[crc & 0xF];
  432. crc=(crc>>4) & 0x0FFF;
  433. crc=crc^r^a_crc_16[(*p>>4) & 0xF];
  434. p++;
  435. }
  436. return(crc);
  437. }/*}}}*/
  438. /*========*/
  439. /* ??? */
  440. /*========*/
  441. int random_int(int min_val, int max_val, unsigned int *seedp)
  442. {/*{{{*/
  443. if(min_val != max_val)
  444. {
  445. struct timeval time_val;
  446. unsigned int seed;
  447. if(min_val > max_val)
  448. {
  449. int tmp;
  450. tmp=min_val;
  451. min_val=max_val;
  452. max_val=tmp;
  453. }
  454. if(seedp)
  455. {
  456. if(*seedp == 0)
  457. {
  458. gettimeofday(&time_val, NULL);
  459. *seedp=(unsigned int)time_val.tv_usec;
  460. }
  461. }
  462. else
  463. {
  464. gettimeofday(&time_val, NULL);
  465. seed=time_val.tv_usec;
  466. seedp=&seed;
  467. }
  468. return(rand_r(seedp)%(max_val-min_val+1)+min_val);
  469. }
  470. return(min_val);
  471. }/*}}}*/
  472. int random_array_int(int array[], int num)
  473. {/*{{{*/
  474. if(!array || num <= 0)
  475. return(RET_ERR_PARA);
  476. if(num > 1)
  477. {
  478. int *arraytmp;
  479. int idx;
  480. unsigned int seed=0;
  481. ALLOC_MULTI_RETERR(arraytmp, int, num, RET_ERR_ALLOC);
  482. memcpy(arraytmp, array, num*sizeof(int));
  483. while(num > 0)
  484. {
  485. idx=random_int(0, num-1, &seed);
  486. array[num-1]=arraytmp[idx];
  487. memmove(&arraytmp[idx], &arraytmp[idx+1],
  488. (num-idx-1)*sizeof(int));
  489. num--;
  490. }
  491. free(arraytmp);
  492. }
  493. return(0);
  494. }/*}}}*/
  495. static char CHARBASE[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  496. char *random_password(char *password, int size)
  497. {/*{{{*/
  498. if(!password)
  499. return(password);
  500. CLR_BUF(password);
  501. if(size <= 0)
  502. return(password);
  503. unsigned int seed=0;
  504. int i;
  505. for(i=0; i<size; ++i)
  506. {
  507. password[i]=CHARBASE[random_int(0, strlen(CHARBASE)-1, &seed)];
  508. }
  509. password[size]=ZERO;
  510. return(password);
  511. }/*}}}*/
  512. /*==============*/
  513. /* ?????? */
  514. /*==============*/
  515. char *extract_dirname(char *filename, char *dirname)
  516. {/*{{{*/
  517. if(dirname)
  518. {
  519. CLR_BUF(dirname);
  520. if(filename)
  521. {
  522. char fullname[LONG_FILE_NM_SIZE+1];
  523. char *p;
  524. int len;
  525. strcpy(fullname, filename);
  526. len=strlen(fullname);
  527. if(strcmp(fullname, "/"))
  528. {
  529. if(fullname[len-1] == SLASH)
  530. fullname[len-1]=ZERO;
  531. if((p=strrchr(fullname, SLASH)))
  532. {
  533. len=p-fullname+1;
  534. strncpy(dirname, fullname, len);
  535. if(len > 1) len--;
  536. dirname[len]=ZERO;
  537. }
  538. else
  539. strcpy(dirname, ".");
  540. }
  541. else
  542. strcpy(dirname, "/");
  543. }
  544. }
  545. return(dirname);
  546. }/*}}}*/
  547. char *extract_basename(char *filename, char *basename)
  548. {/*{{{*/
  549. if(basename)
  550. {
  551. CLR_BUF(basename);
  552. if(filename)
  553. {
  554. char fullname[LONG_FILE_NM_SIZE+1];
  555. char *p;
  556. int len;
  557. strcpy(fullname, filename);
  558. len=strlen(fullname);
  559. if(strcmp(fullname, "/"))
  560. {
  561. if(fullname[len-1] == SLASH)
  562. fullname[len-1]=ZERO;
  563. if((p=strrchr(fullname, SLASH)))
  564. strcpy(basename, p+1);
  565. else
  566. strcpy(basename, fullname);
  567. }
  568. else
  569. strcpy(basename, "/");
  570. }
  571. }
  572. return(basename);
  573. }/*}}}*/
  574. int remove_dir(char *path)
  575. {/*{{{*/
  576. struct stat st;
  577. int len;
  578. DIR *dirp;
  579. struct dirent *dp;
  580. char buf[LONG_FILE_NM_SIZE+1];
  581. int result;
  582. if(stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
  583. { return(-1); }
  584. len=strlen(path);
  585. dirp=opendir(path);
  586. while((dp=readdir(dirp)))
  587. {
  588. if(dp->d_name[0] == '.')
  589. continue;
  590. sprintf(buf, "%s/%s", path, dp->d_name);
  591. if(stat(buf, &st) != 0)
  592. { closedir(dirp); return(-1); }
  593. //printf("%s\n", buf);
  594. if(S_ISDIR(st.st_mode))
  595. {
  596. if((result=remove_dir(buf)))
  597. { closedir(dirp); return(result); }
  598. }
  599. else
  600. if(unlink(buf))
  601. { closedir(dirp); return(-2); }
  602. }
  603. closedir(dirp);
  604. if(rmdir(path))
  605. { return(-3); }
  606. return(0);
  607. }/*}}}*/
  608. int create_dir(char *dir)
  609. {/*{{{*/
  610. int res;
  611. struct stat st;
  612. if(!dir)
  613. return(RET_ERR_PARA);
  614. if(stat(dir, &st))
  615. {
  616. if((res=get_last_error(NULL)) != ENOENT)
  617. { return(-1); }
  618. if(mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR |
  619. S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
  620. { return(-2); }
  621. }
  622. else
  623. {
  624. if(!S_ISDIR(st.st_mode) || !(st.st_mode & S_IRUSR) ||
  625. !(st.st_mode & S_IWUSR) || !(st.st_mode & S_IXUSR))
  626. { return(-3); }
  627. }
  628. return(0);
  629. }/*}}}*/