Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/src/t8_windows.h
898 views
1
/*
2
This file is part of t8code.
3
t8code is a C library to manage a collection (a forest) of multiple
4
connected adaptive space-trees of general element classes in parallel.
5
6
Copyright (C) 2023 the developers
7
8
t8code is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
t8code is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with t8code; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
/** \file t8_windows.h
24
* This file implements additional C functions that are used in t8code but are
25
* not part of the C standard library. Since these functions are missing on
26
* Windows, we provide an alternative implementation here, which is only used
27
* when compiling on Windows.
28
*/
29
30
#ifndef T8_WINDOWS_H
31
#define T8_WINDOWS_H
32
33
/** Read string from file stream up to a given delimiter and store it in a buffer.
34
*
35
* For a full description see https://linux.die.net/man/3/getdelim.
36
*
37
* The implementation is based on a code that was published under the
38
* Zero-Clause BSD license, i.e., as public domain code.
39
*
40
* Source: https://github.com/arnavyc/getdelim/blob/0129a33c182b46e62b3137623a5569449fd3cc94/include/ay/getdelim.h
41
*/
42
static ssize_t
43
getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
44
{
45
int initial_buffer_size = 1024;
46
int c;
47
size_t pos;
48
size_t new_size;
49
char *new_ptr;
50
51
if (lineptr == NULL || stream == NULL || n == NULL) {
52
return -1;
53
}
54
55
c = getc (stream);
56
57
if (c == EOF) {
58
return -1;
59
}
60
61
if (*lineptr == NULL) {
62
*lineptr = (char *) malloc (initial_buffer_size);
63
if (*lineptr == NULL) {
64
return -1;
65
}
66
*n = initial_buffer_size;
67
}
68
69
pos = 0;
70
while (c != EOF) {
71
if (pos + 1 >= *n) {
72
new_size = *n + (*n >> 2);
73
if (new_size < initial_buffer_size) {
74
new_size = initial_buffer_size;
75
}
76
new_ptr = (char *) realloc (*lineptr, new_size);
77
if (new_ptr == NULL) {
78
return -1;
79
}
80
*n = new_size;
81
*lineptr = new_ptr;
82
}
83
84
((unsigned char *) (*lineptr))[pos++] = c;
85
if (c == delimiter) {
86
break;
87
}
88
89
c = getc (stream);
90
}
91
92
(*lineptr)[pos] = '\0';
93
return pos;
94
}
95
96
/** Read line from file stream and store it in a buffer.
97
*
98
* For a full description see https://linux.die.net/man/3/getline.
99
*
100
* The implementation is based on a code that was published under the
101
* Zero-Clause BSD license, i.e., as public domain code.
102
*
103
* Source: https://github.com/arnavyc/getdelim/blob/0129a33c182b46e62b3137623a5569449fd3cc94/include/ay/getdelim.h
104
*/
105
static ssize_t
106
getline (char **lineptr, size_t *n, FILE *stream)
107
{
108
return getdelim (lineptr, n, '\n', stream);
109
}
110
111
/** Extract token from string up to a given delimiter.
112
*
113
* For a full description see https://linux.die.net/man/3/strsep
114
*/
115
static char *
116
strsep (char **stringp, const char *delim)
117
{
118
char *current;
119
char *original = *stringp;
120
121
if (*stringp == NULL) {
122
return NULL;
123
}
124
125
current = *stringp;
126
while (1) {
127
/* Delimiter not found in string: reset *stringp to NULL */
128
if (*current == '\0') {
129
*stringp = NULL;
130
break;
131
}
132
133
/* Delimiter found: overwrite delimiter and reset *stringp to current location */
134
if (*current == *delim) {
135
*current = '\0';
136
*stringp = current + 1;
137
break;
138
}
139
140
current++;
141
}
142
143
return original;
144
}
145
146
#endif /* !T8_WINDOWS_H */
147
148