File src/utils/str.c changed (mode: 100644) (index 41e5d489a..f05c5c0bf) |
18 |
18 |
*/ |
*/ |
19 |
19 |
|
|
20 |
20 |
#include <ctype.h> /* tolower() isspace() */ |
#include <ctype.h> /* tolower() isspace() */ |
21 |
|
#include <stdarg.h> /* va_list va_start() va_end() */ |
|
|
21 |
|
#include <stdarg.h> /* va_list va_start() va_copy() va_end() */ |
22 |
22 |
#include <stdio.h> /* snprintf() */ |
#include <stdio.h> /* snprintf() */ |
23 |
|
#include <stdlib.h> /* mbstowcs() wcstombs() */ |
|
|
23 |
|
#include <stdlib.h> /* malloc() mbstowcs() wcstombs() */ |
24 |
24 |
#include <string.h> /* strncmp() strlen() strcmp() strchr() strrchr() */ |
#include <string.h> /* strncmp() strlen() strcmp() strchr() strrchr() */ |
25 |
25 |
#include <wchar.h> /* vswprintf() wchar_t */ |
#include <wchar.h> /* vswprintf() wchar_t */ |
26 |
26 |
|
|
|
... |
... |
skip_all(const char string[], char ch) |
289 |
289 |
return (char *)string; |
return (char *)string; |
290 |
290 |
} |
} |
291 |
291 |
|
|
|
292 |
|
char * |
|
293 |
|
format_str(const char format[], ...) |
|
294 |
|
{ |
|
295 |
|
va_list ap; |
|
296 |
|
va_list aq; |
|
297 |
|
size_t len; |
|
298 |
|
char *result_buf; |
|
299 |
|
|
|
300 |
|
va_start(ap, format); |
|
301 |
|
va_copy(aq, ap); |
|
302 |
|
|
|
303 |
|
len = vsnprintf(NULL, 0, format, ap); |
|
304 |
|
va_end(ap); |
|
305 |
|
|
|
306 |
|
if((result_buf = malloc(len + 1)) != NULL) |
|
307 |
|
{ |
|
308 |
|
(void)vsprintf(result_buf, format, aq); |
|
309 |
|
} |
|
310 |
|
va_end(aq); |
|
311 |
|
|
|
312 |
|
return result_buf; |
|
313 |
|
} |
|
314 |
|
|
292 |
315 |
#ifdef _WIN32 |
#ifdef _WIN32 |
293 |
316 |
char * |
char * |
294 |
317 |
strtok_r(char str[], const char delim[], char *saveptr[]) |
strtok_r(char str[], const char delim[], char *saveptr[]) |
File src/utils/str.h changed (mode: 100644) (index f55283950..97ac8a399) |
... |
... |
char * escape_chars(const char string[], const char chars[]); |
76 |
76 |
int is_null_or_empty(const char string[]); |
int is_null_or_empty(const char string[]); |
77 |
77 |
/* Returns pointer to first character in the string not equal to the ch. */ |
/* Returns pointer to first character in the string not equal to the ch. */ |
78 |
78 |
char * skip_all(const char string[], char ch); |
char * skip_all(const char string[], char ch); |
|
79 |
|
/* Formats string like printf, but instead of printing it, allocates memory and |
|
80 |
|
* and prints it there. Returns newly allocated string, which should be freed |
|
81 |
|
* by the caller, or NULL if there is not enough memory. */ |
|
82 |
|
char * format_str(const char format[], ...); |
79 |
83 |
#ifdef _WIN32 |
#ifdef _WIN32 |
80 |
84 |
char * strtok_r(char str[], const char delim[], char *saveptr[]); |
char * strtok_r(char str[], const char delim[], char *saveptr[]); |
81 |
85 |
#endif |
#endif |
File tests/misc/format_str.c added (mode: 100644) (index 000000000..b2b3ac602) |
|
1 |
|
#include "seatest.h" |
|
2 |
|
|
|
3 |
|
#include <stdlib.h> /* free() */ |
|
4 |
|
|
|
5 |
|
#include "../../src/utils/str.h" |
|
6 |
|
|
|
7 |
|
static void |
|
8 |
|
test_no_formatting_ok(void) |
|
9 |
|
{ |
|
10 |
|
#define STR "No formatting" |
|
11 |
|
char *str = format_str(STR); |
|
12 |
|
assert_string_equal(STR, str); |
|
13 |
|
free(str); |
|
14 |
|
#undef STR |
|
15 |
|
} |
|
16 |
|
|
|
17 |
|
static void |
|
18 |
|
test_formatting_ok(void) |
|
19 |
|
{ |
|
20 |
|
char *str = format_str("String %swith%s formatting", "'", "'"); |
|
21 |
|
assert_string_equal("String 'with' formatting", str); |
|
22 |
|
free(str); |
|
23 |
|
} |
|
24 |
|
|
|
25 |
|
void |
|
26 |
|
format_str_tests(void) |
|
27 |
|
{ |
|
28 |
|
test_fixture_start(); |
|
29 |
|
|
|
30 |
|
run_test(test_no_formatting_ok); |
|
31 |
|
run_test(test_formatting_ok); |
|
32 |
|
|
|
33 |
|
test_fixture_end(); |
|
34 |
|
} |
|
35 |
|
|
|
36 |
|
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ |
|
37 |
|
/* vim: set cinoptions+=t0 : */ |
File tests/misc/test.c changed (mode: 100644) (index a6226fca0..b2a346445) |
... |
... |
void get_command_name_tests(void); |
27 |
27 |
void format_mount_command_tests(void); |
void format_mount_command_tests(void); |
28 |
28 |
void trim_right_tests(void); |
void trim_right_tests(void); |
29 |
29 |
void echo_tests(void); |
void echo_tests(void); |
|
30 |
|
void format_str_tests(void); |
30 |
31 |
|
|
31 |
32 |
void |
void |
32 |
33 |
all_tests(void) |
all_tests(void) |
|
... |
... |
all_tests(void) |
58 |
59 |
format_mount_command_tests(); |
format_mount_command_tests(); |
59 |
60 |
trim_right_tests(); |
trim_right_tests(); |
60 |
61 |
echo_tests(); |
echo_tests(); |
|
62 |
|
format_str_tests(); |
61 |
63 |
} |
} |
62 |
64 |
|
|
63 |
65 |
int |
int |