Merge with master, resolve conflicts

This commit is contained in:
ado 2024-02-23 02:49:13 +01:00
commit 273e8ad950
2 changed files with 70 additions and 60 deletions

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <cerrno>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include <vector> #include <vector>
namespace ss { namespace ss {
@ -33,54 +35,58 @@ inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
#else #else
using ssize_t = int64_t; using ssize_t = int64_t;
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
size_t pos;
int c;
if (lineptr == nullptr || stream == nullptr || n == nullptr) { ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
if (lineptr == nullptr || n == nullptr || fp == nullptr) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
c = getc(stream); char buff[get_line_initial_buffer_size];
if (c == EOF) {
return -1;
}
if (*lineptr == nullptr) { if (*lineptr == nullptr || *n < sizeof(buff)) {
*lineptr = static_cast<char*>(malloc(get_line_initial_buffer_size)); size_t new_n = sizeof(buff);
if (*lineptr == nullptr) { auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
if (new_lineptr == nullptr) {
errno = ENOMEM;
return -1; return -1;
} }
*n = 128;
*lineptr = new_lineptr;
*n = new_n;
} }
pos = 0; (*lineptr)[0] = '\0';
while (c != EOF) {
if (pos + 1 >= *n) { while (fgets(buff, sizeof(buff), fp) != nullptr) {
size_t new_size = *n + (*n >> 2); size_t line_used = strlen(*lineptr);
if (new_size < get_line_initial_buffer_size) { size_t buff_used = strlen(buff);
new_size = get_line_initial_buffer_size;
} if (*n < buff_used + line_used) {
char* new_ptr = static_cast<char*>( size_t new_n = *n * 2;
realloc(static_cast<void*>(*lineptr), new_size));
if (new_ptr == nullptr) { auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n));
if (new_lineptr == nullptr) {
errno = ENOMEM;
return -1; return -1;
} }
*n = new_size;
*lineptr = new_ptr; *lineptr = new_lineptr;
*n = new_n;
} }
(*lineptr)[pos++] = c; memcpy(*lineptr + line_used, buff, buff_used);
if (c == '\n') { line_used += buff_used;
break; (*lineptr)[line_used] = '\0';
if ((*lineptr)[line_used - 1] == '\n') {
return line_used;
} }
c = getc(stream);
} }
(*lineptr)[pos] = '\0'; return -1;
return pos;
} }
#endif #endif
} /* ss */ } /* ss */

64
ssp.hpp
View File

@ -647,54 +647,58 @@ inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
#else #else
using ssize_t = int64_t; using ssize_t = int64_t;
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
size_t pos;
int c;
if (lineptr == nullptr || stream == nullptr || n == nullptr) { ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
if (lineptr == nullptr || n == nullptr || fp == nullptr) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
c = getc(stream); char buff[get_line_initial_buffer_size];
if (c == EOF) {
return -1;
}
if (*lineptr == nullptr) { if (*lineptr == nullptr || *n < sizeof(buff)) {
*lineptr = static_cast<char*>(malloc(get_line_initial_buffer_size)); size_t new_n = sizeof(buff);
if (*lineptr == nullptr) { auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
if (new_lineptr == nullptr) {
errno = ENOMEM;
return -1; return -1;
} }
*n = 128;
*lineptr = new_lineptr;
*n = new_n;
} }
pos = 0; (*lineptr)[0] = '\0';
while (c != EOF) {
if (pos + 1 >= *n) { while (fgets(buff, sizeof(buff), fp) != nullptr) {
size_t new_size = *n + (*n >> 2); size_t line_used = strlen(*lineptr);
if (new_size < get_line_initial_buffer_size) { size_t buff_used = strlen(buff);
new_size = get_line_initial_buffer_size;
} if (*n < buff_used + line_used) {
char* new_ptr = static_cast<char*>( size_t new_n = *n * 2;
realloc(static_cast<void*>(*lineptr), new_size));
if (new_ptr == nullptr) { auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n));
if (new_lineptr == nullptr) {
errno = ENOMEM;
return -1; return -1;
} }
*n = new_size;
*lineptr = new_ptr; *lineptr = new_lineptr;
*n = new_n;
} }
(*lineptr)[pos++] = c; memcpy(*lineptr + line_used, buff, buff_used);
if (c == '\n') { line_used += buff_used;
break; (*lineptr)[line_used] = '\0';
if ((*lineptr)[line_used - 1] == '\n') {
return line_used;
} }
c = getc(stream);
} }
(*lineptr)[pos] = '\0'; return -1;
return pos;
} }
#endif #endif
} /* ss */ } /* ss */