Sigrid Solveig Haflínudóttirfix warnings1dc9e673 years agoHistory
970 lines · 22.4 KB · C
RawDownload
// for finding memory leaks in debug mode with Visual Studio #if defined _DEBUG && defined _MSC_VER#include <crtdbg.h>#endif #include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <stdbool.h>#include <math.h>#include <ctype.h>#ifdef _WIN32#include <direct.h>#include <io.h>#define WIN32_MEAN_AND_LEAN#include <windows.h>#include <shlobj.h> // SHGetFolderPathW()#else#include <unistd.h>#include <dirent.h>#endif#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <time.h>#include <limits.h>#include "pt2_textout.h"#include "pt2_diskop.h"#include "pt2_tables.h"#include "pt2_module_loader.h"#include "pt2_config.h"#include "pt2_visuals.h"#include "pt2_sample_loader.h"#include "pt2_bmp.h"#include "pt2_askbox.h"#include "pt2_replayer.h" typedef struct fileEntry_t{	UNICHAR *nameU;	char firstAnsiChar; // for handleEntryJumping();	char dateChanged[6 + 1];	bool isDir;	int32_t filesize;} fileEntry_t; // "look for file" flagsenum{	LFF_DONE = 0,	LFF_SKIP = 1,	LFF_OK = 2}; #ifdef _WIN32#define PARENT_DIR_STR L".."static HANDLE hFind;#else#define PARENT_DIR_STR ".."static DIR *hFind;#endif static char fileNameBuffer[PATH_MAX + 1];static UNICHAR pathTmp[PATH_MAX + 2];static fileEntry_t *diskOpEntry; void addSampleFileExt(char *fileName){	switch (diskop.smpSaveType)	{		case DISKOP_SMP_WAV: strcat(fileName, ".wav"); break;		case DISKOP_SMP_IFF: strcat(fileName, ".iff"); break;		default: case DISKOP_SMP_RAW: break;	}} static fileEntry_t *bufferCreateEmptyDir(void) // special case: creates a dir entry with a ".." directory{	fileEntry_t *dirEntry = (fileEntry_t *)malloc(sizeof (fileEntry_t));	if (dirEntry == NULL)		return NULL; 	dirEntry->nameU = UNICHAR_STRDUP(PARENT_DIR_STR);	if (dirEntry->nameU == NULL)	{		free(dirEntry);		return NULL;	} 	dirEntry->isDir = true;	dirEntry->filesize = 0; 	return dirEntry;} // deciding whether to load this entry into the buffer or notstatic bool listEntry(fileEntry_t *f){	int32_t entryName = (int32_t)UNICHAR_STRLEN(f->nameU); #ifdef _WIN32	if (f->isDir && entryName == 2 && !UNICHAR_STRNICMP(f->nameU, "..", 2))		return true; // always show ".." directory#else	if (f->isDir && entryName == 2 && !UNICHAR_STRNICMP(f->nameU, "..", 2) && !(editor.currPath[0] == '/' && editor.currPath[1] == '\0'))		return true; // always show ".." directory (unless in root dir)#endif 	if (!UNICHAR_STRNICMP(f->nameU, ".", 1))		return false; // skip ".name" entries 	if (f->isDir || diskop.mode == DISKOP_MODE_SMP)		return true; // list all entries 	if (entryName >= 4)	{		if (!UNICHAR_STRNICMP(f->nameU, "MOD.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".MOD", 4) ||		    !UNICHAR_STRNICMP(f->nameU, "STK.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".STK", 4) ||		    !UNICHAR_STRNICMP(f->nameU, "M15.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".M15", 4) ||		    !UNICHAR_STRNICMP(f->nameU, "NST.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".NST", 4) ||		    !UNICHAR_STRNICMP(f->nameU, "UST.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".UST", 4) ||		    !UNICHAR_STRNICMP(f->nameU, "PP.",  3) || !UNICHAR_STRNICMP(&f->nameU[entryName - 3], ".PP",  3) ||		    !UNICHAR_STRNICMP(f->nameU, "NT.",  3) || !UNICHAR_STRNICMP(&f->nameU[entryName - 3], ".NT",  3))		{			return true;		}	} 	return false;} static int8_t findFirst(fileEntry_t *searchRec){#ifdef _WIN32	WIN32_FIND_DATAW fData;	SYSTEMTIME sysTime;#else	struct dirent *fData;	struct stat st;#endif 	searchRec->nameU = NULL; // this one must be initialized #ifdef _WIN32	hFind = FindFirstFileW(L"*", &fData);	if (hFind == NULL || hFind == INVALID_HANDLE_VALUE)		return LFF_DONE; 	searchRec->nameU = UNICHAR_STRDUP(fData.cFileName);	if (searchRec->nameU == NULL)		return LFF_SKIP; 	searchRec->filesize = (fData.nFileSizeHigh > 0) ? -1 : fData.nFileSizeLow;	searchRec->isDir = (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;#else	hFind = opendir(".");	if (hFind == NULL)		return LFF_DONE; 	fData = readdir(hFind);	if (fData == NULL)		return LFF_DONE; 	searchRec->nameU = UNICHAR_STRDUP(fData->d_name);	if (searchRec->nameU == NULL)		return LFF_SKIP; 	searchRec->filesize = 0;	searchRec->isDir = false; 	if (stat(fData->d_name, &st) == 0)	{		searchRec->isDir = !!(st.st_mode & S_IFDIR);		searchRec->filesize = ((int64_t)st.st_size > INT32_MAX) ? -1 : (int32_t)st.st_size;	}#endif 	if (searchRec->filesize < -1)		searchRec->filesize = -1; 	if (!listEntry(searchRec))	{		// skip entry		free(searchRec->nameU);		searchRec->nameU = NULL;		return LFF_SKIP;	} #ifdef _WIN32	FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);	snprintf(searchRec->dateChanged, 7, "%02d%02d%02d", sysTime.wDay, sysTime.wMonth, sysTime.wYear % 100);	unicharToAnsi(&searchRec->firstAnsiChar, searchRec->nameU, 1);#else	strftime(searchRec->dateChanged, 7, "%d%m%y", localtime(&st.st_mtime));	searchRec->firstAnsiChar = (char)searchRec->nameU[0];#endif 	return LFF_OK;} static int8_t findNext(fileEntry_t *searchRec){#ifdef _WIN32	WIN32_FIND_DATAW fData;	SYSTEMTIME sysTime;#else	struct dirent *fData;	struct stat st;#endif 	searchRec->nameU = NULL; // important #ifdef _WIN32	if (hFind == NULL || FindNextFileW(hFind, &fData) == 0)		return LFF_DONE; 	searchRec->nameU = UNICHAR_STRDUP(fData.cFileName);	if (searchRec->nameU == NULL)		return LFF_SKIP; 	searchRec->filesize = (fData.nFileSizeHigh > 0) ? -1 : fData.nFileSizeLow;	searchRec->isDir = (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false; 	FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);#else	if (hFind == NULL || (fData = readdir(hFind)) == NULL)		return LFF_DONE; 	searchRec->nameU = UNICHAR_STRDUP(fData->d_name);	if (searchRec->nameU == NULL)		return LFF_SKIP; 	searchRec->filesize = 0;	searchRec->isDir = false; 	if (stat(fData->d_name, &st) == 0)	{		searchRec->isDir = !!(st.st_mode & S_IFDIR);		searchRec->filesize = ((int64_t)st.st_size > INT32_MAX) ? -1 : (int32_t)st.st_size;	}#endif 	if (searchRec->filesize < -1)		searchRec->filesize = -1; 	if (!listEntry(searchRec))	{		// skip entry		free(searchRec->nameU);		searchRec->nameU = NULL;		return LFF_SKIP;	} #ifdef _WIN32	FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);	snprintf(searchRec->dateChanged, 7, "%02d%02d%02d", sysTime.wDay, sysTime.wMonth, sysTime.wYear % 100);	unicharToAnsi(&searchRec->firstAnsiChar, searchRec->nameU, 1);#else	strftime(searchRec->dateChanged, 7, "%d%m%y", localtime(&st.st_mtime));	searchRec->firstAnsiChar = (char)searchRec->nameU[0];#endif 	return LFF_OK;} static void findClose(void){	if (hFind != NULL)	{#ifdef _WIN32		FindClose(hFind);#else		closedir(hFind);#endif		hFind = NULL;	}} void diskOpShowSelectText(void){	if (ui.pointerMode == POINTER_MODE_MSG1 || editor.errorMsgActive)		return; 	if (diskop.mode == DISKOP_MODE_MOD)		setStatusMessage("SELECT MODULE", NO_CARRY);	else		setStatusMessage("SELECT SAMPLE", NO_CARRY);} void handleEntryJumping(SDL_Keycode jumpToChar) // SHIFT+character{	if (diskOpEntry != NULL)	{		fileEntry_t *f;				// if last jump key was the same, go to next page of matching character		if (diskop.numEntries > DISKOP_LINES && diskop.lastEntryJumpKey == jumpToChar)		{			for (int32_t i = 0; i < DISKOP_LINES; i++)			{				int32_t offset = diskop.scrollOffset+DISKOP_LINES;				if (offset > diskop.numEntries-DISKOP_LINES)					offset = diskop.numEntries-DISKOP_LINES; 				f = &diskOpEntry[offset]; 				if (!f->isDir && tolower(f->firstAnsiChar) == jumpToChar)				{					diskop.scrollOffset += DISKOP_LINES;					if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)						diskop.scrollOffset = diskop.numEntries-DISKOP_LINES; 					ui.updateDiskOpFileList = true;					return;				}			}		} 		diskop.lastEntryJumpKey = jumpToChar; 		// jump to first match from the beginning of file list 		f = diskOpEntry;		for (int32_t i = 0; i < diskop.numEntries; i++, f++)		{			if (!f->isDir && tolower(f->firstAnsiChar) == jumpToChar)			{				if (diskop.numEntries > DISKOP_LINES)				{					diskop.scrollOffset = i;					if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)						diskop.scrollOffset = diskop.numEntries-DISKOP_LINES; 					ui.updateDiskOpFileList = true;				} 				return;			}		}	} 	// character not found in file list, show red mouse pointer (error)	editor.errorMsgActive = true;	editor.errorMsgBlock = true;	editor.errorMsgCounter = 0; 	setErrPointer();} bool diskOpEntryIsEmpty(int32_t fileIndex){	if (diskop.scrollOffset+fileIndex >= diskop.numEntries)		return true; 	return false;} bool diskOpEntryIsDir(int32_t fileIndex){	if (diskOpEntry != NULL)	{		if (!diskOpEntryIsEmpty(fileIndex))			return diskOpEntry[diskop.scrollOffset+fileIndex].isDir;	} 	return false; // couldn't look up entry} char *diskOpGetAnsiEntry(int32_t fileIndex){	if (diskOpEntry != NULL && !diskOpEntryIsEmpty(fileIndex))	{		UNICHAR *filenameU = diskOpEntry[diskop.scrollOffset+fileIndex].nameU;		if (filenameU != NULL)		{			unicharToAnsi(fileNameBuffer, filenameU, PATH_MAX);			return fileNameBuffer;		}	} 	return NULL;} UNICHAR *diskOpGetUnicodeEntry(int32_t fileIndex){	if (diskOpEntry != NULL && !diskOpEntryIsEmpty(fileIndex))		return diskOpEntry[diskop.scrollOffset+fileIndex].nameU; 	return NULL;} static void setVisualPathToCwd(void){	memset(editor.currPath, 0, PATH_MAX + 10);	memset(editor.currPathU, 0, (PATH_MAX + 2) * sizeof (UNICHAR));	UNICHAR_GETCWD(editor.currPathU, PATH_MAX); 	if (diskop.mode == DISKOP_MODE_MOD)		UNICHAR_STRCPY(editor.modulesPathU, editor.currPathU);	else		UNICHAR_STRCPY(editor.samplesPathU, editor.currPathU); 	unicharToAnsi(editor.currPath, editor.currPathU, PATH_MAX);	ui.updateDiskOpPathText = true;} bool changePathToDesktop(void){#ifdef _WIN32	UNICHAR pathU[PATH_MAX + 2];	if (SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, pathU) >= 0)	{		if (UNICHAR_CHDIR(pathU) == 0)			return true;	} 	return false;#else	char *homePath = getenv("HOME");	if (homePath != NULL && chdir(homePath) == 0)	{		chdir("Desktop"); // keep home dir in case we couldn't change the dir to desktop		return true;	} 	return false;#endif} #ifndef _WIN32bool changePathToHome(void){	char *homePath = getenv("HOME");	if (homePath != NULL && chdir(homePath) == 0)		return true; 	return false;}#endif void setPathFromDiskOpMode(void){	UNICHAR_CHDIR((diskop.mode == DISKOP_MODE_MOD) ? editor.modulesPathU : editor.samplesPathU);	setVisualPathToCwd();} bool diskOpSetPath(UNICHAR *path, bool cache){	if (path != NULL && UNICHAR_CHDIR(path) == 0)	{		setVisualPathToCwd(); 		if (cache)			diskop.cached = false; 		if (ui.diskOpScreenShown)			ui.updateDiskOpFileList = true; 		diskop.scrollOffset = 0;		diskop.lastEntryJumpKey = SDLK_UNKNOWN; 		return true;	}	else	{		setVisualPathToCwd();		displayErrorMsg("CAN'T OPEN DIR !");		return false;	}} void diskOpSetInitPath(void){	// default module path	if (config.defModulesDir[0] != '\0')	{#ifdef _WIN32		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, config.defModulesDir, -1, pathTmp, PATH_MAX);#else		strcpy(pathTmp, config.defModulesDir);#endif		UNICHAR_STRCPY(editor.modulesPathU, pathTmp);	}	else	{		// no path set in config, set to current working directory		UNICHAR_GETCWD(editor.modulesPathU, PATH_MAX);	} 	// default sample path	if (config.defSamplesDir[0] != '\0')	{#ifdef _WIN32		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, config.defSamplesDir, -1, pathTmp, PATH_MAX);#else		strcpy(pathTmp, config.defSamplesDir);#endif		UNICHAR_STRCPY(editor.samplesPathU, pathTmp);	}	else

Lines 1–500 of 970.

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