1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

use internal implementation of getline

MacOS doesn't have function getline
This commit is contained in:
Dmitry Podgorny 2012-11-18 04:31:32 +02:00
parent cf3d50f855
commit 2630c111be
3 changed files with 54 additions and 12 deletions

View File

@ -129,8 +129,7 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient,
char *filename = _get_log_filename(recipient, login, log_date, FALSE); char *filename = _get_log_filename(recipient, login, log_date, FALSE);
FILE *logp = fopen(filename, "r"); FILE *logp = fopen(filename, "r");
char *line = NULL; char *line;
size_t read = 0;
if (logp != NULL) { if (logp != NULL) {
GString *gs_header = g_string_new(""); GString *gs_header = g_string_new("");
g_string_append_printf(gs_header, "%d/%d/%d:", g_string_append_printf(gs_header, "%d/%d/%d:",
@ -141,16 +140,8 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient,
history = g_slist_append(history, header); history = g_slist_append(history, header);
g_string_free(gs_header, TRUE); g_string_free(gs_header, TRUE);
size_t length = getline(&line, &read, logp); while ((line = prof_getline(logp)) != NULL) {
while (length != -1) { history = g_slist_append(history, line);
char *copy = malloc(length);
copy = strncpy(copy, line, length);
copy[length -1] = '\0';
history = g_slist_append(history, copy);
free(line);
line = NULL;
read = 0;
length = getline(&line, &read, logp);
} }
fclose(logp); fclose(logp);

View File

@ -29,6 +29,10 @@
#include "common.h" #include "common.h"
// assume malloc stores at most 8 bytes for size of allocated memory
// and page size is at least 4KB
#define READ_BUF_SIZE 4088
// backwards compatibility for GLib version < 2.28 // backwards compatibility for GLib version < 2.28
void void
p_slist_free_full(GSList *items, GDestroyNotify free_func) p_slist_free_full(GSList *items, GDestroyNotify free_func)
@ -118,3 +122,48 @@ encode_xml(const char * const xml)
return coded_msg3; return coded_msg3;
} }
char *
prof_getline(FILE *stream)
{
char *buf;
size_t buf_size;
char *result;
char *s = NULL;
size_t s_size = 1;
int need_exit = 0;
buf = (char *)malloc(READ_BUF_SIZE);
while (TRUE) {
result = fgets(buf, READ_BUF_SIZE, stream);
if (result == NULL)
break;
buf_size = strlen(buf);
if (buf[buf_size - 1] == '\n') {
buf_size--;
buf[buf_size] = '\0';
need_exit = 1;
}
result = (char *)realloc(s, s_size + buf_size);
if (result == NULL) {
if (s != NULL) {
free(s);
s = NULL;
}
break;
}
s = result;
memcpy(s + s_size - 1, buf, buf_size);
s_size += buf_size;
s[s_size - 1] = '\0';
if (need_exit != 0 || feof(stream) != 0)
break;
}
free(buf);
return s;
}

View File

@ -23,6 +23,7 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <stdio.h>
#include <glib.h> #include <glib.h>
#if !GLIB_CHECK_VERSION(2,28,0) #if !GLIB_CHECK_VERSION(2,28,0)
@ -41,5 +42,6 @@ char * str_replace(const char *string, const char *substr,
const char *replacement); const char *replacement);
int str_contains(char str[], int size, char ch); int str_contains(char str[], int size, char ch);
char* encode_xml(const char * const xml); char* encode_xml(const char * const xml);
char * prof_getline(FILE *stream);
#endif #endif