Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/gis: modernize getl2() #3850

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions lib/gis/getl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* \author Original author CERL
*/

#include <string.h>
#include <stdio.h>
#include <grass/gis.h>

Expand Down Expand Up @@ -48,55 +49,38 @@ int G_getl(char *buf, int n, FILE *fd)
* i.e. <code>\\n (\\012)</code>, <code>\\r (\\015)</code>, and
* <code>\\r\\n (\\015\\012)</code> style newlines.
*
*
* Reads in at most <i>n-1</i> characters from stream (the last spot
* is reserved for the end-of-string NUL) and stores them into the
* buffer pointed to by <i>buf</i>. Reading stops after an EOF or a
* newline. New line is not stored in the buffer. At least <i>n</i>
* must be allocated for the string buffer.
* newline. New line is not stored in the buffer. At least <i>n</i>
* bytes must be allocated for the string buffer.
*
* \param buf: string buffer to receive read data, at least <i>n</i> must be
* allocated \param n: maximum number of bytes to read \param fd: file
* descriptor structure
* \param buf: string buffer to receive read data, at least <i>n</i>
* bytes must be allocated
* \param n: maximum number of bytes to read
* \param fd: file descriptor structure
*
* \return 1 on success
* \return 0 EOF
*/
int G_getl2(char *buf, int n, FILE *fd)
{
int i = 0;
int c;
int ret = 1;

while (i < n - 1) {
c = fgetc(fd);

if (c == EOF) {
if (i == 0) { /* Read correctly (return 1) last line in file without
'\n' */
ret = 0;
}
break;
}

if (c == '\n')
break; /* UNIX */

if (c == '\r') { /* DOS or MacOS9 */
if ((c = fgetc(fd)) != EOF) {
if (c !=
'\n') { /* MacOS9 - we have to return the char to stream */
ungetc(c, fd);
}
}
break;
}
if (buf == NULL || fd == NULL || n <= 1) {
return 0;
}

buf[i] = c;
if (fgets(buf, n, fd) == NULL) {
return 0; /* EOF or error */
}

i++;
/* Remove newline characters (\n, \r\n, or \r) */
int len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n') {
buf[--len] = '\0';
}
if (len > 0 && buf[len - 1] == '\r') {
buf[--len] = '\0';
}
buf[i] = '\0';

return ret;
return 1;
}
Loading