<commit_before>#include <graphics/PixelFormat.h>
#include <math/Rectangle.h>
#include <system/Clipboard.h>
#include <system/DisplayMode.h>
#include <system/JNI.h>
#include <system/System.h>

#ifdef HX_MACOS
#include <CoreFoundation/CoreFoundation.h>
#endif

#ifdef HX_WINDOWS
#include <shlobj.h>
#include <stdio.h>
//#include <io.h>
//#include <fcntl.h>
#ifdef __MINGW32__
#ifndef CSIDL_MYDOCUMENTS
#define CSIDL_MYDOCUMENTS CSIDL_PERSONAL
#endif
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0
#endif
#endif
#if UNICODE
#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR))
#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UTF-16LE", "UTF-8", (char *)(S), SDL_strlen(S)+1)
#else
#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)(S), (SDL_strlen(S)+1))
#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)(S), SDL_strlen(S)+1)
#endif
#endif

#include <SDL.h>
#include <string>


namespace lime {
	
	
	static int id_bounds;
	static int id_currentMode;
	static int id_dpi;
	static int id_height;
	static int id_name;
	static int id_pixelFormat;
	static int id_refreshRate;
	static int id_supportedModes;
	static int id_width;
	static bool init = false;
	
	
	const char* Clipboard::GetText () {
		
		return SDL_GetClipboardText ();
		
	}
	
	
	bool Clipboard::HasText () {
		
		return SDL_HasClipboardText ();
		
	}
	
	
	void Clipboard::SetText (const char* text) {
		
		SDL_SetClipboardText (text);
		
	}
	
	
	void *JNI::GetEnv () {
		
		#ifdef ANDROID
		return SDL_AndroidGetJNIEnv ();
		#else
		return 0;
		#endif
		
	}
	
	
	bool System::GetAllowScreenTimeout () {
		
		return SDL_IsScreenSaverEnabled ();
		
	}
	
	
	std::wstring* System::GetDirectory (SystemDirectory type, const char* company, const char* title) {
		
		switch (type) {
			
			case APPLICATION: {
				
				char* path = SDL_GetBasePath ();
				std::wstring* result = new std::wstring (path, path + strlen (path));
				SDL_free (path);
				return result;
				break;
				
			}
			
			case APPLICATION_STORAGE: {
				
				char* path = SDL_GetPrefPath (company, title);
				std::wstring* result = new std::wstring (path, path + strlen (path));
				SDL_free (path);
				return result;
				break;
				
			}
			
			case DESKTOP: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::HomeGroup;
				std::wstring* result = new std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif !defined (ANDROID)
				
				std::string path = std::string (getenv ("HOME")) + std::string ("/Desktop");
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
			case DOCUMENTS: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::DocumentsLibrary;
				std::wstring* result = std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/mnt/sdcard/Documents");
				
				#else
				
				std::string path = std::string (getenv ("HOME")) + std::string ("/Documents");
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
			case FONTS: {
				
				#if defined (HX_WINRT)
				
				return 0;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (HX_MACOS)
				
				return new std::wstring (L"/Library/Fonts");
				
				#elif defined (IPHONE)
				
				return new std::wstring (L"/System/Library/Fonts");
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/system/fonts");
				
				#elif defined (BLACKBERRY)
				
				return new std::wstring (L"/usr/fonts/font_repository/monotype");
				
				#else
				
				return new std::wstring (L"/usr/share/fonts/truetype");
				
				#endif
				break;
				
			}
			
			case USER: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::ApplicationData::Current->RoamingFolder;
				std::wstring* result = new std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/mnt/sdcard");
				
				#else
				
				std::string path = getenv ("HOME");
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
		}
		
		return 0;
		
	}
	
	
	value System::GetDisplay (int id) {
		<commit_msg>Guard against getEnv("HOME") returning null on some platforms<commit_after>#include <graphics/PixelFormat.h>
#include <math/Rectangle.h>
#include <system/Clipboard.h>
#include <system/DisplayMode.h>
#include <system/JNI.h>
#include <system/System.h>

#ifdef HX_MACOS
#include <CoreFoundation/CoreFoundation.h>
#endif

#ifdef HX_WINDOWS
#include <shlobj.h>
#include <stdio.h>
//#include <io.h>
//#include <fcntl.h>
#ifdef __MINGW32__
#ifndef CSIDL_MYDOCUMENTS
#define CSIDL_MYDOCUMENTS CSIDL_PERSONAL
#endif
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0
#endif
#endif
#if UNICODE
#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR))
#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UTF-16LE", "UTF-8", (char *)(S), SDL_strlen(S)+1)
#else
#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)(S), (SDL_strlen(S)+1))
#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)(S), SDL_strlen(S)+1)
#endif
#endif

#include <SDL.h>
#include <string>


namespace lime {
	
	
	static int id_bounds;
	static int id_currentMode;
	static int id_dpi;
	static int id_height;
	static int id_name;
	static int id_pixelFormat;
	static int id_refreshRate;
	static int id_supportedModes;
	static int id_width;
	static bool init = false;
	
	
	const char* Clipboard::GetText () {
		
		return SDL_GetClipboardText ();
		
	}
	
	
	bool Clipboard::HasText () {
		
		return SDL_HasClipboardText ();
		
	}
	
	
	void Clipboard::SetText (const char* text) {
		
		SDL_SetClipboardText (text);
		
	}
	
	
	void *JNI::GetEnv () {
		
		#ifdef ANDROID
		return SDL_AndroidGetJNIEnv ();
		#else
		return 0;
		#endif
		
	}
	
	
	bool System::GetAllowScreenTimeout () {
		
		return SDL_IsScreenSaverEnabled ();
		
	}
	
	
	std::wstring* System::GetDirectory (SystemDirectory type, const char* company, const char* title) {
		
		switch (type) {
			
			case APPLICATION: {
				
				char* path = SDL_GetBasePath ();
				std::wstring* result = new std::wstring (path, path + strlen (path));
				SDL_free (path);
				return result;
				break;
				
			}
			
			case APPLICATION_STORAGE: {
				
				char* path = SDL_GetPrefPath (company, title);
				std::wstring* result = new std::wstring (path, path + strlen (path));
				SDL_free (path);
				return result;
				break;
				
			}
			
			case DESKTOP: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::HomeGroup;
				std::wstring* result = new std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif !defined (ANDROID)
				
				char const* home = getenv ("HOME");

				if (home == NULL) {

					return 0;

				}

				std::string path = std::string (home) + std::string ("/Desktop");
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
			case DOCUMENTS: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::DocumentsLibrary;
				std::wstring* result = std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/mnt/sdcard/Documents");
				
				#else
				
				char const* home = getenv ("HOME");

				if (home == NULL) {

					return 0;

				}

				std::string path = std::string (home) + std::string ("/Documents");
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
			case FONTS: {
				
				#if defined (HX_WINRT)
				
				return 0;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (HX_MACOS)
				
				return new std::wstring (L"/Library/Fonts");
				
				#elif defined (IPHONE)
				
				return new std::wstring (L"/System/Library/Fonts");
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/system/fonts");
				
				#elif defined (BLACKBERRY)
				
				return new std::wstring (L"/usr/fonts/font_repository/monotype");
				
				#else
				
				return new std::wstring (L"/usr/share/fonts/truetype");
				
				#endif
				break;
				
			}
			
			case USER: {
				
				#if defined (HX_WINRT)
				
				Windows::Storage::StorageFolder folder = Windows::Storage::ApplicationData::Current->RoamingFolder;
				std::wstring* result = new std::wstring (folder->Begin ());
				return result;
				
				#elif defined (HX_WINDOWS)
				
				char folderPath[MAX_PATH] = "";
				SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, folderPath);
				WIN_StringToUTF8 (folderPath);
				std::string path = std::string (folderPath);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#elif defined (IPHONE)
				
				return System::GetIOSDirectory (type);
				
				#elif defined (ANDROID)
				
				return new std::wstring (L"/mnt/sdcard");
				
				#else
				
				char const* home = getenv ("HOME");

				if (home == NULL) {

					return 0;

				}

				std::string path = std::string (home);
				std::wstring* result = new std::wstring (path.begin (), path.end ());
				return result;
				
				#endif
				break;
				
			}
			
		}
		
		return 0;
		
	}
	
	
	value System::GetDisplay (int id) {
		
{'fraction_non_alphanumeric': 0.13494865211810012, 'fraction_numerical': 0.004412708600770218, 'mean_word_length': 3.1206611570247933, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '655744', 'n_tokens_mistral': 5604, 'n_tokens_neox': 4210, 'n_words': 975}