| 1 |
/* |
| 2 |
Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2011-2018 David Anderson. All Rights Reserved. |
| 4 |
This program is free software; you can redistribute it |
| 5 |
and/or modify it under the terms of version 2 of the GNU |
| 6 |
General Public License as published by the Free Software |
| 7 |
Foundation. |
| 8 |
|
| 9 |
This program is distributed in the hope that it would |
| 10 |
be useful, but WITHOUT ANY WARRANTY; without even the |
| 11 |
implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 |
PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
Further, this software is distributed without any warranty |
| 15 |
that it is free of the rightful claim of any third person |
| 16 |
regarding infringement or the like. Any license provided |
| 17 |
herein, whether implied or otherwise, applies only to |
| 18 |
this software file. Patent licenses, if any, provided |
| 19 |
herein do not apply to combinations of this program with |
| 20 |
other software, or any other product whatsoever. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public |
| 23 |
License along with this program; if not, write the Free |
| 24 |
Software Foundation, Inc., 51 Franklin Street - Fifth |
| 25 |
Floor, Boston MA 02110-1301, USA. |
| 26 |
*/ |
| 27 |
|
| 28 |
/* esb.h |
| 29 |
Extensible string buffer. |
| 30 |
A simple vaguely object oriented extensible string buffer. |
| 31 |
|
| 32 |
The struct could be opaque here, but it seems ok to expose |
| 33 |
the contents: simplifies debugging. |
| 34 |
*/ |
| 35 |
|
| 36 |
#ifndef ESB_H |
| 37 |
#define ESB_H |
| 38 |
|
| 39 |
#ifdef __cplusplus |
| 40 |
extern "C" { |
| 41 |
#endif /* __cplusplus */ |
| 42 |
/* If esb_allocated_size > 0 then esb_string points to |
| 43 |
esb_allocated_size bytes and |
| 44 |
esb_used bytes <= (esb_allocated_size -1) |
| 45 |
All operations that insert or return strings |
| 46 |
ensure, first, that esb_allocated_size is non-zero |
| 47 |
and the requirements here are met. |
| 48 |
|
| 49 |
In esb_constructor esb_allocated_size and esb_used_bytes |
| 50 |
and esb_string are set 0. No malloc done. |
| 51 |
|
| 52 |
Default init alloc sets esb_allocated_size=alloc_size |
| 53 |
and mallocs alloc_size bytes. |
| 54 |
and esb_used_bytes = 0 and esb_string[0] = NUL. |
| 55 |
*/ |
| 56 |
|
| 57 |
struct esb_s { |
| 58 |
char * esb_string; /* pointer to the data itself, or NULL. */ |
| 59 |
size_t esb_allocated_size; /* Size of allocated data or 0 |
| 60 |
The allocated size must include the trailing NUL |
| 61 |
as we do insert a NUL. */ |
| 62 |
size_t esb_used_bytes; /* Amount of space used or 0, |
| 63 |
which does not include the trailing NUL. |
| 64 |
Matches what strlen(esb_string) would return. */ |
| 65 |
/* rigid means never do malloc. |
| 66 |
fixed means the size is a user buffer but |
| 67 |
if we run out of room feel free to malloc space |
| 68 |
(and then unset esb_fixed). |
| 69 |
An esb can be (fixed and rigid), (fixed and not rigid), |
| 70 |
or (not fixed and not rigid). */ |
| 71 |
char esb_fixed; |
| 72 |
char esb_rigid; |
| 73 |
}; |
| 74 |
|
| 75 |
/* Mirroring the broken code in libdwarf.h.in */ |
| 76 |
typedef long long esb_int; |
| 77 |
typedef unsigned long long esb_unsigned; |
| 78 |
|
| 79 |
/* Open/close the null device used during formatting printing */ |
| 80 |
FILE *esb_open_null_device(void); |
| 81 |
void esb_close_null_device(void); |
| 82 |
|
| 83 |
/* string length taken from string itself. */ |
| 84 |
void esb_append(struct esb_s *data, const char * in_string); |
| 85 |
|
| 86 |
/* The 'len' is believed. Do not pass in strings < len bytes long. */ |
| 87 |
void esb_appendn(struct esb_s *data, |
| 88 |
const char * in_string, size_t len); |
| 89 |
|
| 90 |
/* Always returns an empty string or a non-empty string. Never 0. */ |
| 91 |
char * esb_get_string(struct esb_s *data); |
| 92 |
|
| 93 |
/* Sets esb_used_bytes to zero. The string is not freed and |
| 94 |
esb_allocated_size is unchanged. */ |
| 95 |
void esb_empty_string(struct esb_s *data); |
| 96 |
|
| 97 |
/* Return esb_used_bytes. */ |
| 98 |
size_t esb_string_len(struct esb_s *data); |
| 99 |
|
| 100 |
/* The following are for testing esb, not use by dwarfdump. */ |
| 101 |
|
| 102 |
/* *data is presumed to contain garbage, not values, and |
| 103 |
is properly initialized. */ |
| 104 |
void esb_constructor(struct esb_s *data); |
| 105 |
void esb_constructor_rigid(struct esb_s *data, |
| 106 |
char *buf,size_t buflen); |
| 107 |
void esb_constructor_fixed(struct esb_s *data, |
| 108 |
char *buf,size_t buflen); |
| 109 |
|
| 110 |
void esb_force_allocation(struct esb_s *data, size_t minlen); |
| 111 |
|
| 112 |
/* The string is freed, contents of *data set to zeroes. */ |
| 113 |
void esb_destructor(struct esb_s *data); |
| 114 |
|
| 115 |
/* To get all paths in the code tested, this sets the |
| 116 |
allocation/reallocation to the given value, |
| 117 |
which can be quite small |
| 118 |
but must not be zero. */ |
| 119 |
void esb_alloc_size(size_t size); |
| 120 |
size_t esb_get_allocated_size(struct esb_s *data); |
| 121 |
|
| 122 |
/* Append a formatted string */ |
| 123 |
void esb_append_printf_s(struct esb_s *data, |
| 124 |
const char *format,const char *s); |
| 125 |
void esb_append_printf_i(struct esb_s *data, |
| 126 |
const char *format,esb_int); |
| 127 |
void esb_append_printf_u(struct esb_s *data, |
| 128 |
const char *format,esb_unsigned); |
| 129 |
|
| 130 |
/* Get a copy of the internal data buffer */ |
| 131 |
char * esb_get_copy(struct esb_s *data); |
| 132 |
|
| 133 |
#ifdef __cplusplus |
| 134 |
} |
| 135 |
#endif /* __cplusplus */ |
| 136 |
#endif /* ESB_H */ |