M
michael.casey
The purpose of this post is to obtain the communities opinion of the
usefulness, efficiency, and most importantly the correctness of this
small piece of code. I thank everyone in advance for your time in
considering this post, and for your comments.
I wrote this function to simplify the task of combining strings using
mixed sources. I often see the use of sprintf() which results in
several lines of code, and more processing than really for the task.
The function is pretty straight forward, it accepts a variable list of
arguments beginning with the mandatory integer containing the total
number of arguments.
The function allocates a static buffer; I thought the use of malloc()
to be unnecessary, as the responsibility of freeing the memory would be
left to the client. This design choice also minimizes the amount of
required arguments. If the resultant buffer is needed beyond the second
call to the function, it can be copied to another buffer at the clients
digression.
If the strings specified for concatenation exceed the buffer available,
the overall string will simply be truncated. I decided on this
behaviour because I do not believe the output to be essential to a
programs operation, so this error can in most cases go silent, but
still be apparent to the programmer once the result is examined.
If the defined buffer amount is insufficient or is unnecessarily large
for a particular application, it is left to the client to redefine the
buffer amount; I feel that 512 bytes is sufficient for my use.
I find this function most useful when frequently forming messages for
use over a socket, which will not otherwise be reused.
Example use of the function:
char *tmp;
tmp = vast("Welcome to ", HOST, ", you are logged in as ", name, ".");
send(sockfd, tmp, strlen(tmp), 0);
The function is defined as follows:
/*------------------------------------------------------------------*\
|- vast(): Variable Argument String; concatenate arbitrary strings. -|
\*------------------------------------------------------------------*/
#define VAST_BUF 512
char *vast(int num, ...) {
static char buf[VAST_BUF];
int t = 0,
o = 0;
char *v;
va_list ap;
if(num > 1) {
va_start(ap, num);
/* Loop it while its hot. */
while(num--) {
v = va_arg(ap, char *);
/* Copy string to cumulative buffer until NUL is reached */
/* so long as the total does not exceed (MAX_BUF - 1). */
while((buf[o] = *v++) && !(t >= VAST_BUF - 1)) {
/* Condition is met, increment offset and total. */
t++, o++;
}
}
va_end(ap);
}
buf[o] = NUL;
return(buf);
}
usefulness, efficiency, and most importantly the correctness of this
small piece of code. I thank everyone in advance for your time in
considering this post, and for your comments.
I wrote this function to simplify the task of combining strings using
mixed sources. I often see the use of sprintf() which results in
several lines of code, and more processing than really for the task.
The function is pretty straight forward, it accepts a variable list of
arguments beginning with the mandatory integer containing the total
number of arguments.
The function allocates a static buffer; I thought the use of malloc()
to be unnecessary, as the responsibility of freeing the memory would be
left to the client. This design choice also minimizes the amount of
required arguments. If the resultant buffer is needed beyond the second
call to the function, it can be copied to another buffer at the clients
digression.
If the strings specified for concatenation exceed the buffer available,
the overall string will simply be truncated. I decided on this
behaviour because I do not believe the output to be essential to a
programs operation, so this error can in most cases go silent, but
still be apparent to the programmer once the result is examined.
If the defined buffer amount is insufficient or is unnecessarily large
for a particular application, it is left to the client to redefine the
buffer amount; I feel that 512 bytes is sufficient for my use.
I find this function most useful when frequently forming messages for
use over a socket, which will not otherwise be reused.
Example use of the function:
char *tmp;
tmp = vast("Welcome to ", HOST, ", you are logged in as ", name, ".");
send(sockfd, tmp, strlen(tmp), 0);
The function is defined as follows:
/*------------------------------------------------------------------*\
|- vast(): Variable Argument String; concatenate arbitrary strings. -|
\*------------------------------------------------------------------*/
#define VAST_BUF 512
char *vast(int num, ...) {
static char buf[VAST_BUF];
int t = 0,
o = 0;
char *v;
va_list ap;
if(num > 1) {
va_start(ap, num);
/* Loop it while its hot. */
while(num--) {
v = va_arg(ap, char *);
/* Copy string to cumulative buffer until NUL is reached */
/* so long as the total does not exceed (MAX_BUF - 1). */
while((buf[o] = *v++) && !(t >= VAST_BUF - 1)) {
/* Condition is met, increment offset and total. */
t++, o++;
}
}
va_end(ap);
}
buf[o] = NUL;
return(buf);
}