mirror of
https://github.com/red0124/ssp.git
synced 2025-02-02 16:51:12 +01:00
add custom getline for WIN...
This commit is contained in:
parent
208914ff96
commit
3578e7b99c
@ -16,4 +16,61 @@ inline void assert_string_error_defined() {
|
||||
"'string_error' needs to be enabled to use 'error_msg'");
|
||||
}
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#include <cstdint>
|
||||
using ssize_t = int64_t;
|
||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
||||
size_t pos;
|
||||
int c;
|
||||
|
||||
if (lineptr == nullptr || stream == nullptr || n == nullptr) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
c = getc(stream);
|
||||
if (c == EOF) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (*lineptr == nullptr) {
|
||||
*lineptr = static_cast<char*>(malloc(128));
|
||||
if (*lineptr == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
*n = 128;
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while (c != EOF) {
|
||||
if (pos + 1 >= *n) {
|
||||
size_t new_size = *n + (*n >> 2);
|
||||
if (new_size < 128) {
|
||||
new_size = 128;
|
||||
}
|
||||
char* new_ptr = static_cast<char*>(
|
||||
realloc(static_cast<void*>(*lineptr), new_size));
|
||||
if (new_ptr == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
*n = new_size;
|
||||
*lineptr = new_ptr;
|
||||
}
|
||||
|
||||
(*lineptr)[pos++] = c;
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
c = getc(stream);
|
||||
}
|
||||
|
||||
(*lineptr)[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
#else
|
||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
||||
return getline(lineptr, n, stream);
|
||||
}
|
||||
#endif
|
||||
|
||||
} /* ss */
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.hpp"
|
||||
#include "converter.hpp"
|
||||
#include "extract.hpp"
|
||||
#include "restrictions.hpp"
|
||||
@ -379,7 +380,7 @@ private:
|
||||
bool read_next() {
|
||||
memset(next_line_buffer_, '\0', next_line_size_);
|
||||
ssize_t ssize =
|
||||
getline(&next_line_buffer_, &next_line_size_, file_);
|
||||
get_line(&next_line_buffer_, &next_line_size_, file_);
|
||||
|
||||
if (ssize == -1) {
|
||||
return false;
|
||||
@ -500,7 +501,8 @@ private:
|
||||
bool append_next_line_to_buffer(char*& buffer, size_t& size) {
|
||||
undo_remove_eol(buffer, size);
|
||||
|
||||
ssize_t next_ssize = getline(&helper_buffer_, &helper_size_, file_);
|
||||
ssize_t next_ssize =
|
||||
get_line(&helper_buffer_, &helper_size_, file_);
|
||||
if (next_ssize == -1) {
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user