Remove commented code and use a temporary strcpy buffer
This commit is contained in:
parent
35f5f893ce
commit
60fd437b55
92
src/memory.c
92
src/memory.c
|
@ -28,8 +28,10 @@ int readline_mem(Buffer_Info* bs)
|
|||
if (bs->append_pos == NULL)
|
||||
bs->append_pos = bs->buffer;
|
||||
for (;;) {
|
||||
char tmpcpy[513];
|
||||
if (bs->nex_line != NULL) {
|
||||
strcpy(bs->buffer, bs->nex_line);
|
||||
strcpy(tmpcpy, bs->nex_line);
|
||||
strcpy(bs->buffer, tmpcpy);
|
||||
bs->append_pos -= (bs->nex_line - bs->buffer);
|
||||
bs->nex_line = NULL;
|
||||
}
|
||||
|
@ -61,91 +63,3 @@ int readline_mem(Buffer_Info* bs)
|
|||
}
|
||||
return READLINE_LINE_READY;
|
||||
}
|
||||
/*
|
||||
int readline_mem(Buffer_Info* bs)
|
||||
{
|
||||
const int step = 513;
|
||||
bool linefin = false;
|
||||
|
||||
if (bs->fd == NULL || *(bs->fd) <= 0)
|
||||
return READLINE_SOCKET_FAIL;
|
||||
// Initiate buffer with the step size
|
||||
if (bs->buffer == NULL) {
|
||||
if (!init_buff(bs, step))
|
||||
return READLINE_BUFINIT_FAIL;
|
||||
}
|
||||
|
||||
// Read to the last position in the buffer or at the beginning if buffer was just initiated
|
||||
int b_read;
|
||||
char* nl = NULL;
|
||||
while (!linefin) {
|
||||
if ((nl = strchr(bs->nex_line, '\n')) != NULL) {
|
||||
if (nl > bs->nex_line && *(nl - 1) == '\r')
|
||||
*(nl - 1) = '\0';
|
||||
*nl = '\0';
|
||||
bs->cur_line = bs->nex_line;
|
||||
bs->nex_line = nl + 1;
|
||||
break;
|
||||
}
|
||||
if (!realloc_full(bs, step))
|
||||
return READLINE_REALLOC_FAIL;
|
||||
struct pollfd pollinf[] = {{.fd = *bs->fd, .events = POLLRDNORM}};
|
||||
switch (poll(pollinf, 1, 10)) {
|
||||
case -1: {
|
||||
if (errno != EINTR) // We don't throw a error on a damn ^C
|
||||
return READLINE_POLL_FAIL;
|
||||
return READLINE_INTERRUPTED;
|
||||
}
|
||||
default: {
|
||||
if (pollinf[0].revents ^ POLLRDNORM)
|
||||
return READLINE_TRYAGAIN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
b_read = read(*bs->fd, bs->append_pos, bs->end_pos - bs->append_pos);
|
||||
if (b_read == EOF)
|
||||
return READLINE_EOF;
|
||||
bs->append_pos += b_read;
|
||||
*bs->append_pos = '\0';
|
||||
}
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" // SEE NOTE[1]
|
||||
if (b_read == EOF) {
|
||||
free(bs->buffer);
|
||||
return READLINE_EOF;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return READLINE_LINE_READY;
|
||||
}
|
||||
int init_buff(Buffer_Info* bs, int size)
|
||||
{
|
||||
if (size > 1 && (bs->buffer = malloc(size * sizeof(char))) != NULL) {
|
||||
// NOTE[1]: GCC static analysis complains about moving a pointer which is not even relevant to the buffer pointer, assumes memory leak
|
||||
bs->end_pos = bs->buffer + size - 1;
|
||||
bs->append_pos = bs->buffer;
|
||||
bs->nex_line = bs->buffer;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int realloc_full(Buffer_Info* bs, int size)
|
||||
{
|
||||
if (bs->append_pos == bs->end_pos) {
|
||||
char* newpoint;
|
||||
bs->end_pos += size;
|
||||
newpoint = realloc(bs->buffer, (bs->end_pos - bs->buffer) * sizeof(char));
|
||||
if (newpoint != NULL) {
|
||||
int offset = newpoint - bs->buffer;
|
||||
// Move all pointers or you will have a BAAD time (count: 2)
|
||||
bs->append_pos += offset;
|
||||
bs->nex_line += offset;
|
||||
bs->cur_line += offset;
|
||||
bs->end_pos += offset;
|
||||
bs->buffer = newpoint;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 2; // Nothing to do, the buffer is not full yet
|
||||
}
|
||||
*/
|
||||
|
|
Reference in New Issue