#include "sockbuf.h" #include #include #include void sockbuf_write(sockbuf_t* sockbuf, const char* value) { write(sockbuf->socket, value, strlen(value)); } sockbuf_t* init_sockbuf(sockbuf_t* sockbuf, int socket) { memset(sockbuf, 0, sizeof(sockbuf_t)); sockbuf->socket = socket; return sockbuf; } int peek_char(sockbuf_t* sockbuf) { if (sockbuf->next == sockbuf->last) { sockbuf->next = 0; int size = read(sockbuf->socket, sockbuf->buf, sizeof(sockbuf->buf)); sockbuf->last = (uint8_t)size; if (size <= 0) { return size - 1; } } return sockbuf->buf[sockbuf->next]; } int get_char(sockbuf_t* sockbuf) { int ret = peek_char(sockbuf); if (ret >= 0) { sockbuf->next++; } return ret; } char* read_token(sockbuf_t* sockbuf, char* into, size_t sz) { int cur = 0; int ch = get_char(sockbuf); while (ch >= 0 && isspace(ch)) { ch = get_char(sockbuf); } while (ch >= 0 && !isspace(ch)) { if (sz > 1) { into[cur++] = (char)ch; sz--; } ch = get_char(sockbuf); } if (ch < 0) { return NULL; } if (sz >= 1) { into[cur] = 0; } return into; }