Olav SørensenUpdates for pt_pal_editor75aa83e3 years agoHistory
7765 lines · 271.8 KB · C
RawDownload
/* 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.10 [Mar 27, 2023] zlib licence |tiny file| Unique code file created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2023 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 answerhttps://stackoverflow.com/a/47651444 - License -This software is provided 'as-is', without any express or impliedwarranty.  In no event will the authors be held liable for any damagesarising 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 itfreely, subject to the following restrictions:1. The origin of this software must not be misrepresented; you must notclaim that you wrote the original software.  If you use this softwarein a product, an acknowledgment in the product documentation would beappreciated but is not required.2. Altered source versions must be plainly marked as such, and must not bemisrepresented 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.10"; /******************************************************************************************************//**************************************** 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 dialogsbut 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 gdialogfor console mode:  dialog whiptail basicinput no_solution */ static int gWarningDisplayed = 0 ;static char gTitle[]="missing software! (we will try basic console input)"; #ifdef _WIN32char 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";#elsechar 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) ** Disabled by default **/\\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 _WIN32static 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;} 

Lines 1–500 of 7765.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t