summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoão Antônio Cardoso <joao.maker@gmail.com>2020-04-18 00:10:02 -0300
committerFelipe Magno de Almeida <felipe@expertise.dev>2020-05-04 23:17:09 -0300
commit7b9e078cc7c9ff89915127a0a72dc5fc4ea53b46 (patch)
treef8a10ac6d6e7a63cdf056198c86b5e87f69bac1c
parentd40f4e87d9137e416a606a76336f3cbb3c56aeeb (diff)
windows: Add vasprintf as helper
-rw-r--r--src/lib/eina/eina_log.c3
-rw-r--r--src/lib/eina/eina_slstr.c3
-rw-r--r--src/lib/eina/eina_strbuf.c2
-rw-r--r--src/lib/eina/eina_stringshare.c1
-rw-r--r--src/lib/evil/evil_vasprintf.h32
-rw-r--r--src/tests/eina/eina_vasprintf_test.c59
-rwxr-xr-xsrc/tests/eina/eina_vasprintf_test.sh17
7 files changed, 115 insertions, 2 deletions
diff --git a/src/lib/eina/eina_log.c b/src/lib/eina/eina_log.c
index 2768b16e24..2697f4bebd 100644
--- a/src/lib/eina/eina_log.c
+++ b/src/lib/eina/eina_log.c
@@ -21,7 +21,6 @@
21# include "config.h" 21# include "config.h"
22#endif 22#endif
23 23
24#include <stdio.h>
25#include <string.h> 24#include <string.h>
26#include <stdlib.h> 25#include <stdlib.h>
27#include <unistd.h> 26#include <unistd.h>
@@ -58,6 +57,8 @@
58 57
59#include "eina_inline_private.h" 58#include "eina_inline_private.h"
60 59
60#include <evil_vasprintf.h>
61
61/* TODO 62/* TODO
62 * + printing logs to stdout or stderr can be implemented 63 * + printing logs to stdout or stderr can be implemented
63 * using a queue, useful for multiple threads printing 64 * using a queue, useful for multiple threads printing
diff --git a/src/lib/eina/eina_slstr.c b/src/lib/eina/eina_slstr.c
index 2ce638a642..8fd5db6fa6 100644
--- a/src/lib/eina/eina_slstr.c
+++ b/src/lib/eina/eina_slstr.c
@@ -4,13 +4,14 @@
4 4
5#define EINA_SLSTR_INTERNAL 5#define EINA_SLSTR_INTERNAL
6 6
7#include <stdio.h>
8#include <stdlib.h> 7#include <stdlib.h>
9#include <string.h> 8#include <string.h>
10 9
11#include "Eina.h" 10#include "Eina.h"
12#include "eina_private.h" 11#include "eina_private.h"
13 12
13#include <evil_vasprintf.h>
14
14// ========================================================================= // 15// ========================================================================= //
15 16
16static int _slstr_init = 0; 17static int _slstr_init = 0;
diff --git a/src/lib/eina/eina_strbuf.c b/src/lib/eina/eina_strbuf.c
index 9c76e2647e..0bb3b59207 100644
--- a/src/lib/eina/eina_strbuf.c
+++ b/src/lib/eina/eina_strbuf.c
@@ -11,6 +11,8 @@
11#include "eina_strbuf_common.h" 11#include "eina_strbuf_common.h"
12#include "eina_unicode.h" 12#include "eina_unicode.h"
13 13
14#include <evil_vasprintf.h>
15
14/*============================================================================* 16/*============================================================================*
15 * Local * 17 * Local *
16 *============================================================================*/ 18 *============================================================================*/
diff --git a/src/lib/eina/eina_stringshare.c b/src/lib/eina/eina_stringshare.c
index 912c5a1530..2e54a3f93b 100644
--- a/src/lib/eina/eina_stringshare.c
+++ b/src/lib/eina/eina_stringshare.c
@@ -41,6 +41,7 @@
41#include "eina_safety_checks.h" 41#include "eina_safety_checks.h"
42#include "eina_stringshare.h" 42#include "eina_stringshare.h"
43 43
44#include <evil_vasprintf.h>
44 45
45#ifdef CRI 46#ifdef CRI
46#undef CRI 47#undef CRI
diff --git a/src/lib/evil/evil_vasprintf.h b/src/lib/evil/evil_vasprintf.h
new file mode 100644
index 0000000000..989f4469c3
--- /dev/null
+++ b/src/lib/evil/evil_vasprintf.h
@@ -0,0 +1,32 @@
1
2#ifdef HAVE_CONFIG_H
3# include "config.h"
4#endif /* HAVE_CONFIG_H */
5
6#include <stdlib.h>
7#include <stdarg.h>
8#include <stdio.h>
9
10#ifdef _MSC_VER
11
12inline int
13vasprintf(char **strp, const char *fmt, va_list ap)
14{
15 int ret = -1;
16
17 int len = _vscprintf(fmt, ap);
18 if (len != -1)
19 {
20 char *str = (char *)malloc((len + 1) * sizeof(char));
21 if (str != NULL)
22 {
23 ret = vsnprintf(str, (unsigned long) len + 1, fmt, ap);
24 if (ret == len) *strp = str;
25 else free(strp);
26 }
27 }
28
29 return ret;
30}
31
32#endif
diff --git a/src/tests/eina/eina_vasprintf_test.c b/src/tests/eina/eina_vasprintf_test.c
new file mode 100644
index 0000000000..4f1b30cb1a
--- /dev/null
+++ b/src/tests/eina/eina_vasprintf_test.c
@@ -0,0 +1,59 @@
1#include <stdarg.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "eina_stdio.h"
6
7int
8eina_vasprintf_wrap(char **ret, const char *fmt, int nargs, ...)
9{
10 va_list ap;
11 va_start(ap, nargs);
12
13 int len = eina_vasprintf(ret, fmt, ap);
14
15 va_end(ap);
16 return len;
17}
18
19int
20simple_test(void)
21{
22 const char *expected_str =
23 "\tString: Hello World!\n"
24 "\tInteger: 46234\n"
25 "\tDouble: 9.629400E-03\n";
26
27 const char *fmt =
28 "\tString: %s\n"
29 "\tInteger: %d\n"
30 "\tDouble: %E\n";
31
32 char* str;
33 int len = eina_vasprintf_wrap(&str, fmt, 3, "Hello World!", 46234, 0.0096294);
34
35 printf("## Expecting:\n```%s```\n", expected_str);
36 printf("## Returning:\n```%s```\n", str);
37
38 printf("\tResult:");
39 int result = strcmp(str, expected_str);
40 if (!result) printf("........ok!\n");
41 else printf("........nok!\n");
42
43 free(str);
44 return result;
45}
46
47int
48main(void)
49{
50#ifdef GNU
51 printf("Test using vasprintf from GNU:\n");
52#else
53 printf("Test using implementd eina_vasprintf:\n");
54#endif
55
56 simple_test();
57
58 return 0;
59}
diff --git a/src/tests/eina/eina_vasprintf_test.sh b/src/tests/eina/eina_vasprintf_test.sh
new file mode 100755
index 0000000000..fb8360274c
--- /dev/null
+++ b/src/tests/eina/eina_vasprintf_test.sh
@@ -0,0 +1,17 @@
1#!/bin/sh
2
3# clean and update
4\rm -f vasprintf_test eina_stdio.[hc]
5\cp ../../lib/eina/eina_stdio.[hc] . &&
6
7# build
8clang-cl -o eina_vasprintf_test \
9 -Wno-include-next-absolute-path \
10 eina_vasprintf_test.c \
11 eina_stdio.c &&
12
13# test
14./eina_vasprintf_test
15
16# clean again
17\rm -f eina_vasprintf_test eina_stdio.[hc]