22
22
#include <string.h>
23
23
24
24
#include <sys/stat.h>
25
+ #if defined(_WIN32 )
26
+ #include <windows.h>
27
+ #endif
25
28
26
29
static const char * progname ;
27
30
static void usage () {
@@ -34,6 +37,27 @@ static const char* basename(const char* path) {
34
37
return result ? result : path ;
35
38
}
36
39
40
+ #if defined(_WIN32 )
41
+ static wchar_t * convertToMultiByte (const char * s ) {
42
+ int numChars = MultiByteToWideChar (
43
+ /*CodePage=*/ CP_UTF8 ,
44
+ /*dwFlags=*/ 0 ,
45
+ /*lpMultibyteStr=*/ s ,
46
+ /*cbMultiByte=*/ -1 ,
47
+ /*lpWideCharStr=*/ NULL ,
48
+ /*ccWideChar=*/ 0 );
49
+ wchar_t * mbs = malloc (numChars * sizeof (wchar_t ));
50
+ MultiByteToWideChar (
51
+ /*CodePage=*/ CP_UTF8 ,
52
+ /*dwFlags=*/ 0 ,
53
+ /*lpMultibyteStr=*/ s ,
54
+ /*cbMultiByte=*/ -1 ,
55
+ /*lpWideCharStr=*/ mbs ,
56
+ /*ccWideChar=*/ numChars );
57
+ return mbs ;
58
+ }
59
+ #endif
60
+
37
61
// "Fancy" Command Implementation
38
62
39
63
static bool
@@ -61,18 +85,30 @@ fancy_tool_create_command(void *context, const llb_data_t* name) {
61
85
62
86
static bool fs_get_file_contents (void * context , const char * path ,
63
87
llb_data_t * data_out ) {
88
+
89
+ #if defined(_WIN32 )
90
+ wchar_t * wPath = convertToMultiByte (path );
91
+ wprintf (L" -- read file contents: %ls\n" , wPath );
92
+ fflush (stdout );
93
+ FILE * fp ;
94
+ if (_wfopen_s (& fp , wPath , L"rb" )) {
95
+ free (wPath );
96
+ return false;
97
+ }
98
+ free (wPath );
99
+ #else
64
100
printf (" -- read file contents: %s\n" , path );
65
101
fflush (stdout );
66
-
67
102
FILE * fp = fopen (path , "rb" );
68
103
if (!fp ) {
69
104
return false;
70
105
}
71
-
106
+ #endif
107
+
72
108
fseek (fp , 0 , SEEK_END );
73
109
long size = ftell (fp );
74
110
fseek (fp , 0 , SEEK_SET );
75
- uint8_t * buffer = malloc (size );
111
+ uint8_t * buffer = ( uint8_t * ) llb_alloc (size );
76
112
if (!buffer ) {
77
113
return false;
78
114
}
@@ -107,7 +143,7 @@ static void fs_get_file_info(void* context, const char* path,
107
143
static llb_buildsystem_tool_t * lookup_tool (void * context ,
108
144
const llb_data_t * name ) {
109
145
if (name -> length == 5 && memcmp (name -> data , "fancy" , 5 ) == 0 ) {
110
- llb_buildsystem_tool_delegate_t delegate = {};
146
+ llb_buildsystem_tool_delegate_t delegate = {0 };
111
147
delegate .create_command = fancy_tool_create_command ;
112
148
return llb_buildsystem_tool_create (name , delegate );
113
149
}
@@ -136,7 +172,7 @@ static void command_started(void* context,
136
172
llb_buildsystem_command_get_name (command , & name );
137
173
printf ("%s: %.*s -- %s\n" , __FUNCTION__ , (int )name .length , name .data ,
138
174
description );
139
- free (description );
175
+ llb_free (description );
140
176
fflush (stdout );
141
177
}
142
178
@@ -192,12 +228,12 @@ int main(int argc, char **argv) {
192
228
const char * buildFilePath = argv [1 ];
193
229
194
230
// Create an invocation.
195
- llb_buildsystem_invocation_t invocation = {};
231
+ llb_buildsystem_invocation_t invocation = {0 };
196
232
invocation .buildFilePath = buildFilePath ;
197
233
invocation .useSerialBuild = true;
198
234
199
235
// Create a build system delegate.
200
- llb_buildsystem_delegate_t delegate = {};
236
+ llb_buildsystem_delegate_t delegate = {0 };
201
237
delegate .context = NULL ;
202
238
delegate .fs_get_file_contents = fs_get_file_contents ;
203
239
delegate .fs_get_file_info = fs_get_file_info ;
0 commit comments