-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyfiledialogs.c
7486 lines (6827 loc) · 269 KB
/
tinyfiledialogs.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*_________
/ \ tinyfiledialogs.c v3.3.9 [Apr 14, 2019] zlib licence
|tiny file| Unique code file created [November 9, 2014]
| dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com
\____ ___/ http://tinyfiledialogs.sourceforge.net
\| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd
____________________________________________
| |
| email: tinyfiledialogs at ysengrin.com |
|____________________________________________|
___________________________________________________________________
| |
| the windows only wchar_t UTF-16 prototypes are in the header file |
|___________________________________________________________________|
Please upvote my stackoverflow answer https://stackoverflow.com/a/47651444
tiny file dialogs (cross-platform C C++)
InputBox PasswordBox MessageBox ColorPicker
OpenFileDialog SaveFileDialog SelectFolderDialog
Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more
SSH supported via automatic switch to console mode or X11 forwarding
one C file + a header (add them to your C or C++ project) with 8 functions:
- beep
- notify popup (tray)
- message & question
- input & password
- save file
- open file(s)
- select folder
- color picker
Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI
SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW
Open3D IMGUI MyGUI GLT NGL STB & GUI less programs
NO INIT
NO MAIN LOOP
NO LINKING
NO INCLUDE
The dialogs can be forced into console mode
Windows (XP to 10) ASCII MBCS UTF-8 UTF-16
- native code & vbs create the graphic dialogs
- enhanced console mode can use dialog.exe from
http://andrear.altervista.org/home/cdialog.php
- basic console input
Unix (command line calls) ASCII UTF-8
- applescript, kdialog, zenity
- python (2 or 3) + tkinter + python-dbus (optional)
- dialog (opens a console if needed)
- basic console input
The same executable can run across desktops & distributions
C89 & C++98 compliant: tested with C & C++ compilers
VisualStudio MinGW-gcc GCC Clang TinyCC OpenWatcom-v2 BorlandC SunCC ZapCC
on Windows Mac Linux Bsd Solaris Minix Raspbian
using Gnome Kde Enlightenment Mate Cinnamon Budgie Unity Lxde Lxqt Xfce
WindowMaker IceWm Cde Jds OpenBox Awesome Jwm Xdm
Bindings for LUA and C# dll, Haskell
Included in LWJGL(java), Rust, Allegrobasic
Thanks for contributions, bug corrections & thorough testing to:
- Don Heyse http://ldglite.sf.net for bug corrections & thorough testing!
- Paul Rouget
- License -
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef __sun
#define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include "tinyfiledialogs.h"
/* #define TINYFD_NOLIB */
#ifdef _WIN32
#ifdef __BORLANDC__
#define _getch getch
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#ifndef TINYFD_NOLIB
#include <windows.h>
/*#define TINYFD_NOSELECTFOLDERWIN*/
#ifndef TINYFD_NOSELECTFOLDERWIN
#include <shlobj.h>
#endif /*TINYFD_NOSELECTFOLDERWIN*/
#endif
#include <conio.h>
#include <commdlg.h>
#define TINYFD_NOCCSUNICODE
#define SLASH "\\"
int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */
#else
#include <limits.h>
#include <unistd.h>
#include <dirent.h> /* on old systems try <sys/dir.h> instead */
#include <termios.h>
#include <sys/utsname.h>
#include <signal.h> /* on old systems try <sys/signal.h> instead */
#define SLASH "/"
#endif /* _WIN32 */
#define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */
#define MAX_MULTIPLE_FILES 32
char const tinyfd_version [8] = "3.3.9";
int tinyfd_verbose = 0 ; /* on unix: prints the command line calls */
int tinyfd_silent = 1 ; /* 1 (default) or 0 : on unix,
hide errors and warnings from called dialog*/
#if defined(TINYFD_NOLIB) && defined(_WIN32)
int tinyfd_forceConsole = 1 ;
#else
int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */
#endif
/* for unix & windows: 0 (graphic mode) or 1 (console mode).
0: try to use a graphic solution, if it fails then it uses console mode.
1: forces all dialogs into console mode even when the X server is present,
if the package dialog (and a console is present) or dialog.exe is installed.
on windows it only make sense for console applications */
char tinyfd_response[1024];
/* if you pass "tinyfd_query" as aTitle,
the functions will not display the dialogs
but and return 0 for console mode, 1 for graphic mode.
tinyfd_response is then filled with the retain solution.
possible values for tinyfd_response are (all lowercase)
for graphic mode:
windows_wchar windows
applescript kdialog zenity zenity3 matedialog qarma
python2-tkinter python3-tkinter python-dbus perl-dbus
gxmessage gmessage xmessage xdialog gdialog
for console mode:
dialog whiptail basicinput no_solution */
#if defined(TINYFD_NOLIB) && defined(_WIN32)
static int gWarningDisplayed = 1 ;
#else
static int gWarningDisplayed = 0 ;
#endif
static char const gTitle[]="missing software! (we will try basic console input)";
#ifdef _WIN32
char const tinyfd_needs[] = "\
___________\n\
/ \\ \n\
| tiny file |\n\
| dialogs |\n\
\\_____ ____/\n\
\\|\
\ntiny file dialogs on Windows needs:\
\n a graphic display\
\nor dialog.exe (enhanced console mode)\
\nor a console for basic input";
#else
char const tinyfd_needs[] = "\
___________\n\
/ \\ \n\
| tiny file |\n\
| dialogs |\n\
\\_____ ____/\n\
\\|\
\ntiny file dialogs on UNIX needs:\
\n applescript\
\nor kdialog\
\nor zenity (or matedialog or qarma)\
\nor python (2 or 3)\
\n + tkinter + python-dbus (optional)\
\nor dialog (opens console if needed)\
\nor xterm + bash\
\n (opens console for basic input)\
\nor existing console for basic input";
#endif
#ifdef _MSC_VER
#pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
#pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
#pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
#endif
static char * getPathWithoutFinalSlash(
char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */
char const * const aSource) /* aoDestination and aSource can be the same */
{
char const * lTmp ;
if ( aSource )
{
lTmp = strrchr(aSource, '/');
if (!lTmp)
{
lTmp = strrchr(aSource, '\\');
}
if (lTmp)
{
strncpy(aoDestination, aSource, lTmp - aSource );
aoDestination[lTmp - aSource] = '\0';
}
else
{
* aoDestination = '\0';
}
}
else
{
* aoDestination = '\0';
}
return aoDestination;
}
static char * getLastName(
char * const aoDestination, /* make sure it is allocated */
char const * const aSource)
{
/* copy the last name after '/' or '\' */
char const * lTmp ;
if ( aSource )
{
lTmp = strrchr(aSource, '/');
if (!lTmp)
{
lTmp = strrchr(aSource, '\\');
}
if (lTmp)
{
strcpy(aoDestination, lTmp + 1);
}
else
{
strcpy(aoDestination, aSource);
}
}
else
{
* aoDestination = '\0';
}
return aoDestination;
}
static void ensureFinalSlash( char * const aioString )
{
if ( aioString && strlen( aioString ) )
{
char * lastcar = aioString + strlen( aioString ) - 1 ;
if ( strncmp( lastcar , SLASH , 1 ) )
{
strcat( lastcar , SLASH ) ;
}
}
}
static void Hex2RGB( char const aHexRGB [8] ,
unsigned char aoResultRGB [3] )
{
char lColorChannel [8] ;
if ( aoResultRGB )
{
if ( aHexRGB )
{
strcpy(lColorChannel, aHexRGB ) ;
aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16);
lColorChannel[5] = '\0';
aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16);
lColorChannel[3] = '\0';
aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16);
/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
}
else
{
aoResultRGB[0]=0;
aoResultRGB[1]=0;
aoResultRGB[2]=0;
}
}
}
static void RGB2Hex( unsigned char const aRGB [3] ,
char aoResultHexRGB [8] )
{
if ( aoResultHexRGB )
{
if ( aRGB )
{
#if defined(__GNUC__) && defined(_WIN32)
sprintf(aoResultHexRGB, "#%02hx%02hx%02hx",
#else
sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx",
#endif
aRGB[0], aRGB[1], aRGB[2]);
/* printf("aoResultHexRGB %s\n", aoResultHexRGB); */
}
else
{
aoResultHexRGB[0]=0;
aoResultHexRGB[1]=0;
aoResultHexRGB[2]=0;
}
}
}
static void replaceSubStr( char const * const aSource ,
char const * const aOldSubStr ,
char const * const aNewSubStr ,
char * const aoDestination )
{
char const * pOccurence ;
char const * p ;
char const * lNewSubStr = "" ;
size_t lOldSubLen = strlen( aOldSubStr ) ;
if ( ! aSource )
{
* aoDestination = '\0' ;
return ;
}
if ( ! aOldSubStr )
{
strcpy( aoDestination , aSource ) ;
return ;
}
if ( aNewSubStr )
{
lNewSubStr = aNewSubStr ;
}
p = aSource ;
* aoDestination = '\0' ;
while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL )
{
strncat( aoDestination , p , pOccurence - p ) ;
strcat( aoDestination , lNewSubStr ) ;
p = pOccurence + lOldSubLen ;
}
strcat( aoDestination , p ) ;
}
static int filenameValid( char const * const aFileNameWithoutPath )
{
if ( ! aFileNameWithoutPath
|| ! strlen(aFileNameWithoutPath)
|| strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") )
{
return 0 ;
}
return 1 ;
}
#ifndef _WIN32
static int fileExists( char const * const aFilePathAndName )
{
FILE * lIn ;
if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
{
return 0 ;
}
lIn = fopen( aFilePathAndName , "r" ) ;
if ( ! lIn )
{
return 0 ;
}
fclose( lIn ) ;
return 1 ;
}
#elif defined(TINYFD_NOLIB)
static int fileExists( char const * const aFilePathAndName )
{
FILE * lIn ;
if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
{
return 0 ;
}
if ( tinyfd_winUtf8 )
return 1; /* we cannot test */
lIn = fopen( aFilePathAndName , "r" ) ;
if ( ! lIn )
{
return 0 ;
}
fclose( lIn ) ;
return 1 ;
}
#endif
static void wipefile(char const * const aFilename)
{
int i;
struct stat st;
FILE * lIn;
if (stat(aFilename, &st) == 0)
{
if ((lIn = fopen(aFilename, "w")))
{
for (i = 0; i < st.st_size; i++)
{
fputc('A', lIn);
}
}
fclose(lIn);
}
}
#ifdef _WIN32
static int replaceChr( char * const aString ,
char const aOldChr ,
char const aNewChr )
{
char * p ;
int lRes = 0 ;
if ( ! aString )
{
return 0 ;
}
if ( aOldChr == aNewChr )
{
return 0 ;
}
p = aString ;
while ( (p = strchr( p , aOldChr )) )
{
* p = aNewChr ;
p ++ ;
lRes = 1 ;
}
return lRes ;
}
#ifdef TINYFD_NOLIB
static int dirExists(char const * const aDirPath)
{
struct stat lInfo;
if (!aDirPath || !strlen(aDirPath))
return 0;
if (stat(aDirPath, &lInfo) != 0)
return 0;
else if ( tinyfd_winUtf8 )
return 1; /* we cannot test */
else if (lInfo.st_mode & S_IFDIR)
return 1;
else
return 0;
}
void tinyfd_beep()
{
printf("\a");
}
#else /* ndef TINYFD_NOLIB */
void tinyfd_beep()
{
Beep(440,300);
}
static void wipefileW(wchar_t const * const aFilename)
{
int i;
struct _stat st;
FILE * lIn;
if (_wstat(aFilename, &st) == 0)
{
if ((lIn = _wfopen(aFilename, L"w")))
{
for (i = 0; i < st.st_size; i++)
{
fputc('A', lIn);
}
}
fclose(lIn);
}
}
static wchar_t * getPathWithoutFinalSlashW(
wchar_t * const aoDestination, /* make sure it is allocated, use _MAX_PATH */
wchar_t const * const aSource) /* aoDestination and aSource can be the same */
{
wchar_t const * lTmp;
if (aSource)
{
lTmp = wcsrchr(aSource, L'/');
if (!lTmp)
{
lTmp = wcsrchr(aSource, L'\\');
}
if (lTmp)
{
wcsncpy(aoDestination, aSource, lTmp - aSource);
aoDestination[lTmp - aSource] = L'\0';
}
else
{
*aoDestination = L'\0';
}
}
else
{
*aoDestination = L'\0';
}
return aoDestination;
}
static wchar_t * getLastNameW(
wchar_t * const aoDestination, /* make sure it is allocated */
wchar_t const * const aSource)
{
/* copy the last name after '/' or '\' */
wchar_t const * lTmp;
if (aSource)
{
lTmp = wcsrchr(aSource, L'/');
if (!lTmp)
{
lTmp = wcsrchr(aSource, L'\\');
}
if (lTmp)
{
wcscpy(aoDestination, lTmp + 1);
}
else
{
wcscpy(aoDestination, aSource);
}
}
else
{
*aoDestination = L'\0';
}
return aoDestination;
}
static void Hex2RGBW(wchar_t const aHexRGB[8],
unsigned char aoResultRGB[3])
{
wchar_t lColorChannel[8];
if (aoResultRGB)
{
if (aHexRGB)
{
wcscpy(lColorChannel, aHexRGB);
aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16);
lColorChannel[5] = '\0';
aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16);
lColorChannel[3] = '\0';
aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16);
/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
}
else
{
aoResultRGB[0] = 0;
aoResultRGB[1] = 0;
aoResultRGB[2] = 0;
}
}
}
static void RGB2HexW(
unsigned char const aRGB[3],
wchar_t aoResultHexRGB[8])
{
if (aoResultHexRGB)
{
if (aRGB)
{
/* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */
swprintf(aoResultHexRGB,
#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 )
8,
#endif
L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
}
else
{
aoResultHexRGB[0] = 0;
aoResultHexRGB[1] = 0;
aoResultHexRGB[2] = 0;
}
}
}
#if !defined(WC_ERR_INVALID_CHARS)
/* undefined prior to Vista, so not yet in MINGW header file */
#define WC_ERR_INVALID_CHARS 0x00000080
#endif
static int sizeUtf16(char const * const aUtf8string)
{
return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
aUtf8string, -1, NULL, 0);
}
static int sizeUtf8(wchar_t const * const aUtf16string)
{
return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
aUtf16string, -1, NULL, 0, NULL, NULL);
}
static int sizeMbcs(wchar_t const * const aMbcsString)
{
int lRes = WideCharToMultiByte(CP_ACP, 0,
aMbcsString, -1, NULL, 0, NULL, NULL);
/* DWORD licic = GetLastError(); */
return lRes;
}
static wchar_t * utf8to16(char const * const aUtf8string)
{
wchar_t * lUtf16string ;
int lSize = sizeUtf16(aUtf8string);
lUtf16string = (wchar_t *) malloc( lSize * sizeof(wchar_t) );
lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
aUtf8string, -1, lUtf16string, lSize);
if (lSize == 0)
{
free(lUtf16string);
return NULL;
}
return lUtf16string;
}
static wchar_t * mbcsTo16(char const * const aMbcsString)
{
wchar_t * lMbcsString;
int lSize = sizeUtf16(aMbcsString);
lMbcsString = (wchar_t *)malloc(lSize * sizeof(wchar_t));
lSize = MultiByteToWideChar(CP_ACP, 0,
aMbcsString, -1, lMbcsString, lSize);
if (lSize == 0)
{
free(lMbcsString);
return NULL;
}
return lMbcsString;
}
static char * utf16to8(wchar_t const * const aUtf16string)
{
char * lUtf8string ;
int lSize = sizeUtf8(aUtf16string);
lUtf8string = (char *) malloc( lSize );
lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
aUtf16string, -1, lUtf8string, lSize, NULL, NULL);
if (lSize == 0)
{
free(lUtf8string);
return NULL;
}
return lUtf8string;
}
static char * utf16toMbcs(wchar_t const * const aUtf16string)
{
char * lMbcsString;
int lSize = sizeMbcs(aUtf16string);
lMbcsString = (char *)malloc(lSize);
lSize = WideCharToMultiByte(CP_ACP, 0,
aUtf16string, -1, lMbcsString, lSize, NULL, NULL);
if (lSize == 0)
{
free(lMbcsString);
return NULL;
}
return lMbcsString;
}
static int dirExists(char const * const aDirPath)
{
struct _stat lInfo;
wchar_t * lTmpWChar;
int lStatRet;
int lDirLen;
if (!aDirPath)
return 0;
lDirLen = strlen(aDirPath);
if (!lDirLen)
return 1;
if ( (lDirLen == 2) && (aDirPath[1] == ':') )
return 1;
if (tinyfd_winUtf8)
{
lTmpWChar = utf8to16(aDirPath);
lStatRet = _wstat(lTmpWChar, &lInfo);
free(lTmpWChar);
if (lStatRet != 0)
return 0;
else if (lInfo.st_mode & S_IFDIR)
return 1;
else
return 0;
}
else if (_stat(aDirPath, &lInfo) != 0)
return 0;
else if (lInfo.st_mode & S_IFDIR)
return 1;
else
return 0;
}
static int fileExists(char const * const aFilePathAndName)
{
struct _stat lInfo;
wchar_t * lTmpWChar;
int lStatRet;
FILE * lIn;
if (!aFilePathAndName || !strlen(aFilePathAndName))
{
return 0;
}
if (tinyfd_winUtf8)
{
lTmpWChar = utf8to16(aFilePathAndName);
lStatRet = _wstat(lTmpWChar, &lInfo);
free(lTmpWChar);
if (lStatRet != 0)
return 0;
else if (lInfo.st_mode & _S_IFREG)
return 1;
else
return 0;
}
else
{
lIn = fopen(aFilePathAndName, "r");
if (!lIn)
{
return 0;
}
fclose(lIn);
return 1;
}
}
static int replaceWchar(wchar_t * const aString,
wchar_t const aOldChr,
wchar_t const aNewChr)
{
wchar_t * p;
int lRes = 0;
if (!aString)
{
return 0;
}
if (aOldChr == aNewChr)
{
return 0;
}
p = aString;
while ((p = wcsrchr(p, aOldChr)))
{
*p = aNewChr;
#ifdef TINYFD_NOCCSUNICODE
p++;
#endif
p++;
lRes = 1;
}
return lRes;
}
#endif /* TINYFD_NOLIB */
#endif /* _WIN32 */
/* source and destination can be the same or ovelap*/
static char const * ensureFilesExist(char * const aDestination,
char const * const aSourcePathsAndNames)
{
char * lDestination = aDestination;
char const * p;
char const * p2;
size_t lLen;
if (!aSourcePathsAndNames)
{
return NULL;
}
lLen = strlen(aSourcePathsAndNames);
if (!lLen)
{
return NULL;
}
p = aSourcePathsAndNames;
while ((p2 = strchr(p, '|')) != NULL)
{
lLen = p2 - p;
memmove(lDestination, p, lLen);
lDestination[lLen] = '\0';
if (fileExists(lDestination))
{
lDestination += lLen;
*lDestination = '|';
lDestination++;
}
p = p2 + 1;
}
if (fileExists(p))
{
lLen = strlen(p);
memmove(lDestination, p, lLen);
lDestination[lLen] = '\0';
}
else
{
*(lDestination - 1) = '\0';
}
return aDestination;
}
#ifdef _WIN32
#ifndef TINYFD_NOLIB
static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
wchar_t lTitleName[MAX_PATH];
GetWindowTextW(hwnd, lTitleName, MAX_PATH);
/* wprintf(L"lTitleName %ls \n", lTitleName); */
if (wcscmp(L"tinyfiledialogsTopWindow", lTitleName) == 0)
{
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return 0;
}
return 1;
}
static void hiddenConsoleW(wchar_t const * const aString, wchar_t const * const aDialogTitle, int const aInFront)
{
STARTUPINFOW StartupInfo;
PROCESS_INFORMATION ProcessInfo;
if (!aString || !wcslen(aString) ) return;
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFOW);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
if (!CreateProcessW(NULL, (LPWSTR)aString, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL,
&StartupInfo, &ProcessInfo))
{
return; /* GetLastError(); */
}
WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
if (aInFront)
{
while (EnumWindows(EnumThreadWndProc, (LPARAM)NULL)) {}
SetWindowTextW(GetForegroundWindow(), aDialogTitle);
}
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
int tinyfd_messageBoxW(
wchar_t const * const aTitle, /* NULL or "" */
wchar_t const * const aMessage, /* NULL or "" may contain \n and \t */
wchar_t const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
wchar_t const * const aIconType, /* "info" "warning" "error" "question" */
int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
{
int lBoxReturnValue;
UINT aCode;
if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; }
if (aIconType && !wcscmp(L"warning", aIconType))
{
aCode = MB_ICONWARNING;
}
else if (aIconType && !wcscmp(L"error", aIconType))
{
aCode = MB_ICONERROR;
}
else if (aIconType && !wcscmp(L"question", aIconType))
{
aCode = MB_ICONQUESTION;
}
else
{
aCode = MB_ICONINFORMATION;
}
if (aDialogType && !wcscmp(L"okcancel", aDialogType))
{
aCode += MB_OKCANCEL;
if (!aDefaultButton)
{
aCode += MB_DEFBUTTON2;
}
}
else if (aDialogType && !wcscmp(L"yesno", aDialogType))
{
aCode += MB_YESNO;
if (!aDefaultButton)
{
aCode += MB_DEFBUTTON2;
}
}
else
{
aCode += MB_OK;
}
aCode += MB_TOPMOST;
lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode);
if (((aDialogType
&& wcscmp(L"okcancel", aDialogType)
&& wcscmp(L"yesno", aDialogType)))
|| (lBoxReturnValue == IDOK)
|| (lBoxReturnValue == IDYES))
{
return 1;
}
else
{
return 0;
}
}
static int messageBoxWinGui8(
char const * const aTitle, /* NULL or "" */
char const * const aMessage, /* NULL or "" may contain \n and \t */
char const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */