B
bwaichu
I am writing a function that:
1) checks the size of the a non c string being copied against the
available
buffer space; len should not be large enough to contain a nul
terminator (should I check for
this?)
2) if the space is too small, the function increases the buffer size by
a block and repeats as
necessary; there should be more effective ways to do this. I
started to experiment with this
in an earlier post here.
3) then memcpy's the string to the end of the written part of the
buffer
4) returns the current used amount (might want to return something
different; still undecided)
The buffer structure was built based on the advice I received here to
manage buffer information. What I want to know is if there is a better
way to approach this problem?
Ideally, I would also want a function that would tag a nul to the end
of the string to make it
a c string if I need that functionality. My goal is general use buffer
functions for network applications. If you see any c mistakes, please
tell me.
size_t
buffer_append(BUF *buffer, const char *string, size_t len) {
char *buf_temp = NULL;
allocate:
if (buffer_remaining(buffer) < len) {
buf_temp = realloc(buffer->buffer, buffer->size + 512);
if (buf_temp == NULL) {
free(buffer->buffer);
buffer->buffer = NULL;
errx(1, "realloc failed");
}
buffer->buffer = buf_temp;
buffer->size += 512;
if (buffer_remaining(buffer) < len)
goto allocate;
}
memcpy(buffer->buffer + buffer->end, string, len);
buffer->end += len;
return buffer->end;
}
size_t
buffer_remaining(BUF *buffer) {
return buffer->size - buffer->end;
}
And here's the BUF structure:
typedef struct {
char *buffer; /* buffer */
size_t size; /* allocated size of buffer */
size_t offset; /* offset of buffer -- used to walk the buffer*/
size_t end; /* end of buffer */
} BUF;
1) checks the size of the a non c string being copied against the
available
buffer space; len should not be large enough to contain a nul
terminator (should I check for
this?)
2) if the space is too small, the function increases the buffer size by
a block and repeats as
necessary; there should be more effective ways to do this. I
started to experiment with this
in an earlier post here.
3) then memcpy's the string to the end of the written part of the
buffer
4) returns the current used amount (might want to return something
different; still undecided)
The buffer structure was built based on the advice I received here to
manage buffer information. What I want to know is if there is a better
way to approach this problem?
Ideally, I would also want a function that would tag a nul to the end
of the string to make it
a c string if I need that functionality. My goal is general use buffer
functions for network applications. If you see any c mistakes, please
tell me.
size_t
buffer_append(BUF *buffer, const char *string, size_t len) {
char *buf_temp = NULL;
allocate:
if (buffer_remaining(buffer) < len) {
buf_temp = realloc(buffer->buffer, buffer->size + 512);
if (buf_temp == NULL) {
free(buffer->buffer);
buffer->buffer = NULL;
errx(1, "realloc failed");
}
buffer->buffer = buf_temp;
buffer->size += 512;
if (buffer_remaining(buffer) < len)
goto allocate;
}
memcpy(buffer->buffer + buffer->end, string, len);
buffer->end += len;
return buffer->end;
}
size_t
buffer_remaining(BUF *buffer) {
return buffer->size - buffer->end;
}
And here's the BUF structure:
typedef struct {
char *buffer; /* buffer */
size_t size; /* allocated size of buffer */
size_t offset; /* offset of buffer -- used to walk the buffer*/
size_t end; /* end of buffer */
} BUF;