2222#include <string.h>
2323
2424#include <sys/stat.h>
25+ #if defined(_WIN32 )
26+ #include <windows.h>
27+ #endif
2528
2629static const char * progname ;
2730static void usage () {
@@ -34,6 +37,27 @@ static const char* basename(const char* path) {
3437 return result ? result : path ;
3538}
3639
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+
3761// "Fancy" Command Implementation
3862
3963static bool
@@ -61,18 +85,30 @@ fancy_tool_create_command(void *context, const llb_data_t* name) {
6185
6286static bool fs_get_file_contents (void * context , const char * path ,
6387 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
64100 printf (" -- read file contents: %s\n" , path );
65101 fflush (stdout );
66-
67102 FILE * fp = fopen (path , "rb" );
68103 if (!fp ) {
69104 return false;
70105 }
71-
106+ #endif
107+
72108 fseek (fp , 0 , SEEK_END );
73109 long size = ftell (fp );
74110 fseek (fp , 0 , SEEK_SET );
75- uint8_t * buffer = malloc (size );
111+ uint8_t * buffer = ( uint8_t * ) llb_alloc (size );
76112 if (!buffer ) {
77113 return false;
78114 }
@@ -107,7 +143,7 @@ static void fs_get_file_info(void* context, const char* path,
107143static llb_buildsystem_tool_t * lookup_tool (void * context ,
108144 const llb_data_t * name ) {
109145 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 };
111147 delegate .create_command = fancy_tool_create_command ;
112148 return llb_buildsystem_tool_create (name , delegate );
113149 }
@@ -136,7 +172,7 @@ static void command_started(void* context,
136172 llb_buildsystem_command_get_name (command , & name );
137173 printf ("%s: %.*s -- %s\n" , __FUNCTION__ , (int )name .length , name .data ,
138174 description );
139- free (description );
175+ llb_free (description );
140176 fflush (stdout );
141177}
142178
@@ -192,12 +228,12 @@ int main(int argc, char **argv) {
192228 const char * buildFilePath = argv [1 ];
193229
194230 // Create an invocation.
195- llb_buildsystem_invocation_t invocation = {};
231+ llb_buildsystem_invocation_t invocation = {0 };
196232 invocation .buildFilePath = buildFilePath ;
197233 invocation .useSerialBuild = true;
198234
199235 // Create a build system delegate.
200- llb_buildsystem_delegate_t delegate = {};
236+ llb_buildsystem_delegate_t delegate = {0 };
201237 delegate .context = NULL ;
202238 delegate .fs_get_file_contents = fs_get_file_contents ;
203239 delegate .fs_get_file_info = fs_get_file_info ;
0 commit comments