Pārlūkot izejas kodu

faux: Unfinished chunk subclass for file class

Serj Kalichev 4 gadi atpakaļ
vecāks
revīzija
c4baeccc73
3 mainītis faili ar 138 papildinājumiem un 0 dzēšanām
  1. 1 0
      faux/file/Makefile.am
  2. 128 0
      faux/file/chunk.c
  3. 9 0
      faux/file/private.h

+ 1 - 0
faux/file/Makefile.am

@@ -1,3 +1,4 @@
 libfaux_la_SOURCES += \
+	faux/file/chunk.c \
 	faux/file/file.c \
 	faux/file/private.h

+ 128 - 0
faux/file/chunk.c

@@ -0,0 +1,128 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "private.h"
+
+faux_chunk_t *faux_chunk_new(size_t size) {
+
+	faux_chunk_t *chunk = NULL;
+
+	if (0 == size) // Illegal 0 size
+		return NULL;
+
+	chunk = faux_zmalloc(sizeof(*chunk));
+	if (!chunk)
+		return NULL;
+
+	// Init
+	chunk->buf = faux_zmalloc(size);
+	if (!chunk->buf) {
+		faux_free(chunk);
+		return NULL;
+	}
+	chunk->size = size;
+	chunk->start = chunk->buf;
+	chunk->end = chunk->buf;
+
+	return chunk;
+}
+
+
+void faux_chunk_free(faux_chunk_t *chunk) {
+
+	// Without assert()
+	if (!chunk)
+		return;
+
+	faux_free(chunk->buf);
+	faux_free(chunk);
+}
+
+
+size_t faux_chunk_len(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return 0;
+
+	return (chunk->end - chunk->start);
+}
+
+
+ssize_t faux_chunk_inc_len(faux_chunk_t *chunk, size_t inc_len) {
+
+	assert(chunk);
+	if (!chunk)
+		return -1;
+	assert((faux_chunk_len(chunk) + inc_len) <= chunk->size);
+	if ((faux_chunk_len(chunk) + inc_len) > chunk->size)
+		return -1;
+	chunk->end += inc_len;
+
+	return faux_chunk_len(chunk);
+}
+
+
+ssize_t faux_chunk_dec_len(faux_chunk_t *chunk, size_t dec_len) {
+
+	assert(chunk);
+	if (!chunk)
+		return -1;
+	assert(faux_chunk_len(chunk) >= dec_len);
+	if (faux_chunk_len(chunk) < dec_len)
+		return -1;
+	chunk->start += dec_len;
+
+	return faux_chunk_len(chunk);
+}
+
+
+ssize_t faux_chunk_size(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return -1;
+
+	return chunk->size;
+}
+
+
+void *faux_chunk_buf(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return NULL;
+
+	return chunk->buf;
+}
+
+
+void *faux_chunk_write_pos(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return NULL;
+
+	return chunk->end;
+}
+
+
+void *faux_chunk_read_pos(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return NULL;
+
+	return chunk->start;
+}
+
+
+ssize_t faux_chunk_left(faux_chunk_t *chunk) {
+
+	assert(chunk);
+	if (!chunk)
+		return -1;
+
+	return (chunk->buf + chunk->size - chunk->end);
+}

+ 9 - 0
faux/file/private.h

@@ -4,6 +4,15 @@
 /** @brief Chunk size to allocate buffer */
 #define FAUX_FILE_CHUNK_SIZE 128
 
+struct faux_chunk_s {
+	void *buf; // Allocated buffer
+	size_t size; // Size of allocated buffer
+	void *start; // Pointer to data start
+	void *end; // Pointer to data end
+};
+
+typedef struct faux_chunk_s faux_chunk_t;
+
 struct faux_file_s {
 	int fd; // File descriptor
 	char *buf; // Data buffer