memzap

replay memory writes
git clone git://git.2f30.org/memzap
Log | Files | Refs | README | LICENSE

memstream.c (653B)


      1 #include <string.h>
      2 #include <stdio.h>
      3 #include <unistd.h>
      4 #include "memstream.h"
      5 
      6 void memstream_init(struct memstream *s, int fd, void* buffer, size_t len) {
      7 	s->file = fd,
      8 	s->data = buffer;
      9 	s->pos = 0;
     10 	s->capa = len;
     11 }
     12 
     13 void memstream_flush(struct memstream *s) {
     14 	write(s->file, s->data, s->pos);
     15 	s->pos = 0;
     16 }
     17 
     18 void memstream_write(struct memstream *s, const void* source, size_t len) {
     19 	const unsigned char* data = source;
     20 	if(s->pos + len > s->capa) memstream_flush(s);
     21 	memcpy(s->data + s->pos, data, len);
     22 	s->pos += len;
     23 }
     24 
     25 void memstream_puts(struct memstream *s, const char* str) {
     26 	memstream_write(s, (const void*) str, strlen(str));
     27 }