/*1This file is part of t8code.2t8code is a C library to manage a collection (a forest) of multiple3connected adaptive space-trees of general element classes in parallel.45Copyright (C) 2023 the developers67t8code is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2 of the License, or10(at your option) any later version.1112t8code is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with t8code; if not, write to the Free Software Foundation, Inc.,1951 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.20*/2122/** \file t8_windows.h23* This file implements additional C functions that are used in t8code but are24* not part of the C standard library. Since these functions are missing on25* Windows, we provide an alternative implementation here, which is only used26* when compiling on Windows.27*/2829#ifndef T8_WINDOWS_H30#define T8_WINDOWS_H3132/** Read string from file stream up to a given delimiter and store it in a buffer.33*34* For a full description see https://linux.die.net/man/3/getdelim.35*36* The implementation is based on a code that was published under the37* Zero-Clause BSD license, i.e., as public domain code.38*39* Source: https://github.com/arnavyc/getdelim/blob/0129a33c182b46e62b3137623a5569449fd3cc94/include/ay/getdelim.h40*/41static ssize_t42getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)43{44int initial_buffer_size = 1024;45int c;46size_t pos;47size_t new_size;48char *new_ptr;4950if (lineptr == NULL || stream == NULL || n == NULL) {51return -1;52}5354c = getc (stream);5556if (c == EOF) {57return -1;58}5960if (*lineptr == NULL) {61*lineptr = (char *) malloc (initial_buffer_size);62if (*lineptr == NULL) {63return -1;64}65*n = initial_buffer_size;66}6768pos = 0;69while (c != EOF) {70if (pos + 1 >= *n) {71new_size = *n + (*n >> 2);72if (new_size < initial_buffer_size) {73new_size = initial_buffer_size;74}75new_ptr = (char *) realloc (*lineptr, new_size);76if (new_ptr == NULL) {77return -1;78}79*n = new_size;80*lineptr = new_ptr;81}8283((unsigned char *) (*lineptr))[pos++] = c;84if (c == delimiter) {85break;86}8788c = getc (stream);89}9091(*lineptr)[pos] = '\0';92return pos;93}9495/** Read line from file stream and store it in a buffer.96*97* For a full description see https://linux.die.net/man/3/getline.98*99* The implementation is based on a code that was published under the100* Zero-Clause BSD license, i.e., as public domain code.101*102* Source: https://github.com/arnavyc/getdelim/blob/0129a33c182b46e62b3137623a5569449fd3cc94/include/ay/getdelim.h103*/104static ssize_t105getline (char **lineptr, size_t *n, FILE *stream)106{107return getdelim (lineptr, n, '\n', stream);108}109110/** Extract token from string up to a given delimiter.111*112* For a full description see https://linux.die.net/man/3/strsep113*/114static char *115strsep (char **stringp, const char *delim)116{117char *current;118char *original = *stringp;119120if (*stringp == NULL) {121return NULL;122}123124current = *stringp;125while (1) {126/* Delimiter not found in string: reset *stringp to NULL */127if (*current == '\0') {128*stringp = NULL;129break;130}131132/* Delimiter found: overwrite delimiter and reset *stringp to current location */133if (*current == *delim) {134*current = '\0';135*stringp = current + 1;136break;137}138139current++;140}141142return original;143}144145#endif /* !T8_WINDOWS_H */146147148