/* Return: * -1 if error read (perror) * 0 close fd (check not here) * >0 read from fd one symbol. write to buffer then check overflow buf * and extend if it need. */ int ReadToBuffer (struct client * user, int fd) { int rc; char c; user->buf->str = user->buf->str; rc = read (fd, &c, sizeof(char)); if ( rc == -1 ) { perror ("read"); } if ( user->buf->cnt == BUF_SIZE * user->buf->part - 1) { ExtendBuffer (user->buf); } if ( rc != 0 ) { user->buf->str[user->buf->cnt++] = c; user->buf->str[user->buf->cnt] = '\0'; } return rc; }
static int __gz_lines_iterator (lua_State *L) { gzFile zf = (gzFile) lua_topointer (L, lua_upvalueindex(1)); int ch = '\0'; char* ret; if (content_buffer_length == 0) ExtendBuffer (); else ResetBuffer (); #ifdef READ_LINE_ONE_BY_ONE while ( (ch = gzgetc(zf)) != -1 && ch != '\n') { AddToBuffer ((char) ch); } #else do { ret = gzgets (zf, content_buffer + BufferFill (), BufferFree ()); if (ret == Z_NULL) break; int l = strlen (content_buffer); content_length = l; if (l > 0) { ch = content_buffer[l - 1]; if (ch != '\n') ExtendBuffer (); else content_buffer[l-1] = '\0'; } } while (ret && ch != '\n'); #endif if (ch == '\n' || BufferFill () > 0) { if (ch == '\n') lua_pushstring (L, FinishBuffer ()); return 1; } else { return pushresult(L, &zf, NULL); } }
static inline void AddToBuffer (char c) { if (content_buffer_length == content_length) ExtendBuffer (); content_buffer [content_length++] = c; }