From c1a6303fea322f1de432897524b25fa238896225 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 16 May 2007 21:13:17 +0200 Subject: [PATCH] we have buffers, add an API to readln directly in a buffer instead of implementing it twice. use it in pop.c; Signed-off-by: Pierre Habouzit --- lib-lib/buffer.h | 5 +++ lib-sys/mutt_socket.c | 17 ++++++++++ lib-sys/mutt_socket.h | 2 ++ pop.c | 75 ++++++++++++++++++++----------------------- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index a4d1912..9e919b7 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -86,6 +86,11 @@ static inline void buffer_addch(buffer_t *buf, int c) { buffer_extendch(buf, 1, c); } +static inline void buffer_reset(buffer_t *buf) { + buf->len = 0; + buf->data[0] = '\0'; +} + /****** LEGACY BUFFERS *******/ diff --git a/lib-sys/mutt_socket.c b/lib-sys/mutt_socket.c index 3528db4..194ad5a 100644 --- a/lib-sys/mutt_socket.c +++ b/lib-sys/mutt_socket.c @@ -150,6 +150,23 @@ int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn) return i + 1; } +int mutt_socket_readln2(buffer_t *buf, CONNECTION *conn) +{ + char ch; + + while (mutt_socket_readchar(conn, &ch) == 1) { + if (ch == '\n') { + if (buf->data[buf->len - 1] == '\r') { + buf->data[--buf->len] = '\0'; + } + return 0; + } + buffer_addch(buf, ch); + } + + return -1; +} + CONNECTION *mutt_socket_head (void) { return Connections; diff --git a/lib-sys/mutt_socket.h b/lib-sys/mutt_socket.h index 7df5d56..02494a4 100644 --- a/lib-sys/mutt_socket.h +++ b/lib-sys/mutt_socket.h @@ -50,6 +50,8 @@ int mutt_socket_readchar (CONNECTION * conn, char *c); int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn); int mutt_socket_write(CONNECTION * conn, const char *buf); +int mutt_socket_readln2(buffer_t *buf, CONNECTION *conn); + int mutt_ssl_starttls (CONNECTION * conn); int mutt_ssl_socket_setup (CONNECTION * conn); diff --git a/pop.c b/pop.c index 30dcf2d..512dfda 100644 --- a/pop.c +++ b/pop.c @@ -454,55 +454,48 @@ static pop_query_status pop_fetch_data(pop_data_t *pop_data, const char *query, progress_t *bar, int (*funct)(char *, void *), void *data) { - char buf[LONG_STRING]; - char *inbuf; - char *p; - pop_query_status ret; - int chunk = 0; - long pos = 0; - ssize_t lenbuf = 0; + pop_query_status ret; + char buf[LONG_STRING]; + buffer_t inbuf; + ssize_t pos = 0; - m_strcpy(buf, sizeof(buf), query); - ret = _pop_query(pop_data, buf, sizeof(buf)); - if (ret != PQ_OK) - return ret; + buffer_init(&inbuf); - inbuf = p_new(char, sizeof(buf)); + m_strcpy(buf, sizeof(buf), query); + ret = _pop_query(pop_data, buf, sizeof(buf)); + if (ret != PQ_OK) + return ret; - for (;;) { - chunk = - mutt_socket_readln(buf, sizeof (buf), pop_data->conn); - if (chunk < 0) { - pop_data->status = POP_DISCONNECTED; - ret = PQ_NOT_CONNECTED; - break; - } + for (;;) { + int dot = 0; - p = buf; - if (!lenbuf && buf[0] == '.') { - if (buf[1] != '.') - break; - p++; - } + if (mutt_socket_readln2(&inbuf, pop_data->conn) < 0) { + pop_data->status = POP_DISCONNECTED; + ret = PQ_NOT_CONNECTED; + break; + } - m_strcpy(inbuf + lenbuf,sizeof(buf), p); - pos += chunk; + if (bar) { + mutt_progress_bar(bar, pos += inbuf.len); + } - if (chunk >= ssizeof(buf)) { - lenbuf += strlen (p); - } else { - if (bar) - mutt_progress_bar (bar, pos); - if (ret == 0 && funct (inbuf, data) < 0) - ret = PFD_FUNCT_ERROR; - lenbuf = 0; - } + if (inbuf.data[0] == '.') { + if (inbuf.data[1] != '.') + break; + dot = 1; + } - p_realloc(&inbuf, lenbuf + sizeof(buf)); - } + if (funct(inbuf.data + dot, data) < 0) { + buffer_wipe(&inbuf); + ret = PFD_FUNCT_ERROR; + break; + } - p_delete(&inbuf); - return ret; + buffer_reset(&inbuf); + } + + buffer_wipe(&inbuf); + return ret; } static int fetch_capa (char *line, void *data) -- 2.20.1