t8  1.2.0
t8code is a C library to manage a forest of adaptive space-trees of general element classes in parallel.
t8_windows.h
Go to the documentation of this file.
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 
30 #ifndef T8_WINDOWS_H
31 #define T8_WINDOWS_H
32 
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 
105 static ssize_t
106 getline (char **lineptr, size_t *n, FILE *stream)
107 {
108  return getdelim (lineptr, n, '\n', stream);
109 }
110 
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 */