make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* $NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $ */
|
1995-06-14 19:18:37 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
|
2003-08-07 15:13:06 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Copyright (c) 1988, 1989 by Adam de Boor
|
|
|
|
* Copyright (c) 1989 by Berkeley Softworks
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-05-07 04:04:38 +04:00
|
|
|
#ifndef MAKE_NATIVE
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
static char rcsid[] = "$NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $";
|
1997-09-28 07:30:58 +04:00
|
|
|
#else
|
1997-07-02 01:17:00 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-06-14 19:18:37 +04:00
|
|
|
#if 0
|
1996-11-06 20:58:58 +03:00
|
|
|
static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
|
1995-06-14 19:18:37 +04:00
|
|
|
#else
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
__RCSID("$NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $");
|
1995-06-14 19:18:37 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
1997-09-28 07:30:58 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Functions for automatically-expanded NUL-terminated buffers. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2020-07-26 16:39:30 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include "make.h"
|
|
|
|
#include "buf.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifndef max
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BUF_DEF_SIZE 256 /* Default buffer size */
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Extend the buffer for adding a single byte. */
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
2009-01-17 16:29:37 +03:00
|
|
|
Buf_Expand_1(Buffer *bp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-01-17 16:29:37 +03:00
|
|
|
bp->size += max(bp->size, 16);
|
|
|
|
bp->buffer = bmake_realloc(bp->buffer, bp->size);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Add the given bytes to the buffer. */
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Buf_AddBytesZ(Buffer *bp, const Byte *bytesPtr, size_t numBytes)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
size_t count = bp->count;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2009-01-17 16:29:37 +03:00
|
|
|
if (__predict_false(count + numBytes >= bp->size)) {
|
|
|
|
bp->size += max(bp->size, numBytes + 16);
|
|
|
|
bp->buffer = bmake_realloc(bp->buffer, bp->size);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Byte *ptr = bp->buffer + count;
|
2009-01-17 16:29:37 +03:00
|
|
|
bp->count = count + numBytes;
|
|
|
|
memcpy(ptr, bytesPtr, numBytes);
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
ptr[numBytes] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Add the bytes between start and end to the buffer. */
|
2020-07-26 18:09:10 +03:00
|
|
|
void
|
|
|
|
Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
|
|
|
|
{
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Buf_AddBytesZ(bp, start, (size_t)(end - start));
|
2020-07-26 18:09:10 +03:00
|
|
|
}
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Add the given string to the buffer. */
|
2020-07-26 18:09:10 +03:00
|
|
|
void
|
|
|
|
Buf_AddStr(Buffer *bp, const char *str)
|
|
|
|
{
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Buf_AddBytesZ(bp, str, strlen(str));
|
2020-07-26 18:09:10 +03:00
|
|
|
}
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Add the given number to the buffer. */
|
2020-07-26 16:39:30 +03:00
|
|
|
void
|
|
|
|
Buf_AddInt(Buffer *bp, int n)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We need enough space for the decimal representation of an int.
|
|
|
|
* We calculate the space needed for the octal representation, and
|
|
|
|
* add enough slop to cope with a '-' sign and a trailing '\0'.
|
|
|
|
*/
|
|
|
|
size_t bits = sizeof(int) * CHAR_BIT;
|
|
|
|
char buf[1 + (bits + 2) / 3 + 1];
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
|
|
|
|
Buf_AddBytesZ(bp, buf, len);
|
2020-07-26 16:39:30 +03:00
|
|
|
}
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Get the data (usually a string) from the buffer.
|
|
|
|
* The returned data is valid until the next modifying operation
|
|
|
|
* on the buffer.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
* Returns the pointer to the data and optionally the length of the
|
|
|
|
* data in the buffer. */
|
1993-03-21 12:45:37 +03:00
|
|
|
Byte *
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Buf_GetAllZ(Buffer *bp, size_t *numBytesPtr)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-01-17 16:29:37 +03:00
|
|
|
if (numBytesPtr != NULL)
|
|
|
|
*numBytesPtr = bp->count;
|
2020-07-03 11:02:55 +03:00
|
|
|
return bp->buffer;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Mark the buffer as empty, so it can be filled with data again. */
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
2009-01-17 16:29:37 +03:00
|
|
|
Buf_Empty(Buffer *bp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-01-17 16:29:37 +03:00
|
|
|
bp->count = 0;
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
bp->buffer[0] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Initialize a buffer.
|
|
|
|
* If the given initial size is 0, a reasonable default is used. */
|
2009-01-17 16:29:37 +03:00
|
|
|
void
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Buf_InitZ(Buffer *bp, size_t size)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2008-02-16 00:29:50 +03:00
|
|
|
if (size <= 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
size = BUF_DEF_SIZE;
|
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
bp->size = size;
|
|
|
|
bp->count = 0;
|
2008-10-07 02:09:21 +04:00
|
|
|
bp->buffer = bmake_malloc(size);
|
2009-01-17 16:29:37 +03:00
|
|
|
*bp->buffer = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Reset the buffer.
|
|
|
|
* If freeData is TRUE, the data from the buffer is freed as well.
|
|
|
|
* Otherwise it is kept and returned. */
|
2009-01-17 16:29:37 +03:00
|
|
|
Byte *
|
|
|
|
Buf_Destroy(Buffer *buf, Boolean freeData)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Byte *data = buf->buffer;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (freeData) {
|
2009-01-17 16:29:37 +03:00
|
|
|
free(data);
|
|
|
|
data = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-01-17 16:29:37 +03:00
|
|
|
|
|
|
|
buf->size = 0;
|
|
|
|
buf->count = 0;
|
|
|
|
buf->buffer = NULL;
|
|
|
|
|
|
|
|
return data;
|
1996-11-06 20:58:58 +03:00
|
|
|
}
|
2012-04-25 00:26:58 +04:00
|
|
|
|
|
|
|
#ifndef BUF_COMPACT_LIMIT
|
|
|
|
# define BUF_COMPACT_LIMIT 128 /* worthwhile saving */
|
|
|
|
#endif
|
|
|
|
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
/* Reset the buffer and return its data.
|
|
|
|
*
|
|
|
|
* If the buffer size is much greater than its content,
|
|
|
|
* a new buffer will be allocated and the old one freed. */
|
2012-04-25 00:26:58 +04:00
|
|
|
Byte *
|
|
|
|
Buf_DestroyCompact(Buffer *buf)
|
|
|
|
{
|
|
|
|
#if BUF_COMPACT_LIMIT > 0
|
|
|
|
if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
|
|
|
|
/* We trust realloc to be smart */
|
make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.
The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.
All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len. These changes have been reviewed
thoroughly and manually, like all the others in this commit.
Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten. They will be renamed back in a follow-up commit.
As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.
The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-02 00:40:49 +03:00
|
|
|
Byte *data = bmake_realloc(buf->buffer, buf->count + 1);
|
|
|
|
if (data) { /* XXX: can never be NULL */
|
2012-04-25 00:26:58 +04:00
|
|
|
data[buf->count] = 0;
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return Buf_Destroy(buf, FALSE);
|
|
|
|
}
|