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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'ext/hopcsv.c', line 14
static VALUE foreach(int argc, VALUE* argv, VALUE self) {
char DELIM=DEF_DELIM;
char *line = NULL;
size_t len = 0;
char *token,*start,*nobackslash,*t2, *str;
int idx,count,pairs_count,searchfield,flag,i,array_length,range_i,len2;
long check;
FILE *file;
ID min_method, max_method;
VALUE min_val, max_val;
VALUE tmp_value, rest_args, filename;
ID array_length_method; /*----------------------------------------*/
struct pair_st pairs[MAX_INTERVALS];
VALUE ary;
rb_scan_args(argc,argv,"1*", &filename, &rest_args);
/* if (argc == 0) { // there should only be 1 or 2 arguments
rb_raise(rb_eArgError, "wrong number of arguments");
}
file = fopen(StringValueCStr(argv[0]), "r");
if (file == NULL)
rb_raise(rb_eRuntimeError, "File not found");
*/
file = fopen(StringValueCStr(filename), "r");
if(file==NULL){
rb_raise(rb_eRuntimeError, "File not found");
}
if (argc >1 ) { /* delimiter */
tmp_value=rb_ary_entry(rest_args,0);
str=StringValueCStr(tmp_value);
DELIM=str[0];
}
if (argc >2 ) { /* search index */
tmp_value=rb_ary_entry(rest_args,1);
searchfield=NUM2INT(tmp_value);
}
else{
searchfield=-1;
}
min_val=rb_funcall(rest_args,rb_intern("length"), 0);
array_length=NUM2INT(min_val);
/*rb_warn("Length=%d",array_length);*/
min_method = rb_intern("min");
max_method = rb_intern("max");
/*------------test_id = rb_intern("class");*/
range_i=0;
for(idx=2;idx<array_length;++idx){
min_val=rb_funcall(rb_ary_entry(rest_args,idx),rb_intern("length"), 0);
len2=NUM2INT(min_val);
for(i=0;i<len2;++i){
VALUE e=rb_ary_entry(rb_ary_entry(rest_args,idx),i);
if(range_i>MAX_INTERVALS)
rb_raise(rb_eRuntimeError, "Too much ranges passed");
if(TYPE(e) == T_NIL){
pairs[range_i].low=LONG_MIN;
pairs[range_i].high=LONG_MAX;
continue; /* just skip nil */
}
if (! (rb_respond_to(e, min_method) & rb_respond_to(e, max_method)))
rb_raise(rb_eRuntimeError, "Not range passed to Hopcsv.foreach");
min_val=rb_funcall(e, min_method, 0);
max_val=rb_funcall(e, max_method, 0);
/* rb_warn("!\n");*/
pairs[range_i].low=NUM2LONG(min_val);
/*rb_warn("2\n");*/
pairs[range_i].high=NUM2LONG(max_val);
/*rb_warn("RANGE: %ld .. %ld (%d)\n",(long)pairs[range_i].low,(long)pairs[range_i].high,(int)(range_i));*/
range_i++;
}
}
pairs_count=range_i;
while (getline(&line, &len, file) != -1) {
/* chomp! */
if(token=index(line,EOL)){
*token='\0';
}
/*rb_warning("4\n");*/
ary = rb_ary_new();
start=line;
nobackslash=line;
while(token=index(nobackslash, DELIM)){
/* rb_warning("5\n");*/
count=0;
t2=token-1;
while((t2>=line) && (*t2=='\\'))
{++count;--t2;}
if(count%2 ==1){ /* backslashed! skip */
nobackslash=token;
continue;
}
break;
}
idx = 0;
flag=1;
while (token != NULL) {
*token='\0';
if(searchfield==idx){
flag=0;
/* do check! */
sscanf(start,"%ld",&check);
for(i=0;i<pairs_count;++i){
/*rb_warn("check %ld: [%ld .. %ld]",check,pairs[i].low,pairs[i].high);*/
if(pairs[i].low<check && pairs[i].high>check){
/*rb_warn("check passed");*/
flag=1; /* yahooo! */
break;
}
}
}
/* not in ranges! */
if(flag==0)
break;
rb_ary_store(ary, idx, rb_str_new(start, token-start));
idx++;
nobackslash=start=token+1;
while(token=index(nobackslash, DELIM)){
count=0;
t2=token-1;
while((t2>=line) && (*t2=='\\'))
{++count;--t2;}
if(count%2 ==1){ /* backslashed! skip */
nobackslash=token;
continue;
}
break;
}
}
if(flag==0)
continue;
/* last item */
rb_ary_store(ary, idx, rb_str_new(start, strlen(start)));
/* OBJ_FREEZE(ary); */
rb_yield(ary);
/* FL_UNSET((ary), FL_FREEZE); */
/* for(idx = 0; idx < RARRAY_LEN(ary); idx ++) {
rb_ary_store(ary, idx, Qnil);
} */
}
fclose(file);
return Qnil;
}
|