GRASS GIS 7 Programmer's Manual
7.8.4(2020)-exported
getl.c
Go to the documentation of this file.
1
/*!
2
* \file lib/gis/getl.c
3
*
4
* \brief GIS Library - Get line of text from file
5
*
6
* (C) 2001-2009 by the GRASS Development Team
7
*
8
* This program is free software under the GNU General Public License
9
* (>=v2). Read the file COPYING that comes with GRASS for details.
10
*
11
* \author Original author CERL
12
*/
13
14
#include <stdio.h>
15
#include <grass/gis.h>
16
17
/*!
18
* \brief Gets a line of text from a file
19
*
20
* This routine runs fgets() to fetch a line of text from a file
21
* (advancing file pointer) and removes trailing newline. fgets() does
22
* not recognize '<code>\\r</code>' as an EOL and will read past * it.
23
*
24
* \param buf string buffer to receive read data
25
* \param n maximum number of bytes to read
26
* \param fd file descriptor structure
27
*
28
* \return 1 on success
29
* \return 0 EOF
30
*/
31
int
G_getl
(
char
*buf,
int
n, FILE * fd)
32
{
33
if
(!fgets(buf, n, fd))
34
return
0;
35
36
for
(; *buf && *buf !=
'\n'
; buf++) ;
37
*buf = 0;
38
39
return
1;
40
}
41
42
/*!
43
* \brief Gets a line of text from a file of any pedigree
44
*
45
* This routine is like G_getl() but is more portable. It supports
46
* text files created on various platforms (UNIX, MacOS9, DOS),
47
* i.e. <code>\\n (\\012)</code>, <code>\\r (\\015)</code>, and
48
* <code>\\r\\n (\\015\\012)</code> style newlines.
49
*
50
*
51
* Reads in at most <i>n-1</i> characters from stream (the last spot
52
* is reserved for the end-of-string NUL) and stores them into the
53
* buffer pointed to by <i>buf</i>. Reading stops after an EOF or a
54
* newline. New line is not stored in the buffer. At least <i>n</i>
55
* must be allocated for the string buffer.
56
*
57
* \param buf: string buffer to receive read data, at least <i>n</i> must be allocated
58
* \param n: maximum number of bytes to read
59
* \param fd: file descriptor structure
60
*
61
* \return 1 on success
62
* \return 0 EOF
63
*/
64
int
G_getl2
(
char
*buf,
int
n, FILE * fd)
65
{
66
int
i = 0;
67
int
c;
68
int
ret = 1;
69
70
while
(i < n - 1) {
71
c = fgetc(fd);
72
73
if
(c == EOF) {
74
if
(i == 0) {
/* Read correctly (return 1) last line in file without '\n' */
75
ret = 0;
76
}
77
break
;
78
}
79
80
if
(c ==
'\n'
)
81
break
;
/* UNIX */
82
83
if
(c ==
'\r'
) {
/* DOS or MacOS9 */
84
if
((c = fgetc(fd)) != EOF) {
85
if
(c !=
'\n'
) {
/* MacOS9 - we have to return the char to stream */
86
ungetc(c, fd);
87
}
88
}
89
break
;
90
}
91
92
buf[i] = c;
93
94
i++;
95
}
96
buf[i] =
'\0'
;
97
98
return
ret;
99
}
G_getl
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition:
getl.c:31
G_getl2
int G_getl2(char *buf, int n, FILE *fd)
Gets a line of text from a file of any pedigree.
Definition:
getl.c:64
gis
getl.c
Generated on Thu Nov 19 2020 20:26:47 for GRASS GIS 7 Programmer's Manual by
1.8.20