Add helper function concat_strings_alloc() to tools module

This commit is contained in:
Thomas Jensen 2021-02-22 20:59:42 +01:00
parent 877336a114
commit 033c879ae6
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
3 changed files with 53 additions and 19 deletions

View File

@ -157,22 +157,11 @@ static int can_read_dir(const char *dirname)
static char *combine(const char *dirname, const char *filename) static char *combine(const char *dirname, const char *filename)
{ {
const size_t dirname_len = strlen(dirname); const size_t dirname_len = strlen(dirname);
const size_t filename_len = strlen(filename);
char *result = (char *) malloc(dirname_len + filename_len + 2); if (dirname[dirname_len - 1] == '/') {
char *p = result; return concat_strings_alloc(2, dirname, filename);
strcpy(result, dirname); // TODO leverage concat_strings
p += dirname_len;
if (dirname[dirname_len - 1] != '/') {
result[dirname_len] = '/';
p++;
} }
strcpy(p, filename); return concat_strings_alloc(3, dirname, "/", filename);
p += filename_len;
*p = '\0';
return result;
} }
@ -223,10 +212,7 @@ static char *from_env_var(const char *env_var, const char *postfix)
fprintf(stderr, "%s: from_env_var(): getenv(\"%s\") --> %s\n", PROJECT, env_var, result); fprintf(stderr, "%s: from_env_var(): getenv(\"%s\") --> %s\n", PROJECT, env_var, result);
#endif #endif
if (result != NULL) { if (result != NULL) {
size_t result_len = strlen(result) + strlen(postfix) + 1; result = concat_strings_alloc(2, result, postfix);
char *combined = (char *) malloc(result_len);
concat_strings(combined, result_len, 2, result, postfix);
result = combined;
} }
return result; return result;
} }
@ -374,7 +360,7 @@ static int open_yy_config_file(const char *config_file_name)
fprintf(stderr, "%s: Couldn't open config file '%s' for input\n", PROJECT, config_file_name); fprintf(stderr, "%s: Couldn't open config file '%s' for input\n", PROJECT, config_file_name);
return 1; return 1;
} }
yyfilename = config_file_name; yyfilename = (char *) config_file_name;
yyin = new_yyin; yyin = new_yyin;
return 0; return 0;
} }

View File

@ -213,6 +213,44 @@ void concat_strings(char *dst, int max_len, int count, ...)
char *concat_strings_alloc(size_t count, ...)
{
if (count < 1) {
return strdup("");
}
size_t total_len = 0;
const char *src;
va_list va;
va_start (va, count);
for (size_t i = 0; i < count; i++) {
src = va_arg (va, const char *);
if (src != NULL) {
total_len += strlen(src);
}
}
va_end (va);
char *result = malloc(total_len + 1);
char *p = result;
va_start (va, count);
for (size_t i = 0; i < count; i++) {
src = va_arg (va, const char *);
if (src != NULL && src[0] != '\0') {
strcpy(p, src);
p += strlen(src);
}
}
va_end (va);
*p = '\0';
return result;
}
int empty_line(const line_t *line) int empty_line(const line_t *line)
/* /*
* Return true if line is empty. * Return true if line is empty.

View File

@ -30,6 +30,7 @@
#include "boxes.h" #include "boxes.h"
#define BMAX(a, b) ({ /* return the larger value */ \ #define BMAX(a, b) ({ /* return the larger value */ \
__typeof__ (a) _a = (a); \ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \ __typeof__ (b) _b = (b); \
@ -64,6 +65,15 @@ int strisyes(const char *s);
int strisno(const char *s); int strisno(const char *s);
/**
* Concatenate variable number of strings into one. This would normally be achieved via snprintf(), but that's not
* available on all platforms where boxes is compiled.
* @param <count> number of strings given in the following
* @param <...> the strings
* @returns a new string, for which new memory was allocated
*/
char *concat_strings_alloc(size_t count, ...);
void concat_strings(char *dst, int max_len, int count, ...); void concat_strings(char *dst, int max_len, int count, ...);
char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len); char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len);