GRASS 8 Programmer's Manual 8.6.0dev(2026)-0f6a7341fc
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <string.h>
13#include <stdio.h>
14#include <grass/gis.h>
15
16/*!
17 * \brief Gets a line of text from a file
18 *
19 * This routine runs fgets() to fetch a line of text from a file
20 * (advancing file pointer) and removes trailing newline.
21 *
22 * \param buf string buffer to receive read data
23 * \param n maximum number of bytes to read
24 * \param fd file descriptor structure
25 *
26 * \return 1 on success
27 * \return 0 EOF
28 *
29 * \see G_getl2()
30 */
31int G_getl(char *buf, int n, FILE *fd)
32{
33 return G_getl2(buf, n, fd);
34}
35
36/*!
37 * \brief Gets a line of text from a file of any pedigree
38 *
39 * This routine supports
40 * text files created on various platforms (UNIX, MacOS9, DOS),
41 * i.e. <code>\\n (\\012)</code>, <code>\\r (\\015)</code>, and
42 * <code>\\r\\n (\\015\\012)</code> style newlines.
43 *
44 * Reads in at most <i>n-1</i> characters from stream (the last spot
45 * is reserved for the end-of-string NUL) and stores them into the
46 * buffer pointed to by <i>buf</i>. Reading stops after an EOF or a
47 * newline. New line is not stored in the buffer. At least <i>n</i>
48 * bytes must be allocated for the string buffer.
49 *
50 * \param buf: string buffer to receive read data, at least <i>n</i>
51 * bytes must be allocated
52 * \param n: maximum number of bytes to read
53 * \param fd: file descriptor structure
54 *
55 * \return 1 on success
56 * \return 0 EOF
57 */
58int G_getl2(char *buf, int n, FILE *fd)
59{
60 if (buf == NULL || fd == NULL || n <= 1) {
61 return 0;
62 }
63
64 if (fgets(buf, n, fd) == NULL) {
65 return 0; /* EOF or error */
66 }
67
68 /* Remove newline characters (\n, \r\n, or \r) */
69 int len = strlen(buf);
70 if (len > 0 && buf[len - 1] == '\n') {
71 buf[--len] = '\0';
72 }
73 if (len > 0 && buf[len - 1] == '\r') {
74 buf[--len] = '\0';
75 }
76
77 return 1;
78}
#define NULL
Definition ccmath.h:32
int G_getl2(char *buf, int n, FILE *fd)
Gets a line of text from a file of any pedigree.
Definition getl.c:58
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition getl.c:31