This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy patharraytoc.c
85 lines (78 loc) · 1.91 KB
/
arraytoc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "arraytoc.h"
#include <stdio.h>
/* dump uint32_t as hex digits */
void uint32_array_to_c_hex(const uint32_t* ary, size_t sz, const char* name)
{
size_t i = 0;
printf("static const uint32_t %s[%d] = {\n ", name, (int)sz);
for (;;) {
printf("0x%08x", ary[i]);
++i;
if (i == sz)
break;
if (i % 6 == 0) {
printf(",\n ");
} else {
printf(", ");
}
}
printf("\n};\n");
}
/**
* prints char array as a c program snippet
*/
void char_array_to_c(const char* ary, size_t sz, const char* name)
{
uint8_t tmp;
size_t i = 0;
printf("static const uint8_t %s[%d] = {\n ", name, (int)sz);
for (;;) {
if (ary[i] == 0) {
printf("'\\0'");
} else if (ary[i] == '\n') {
printf("'\\n'");
} else if (ary[i] == '\t') {
printf("'\\t'");
} else if (ary[i] == '\r') {
printf("'\\r'");
} else if (ary[i] == '\'') {
printf("'\\''");
} else if (ary[i] == '\\') {
printf("'\\\\'");
} else if (ary[i] < 32 || ary[i] > 126) {
tmp = (uint8_t)ary[i];
printf("0x%02x", tmp);
} else {
printf("'%c'", (char)ary[i]);
}
++i;
if (i == sz)
break;
if (i % 10 == 0) {
printf(",\n ");
} else {
printf(", ");
}
}
printf("\n};\n");
}
/**
* prints an uint array as a c program snippet
*/
void uint32_array_to_c(const uint32_t* ary, size_t sz, const char* name)
{
size_t i = 0;
printf("static const uint32_t %s[%d] = {\n ", name, (int)sz);
for (;;) {
printf("%u", ary[i]);
++i;
if (i == sz)
break;
if (i % 12 == 0) {
printf(",\n ");
} else {
printf(", ");
}
}
printf("\n};\n");
}