-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeach.h
79 lines (57 loc) · 2.25 KB
/
foreach.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef __FOREACH_H__
#define __FOREACH_H__
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <errno.h>
#include <stdlib.h>
#include <math.h>
/* -D_XOPEN_SOURCE //remove below if this option is enabled*/
/* extern char *strtok_r(char *s, const char *delim, char **ptrptr); */
/* extern FILE *popen (__const char *__command, __const char *__modes); */
/* extern int pclose (FILE *__stream); */
/* FREE */
#define freey(ptr)\
if(ptr != NULL) free(ptr);
#define freey2d(ptr,sz1) \
for (register int _i = 0; _i < sz1 ; _i++)\
if(ptr[_i] != NULL) free(ptr[_i]);\
if(ptr != NULL) free(ptr);
/* FOREACH: C needs some good looping macros: */
#define foreach(type, var, array)\
for (register type (var), *_p = (type*)(array)->pdata;\
_p != NULL; _p = NULL)\
for (register int _i = 0, _l = (array)->len;\
(_i < _l) && ((var = _p[_i]) || 1); _i++)
#define foreach_ptr(ptr, array)\
for (register gpointer *ptr = (array)->pdata;\
ptr != NULL; ptr = NULL)\
for (register int _i = 0, _l = (array)->len;\
(_i < _l); _i++, ptr++)
#define foreach_int(var, lo, hi)\
for (register int var = (lo), _hi = (hi); var < _hi; var++)
#define foreach_char(var, str)\
for (register char var, *_p = (str);\
(var = *_p) != 0; _p++)
#define LINE (1<<20)
extern FILE *popen (__const char *__command, __const char *__modes);
extern int pclose (FILE *__stream);
#define _myopen(f) ((*(f)==0)?stdin:(*(f)=='|')?popen((f)+1,"r"):fopen((f),"r"))
#define _myclose(f,fp) ((*(f)==0)?0:(*(f)=='|')?pclose(fp):fclose(fp))
#define foreach_line(str, fname)\
errno = 0; \
for (FILE *_fp = _myopen(fname); \
(_fp != NULL) || (errno && (perror(fname), exit(errno), 0)); \
_fp = (_myclose(fname, _fp), NULL)) \
for (char str[LINE];\
((str[LINE - 1] = -1) &&\
fgets(str, LINE, _fp) &&\
((str[LINE - 1] != 0) ||\
(perror("Line too long"), exit(-1), 0))); )
#define foreach_token(tok, str)\
for (char *_ptr = NULL, *(tok) = strtok_r((str), " \t\n\r\f\v", &_ptr);\
(tok) != NULL; (tok) = strtok_r(NULL," \t\n\r\f\v", &_ptr))
#define foreach_token3(tok, str, sep)\
for (char *_ptr = NULL, *(tok) = strtok_r((str), (sep), &_ptr);\
(tok) != NULL; (tok) = strtok_r(NULL, (sep), &_ptr))
#endif