From b1965eba20eb28010dca4d5965b389420c0c941e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 25 Oct 2010 12:20:54 +0100 Subject: Reformat files, add comments, add copyright and license info. --- dynstr.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- dynstr.h | 13 +++++++++++++ unittests.c | 20 +++++++++++++++++++- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/dynstr.c b/dynstr.c index a77b2b6..d4ae012 100644 --- a/dynstr.c +++ b/dynstr.c @@ -1,5 +1,28 @@ +/* + * dynstr.c - implement dynamic byte strings + * Copyright 2010 Lars Wirzenius + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/* We shamelessly use GNU extensions to the standard C library. + * Primarily this is the memmem(3) function. Patches are welcome to + * get rid of this, but I did not feel like re-inventing the wheel. + */ #define _GNU_SOURCE -#include + #include #include #include @@ -9,9 +32,7 @@ #include "dynstr.h" -#define DYNSTR_ERROR ((size_t) -1) - - +/* This is how the dynamic strings are represented. */ struct Dynstr { const unsigned char *mem; size_t size; @@ -19,6 +40,11 @@ struct Dynstr { }; +/* Return value for readline_helper's callback functions. */ +#define DYNSTR_ERROR ((size_t) -1) + + +/* The current malloc and malloc error handler functions. */ static dynstr_error_handler *error_handler = dynstr_malloc_error_indicate; static void *(*current_malloc)(size_t) = malloc; @@ -29,26 +55,31 @@ void dynstr_init(void) current_malloc = malloc; } + void *(*dynstr_get_malloc(void))(size_t) { return current_malloc; } + void dynstr_set_malloc(void *(*new_malloc)(size_t)) { current_malloc = new_malloc; } + dynstr_error_handler *dynstr_get_malloc_error_handler(void) { return error_handler; } + void dynstr_set_malloc_error_handler(dynstr_error_handler *handler) { error_handler = handler; } + void dynstr_malloc_error_indicate(int error, size_t size, void *oldptr) { } @@ -58,7 +89,8 @@ void dynstr_malloc_error_abort(int error, size_t size, void *oldptr) abort(); } -/* Dynamically allocate a number of bytes. */ + +/* Dynamically allocate a number of bytes. Call error handler if necessary. */ static void *alloc(size_t size) { void *p; @@ -69,6 +101,7 @@ static void *alloc(size_t size) return p; } + /* Allocate a new dynamic string. If dynamic is true, the contents of the * new string is copied from mem. Otherwise the new string just uses mem * directly. */ @@ -97,31 +130,37 @@ static Dynstr *new(const void *mem, size_t size, bool dynamic) return dynstr; } + Dynstr *dynstr_new_empty(void) { return new(NULL, 0, true); } + Dynstr *dynstr_new_from_cstring(const char *cstring) { return new(cstring, strlen(cstring), true); } + Dynstr *dynstr_new_from_memory(const void *mem, size_t size) { return new(mem, size, true); } + Dynstr *dynstr_new_from_constant_cstring(const char *cstring) { return new(cstring, strlen(cstring), false); } + Dynstr *dynstr_new_from_constant_memory(const void *mem, size_t size) { return new(mem, size, false); } + void dynstr_free(Dynstr *dynstr) { if (dynstr) { @@ -131,16 +170,19 @@ void dynstr_free(Dynstr *dynstr) } } + size_t dynstr_len(Dynstr *dynstr) { return dynstr->size; } + bool dynstr_is_empty(Dynstr *dynstr) { return dynstr_len(dynstr) == 0; } + size_t dynstr_memcpy(void *mem, Dynstr *dynstr, size_t offset, size_t size) { size_t len; @@ -154,6 +196,7 @@ size_t dynstr_memcpy(void *mem, Dynstr *dynstr, size_t offset, size_t size) return size; } + Dynstr *dynstr_substr(Dynstr *dynstr, size_t offset, size_t size) { if (offset >= dynstr->size) { @@ -165,6 +208,7 @@ Dynstr *dynstr_substr(Dynstr *dynstr, size_t offset, size_t size) return new(dynstr->mem + offset, size, true); } + Dynstr *dynstr_cat(Dynstr *dynstr, ...) { va_list args; @@ -305,6 +349,8 @@ ssize_t dynstr_write(int fd, Dynstr *dynstr) } +/* A helper function for dynstr_read and dynstr_fread. The callback does + * the actual reading. */ static Dynstr *read_helper(size_t (*callback)(FILE *f, int fd, unsigned char *buf, size_t size), FILE *f, int fd, size_t size) @@ -370,6 +416,9 @@ Dynstr *dynstr_read(int file, size_t size) } +/* Helper function for dynstr_readline and dynstr_freadline. The callback + * does the actual reading. Reading happens one byte at a time, since we + * must not read past the newline that terminates a line. */ static Dynstr *readline_helper(int (*callback)(FILE *, int), FILE *f, int fd) { Dynstr *line; diff --git a/dynstr.h b/dynstr.h index 5c5bc86..4009c62 100644 --- a/dynstr.h +++ b/dynstr.h @@ -15,6 +15,19 @@ * called before NULL is returned. This function can, for example, abort * the program (see dynstr_malloc_error_abort), or use longjmp to jump * some error handling routine. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #ifndef DYNSTR_H diff --git a/unittests.c b/unittests.c index 32bb270..a0d84c8 100644 --- a/unittests.c +++ b/unittests.c @@ -1,4 +1,22 @@ -#define _POSIX_C_SOURCE 200112L +/* + * unittests.c - unit tests for dynamic byte strings + * Copyright 2010 Lars Wirzenius + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + #define _GNU_SOURCE #include -- cgit v1.2.1