The R Project SVN R

Rev

Rev 15320 | Rev 37744 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4394 ripley 1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
15461 ripley 3
 *  Copyright (C) 1998--2001  Guido Masarotto and Brian Ripley
4394 ripley 4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
5458 ripley 17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4394 ripley 18
 */
19
 
6994 pd 20
#ifdef HAVE_CONFIG_H
7701 hornik 21
#include <config.h>
6994 pd 22
#endif
23
 
4394 ripley 24
#include <stdio.h>
25
#include <stdlib.h>
6994 pd 26
#include <string.h>
4394 ripley 27
 
28
static FILE *ff = NULL;
29
static char optfl[MAX_PATH];
30
static int optln;
31
 
32
void optclosefile()
33
{
34
    if (!ff) return;
35
    fclose(ff);
36
    ff = NULL;
37
}
38
 
39
 
40
int optopenfile(char *fname)
41
{
42
    optclosefile();
43
    if (!fname || !(ff=fopen(fname,"r"))) return 0;
44
    strcpy(optfl,fname);
45
    optln = 0;
46
    return 1;
47
}
48
 
49
 
50
 
51
char *
52
optfile()
53
{
54
    return optfl;
55
}
56
 
57
int 
58
optline()
59
{
60
    return optln;
61
}
62
 
63
 
64
static char *rmspace(char *s)
65
{
66
    int   i;
67
 
15320 luke 68
    for (i = strlen(s) - 1; i >= 0 && s[i] == ' '; i--)
4394 ripley 69
	s[i] = '\0';
70
    for (i = 0; s[i] == ' '; i++);
71
    return &s[i];
72
}
73
 
74
 
75
int optread(char *opt[],char sep)
76
{
77
    static char sm[120];
78
    char *p, *s;
79
    int   l;
80
 
81
    if (!ff)
82
	return 0;
83
    l = 0;
84
    while (l == 0) {
85
	if (!fgets(sm, 120, ff)) {
86
	    fclose(ff);
87
	    ff = NULL;
88
	    return 0;
89
	}
90
	optln += 1;
91
	l = strlen(sm);
92
	if (sm[l - 1] != '\n')
93
	    return 1;
94
	else
95
	    sm[l - 1] = '\0';
96
	s = rmspace(sm);
97
	l = (*s == '#') ? 0 : strlen(s);
98
    }
99
    for (p = s; *p; p++)
100
	if (*p == '#') {
101
	    *p = '\0';
102
	    s = rmspace(s);
103
	    l = strlen(s);
104
	    break;
105
	}
106
    for (p = s; *p; p++)
107
	if (*p == sep)
108
	    break;
109
    if (!*p)
110
	return 1;
111
    *p = '\0';
112
    opt[0] = rmspace(s);
113
    opt[1] = rmspace(p + 1);
114
    if (strlen(opt[0]) && strlen(opt[1]))
115
	return 2;
116
    else
117
	return 1;
118
}