The R Project SVN R

Rev

Rev 12976 | Rev 36820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12976 Rev 19912
Line 158... Line 158...
158
 
158
 
159
 
159
 
160
/* The following code is used to recursive traverse a block */
160
/* The following code is used to recursive traverse a block */
161
/* of code and extract all the symbols present in that code. */
161
/* of code and extract all the symbols present in that code. */
162
 
162
 
-
 
163
typedef struct {
163
static SEXP	ans;
164
 SEXP	ans;
164
static int	UniqueNames;
165
 int	UniqueNames;
165
static int	IncludeFunctions;
166
 int	IncludeFunctions;
166
static int	StoreValues;
167
 int	StoreValues;
167
static int	ItemCounts;
168
 int	ItemCounts;
168
static int	MaxCount;
169
 int	MaxCount;
-
 
170
} NameWalkData;
169
 
171
 
170
static void namewalk(SEXP s)
172
static void namewalk(SEXP s, NameWalkData *d)
171
{
173
{
172
    int i, j, n;
174
    int i, j, n;
173
    SEXP name;
175
    SEXP name;
174
 
176
 
175
    switch(TYPEOF(s)) {
177
    switch(TYPEOF(s)) {
176
    case SYMSXP:
178
    case SYMSXP:
177
	name = PRINTNAME(s);
179
	name = PRINTNAME(s);
178
	/* skip blank symbols */
180
	/* skip blank symbols */
179
	if(strlen(CHAR(name)) == 0) goto ignore;
181
	if(strlen(CHAR(name)) == 0) goto ignore;
180
	if(ItemCounts < MaxCount) {
182
	if(d->ItemCounts < d->MaxCount) {
181
	    if(StoreValues) {
183
	    if(d->StoreValues) {
182
		if(UniqueNames) {
184
		if(d->UniqueNames) {
183
		    for(j = 0 ; j < ItemCounts ; j++) {
185
		    for(j = 0 ; j < d->ItemCounts ; j++) {
184
			if(STRING_ELT(ans, j) == name)
186
			if(STRING_ELT(d->ans, j) == name)
185
			    goto ignore;
187
			    goto ignore;
186
		    }
188
		    }
187
		}
189
		}
188
		SET_STRING_ELT(ans, ItemCounts, name);
190
		SET_STRING_ELT(d->ans, d->ItemCounts, name);
189
	    }
191
	    }
190
	    ItemCounts += 1;
192
	    d->ItemCounts += 1;
191
	}
193
	}
192
    ignore:
194
    ignore:
193
	break;
195
	break;
194
    case LANGSXP:
196
    case LANGSXP:
195
	if(!IncludeFunctions) s = CDR(s);
197
	if(!d->IncludeFunctions) s = CDR(s);
196
	while(s != R_NilValue) {
198
	while(s != R_NilValue) {
197
	    namewalk(CAR(s));
199
	    namewalk(CAR(s), d);
198
	    s = CDR(s);
200
	    s = CDR(s);
199
	}
201
	}
200
	break;
202
	break;
201
    case EXPRSXP:
203
    case EXPRSXP:
202
	n = length(s);
204
	n = length(s);
203
	for(i=0 ; i<n ; i++)
205
	for(i=0 ; i<n ; i++)
204
	    namewalk(VECTOR_ELT(s, i));
206
	    namewalk(VECTOR_ELT(s, i), d);
205
	break;
207
	break;
206
    }
208
    }
207
}
209
}
208
 
210
 
209
/* Also does all.vars wiht functions=FALSE
211
/* Also does all.vars wiht functions=FALSE
210
   .Internal(all.names(expr, functions, max.names, unique)) */
212
   .Internal(all.names(expr, functions, max.names, unique)) */
211
SEXP do_allnames(SEXP call, SEXP op, SEXP args, SEXP env)
213
SEXP do_allnames(SEXP call, SEXP op, SEXP args, SEXP env)
212
{
214
{
213
    SEXP expr;
215
    SEXP expr;
214
    int i, savecount;
216
    int i, savecount;
-
 
217
    NameWalkData data = {NULL, 0, 0, 0, 0, 0};
215
 
218
 
216
    checkArity(op, args);
219
    checkArity(op, args);
217
 
220
 
218
    expr = CAR(args);
221
    expr = CAR(args);
219
    args = CDR(args);
222
    args = CDR(args);
220
 
223
 
221
    IncludeFunctions = asLogical(CAR(args));
224
    data.IncludeFunctions = asLogical(CAR(args));
222
    if(IncludeFunctions == NA_LOGICAL)
225
    if(data.IncludeFunctions == NA_LOGICAL)
223
	IncludeFunctions = 0;
226
	data.IncludeFunctions = 0;
224
    args = CDR(args);
227
    args = CDR(args);
225
 
228
 
226
    MaxCount = asInteger(CAR(args));
229
    data.MaxCount = asInteger(CAR(args));
227
    if(MaxCount < 0 || MaxCount == NA_INTEGER)
230
    if(data.MaxCount < 0 || data.MaxCount == NA_INTEGER)
228
	MaxCount = 0;
231
	data.MaxCount = 0;
229
    args = CDR(args);
232
    args = CDR(args);
230
 
233
 
231
    UniqueNames = asLogical(CAR(args));
234
    data.UniqueNames = asLogical(CAR(args));
232
    if(UniqueNames == NA_LOGICAL)
235
    if(data.UniqueNames == NA_LOGICAL)
233
	UniqueNames = 1;
236
	data.UniqueNames = 1;
234
 
237
 
235
    StoreValues = 0;
-
 
236
    ItemCounts = 0;
-
 
237
    namewalk(expr);
238
    namewalk(expr, &data);
238
    savecount = ItemCounts;
239
    savecount = data.ItemCounts;
239
 
240
 
240
    ans = allocVector(STRSXP, ItemCounts);
241
    data.ans = allocVector(STRSXP, data.ItemCounts);
241
 
242
 
242
    StoreValues = 1;
243
    data.StoreValues = 1;
243
    ItemCounts = 0;
244
    data.ItemCounts = 0;
244
    namewalk(expr);
245
    namewalk(expr, &data);
245
 
246
 
246
    if(ItemCounts != savecount) {
247
    if(data.ItemCounts != savecount) {
247
	PROTECT(expr = ans);
248
	PROTECT(expr = data.ans);
248
	ans = allocVector(STRSXP, ItemCounts);
249
	data.ans = allocVector(STRSXP, data.ItemCounts);
249
	for(i = 0 ; i < ItemCounts ; i++)
250
	for(i = 0 ; i < data.ItemCounts ; i++)
250
	    SET_STRING_ELT(ans, i, STRING_ELT(expr, i));
251
	    SET_STRING_ELT(data.ans, i, STRING_ELT(expr, i));
251
	UNPROTECT(1);
252
	UNPROTECT(1);
252
    }
253
    }
253
 
254
 
254
    return ans;
255
    return data.ans;
255
}
256
}