Browse Source

add buffer_init_allocbuf, buffer_init_read, buffer_init_write, buffer_init_read_allocbuf, buffer_init_write_allocbuf

master
Felix von Leitner 3 months ago
parent
commit
93111d2dc7
7 changed files with 72 additions and 2 deletions
  1. +1
    -0
      CHANGES
  2. +26
    -2
      buffer.h
  3. +13
    -0
      buffer/buffer_init_allocbuf.c
  4. +9
    -0
      buffer/buffer_init_read.c
  5. +7
    -0
      buffer/buffer_init_read_allocbuf.c
  6. +9
    -0
      buffer/buffer_init_write.c
  7. +7
    -0
      buffer/buffer_init_write_allocbuf.c

+ 1
- 0
CHANGES View File

@ -9,6 +9,7 @@
add fmt_strm_malloc
add cross references to open_* and mmap_* man pages
add fmt_strm_alloca and fmt_strm_malloc man pages
add buffer_init_allocbuf, buffer_init_read, buffer_init_write, buffer_init_read_allocbuf, buffer_init_write_allocbuf
0.32:
remove OpenBSD #warning (obsd maintainer says no longer needed)


+ 26
- 2
buffer.h View File

@ -44,15 +44,39 @@ typedef struct buffer {
#define BUFFER_OUTSIZE 8192
/* Initialize a buffer with an existing memory area, which the buffer
* will NOT take ownership of (i.e. won't free the memory when it's done */
* will NOT take ownership of (i.e. won't free the memory when it's done) */
att_writen(4,5)
void buffer_init(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
void buffer_init(buffer* b, ssize_t (*op)(), int fd, char* y, size_t ylen);
/* Initialize a buffer with an existing memory area, which the buffer
* WILL take ownership of (it will call free() on it when it's done) */
att_writen(4,5)
void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
/* Call buffer_init with op=read(), return 0.
* If fd==-1, return -1 instead, leaving b untouched. */
att_writen(3,4)
int buffer_init_read(buffer* b, int fd, char* y, size_t ylen);
/* Call buffer_init with op=write(), return 0.
* If fd==-1, return -1 instead, leaving b untouched. */
att_writen(3,4)
int buffer_init_write(buffer* b, int fd, char* y, size_t ylen);
/* Will allocate a buffer of size ylen and then call
* buffer_init_free(b, op, fd, thebuffer, ylen) on it.
* Returns 0 on success, -1 on error (setting errno).
* Passing fd==-1 is treated as error (so you can pass open*() for fd). */
int buffer_init_allocbuf(buffer* b, ssize_t (*op)(), int fd, size_t ylen);
/* Call buffer_init_allocbuf with op=read */
int buffer_init_read_allocbuf(buffer* b, int fd, size_t ylen);
/* Call buffer_init_allocbuf with op=read */
int buffer_init_write_allocbuf(buffer* b, int fd, size_t ylen);
/* Initialize a buffer without actual I/O.
* You give it a pre-existing memory area.
* When reading from this buffer, it will simply return the data from


+ 13
- 0
buffer/buffer_init_allocbuf.c View File

@ -0,0 +1,13 @@
#include <stdlib.h>
#include <unistd.h>
#include "buffer.h"
int buffer_init_allocbuf(buffer* b, ssize_t (*op)(), int fd, size_t ylen) {
if (fd==-1) return -1;
char* thebuffer;
if (!(thebuffer=malloc(ylen)))
return -1;
buffer_init_free(b, op, fd, thebuffer, ylen);
return 0;
}

+ 9
- 0
buffer/buffer_init_read.c View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <buffer.h>
int buffer_init_read(buffer* b, int fd, char* y,size_t ylen) {
if (fd==-1) return -1;
buffer_init(b, read, fd, y, ylen);
return 0;
}

+ 7
- 0
buffer/buffer_init_read_allocbuf.c View File

@ -0,0 +1,7 @@
#include <unistd.h>
#include "buffer.h"
int buffer_init_read_allocbuf(buffer* b, int fd, size_t ylen) {
return buffer_init_allocbuf(b, read, fd, ylen);
}

+ 9
- 0
buffer/buffer_init_write.c View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <buffer.h>
int buffer_init_write(buffer* b, int fd, char* y,size_t ylen) {
if (fd==-1) return -1;
buffer_init(b, write, fd, y, ylen);
return 0;
}

+ 7
- 0
buffer/buffer_init_write_allocbuf.c View File

@ -0,0 +1,7 @@
#include <unistd.h>
#include "buffer.h"
int buffer_init_write_allocbuf(buffer* b, int fd, size_t ylen) {
return buffer_init_allocbuf(b, write, fd, ylen);
}

Loading…
Cancel
Save