/** * Check if a complete line of input is present in the input buffer, * and duplicates it. It is removed from the input buffer. The * return value has to be freed with g_free(). */ static char * httpd_client_read_line(struct httpd_client *client) { assert(client != NULL); assert(client->state != RESPONSE); const char *p, *newline; size_t length; char *line; p = fifo_buffer_read(client->input, &length); if (p == NULL) /* empty input buffer */ return NULL; newline = memchr(p, '\n', length); if (newline == NULL) /* incomplete line */ return NULL; line = g_strndup(p, newline - p); fifo_buffer_consume(client->input, newline - p + 1); /* remove trailing whitespace (e.g. '\r') */ return g_strchomp(line); }
static size_t null_encoder_read(struct encoder *_encoder, void *dest, size_t length) { struct null_encoder *encoder = (struct null_encoder *)_encoder; size_t max_length; const void *src = fifo_buffer_read(encoder->buffer, &max_length); if (src == NULL) return 0; if (length > max_length) length = max_length; memcpy(dest, src, length); fifo_buffer_consume(encoder->buffer, length); return length; }
static gboolean mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel *_source, G_GNUC_UNUSED GIOCondition condition, gpointer data) { struct mpd_inotify_source *source = data; void *dest; size_t length; ssize_t nbytes; const struct inotify_event *event; dest = fifo_buffer_write(source->buffer, &length); if (dest == NULL) MPD_ERROR("buffer full"); nbytes = read(source->fd, dest, length); if (nbytes < 0) MPD_ERROR("failed to read from inotify: %s", g_strerror(errno)); if (nbytes == 0) MPD_ERROR("end of file from inotify"); fifo_buffer_append(source->buffer, nbytes); while (true) { const char *name; event = fifo_buffer_read(source->buffer, &length); if (event == NULL || length < sizeof(*event) || length < sizeof(*event) + event->len) break; if (event->len > 0 && event->name[event->len - 1] == 0) name = event->name; else name = NULL; source->callback(event->wd, event->mask, name, source->callback_ctx); fifo_buffer_consume(source->buffer, sizeof(*event) + event->len); } return true; }
static char * client_read_line(struct client *client) { const char *p, *newline; size_t length; char *line; p = fifo_buffer_read(client->input, &length); if (p == NULL) return NULL; newline = memchr(p, '\n', length); if (newline == NULL) return NULL; line = g_strndup(p, newline - p); fifo_buffer_consume(client->input, newline - p + 1); return g_strchomp(line); }
static OSStatus osx_render(void *vdata, G_GNUC_UNUSED AudioUnitRenderActionFlags *io_action_flags, G_GNUC_UNUSED const AudioTimeStamp *in_timestamp, G_GNUC_UNUSED UInt32 in_bus_number, G_GNUC_UNUSED UInt32 in_number_frames, AudioBufferList *buffer_list) { struct osx_output *od = (struct osx_output *) vdata; AudioBuffer *buffer = &buffer_list->mBuffers[0]; size_t buffer_size = buffer->mDataByteSize; assert(od->buffer != NULL); g_mutex_lock(od->mutex); size_t nbytes; const void *src = fifo_buffer_read(od->buffer, &nbytes); if (src != NULL) { if (nbytes > buffer_size) nbytes = buffer_size; memcpy(buffer->mData, src, nbytes); fifo_buffer_consume(od->buffer, nbytes); } else nbytes = 0; g_cond_signal(od->condition); g_mutex_unlock(od->mutex); if (nbytes < buffer_size) memset((unsigned char*)buffer->mData + nbytes, 0, buffer_size - nbytes); return 0; }