The R Project SVN R

Rev

Rev 68956 | 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
83695 kalibera 4
 *            (C) 2023        The R Core Team
4394 ripley 5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
42300 ripley 17
 *  along with this program; if not, a copy is available at
68956 ripley 18
 *  https://www.R-project.org/Licenses/
4394 ripley 19
 */
20
 
6994 pd 21
#ifdef HAVE_CONFIG_H
7701 hornik 22
#include <config.h>
6994 pd 23
#endif
24
 
4394 ripley 25
#include <stdio.h>
26
#include <stdlib.h>
6994 pd 27
#include <string.h>
40700 ripley 28
#include <Fileio.h>
4394 ripley 29
 
30
static FILE *ff = NULL;
83695 kalibera 31
static char *optfl = NULL;
4394 ripley 32
static int optln;
33
 
44201 ripley 34
void optclosefile(void)
4394 ripley 35
{
36
    if (!ff) return;
37
    fclose(ff);
38
    ff = NULL;
83695 kalibera 39
    if (optfl) {
40
	free(optfl);
41
	optfl = NULL;
42
   } 
4394 ripley 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;
83695 kalibera 50
    optfl = (char *) malloc(strlen(fname) + 1);
51
    if (!optfl) return 0;
4394 ripley 52
    strcpy(optfl,fname);
53
    optln = 0;
54
    return 1;
55
}
56
 
57
char *
44201 ripley 58
optfile(void)
4394 ripley 59
{
60
    return optfl;
61
}
62
 
45070 ripley 63
int
44201 ripley 64
optline(void)
4394 ripley 65
{
66
    return optln;
67
}
68
 
69
 
70
static char *rmspace(char *s)
71
{
72
    int   i;
73
 
15320 luke 74
    for (i = strlen(s) - 1; i >= 0 && s[i] == ' '; i--)
4394 ripley 75
	s[i] = '\0';
76
    for (i = 0; s[i] == ' '; i++);
77
    return &s[i];
78
}
79
 
80
 
43800 ripley 81
int optread(char *opt[], const char sep)
4394 ripley 82
{
83
    static char sm[120];
84
    char *p, *s;
85
    int   l;
86
 
87
    if (!ff)
88
	return 0;
89
    l = 0;
90
    while (l == 0) {
91
	if (!fgets(sm, 120, ff)) {
92
	    fclose(ff);
93
	    ff = NULL;
94
	    return 0;
95
	}
96
	optln += 1;
97
	l = strlen(sm);
98
	if (sm[l - 1] != '\n')
99
	    return 1;
100
	else
101
	    sm[l - 1] = '\0';
102
	s = rmspace(sm);
103
	l = (*s == '#') ? 0 : strlen(s);
104
    }
105
    for (p = s; *p; p++)
106
	if (*p == '#') {
107
	    *p = '\0';
108
	    s = rmspace(s);
109
	    l = strlen(s);
110
	    break;
111
	}
112
    for (p = s; *p; p++)
113
	if (*p == sep)
114
	    break;
115
    if (!*p)
116
	return 1;
117
    *p = '\0';
118
    opt[0] = rmspace(s);
119
    opt[1] = rmspace(p + 1);
120
    if (strlen(opt[0]) && strlen(opt[1]))
121
	return 2;
37744 ripley 122
    else if (strlen(opt[0]))
123
	return 3;
4394 ripley 124
    else
125
	return 1;
126
}
83695 kalibera 127