libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
lacf_getline_impl.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license in the file COPYING
10 * or http://www.opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file COPYING.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2023 Saso Kiselkov. All rights reserved.
24 */
31#ifndef _ACF_UTILS_LACF_GETLINE_IMPL_H_
32#define _ACF_UTILS_LACF_GETLINE_IMPL_H_
33
34#if defined(ACFUTILS_BUILD) || defined(ACFUTILS_GZIP_PARSER)
35#include <zlib.h>
36#endif
37
38#include "core.h"
39#include "safe_alloc.h"
40
41#ifndef _LACF_GETLINE_INCLUDED
42#error "Don't include lacf_getline_impl.h directly. Include acfutils/helpers.h"
43#endif
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
53#if defined(ACFUTILS_BUILD) || defined(ACFUTILS_GZIP_PARSER)
54UNUSED_ATTR static ssize_t
55lacf_getline_impl(char **line_p, size_t *cap_p, void *fp, bool_t compressed)
56#else /* !defined(ACFUTILS_BUILD) && !defined(ACFUTILS_GZIP_PARSER) */
57UNUSED_ATTR static ssize_t
58lacf_getline_impl(char **line_p, size_t *cap_p, void *fp)
59#endif /* !defined(ACFUTILS_BUILD) && !defined(ACFUTILS_GZIP_PARSER) */
60{
61 ASSERT(line_p != NULL);
62 ASSERT(cap_p != NULL);
63 ASSERT(fp != NULL);
64
65 char *line = *line_p;
66 size_t cap = *cap_p, n = 0;
67
68#if (defined(ACFUTILS_BUILD) || defined(ACFUTILS_GZIP_PARSER)) && \
69 (APL || LIN)
70 /* On POSIX we can use the libc version when uncompressed */
71 if (!compressed)
72 return (getline(line_p, cap_p, (FILE *)fp));
73#endif /* defined(ACFUTILS_BUILD) && (APL || LIN) */
74 do {
75 char *p;
76
77 if (n + 1 >= cap) {
78 cap += 256;
79 line = (char *)safe_realloc(line, cap);
80 }
81 ASSERT(n < cap);
82#if defined(ACFUTILS_BUILD) || defined(ACFUTILS_GZIP_PARSER)
83 p = (compressed ? gzgets((gzFile)fp, &line[n], cap - n) :
84 fgets(&line[n], cap - n, (FILE *)fp));
85#else /* !defined(ACFUTILS_BUILD) */
86 p = fgets(&line[n], cap - n, (FILE *)fp);
87#endif /* !defined(ACFUTILS_BUILD) */
88 if (p == NULL) {
89 if (n != 0) {
90 break;
91 } else {
92 *line_p = line;
93 *cap_p = cap;
94 return (-1);
95 }
96 }
97 n = strlen(line);
98 } while (n > 0 && line[n - 1] != '\n');
99
100 *line_p = line;
101 *cap_p = cap;
102
103 return (n);
104}
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif /* _ACF_UTILS_LACF_GETLINE_IMPL_H_ */
#define ASSERT(x)
Definition assert.h:208
#define UNUSED_ATTR
Definition core.h:95
static ssize_t lacf_getline_impl(char **line_p, size_t *cap_p, void *fp)
static void * safe_realloc(void *oldptr, size_t size)
Definition safe_alloc.h:86