| 1 | /*** sbuffer.c **************************************************************** |
| 2 | ** |
| 3 | ** This file is part of BibTool. |
| 4 | ** It is distributed under the GNU General Public License. |
| 5 | ** See the file COPYING for details. |
| 6 | ** |
| 7 | ** (c) 1996-2020 Gerd Neugebauer |
| 8 | ** |
| 9 | ** Net: [email protected] |
| 10 | ** |
| 11 | ** This program is free software; you can redistribute it and/or modify |
| 12 | ** it under the terms of the GNU General Public License as published by |
| 13 | ** the Free Software Foundation; either version 2, or (at your option) |
| 14 | ** any later version. |
| 15 | ** |
| 16 | ** This program is distributed in the hope that it will be useful, |
| 17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ** GNU General Public License for more details. |
| 20 | ** |
| 21 | ** You should have received a copy of the GNU General Public License |
| 22 | ** along with this program; if not, write to the Free Software |
| 23 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | ** |
| 25 | **----------------------------------------------------------------------------- |
| 26 | ** Description: |
| 27 | ** This module contains functions for dealing with strings of |
| 28 | ** aribtrary size. The allocation of memory is done automatically |
| 29 | ** when more characters are added. |
| 30 | ** |
| 31 | ** The functions are modeled after the stream functions of C. |
| 32 | ** Currently a |printf|-like function is missing because one was |
| 33 | ** not needed yet and it is not so easy to implement---portably. |
| 34 | ** |
| 35 | ** The functions in this module are very handy to deal with |
| 36 | ** strings of arbitrary length where the length is not known in |
| 37 | ** advance. E.g. consider the case that a line has to be read |
| 38 | ** from a file |file| and the line length should not be restricted by |
| 39 | ** some artificial boundry. This can be implemented as follows: |
| 40 | ** \\ |
| 41 | ** |{ StringBuffer *sb = sb_open();|\Ccomment{Declare and |
| 42 | ** initialize a string buffer.}\\ |
| 43 | ** | int c;|\Ccomment{Variable to store a single character.}\\ |
| 44 | ** | char *s;|\Ccomment{Variable to hold the string at the end.}\\ |
| 45 | ** | while ( (c=fgetc(file) != EOF|\\ |
| 46 | ** | && c != '\n')|\\ |
| 47 | ** | { sbputchar(c,sb); }|\Ccomment{Store each character in the |
| 48 | ** string buffer.}\\ |
| 49 | ** | s = sbflush(sb);|\Ccomment{Get the string from the |
| 50 | ** string buffer.}\\ |
| 51 | ** | puts(s);|\Ccomment{Process the string; e.g. print it.}\\ |
| 52 | ** | sb_close(sb);|\Ccomment{Free the string buffer.}\\ |
| 53 | ** |}| |
| 54 | ** |
| 55 | ** Note that the flushing of the string buffer returns a C string |
| 56 | ** which is managed by the string buffer. This memory is freed |
| 57 | ** or reused whenever the string buffer needs to. Thus you should |
| 58 | ** make a private copy of this string if it should survive the |
| 59 | ** next operation of the string buffer. Especially, after the |
| 60 | ** call to |sb_close()| this memory has been returned to the |
| 61 | ** operating system and is not available any more. |
| 62 | ** |
| 63 | ******************************************************************************/ |
| 64 | |
| 65 | #include <bibtool/general.h> |
| 66 | #include <bibtool/sbuffer.h> |
| 67 | |
| 68 | /*****************************************************************************/ |
| 69 | /* Internal Programs */ |
| 70 | /*===========================================================================*/ |
| 71 | |
| 72 | |
| 73 | /*----------------------------------------------------------------------------- |
| 74 | ** Function: sbopen() |
| 75 | ** Purpose: Allocate a new string buffer. |
| 76 | ** Return a pointer to the new string buffer or |NULL| if |
| 77 | ** none was available. |
| 78 | ** Arguments: none. |
| 79 | ** Returns: pointer to new string buffer or NULL |
| 80 | **___________________________________________________ */ |
| 81 | StringBuffer* sbopen() /* */ |
| 82 | { register StringBuffer* sb; /* */ |
| 83 | /* */ |
| 84 | if ( (sb=(StringBuffer*)malloc(sizeof(StringBuffer))) == NULL )/* */ |
| 85 | { return NULL; } /* */ |
| 86 | sb->sb__string = NULL; /* */ |
| 87 | sb->sb__ptr = sb->sb__size = 0; /* */ |
| 88 | return sb; /* */ |
| 89 | } /*------------------------*/ |
| 90 | |
| 91 | /*----------------------------------------------------------------------------- |
| 92 | ** Function: sbclose() |
| 93 | ** Purpose: Free an old string buffer. |
| 94 | ** Arguments: |
| 95 | ** sb Pointer to string buffer which should be closed |
| 96 | ** Returns: Return |false| upon failure. |
| 97 | **___________________________________________________ */ |
| 98 | bool sbclose(sb) /* */ |
| 99 | StringBuffer* sb; /* */ |
| 100 | { /* */ |
| 101 | if (sb == NULL) return true; /* */ |
| 102 | if (sb->sb__string != NULL) free(sb->sb__string);/* */ |
| 103 | free((char*)sb); /* */ |
| 104 | return false; /* */ |
| 105 | } /*------------------------*/ |
| 106 | |
| 107 | /*----------------------------------------------------------------------------- |
| 108 | ** Function: sbputs() |
| 109 | ** Purpose: Push a whole string onto a string buffer. |
| 110 | ** Arguments: |
| 111 | ** s String to be pushed. |
| 112 | ** sb Destination string buffer. |
| 113 | ** Returns: |false| if something went wrong. |
| 114 | **___________________________________________________ */ |
| 115 | bool sbputs(s,sb) /* */ |
| 116 | char *s; /* */ |
| 117 | StringBuffer* sb; /* */ |
| 118 | { /* */ |
| 119 | if (sb == NULL) return false; /* */ |
| 120 | /* */ |
| 121 | while ( *s ) /* */ |
| 122 | { (void)sbputchar(*s,sb); ++s; /* */ |
| 123 | } /* */ |
| 124 | return true; /* */ |
| 125 | } /*------------------------*/ |
| 126 | |
| 127 | /*----------------------------------------------------------------------------- |
| 128 | ** Function: sbputc() |
| 129 | ** Purpose: Push a single character onto a string buffer. In |
| 130 | ** contrast to the macro this function handles the |
| 131 | ** reallocation of the memory. For the user it should not |
| 132 | ** make a difference since the macros uses this function |
| 133 | ** when needed. |
| 134 | ** |
| 135 | ** When no memory is left then the character is discarded |
| 136 | ** and this action is signaled via the return value. |
| 137 | ** Arguments: |
| 138 | ** c Character to put to the string buffer. |
| 139 | ** sb Destination string buffer. |
| 140 | ** Returns: |false| if no memory is left. |
| 141 | **___________________________________________________ */ |
| 142 | bool sbputc(c,sb) /* */ |
| 143 | register int c; /* */ |
| 144 | register StringBuffer* sb; /* */ |
| 145 | { register char *cp; /* */ |
| 146 | /* */ |
| 147 | if ( sb->sb__ptr >= sb->sb__size ) /* */ |
| 148 | { sb->sb__size += StringBufferIncrement; /* */ |
| 149 | if ( (cp=( sb->sb__ptr == 0 /* */ |
| 150 | ? malloc(sb->sb__size) /* */ |
| 151 | : realloc(sb->sb__string,sb->sb__size))/* */ |
| 152 | ) == NULL ) /* */ |
| 153 | { sb->sb__size -= StringBufferIncrement; /* */ |
| 154 | return false; /* */ |
| 155 | } /* */ |
| 156 | sb->sb__string = cp; /* */ |
| 157 | } /* */ |
| 158 | /* */ |
| 159 | sb->sb__string[sb->sb__ptr++] = c; /* */ |
| 160 | return true; /* */ |
| 161 | } /*------------------------*/ |
| 162 | |
| 163 | /*----------------------------------------------------------------------------- |
| 164 | ** Function: sbflush() |
| 165 | ** Purpose: Close a string buffer with a trailing |\0| and reset the |
| 166 | ** current pointer to the beginning. |
| 167 | ** The next write operation starts right at the end. Thus |
| 168 | ** additional write operations will overwrite the |
| 169 | ** terminating byte. |
| 170 | ** Arguments: |
| 171 | ** sb String buffer to close. |
| 172 | ** Returns: The string contained in the string buffer as a proper |
| 173 | ** C string. |
| 174 | **___________________________________________________ */ |
| 175 | char* sbflush(sb) /* */ |
| 176 | StringBuffer* sb; /* */ |
| 177 | { /* */ |
| 178 | (void)sbputchar('\0',sb); /* */ |
| 179 | sb->sb__ptr--; /* */ |
| 180 | return sb->sb__string; /* */ |
| 181 | } /*------------------------*/ |
| 182 | |
| 183 | /*----------------------------------------------------------------------------- |
| 184 | ** Function: sbrewind() |
| 185 | ** Purpose: Reset the string buffer pointer to the beginning. |
| 186 | ** The next write or read will operate there. |
| 187 | ** Arguments: |
| 188 | ** sb String buffer to consider. |
| 189 | ** Returns: nothing |
| 190 | **___________________________________________________ */ |
| 191 | void sbrewind(sb) /* */ |
| 192 | StringBuffer* sb; /* */ |
| 193 | { sb->sb__ptr = 0; /* */ |
| 194 | } /*------------------------*/ |
| 195 | |
| 196 | /*----------------------------------------------------------------------------- |
| 197 | ** Function: sbtell() |
| 198 | ** Purpose: Return the current pointer to the string buffer position. |
| 199 | ** This can be used with |sbseek()| to reset it. |
| 200 | ** Arguments: |
| 201 | ** sb String buffer to consider. |
| 202 | ** Returns: The relative byte position of the current writing |
| 203 | ** position. This is an integer offset from the beginning |
| 204 | ** of the string buffer. |
| 205 | **___________________________________________________ */ |
| 206 | int sbtell(sb) /* */ |
| 207 | StringBuffer* sb; /* */ |
| 208 | { return sb->sb__ptr; /* */ |
| 209 | } /*------------------------*/ |
| 210 | |
| 211 | /*----------------------------------------------------------------------------- |
| 212 | ** Function: sbseek() |
| 213 | ** Purpose: Reset the current pointer to the position given. If |
| 214 | ** the position is outside the valid region then |true| |
| 215 | ** is returned and the position is left unchanged. |
| 216 | ** Arguments: |
| 217 | ** sb String buffer to reposition. |
| 218 | ** pos New position of the string buffer. |
| 219 | ** Returns: |false| if everything went right. |
| 220 | **___________________________________________________ */ |
| 221 | bool sbseek(sb, pos) /* */ |
| 222 | StringBuffer* sb; /* */ |
| 223 | int pos; /* */ |
| 224 | { /* */ |
| 225 | if ( pos < 0 || pos > sb->sb__ptr ) return true; /* */ |
| 226 | sb->sb__ptr = pos; /* */ |
| 227 | return false; /* */ |
| 228 | } /*------------------------*/ |