The R Project SVN R

Rev

Rev 44201 | 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
41808 ripley 3
 *  Copyright (C) 1998--2007  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
42300 ripley 16
 *  along with this program; if not, a copy is available at
17
 *  http://www.r-project.org/Licenses/
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>
40700 ripley 27
#include <Fileio.h>
4394 ripley 28
 
42579 ripley 29
#ifndef MAX_PATH
30
# include <windows.h>
31
#endif
32
 
33
 
4394 ripley 34
static FILE *ff = NULL;
35
static char optfl[MAX_PATH];
36
static int optln;
37
 
44201 ripley 38
void optclosefile(void)
4394 ripley 39
{
40
    if (!ff) return;
41
    fclose(ff);
42
    ff = NULL;
43
}
44
 
45
 
41808 ripley 46
int optopenfile(const char *fname)
4394 ripley 47
{
48
    optclosefile();
40700 ripley 49
    if (!fname || !(ff = R_fopen(fname,"r"))) return 0;
4394 ripley 50
    strcpy(optfl,fname);
51
    optln = 0;
52
    return 1;
53
}
54
 
55
char *
44201 ripley 56
optfile(void)
4394 ripley 57
{
58
    return optfl;
59
}
60
 
45070 ripley 61
int
44201 ripley 62
optline(void)
4394 ripley 63
{
64
    return optln;
65
}
66
 
67
 
68
static char *rmspace(char *s)
69
{
70
    int   i;
71
 
15320 luke 72
    for (i = strlen(s) - 1; i >= 0 && s[i] == ' '; i--)
4394 ripley 73
	s[i] = '\0';
74
    for (i = 0; s[i] == ' '; i++);
75
    return &s[i];
76
}
77
 
78
 
43800 ripley 79
int optread(char *opt[], const char sep)
4394 ripley 80
{
81
    static char sm[120];
82
    char *p, *s;
83
    int   l;
84
 
85
    if (!ff)
86
	return 0;
87
    l = 0;
88
    while (l == 0) {
89
	if (!fgets(sm, 120, ff)) {
90
	    fclose(ff);
91
	    ff = NULL;
92
	    return 0;
93
	}
94
	optln += 1;
95
	l = strlen(sm);
96
	if (sm[l - 1] != '\n')
97
	    return 1;
98
	else
99
	    sm[l - 1] = '\0';
100
	s = rmspace(sm);
101
	l = (*s == '#') ? 0 : strlen(s);
102
    }
103
    for (p = s; *p; p++)
104
	if (*p == '#') {
105
	    *p = '\0';
106
	    s = rmspace(s);
107
	    l = strlen(s);
108
	    break;
109
	}
110
    for (p = s; *p; p++)
111
	if (*p == sep)
112
	    break;
113
    if (!*p)
114
	return 1;
115
    *p = '\0';
116
    opt[0] = rmspace(s);
117
    opt[1] = rmspace(p + 1);
118
    if (strlen(opt[0]) && strlen(opt[1]))
119
	return 2;
37744 ripley 120
    else if (strlen(opt[0]))
121
	return 3;
4394 ripley 122
    else
123
	return 1;
124
}