-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyfiledialogs.c
7750 lines (7045 loc) · 279 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
/* this file can be renamed with extension ".cpp" and compiled as C++.
The code is 100% compatible C C++
(just comment out << extern "C" >> in the header file) */
/*_________
/ \ tinyfiledialogs.c v3.8.8 [Apr 22, 2021] zlib licence
|tiny file| Unique code file created [November 9, 2014]
| dialogs | Copyright (c) 2014 - 2021 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 at the bottom of the header file |
|_________________________________________________________________________________|
_________________________________________________________
| |
| on windows: - since v3.6 char is UTF-8 by default |
| - if you want MBCS set tinyfd_winUtf8 to 0 |
| - functions like fopen expect MBCS |
|_________________________________________________________|
If you like tinyfiledialogs, please upvote my stackoverflow answer
https://stackoverflow.com/a/47651444
- 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.
-----------
Thanks for contributions, bug corrections & thorough testing to:
- Don Heyse http://ldglite.sf.net for bug corrections & thorough testing!
- Paul Rouget
*/
#ifndef __sun
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */
#endif
#endif
#if !defined(_WIN32) && ( defined(__GNUC__) || defined(__clang__) )
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#ifdef _WIN32
#ifdef __BORLANDC__
#define _getch getch
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#include <windows.h>
#include <commdlg.h>
#include <shlobj.h>
#include <conio.h>
#include <direct.h>
#define TINYFD_NOCCSUNICODE
#define SLASH "\\"
#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 */
#include "tinyfiledialogs.h"
#define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */
#ifndef MAX_MULTIPLE_FILES
#define MAX_MULTIPLE_FILES 1024
#endif
#define LOW_MULTIPLE_FILES 32
char tinyfd_version[8] = "3.8.8";
/******************************************************************************************************/
/**************************************** UTF-8 on Windows ********************************************/
/******************************************************************************************************/
#ifdef _WIN32
/* if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of tinyfiledialogs.h )
Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */
int tinyfd_winUtf8 = 1; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */
/* for MBCS change this to 0, here or in your code */
#endif
/******************************************************************************************************/
/******************************************************************************************************/
/******************************************************************************************************/
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 dialogs */
/* Curses dialogs are difficult to use, on windows they are only ascii and uses the unix backslah */
int tinyfd_allowCursesDialogs = 0 ; /* 0 (default) or 1 */
int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */
/* 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.
it can use the package dialog or dialog.exe.
on windows it only make sense for console applications */
int tinyfd_assumeGraphicDisplay = 0; /* 0 (default) or 1 */
/* some systems don't set the environment variable DISPLAY even when a graphic display is present.
set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */
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
shellementary qarma yad python2-tkinter python3-tkinter python-dbus
perl-dbus gxmessage gmessage xmessage xdialog gdialog
for console mode:
dialog whiptail basicinput no_solution */
static int gWarningDisplayed = 0 ;
static char gTitle[]="missing software! (we will try basic console input)";
#ifdef _WIN32
char tinyfd_needs[] = "\
___________\n\
/ \\ \n\
| tiny file |\n\
| dialogs |\n\
\\_____ ____/\n\
\\|\
\ntiny file dialogs on Windows needs:\
\n a graphic display\
\nor dialog.exe (curses console mode)\
\nor a console for basic input";
#else
char tinyfd_needs[] = "\
___________\n\
/ \\ \n\
| tiny file |\n\
| dialogs |\n\
\\_____ ____/\n\
\\|\
\ntiny file dialogs on UNIX needs:\
\n applescript or kdialog or yad or Xdialog\
\nor zenity (or matedialog or shellementary or qarma)\
\nor python (2 or 3) + tkinter + python-dbus (optional)\
\nor dialog (opens console if needed)\
\nor xterm + bash (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 int getenvDISPLAY(void)
{
return tinyfd_assumeGraphicDisplay || getenv("DISPLAY");
}
static char * getCurDir(void)
{
static char lCurDir[MAX_PATH_OR_CMD];
return getcwd(lCurDir, sizeof(lCurDir));
}
static char * getPathWithoutFinalSlash(
char * aoDestination, /* make sure it is allocated, use _MAX_PATH */
char 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 * aoDestination, /* make sure it is allocated */
char 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 * 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(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
#else
sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]);
#endif
/*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/
}
else
{
aoResultHexRGB[0]=0;
aoResultHexRGB[1]=0;
aoResultHexRGB[2]=0;
}
}
}
void tfd_replaceSubStr( char const * aSource, char const * aOldSubStr,
char const * aNewSubStr, char * 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 * aFileNameWithoutPath )
{
if ( ! aFileNameWithoutPath
|| ! strlen(aFileNameWithoutPath)
|| strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") )
{
return 0 ;
}
return 1 ;
}
#ifndef _WIN32
static int fileExists( char const * aFilePathAndName )
{
FILE * lIn ;
if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
{
return 0 ;
}
lIn = fopen( aFilePathAndName , "r" ) ;
if ( ! lIn )
{
return 0 ;
}
fclose( lIn ) ;
return 1 ;
}
#endif
static void wipefile(char 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);
}
}
}
int tfd_quoteDetected(char const * aString)
{
char const * p;
if (!aString) return 0;
p = aString;
while ((p = strchr(p, '\'')))
{
return 1;
}
p = aString;
while ((p = strchr(p, '\"')))
{
return 1;
}
return 0;
}
char const * tinyfd_getGlobalChar(char const * aCharVariableName) /* to be called from C# (you don't need this in C or C++) */
{
if (!aCharVariableName || !strlen(aCharVariableName)) return NULL;
else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version;
else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs;
else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response;
else return NULL ;
}
int tinyfd_getGlobalInt(char const * aIntVariableName) /* to be called from C# (you don't need this in C or C++) */
{
if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ;
else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ;
else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ;
else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ;
else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ;
else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ;
#ifdef _WIN32
else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ;
#endif
else return -1;
}
int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */
{
if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ;
else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; }
else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; }
else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; }
else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; }
else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; }
#ifdef _WIN32
else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; }
#endif
else return -1;
}
#ifdef _WIN32
static int powershellPresent(void)
{ /*only on vista and above (or installed on xp)*/
static int lPowershellPresent = -1;
char lBuff[MAX_PATH_OR_CMD];
FILE* lIn;
char const* lString = "powershell.exe";
if (lPowershellPresent < 0)
{
if (!(lIn = _popen("where powershell.exe", "r")))
{
lPowershellPresent = 0;
return 0;
}
while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
{
}
_pclose(lIn);
if (lBuff[strlen(lBuff) - 1] == '\n')
{
lBuff[strlen(lBuff) - 1] = '\0';
}
if (strcmp(lBuff + strlen(lBuff) - strlen(lString), lString))
{
lPowershellPresent = 0;
}
else
{
lPowershellPresent = 1;
}
}
return lPowershellPresent;
}
static int windowsVersion(void)
{
#if !defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR)
typedef LONG NTSTATUS ;
typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
HMODULE hMod;
RtlGetVersionPtr lFxPtr;
RTL_OSVERSIONINFOW lRovi = { 0 };
hMod = GetModuleHandleW(L"ntdll.dll");
if (hMod) {
lFxPtr = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion");
if (lFxPtr)
{
lRovi.dwOSVersionInfoSize = sizeof(lRovi);
if (!lFxPtr(&lRovi))
{
return lRovi.dwMajorVersion;
}
}
}
#endif
if (powershellPresent()) return 6; /*minimum is vista or installed on xp*/
return 0;
}
static void replaceChr(char * aString, char aOldChr, char aNewChr)
{
char * p;
if (!aString) return;
if (aOldChr == aNewChr) return;
p = aString;
while ((p = strchr(p, aOldChr)))
{
*p = aNewChr;
p++;
}
return;
}
#if !defined(WC_ERR_INVALID_CHARS)
/* undefined prior to Vista, so not yet in MINGW header file */
#define WC_ERR_INVALID_CHARS 0x00000000 /* 0x00000080 for MINGW maybe ? */
#endif
static int sizeUtf16From8(char const * aUtf8string)
{
return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
aUtf8string, -1, NULL, 0);
}
static int sizeUtf16FromMbcs(char const * aMbcsString)
{
return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
aMbcsString, -1, NULL, 0);
}
static int sizeUtf8(wchar_t const * aUtf16string)
{
return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
aUtf16string, -1, NULL, 0, NULL, NULL);
}
static int sizeMbcs(wchar_t const * aMbcsString)
{
int lRes = WideCharToMultiByte(CP_ACP, 0,
aMbcsString, -1, NULL, 0, NULL, NULL);
/* DWORD licic = GetLastError(); */
return lRes;
}
wchar_t* tinyfd_mbcsTo16(char const* aMbcsString)
{
static wchar_t* lMbcsString = NULL;
int lSize;
free(lMbcsString);
if (!aMbcsString) { lMbcsString = NULL; return NULL; }
lSize = sizeUtf16FromMbcs(aMbcsString);
if (lSize)
{
lMbcsString = (wchar_t*)malloc(lSize * sizeof(wchar_t));
lSize = MultiByteToWideChar(CP_ACP, 0, aMbcsString, -1, lMbcsString, lSize);
}
else wcscpy(lMbcsString, L"");
return lMbcsString;
}
wchar_t * tinyfd_utf8to16(char const * aUtf8string)
{
static wchar_t * lUtf16string = NULL;
int lSize;
free(lUtf16string);
if (!aUtf8string) {lUtf16string = NULL; return NULL;}
lSize = sizeUtf16From8(aUtf8string);
if (lSize)
{
lUtf16string = (wchar_t*)malloc(lSize * sizeof(wchar_t));
lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
aUtf8string, -1, lUtf16string, lSize);
return lUtf16string;
}
else
{
/* let's try mbcs anyway */
lUtf16string = NULL;
return tinyfd_mbcsTo16(aUtf8string);
}
}
char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string)
{
static char * lMbcsString = NULL;
int lSize;
free(lMbcsString);
if (!aUtf16string) { lMbcsString = NULL; return NULL; }
lSize = sizeMbcs(aUtf16string);
if (lSize)
{
lMbcsString = (char*)malloc(lSize);
lSize = WideCharToMultiByte(CP_ACP, 0, aUtf16string, -1, lMbcsString, lSize, NULL, NULL);
}
else strcpy(lMbcsString, "");
return lMbcsString;
}
char * tinyfd_utf8toMbcs(char const * aUtf8string)
{
wchar_t const * lUtf16string;
lUtf16string = tinyfd_utf8to16(aUtf8string);
return tinyfd_utf16toMbcs(lUtf16string);
}
char * tinyfd_utf16to8(wchar_t const * aUtf16string)
{
static char * lUtf8string = NULL;
int lSize;
free(lUtf8string);
if (!aUtf16string) { lUtf8string = NULL; return NULL; }
lSize = sizeUtf8(aUtf16string);
if (lSize)
{
lUtf8string = (char*)malloc(lSize);
lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, aUtf16string, -1, lUtf8string, lSize, NULL, NULL);
}
else strcpy(lUtf8string, "");
return lUtf8string;
}
char * tinyfd_mbcsTo8(char const * aMbcsString)
{
wchar_t const * lUtf16string;
lUtf16string = tinyfd_mbcsTo16(aMbcsString);
return tinyfd_utf16to8(lUtf16string);
}
void tinyfd_beep(void)
{
if (windowsVersion() > 5) Beep(440, 300);
else MessageBeep(-1);
}
static void wipefileW(wchar_t const * aFilename)
{
int i;
FILE * lIn;
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
struct _stat st;
if (_wstat(aFilename, &st) == 0)
#else
struct __stat64 st;
if (_wstat64(aFilename, &st) == 0)
#endif
{
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 * aoDestination, /* make sure it is allocated, use _MAX_PATH */
wchar_t 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 * aoDestination, /* make sure it is allocated */
wchar_t 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 (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx";
#else
wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx";
#endif
if (aoResultHexRGB)
{
if (aRGB)
{
/* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */
#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
#else
swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
#endif
}
else
{
aoResultHexRGB[0] = 0;
aoResultHexRGB[1] = 0;
aoResultHexRGB[2] = 0;
}
}
}
static int dirExists(char const * aDirPath)
{
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
struct _stat lInfo;
#else
struct __stat64 lInfo;
#endif
wchar_t * lTmpWChar;
int lStatRet;
size_t lDirLen;
if (!aDirPath)
return 0;
lDirLen = strlen(aDirPath);
if (!lDirLen)
return 1;
if ( (lDirLen == 2) && (aDirPath[1] == ':') )
return 1;
if (tinyfd_winUtf8)
{
lTmpWChar = tinyfd_utf8to16(aDirPath);
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
lStatRet = _wstat(lTmpWChar, &lInfo);
#else
lStatRet = _wstat64(lTmpWChar, &lInfo);
#endif
if (lStatRet != 0)
return 0;
else if (lInfo.st_mode & S_IFDIR)
return 1;
else
return 0;
}
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
else if (_stat(aDirPath, &lInfo) != 0)
#else
else if (_stat64(aDirPath, &lInfo) != 0)
#endif
return 0;
else if (lInfo.st_mode & S_IFDIR)
return 1;
else
return 0;
}
static int fileExists(char const * aFilePathAndName)
{
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
struct _stat lInfo;
#else
struct __stat64 lInfo;
#endif
wchar_t * lTmpWChar;
int lStatRet;
FILE * lIn;
if (!aFilePathAndName || !strlen(aFilePathAndName))
{
return 0;
}
if (tinyfd_winUtf8)
{
lTmpWChar = tinyfd_utf8to16(aFilePathAndName);
#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
lStatRet = _wstat(lTmpWChar, &lInfo);
#else
lStatRet = _wstat64(lTmpWChar, &lInfo);
#endif
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 void replaceWchar(wchar_t * aString,
wchar_t aOldChr,
wchar_t aNewChr)
{
wchar_t * p;
if (!aString)
{
return ;
}
if (aOldChr == aNewChr)
{
return ;
}
p = aString;
while ((p = wcsrchr(p, aOldChr)))
{
*p = aNewChr;
#ifdef TINYFD_NOCCSUNICODE
p++;
#endif
p++;
}
return ;
}
static int quoteDetectedW(wchar_t const * aString)
{
wchar_t const * p;
if (!aString) return 0;
p = aString;
while ((p = wcsrchr(p, L'\'')))
{
return 1;
}
p = aString;
while ((p = wcsrchr(p, L'\"')))
{
return 1;
}
return 0;
}
#endif /* _WIN32 */
/* source and destination can be the same or ovelap*/
static char * ensureFilesExist(char * aDestination,
char 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';