PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/soar/1.1/common/c/include/basic/misc.h

http://arronwork.googlecode.com/
C++ Header | 524 lines | 368 code | 56 blank | 100 comment | 19 complexity | 719fa5fa4758f92be76c0037652e2163 MD5 | raw file
  1. #ifndef __BASIC_MISC_H__
  2. #define __BASIC_MISC_H__
  3. #if defined(__linux__)
  4. #ifndef _GNU_SOURCE
  5. #define _GNU_SOURCE
  6. #endif
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <inttypes.h>
  11. #include <time.h>
  12. #include <math.h>
  13. #include <string.h>
  14. #include <strings.h>
  15. #include <errno.h>
  16. #include <ctype.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #include <sys/wait.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <stdarg.h>
  24. #include <pthread.h>
  25. #include <signal.h>
  26. #include <semaphore.h>
  27. #include <mqueue.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/mman.h>
  31. #include <sys/sem.h>
  32. #include <sys/resource.h>
  33. #include <dirent.h>
  34. #include <getopt.h>
  35. #include <regex.h>
  36. #include <netinet/in.h>
  37. #include <sys/socket.h>
  38. #include <arpa/inet.h>
  39. #include <sys/un.h>
  40. #include <sys/select.h>
  41. #include <sys/poll.h>
  42. #include <netdb.h>
  43. #include <pwd.h>
  44. #if defined(__APPLE__) || defined(__FreeBSD__)
  45. #include <machine/endian.h>
  46. #else
  47. #include <endian.h>
  48. #endif
  49. __BEGIN_DECLS
  50. /*======*/
  51. /* ?? */
  52. /*======*/
  53. #define ZERO '\0' /* ??0 */
  54. #define ZEROS "" /* ??? */
  55. #define CHAR0 '0' /* ??'0' */
  56. #define RTN '\n' /* ?? */
  57. #define RTNS "\n" /* ???? */
  58. #define CRLF "\r\n" /* ??? */
  59. #define SLASH '/' /* ?? */
  60. #define BACKSLASH '\\' /* ??? */
  61. #define TAB '\t' /* TAB */
  62. #define TABS "\t" /* TAB? */
  63. #define SPACE ' ' /* ?? */
  64. #define SPACES " " /* ?????*/
  65. #define OPTION_FMT "%1d" /* ???? */
  66. #define SHORT_NUMBER_FMT "%06d" /* ????? */
  67. #define LONG_NUMBER_FMT "%012ld" /* ????? */
  68. #define LLONG_NUMBER_FMT "%012lld" /* ?????? */
  69. #define IS_TRUE 1 /* ?? */
  70. #define IS_FALSE 0 /* ?? */
  71. #define BIT_OF_BYTE 8 /* ???? */
  72. #define BIT_OF_CHAR BIT_OF_BYTE /* char?? */
  73. #define BIT_OF_INT (sizeof(int)*BIT_OF_CHAR) /* int?? */
  74. #define KB(n) ((n)*1024) /* n KB */
  75. #define MB(n) ((n)*KB(1024)) /* n MB */
  76. #define GB(n) ((n)*MB(1024)) /* n GB */
  77. #define MSG_SIZE_SIZE 6 /* ??????? */
  78. #define MSG_SIZE KB(1024) /* ???? */
  79. #define BUFFER_SIZE KB(1) /* ????? */
  80. #define FILE_NM_SIZE 80 /* ????? */
  81. #define LONG_FILE_NM_SIZE 200 /* ??????? */
  82. #define IP_SIZE 15 /* IP???? */
  83. #define OPTION_SIZE 1 /* ???? */
  84. #define SHORT_NUMBER_SIZE 6 /* ????? */
  85. #define LONG_NUMBER_SIZE 12 /* ????? */
  86. #define LLONG_NUMBER_SIZE 12 /* ?????? */
  87. #define NAME_SIZE 30 /* ???? */
  88. #define LONG_NAME_SIZE 200 /* ????? */
  89. #define PASSWD_MIN_SIZE 6 /* ???? */
  90. #define PASSWD_MAX_SIZE 15 /* ???? */
  91. #define PASSWD_ENC_SIZE 32 /* ??????? */
  92. /*======*/
  93. /* ?? */
  94. /*======*/
  95. #define FLAG_MULTI_THREAD 0x80000000 /* ??? */
  96. #define FLAG_LOCAL 0x40000000 /* ?? */
  97. #define FLAG_CASE_SENSITIVE 0x20000000 /* ????? */
  98. /*----- ???? -----*/
  99. #define CLR_BUF(_buf) (_buf)[0]=ZERO
  100. #define BUF_IS_CLR(_buf) ((_buf)[0]==ZERO)
  101. /*
  102. * 16????????
  103. * ???
  104. */
  105. extern char * /* ??str:?? NULL:??????0 */
  106. expand_to_str(
  107. unsigned char *hex, /* [IN] 16??? */
  108. int size, /* [IN] 16????? */
  109. char *str /* [OUT] ?? */
  110. );
  111. /*
  112. * ????16???
  113. */
  114. extern unsigned char * /* ??hex:?? NULL:??????0???2??? */
  115. collapse_from_str(
  116. char *str, /* [IN] ?? */
  117. int size, /* [IN] ???? */
  118. unsigned char *hex /* [OUT] 16??? */
  119. );
  120. /*
  121. * ?????????
  122. */
  123. extern char * /* ??str */
  124. trim_head_space(
  125. char *str /* [IN,OUT] ??? */
  126. );
  127. /*
  128. * ?????????
  129. */
  130. extern char * /* ??str */
  131. trim_tail_space(
  132. char *str /* [IN,OUT] ??? */
  133. );
  134. /*
  135. * ???????????????????????
  136. */
  137. extern char * /* ??str */
  138. trim_tail_space_notnull(
  139. char *str /* [IN,OUT] ??? */
  140. );
  141. /*
  142. * ??????
  143. */
  144. extern char * /* ??str */
  145. trim_redundant_space(
  146. char *str /* [IN,OUT] ??? */
  147. );
  148. /*
  149. * ?????????
  150. */
  151. extern char * /* ??str */
  152. trim_space(
  153. char *str /* [IN,OUT] ??? */
  154. );
  155. /*
  156. * ????????
  157. */
  158. extern char * /* ??str:?? NULL:?????<=0 */
  159. pad_space(
  160. char *str, /* [IN,OUT] ??? */
  161. int buflen /* [IN] ???????? */
  162. );
  163. /*
  164. * ?tab?????
  165. */
  166. extern char * /* ??str:?? NULL:??????? */
  167. expand_tab(
  168. char *str, /* [IN,OUT] ??? */
  169. int buflen, /* [IN] ???????? <=0:????? */
  170. int unit /* [IN] ???? <=0:??4?? */
  171. );
  172. /*
  173. * ????HASH?
  174. */
  175. extern unsigned int /* HASH?(???):?? 0:????????????0 */
  176. get_hash(
  177. unsigned char *data, /* [IN] ??? */
  178. int len /* [IN] ??HASH?????? */
  179. );
  180. /*
  181. * ??????
  182. */
  183. extern int /* 0:?? -1:???? */
  184. wrap_size(
  185. unsigned char *msg_in, /* [IN] ???? */
  186. int *msg_size, /* [IN] ?????? [OUT] ?????? */
  187. unsigned char *msg_out /* [OUT] ???????msg_in?? */
  188. );
  189. /*
  190. * ??????
  191. */
  192. extern int /* 0:?? -1:???? */
  193. unwrap_size(
  194. unsigned char *msg_in, /* [IN] ???? */
  195. int *msg_size, /* [IN] ?????? [OUT] ?????? */
  196. unsigned char *msg_out /* [OUT] ???????msg_in?? */
  197. );
  198. /*
  199. * ?????
  200. */
  201. extern int /* >0:????? <=0:???? */
  202. str_split(
  203. char *str, /* [IN] ????? */
  204. char delim, /* [IN] ????0:????tab???? */
  205. char ***arrp /* [OUT] ???? ?FREE_PTR_ARRAY?? */
  206. );
  207. /*
  208. * ???????????
  209. */
  210. extern char * /* ??str */
  211. str_toupper(
  212. char *str /* [IN,OUT] ??? */
  213. );
  214. /*
  215. * ???????????
  216. */
  217. extern char * /* ??str */
  218. str_tolower(
  219. char *str /* [IN,OUT] ??? */
  220. );
  221. /*
  222. * ?????
  223. */
  224. extern char * /* ?????????dest */
  225. str_replace(
  226. char *src, /* [IN] ???? */
  227. char *old, /* [IN] ?????? */
  228. char *rep, /* [IN] ????? */
  229. char *dest /* [OUT] ???????????????NULL????????????? */
  230. );
  231. /*
  232. * ?????
  233. */
  234. extern char * /* ????????str */
  235. str_insert(
  236. char *str, /* [IN,OUT] ???? */
  237. int start, /* [IN] ???? */
  238. char *ins /* [IN] ????? */
  239. );
  240. /*
  241. * ????????10??????
  242. */
  243. extern int /* 1:? 0:? */
  244. str_is_dec(
  245. char *str /* [IN] ??? */
  246. );
  247. /*
  248. * ????????16??????
  249. */
  250. extern int /* 1:? 0:? */
  251. str_is_hex(
  252. char *str /* [IN] ??? */
  253. );
  254. /*
  255. * ????10??????16???
  256. */
  257. extern int /* 16??? */
  258. dec_to_hex(
  259. int dec /* [IN] 10??? */
  260. );
  261. /*
  262. * ????
  263. */
  264. extern int /* ????? */
  265. pack_size(
  266. int org_size, /* [IN] ??? */
  267. int unit /* [IN] ???? */
  268. );
  269. /*----- ?? -----*/
  270. /*
  271. * CRC????
  272. */
  273. extern unsigned short /* 0:?? -1:???? */
  274. get_crc_16(
  275. unsigned char *p, /* [IN] ????? */
  276. int count /* [IN] ???? */
  277. );
  278. /*----- ???? -----*/
  279. #define ALLOC_MULTI(_ptr,_type,_num,_errlabel) \
  280. do \
  281. { \
  282. if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
  283. { \
  284. goto _errlabel; \
  285. } \
  286. } while(0)
  287. #define ALLOC(_ptr,_type,_errlabel) \
  288. ALLOC_MULTI(_ptr,_type,1,_errlabel)
  289. #define ALLOC_ARRAY(_ptr,_type,_num,_size,_errlabel) \
  290. do \
  291. { \
  292. int _inc; \
  293. \
  294. _inc=(_num); \
  295. ALLOC_MULTI(_ptr,_type,_inc,_errlabel); \
  296. _size=_inc; \
  297. } while(0)
  298. #define EXPAND_ARRAY(_ptr,_type,_num,_size,_errlabel) \
  299. do \
  300. { \
  301. int _inc; \
  302. _type *_ptr_tmp; \
  303. \
  304. _inc=(_num); \
  305. if((_ptr_tmp=(_type *)realloc((_ptr), \
  306. sizeof(_type)*((_size)+(_inc)))) == NULL) \
  307. { \
  308. goto _errlabel; \
  309. } \
  310. _size+=_inc; \
  311. _ptr=_ptr_tmp; \
  312. } while(0)
  313. #define ALLOC_MULTI_RETERR(_ptr,_type,_num,_errcode) \
  314. do \
  315. { \
  316. if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
  317. { \
  318. return(_errcode); \
  319. } \
  320. } while(0)
  321. #define ALLOC_RETERR(_ptr,_type,_errcode) \
  322. ALLOC_MULTI_RETERR(_ptr,_type,1,_errcode)
  323. #define ALLOC_ARRAY_RETERR(_ptr,_type,_num,_size,_errcode) \
  324. do \
  325. { \
  326. int _inc; \
  327. \
  328. _inc=(_num); \
  329. ALLOC_MULTI_RETERR(_ptr,_type,_inc,_errcode); \
  330. _size=_inc; \
  331. } while(0)
  332. #define EXPAND_ARRAY_RETERR(_ptr,_type,_num,_size,_errcode) \
  333. do \
  334. { \
  335. int _inc; \
  336. _type *_ptr_tmp; \
  337. \
  338. _inc=(_num); \
  339. if((_ptr_tmp=(_type *)realloc((_ptr), \
  340. sizeof(_type)*((_size)+(_inc)))) == NULL) \
  341. { \
  342. return(_errcode); \
  343. } \
  344. _size+=_inc; \
  345. _ptr=_ptr_tmp; \
  346. } while(0)
  347. #define ALLOC_MULTI_DOERR(_ptr,_type,_num,_doerr) \
  348. do \
  349. { \
  350. if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
  351. { \
  352. _doerr; \
  353. } \
  354. } while(0)
  355. #define ALLOC_DOERR(_ptr,_type,_doerr) \
  356. ALLOC_MULTI_DOERR(_ptr,_type,1,_doerr)
  357. #define ALLOC_ARRAY_DOERR(_ptr,_type,_num,_size,_doerr) \
  358. do \
  359. { \
  360. int _inc; \
  361. \
  362. _inc=(_num); \
  363. ALLOC_MULTI_DOERR(_ptr,_type,_inc,_doerr); \
  364. _size=_inc; \
  365. } while(0)
  366. #define EXPAND_ARRAY_DOERR(_ptr,_type,_num,_size,_doerr) \
  367. do \
  368. { \
  369. int _inc; \
  370. _type *_ptr_tmp; \
  371. \
  372. _inc=(_num); \
  373. if((_ptr_tmp=(_type *)realloc((_ptr), \
  374. sizeof(_type)*((_size)+(_inc)))) == NULL) \
  375. { \
  376. _doerr; \
  377. } \
  378. _size+=_inc; \
  379. _ptr=_ptr_tmp; \
  380. } while(0)
  381. #define FREE(_ptr) \
  382. do \
  383. { \
  384. if(_ptr) free(_ptr); \
  385. _ptr=NULL; \
  386. } while(0)
  387. #define FREE_PTR_ARRAY(_array, _size) \
  388. do \
  389. { \
  390. if(_array) \
  391. { \
  392. int _i; \
  393. for(_i=0; _i<_size; _i++) \
  394. { if(_array[_i]) free(_array[_i]); } \
  395. free(_array); _array=NULL; \
  396. } \
  397. } while(0)
  398. #define CLR_PTR_ARRAY(_array, _size) \
  399. do \
  400. { \
  401. if(_array) \
  402. { \
  403. int _i; \
  404. for(_i=0; _i<_size; _i++) \
  405. (_array[_i])=NULL; \
  406. } \
  407. } while(0)
  408. /*----- ??? -----*/
  409. /*
  410. * ???????
  411. */
  412. extern int /* ????? */
  413. random_int(
  414. int min_val, /* [IN] ??? */
  415. int max_val, /* [IN] ??? */
  416. unsigned int *seedp /* [IN,OUT] ?? */
  417. );
  418. /*
  419. * ????????
  420. */
  421. extern int
  422. random_array_int(
  423. int array[], /* [IN,OUT] ???? */
  424. int num /* [IN] ???? */
  425. );
  426. /*
  427. * ????
  428. */
  429. extern char * /* ??????password */
  430. random_password(
  431. char *password, /* [IN,OUT] ?? */
  432. int size /* [IN] ???? */
  433. );
  434. /*----- ?????? -----*/
  435. /*
  436. * ?????
  437. */
  438. extern char * /* ???????dirname */
  439. extract_dirname(
  440. char *filename, /* [IN] ????? */
  441. char *dirname /* [OUT] ??? */
  442. );
  443. /*
  444. * ?????
  445. */
  446. extern char * /* ???????basename */
  447. extract_basename(
  448. char *filename, /* [IN] ????? */
  449. char *basename /* [OUT] ??? */
  450. );
  451. /*
  452. * ????
  453. */
  454. extern int /* 0:?? -1:??????????? -2:?????? -3:?????? */
  455. remove_dir(
  456. char *path /* [IN] ?? */
  457. );
  458. /*
  459. * ????
  460. */
  461. extern int /* 0:?? -1:??????????? -2:?????? -3:mkdir?? */
  462. create_dir(
  463. char *dir /* [IN] ?? */
  464. );
  465. /*----- ?? -----*/
  466. #define DEEP_SLEEP select(0, NULL, NULL, NULL, NULL)
  467. __END_DECLS
  468. #endif