summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src/FileUtil.cpp
blob: 728628e9e7539ff109cc7f2ee6b940cb6d221b3b (plain)
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
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include "Common.h"
#include "FileUtil.h"
#include "StringUtil.h"

#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>		// for SHGetFolderPath
#include <shellapi.h>
#include <commdlg.h>	// for GetSaveFileName
#include <io.h>
#include <direct.h>		// getcwd
#else
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#endif

#if defined(__APPLE__)
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFBundle.h>
#include <sys/param.h>
#endif

#include <fstream>
#include <sys/stat.h>

#ifndef S_ISDIR
#define S_ISDIR(m)  (((m)&S_IFMT) == S_IFDIR)
#endif

// This namespace has various generic functions related to files and paths.
// The code still needs a ton of cleanup.
// REMEMBER: strdup considered harmful!
namespace File
{

// Remove any ending forward slashes from directory paths
// Modifies argument.
static char *StripTailDirSlashes(char *fname)
{
	int len = (int)strlen(fname);
	int i = len - 1;
	if (len > 1)
		while (fname[i] == DIR_SEP_CHR)
			fname[i--] = '\0';
	return fname;
}

// Returns true if file filename exists
bool Exists(const char *filename)
{
	struct stat64 file_info;
		
	char *copy = StripTailDirSlashes(__strdup(filename));
	int result = stat64(copy, &file_info);
	free(copy);

	return (result == 0);
}

// Returns true if filename is a directory
bool IsDirectory(const char *filename)
{
	struct stat64 file_info;

	char *copy = StripTailDirSlashes(__strdup(filename));

	int result = stat64(copy, &file_info);
	free(copy);

	if (result < 0) {
		WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s", 
				 filename, GetLastErrorMsg());
		return false;
	}

	return S_ISDIR(file_info.st_mode);
}

// Deletes a given filename, return true on success
// Doesn't supports deleting a directory
bool Delete(const char *filename)
{
	INFO_LOG(COMMON, "Delete: file %s", filename);

	// Return true because we care about the file no 
	// being there, not the actual delete.
	if (!Exists(filename)) {
		WARN_LOG(COMMON, "Delete: %s does not exists", filename);
		return true;
	}

	// We can't delete a directory
	if (IsDirectory(filename)) {
		WARN_LOG(COMMON, "Delete: %s is a directory", filename);
		return false;
	}

#ifdef _WIN32
	if (!DeleteFile(filename)) {
		WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", 
				 filename, GetLastErrorMsg());
		return false;
	}
#else
	if (unlink(filename) == -1) {
		WARN_LOG(COMMON, "Delete: unlink failed on %s: %s", 
				 filename, GetLastErrorMsg());
		return false;
	}
#endif

	return true;
}

// Returns true if successful, or path already exists.
bool CreateDir(const char *path)
{
	INFO_LOG(COMMON, "CreateDir: directory %s", path);
#ifdef _WIN32
	if (::CreateDirectory(path, NULL))
		return true;
	DWORD error = GetLastError();
	if (error == ERROR_ALREADY_EXISTS) 	{
		WARN_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: already exists", path);
		return true;
	}
	ERROR_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: %i", path, error);
	return false;
#else
	if (mkdir(path, 0755) == 0)
		return true;

	int err = errno;

	if (err == EEXIST) {
		WARN_LOG(COMMON, "CreateDir: mkdir failed on %s: already exists", path);
		return true;
	}

	ERROR_LOG(COMMON, "CreateDir: mkdir failed on %s: %s", path, strerror(err));
	return false;
#endif
}

// Creates the full path of fullPath returns true on success
bool CreateFullPath(const char *fullPath)
{
	int panicCounter = 100;
	INFO_LOG(COMMON, "CreateFullPath: path %s", fullPath);
		
	if (File::Exists(fullPath)) {
		INFO_LOG(COMMON, "CreateFullPath: path exists %s", fullPath);
		return true;
	}

	// safety check to ensure we have good dir seperators
	std::string strFullPath(fullPath);
	NormalizeDirSep(&strFullPath);

	const char *position = strFullPath.c_str();
	while (true) {
		// Find next sub path
		position = strchr(position, DIR_SEP_CHR);

		// we're done, yay!
		if (! position)
			return true;
			
		position++;

		// Create next sub path
		int sLen = (int)(position - strFullPath.c_str());
		if (sLen > 0) {
			char *subPath = strndup(strFullPath.c_str(), sLen);
			if (!File::IsDirectory(subPath)) {
				File::CreateDir(subPath);
			}
			free(subPath);
		}

		// A safety check
		panicCounter--;
		if (panicCounter <= 0) {
			ERROR_LOG(COMMON, "CreateFullPath: directory structure too deep");
			return false;
		}
	}
}


// Deletes a directory filename, returns true on success
bool DeleteDir(const char *filename)
{
	INFO_LOG(COMMON, "DeleteDir: directory %s", filename);

	// check if a directory
	if (!File::IsDirectory(filename)) {
		ERROR_LOG(COMMON, "DeleteDir: Not a directory %s",
				  filename);
		return false;
	}

#ifdef _WIN32
	if (::RemoveDirectory(filename))
		return true;
#else
	if (rmdir(filename) == 0)
		return true;
#endif
	ERROR_LOG(COMMON, "DeleteDir: %s: %s",
			  filename, GetLastErrorMsg());

	return false;
}

// renames file srcFilename to destFilename, returns true on success 
bool Rename(const char *srcFilename, const char *destFilename)
{
	INFO_LOG(COMMON, "Rename: %s --> %s", 
			 srcFilename, destFilename);
	if (rename(srcFilename, destFilename) == 0)
		return true;
	ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s", 
			  srcFilename, destFilename, GetLastErrorMsg());
	return false;
}

// copies file srcFilename to destFilename, returns true on success 
bool Copy(const char *srcFilename, const char *destFilename)
{
	INFO_LOG(COMMON, "Copy: %s --> %s", 
			 srcFilename, destFilename);
#ifdef _WIN32
	if (CopyFile(srcFilename, destFilename, FALSE))
		return true;
		
	ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", 
			  srcFilename, destFilename, GetLastErrorMsg());
	return false;
#else

	// buffer size
#define BSIZE 1024

	char buffer[BSIZE];

	// Open input file
	FILE *input = fopen(srcFilename, "rb");
	if (!input)
	{
		ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s", 
				  srcFilename, destFilename, GetLastErrorMsg());
		return false;
	}

	// open output file
	FILE *output = fopen(destFilename, "wb");
	if (!output)
	{
		fclose(input);
		ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s", 
				  srcFilename, destFilename, GetLastErrorMsg());
		return false;
	}

	// copy loop
	while (!feof(input))
	{
		// read input
		int rnum = fread(buffer, sizeof(char), BSIZE, input);
		if (rnum != BSIZE)
		{
			if (ferror(input) != 0) {
				ERROR_LOG(COMMON, 
						  "Copy: failed reading from source, %s --> %s: %s", 
						  srcFilename, destFilename, GetLastErrorMsg());
				return false;
			}
		}

		// write output
		int wnum = fwrite(buffer, sizeof(char), rnum, output);
		if (wnum != rnum)
		{
			ERROR_LOG(COMMON, 
					  "Copy: failed writing to output, %s --> %s: %s", 
					  srcFilename, destFilename, GetLastErrorMsg());
			return false;
		}
	}
	// close flushs
	fclose(input);
	fclose(output);
	return true;
#endif
}

// Returns the size of filename (64bit)
u64 GetSize(const char *filename)
{
	if (!Exists(filename)) {
		WARN_LOG(COMMON, "GetSize: failed %s: No such file"
				 ,filename);
		return 0;
	}

	if (IsDirectory(filename)) {
		WARN_LOG(COMMON, "GetSize: failed %s: is a directory"
				 ,filename);
		return 0;
	}
	// on windows it's actually _stat64 defined in commonFuncs
	struct stat64 buf;
	if (stat64(filename, &buf) == 0) {
		DEBUG_LOG(COMMON, "GetSize: %s: %ld", filename, buf.st_size);
		return buf.st_size;
	}

	ERROR_LOG(COMMON, "GetSize: Stat failed %s: %s",
			  filename, GetLastErrorMsg());
	return 0;
}

// creates an empty file filename, returns true on success 
bool CreateEmptyFile(const char *filename)
{
	INFO_LOG(COMMON, "CreateEmptyFile: %s", filename); 

	FILE *pFile = fopen(filename, "wb");
	if (!pFile) {
		ERROR_LOG(COMMON, "CreateEmptyFile: failed %s: %s",
				  filename, GetLastErrorMsg());
		return false;
	}
	fclose(pFile);
	return true;
}


// Scans the directory tree gets, starting from _Directory and adds the
// results into parentEntry. Returns the number of files+directories found
u32 ScanDirectoryTree(const char *directory, FSTEntry& parentEntry)
{
	INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory);
	// How many files + directories we found
	u32 foundEntries = 0;
	char *virtualName;
#ifdef _WIN32
	// Find the first file in the directory.
	WIN32_FIND_DATA ffd;
	char searchName[MAX_PATH + 3];
	strncpy(searchName, directory, MAX_PATH);
	strcat(searchName, "\\*");

	HANDLE hFind = FindFirstFile(searchName, &ffd);
	if (hFind == INVALID_HANDLE_VALUE) {
		FindClose(hFind);
		return foundEntries;
	}
	// windows loop
	do {
		FSTEntry entry;
		virtualName = ffd.cFileName;
#else
	struct dirent dirent, *result = NULL;
	
	DIR *dirp = opendir(directory);
	if (!dirp)
		return 0;

	// non windows loop
	while (!readdir_r(dirp, &dirent, &result) && result) {
		FSTEntry entry;
		virtualName = result->d_name;
#endif
		// check for "." and ".."
		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
			((virtualName[0] == '.') && (virtualName[1] == '.') && 
			 (virtualName[2] == '\0')))
			continue;
		entry.virtualName = virtualName;
		entry.physicalName = directory;
		entry.physicalName += DIR_SEP + entry.virtualName;
		
		if (IsDirectory(entry.physicalName.c_str())) {
			entry.isDirectory = true;
			// is a directory, lets go inside
			entry.size = ScanDirectoryTree(entry.physicalName.c_str(), entry);
			foundEntries += (u32)entry.size;
		} else { // is a file 
			entry.isDirectory = false;
			entry.size = GetSize(entry.physicalName.c_str());
		}
		++foundEntries;
		// Push into the tree
		parentEntry.children.push_back(entry);		
#ifdef _WIN32 
	} while (FindNextFile(hFind, &ffd) != 0);
	FindClose(hFind);
#else
	}
	closedir(dirp);
#endif
	// Return number of entries found.
	return foundEntries;
}

	
// deletes the given directory and anything under it. Returns true on
// success.
bool DeleteDirRecursively(const char *directory)
{
	INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory);
#ifdef _WIN32
	// Find the first file in the directory.
	WIN32_FIND_DATA ffd;
	char searchName[MAX_PATH + 3] = {0};

	strncpy(searchName, directory, MAX_PATH);
	strcat(searchName, "\\*");

	HANDLE hFind = FindFirstFile(searchName, &ffd);

	if (hFind == INVALID_HANDLE_VALUE) {
		FindClose(hFind);
		return false;
	}
		
	// windows loop
	do {
		char *virtualName = ffd.cFileName;
#else
	struct dirent dirent, *result = NULL;
	DIR *dirp = opendir(directory);
	if (!dirp)
		return false;

	// non windows loop
	while (!readdir_r(dirp, &dirent, &result) && result) {
		char *virtualName = result->d_name;
#endif

		// check for "." and ".."
		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
			((virtualName[0] == '.') && (virtualName[1] == '.') && 
			 (virtualName[2] == '\0')))
			continue;

		char newPath[MAX_PATH];
		sprintf(newPath, "%s%c%s", directory, DIR_SEP_CHR, virtualName);
		if (IsDirectory(newPath)) {
			if (!DeleteDirRecursively(newPath))
				return false;
		} else {
			if (!File::Delete(newPath))
				return false;
		}

#ifdef _WIN32
	} while (FindNextFile(hFind, &ffd) != 0);
	FindClose(hFind);
#else
	}
	closedir(dirp);
#endif
	File::DeleteDir(directory);
		
	return true;
}

//Create directory and copy contents (does not overwrite existing files)
void CopyDir(const char *source_path, const char *dest_path)
{
#ifndef _WIN32
	if (!strcmp(source_path, dest_path)) return;
	if (!File::Exists(source_path)) return;
	if (!File::Exists(dest_path)) File::CreateFullPath(dest_path);

	char *virtualName;
	struct dirent dirent, *result = NULL;
	DIR *dirp = opendir(source_path);
	if (!dirp) return;

	while (!readdir_r(dirp, &dirent, &result) && result)
	{
		virtualName = result->d_name;
		// check for "." and ".."
		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
			((virtualName[0] == '.') && (virtualName[1] == '.') &&
			(virtualName[2] == '\0')))
			continue;

		char source[FILENAME_MAX], dest[FILENAME_MAX];
		sprintf(source, "%s%s", source_path, virtualName);
		sprintf(dest, "%s%s", dest_path, virtualName);
		if (IsDirectory(source))
		{
			const unsigned int srclen = strlen(source);
			const unsigned int destlen = strlen(dest);
			source[srclen] = '/'; source[srclen+1] = '\0';
			dest[destlen]  = '/'; dest[destlen+1]  = '\0';
			if (!File::Exists(dest)) File::CreateFullPath(dest);
			CopyDir(source, dest);
		}
		else if (!File::Exists(dest)) File::Copy(source, dest);
	}
	closedir(dirp);
#endif
}

// Returns the current directory
std::string GetCurrentDir()
{
	char *dir;
	// Get the current working directory (getcwd uses malloc) 
	if (!(dir = __getcwd(NULL, 0))) {

		ERROR_LOG(COMMON, "GetCurrentDirectory failed: %s",
				  GetLastErrorMsg());
		return NULL;
	}
	std::string strDir = dir;
	free(dir);
	return strDir;
}

// Sets the current directory to the given directory
bool SetCurrentDir(const char *_rDirectory)
{
	return __chdir(_rDirectory) == 0;
}

#if defined(__APPLE__)
std::string GetBundleDirectory() 
{
	CFURLRef BundleRef;
	char AppBundlePath[MAXPATHLEN];
	// Get the main bundle for the app
	BundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
	CFStringRef BundlePath = CFURLCopyFileSystemPath(BundleRef, kCFURLPOSIXPathStyle);
	CFStringGetFileSystemRepresentation(BundlePath, AppBundlePath, sizeof(AppBundlePath));
	CFRelease(BundleRef);
	CFRelease(BundlePath);
#if defined(HAVE_WX) && HAVE_WX
	return AppBundlePath;
#else
	std::string NoWxBundleDirectory;
	NoWxBundleDirectory=AppBundlePath;
	NoWxBundleDirectory+=DIR_SEP;
	NoWxBundleDirectory+="Dolphin.app";
	return NoWxBundleDirectory;
#endif
}
#endif

std::string GetPluginsDirectory()
{
	std::string pluginsDir;

#if defined (__APPLE__)
	pluginsDir = GetBundleDirectory();
	pluginsDir += DIR_SEP;
	pluginsDir += PLUGINS_DIR;
#elif defined __linux__
	pluginsDir = PLUGINS_DIR;
	// FIXME global install
#else
	pluginsDir = PLUGINS_DIR;	
#endif

	pluginsDir += DIR_SEP;

	INFO_LOG(COMMON, "GetPluginsDirectory: Setting to %s:", pluginsDir.c_str());
	return pluginsDir;
}

std::string GetSysDirectory()
{
	std::string sysDir;

#if defined (__APPLE__)
	sysDir = GetBundleDirectory();
	sysDir += DIR_SEP;
	sysDir += SYSDATA_DIR;
#elif defined __linux__
	sysDir = SYSDATA_DIR;
	// FIXME global install
#else
	sysDir = SYSDATA_DIR;	
#endif

	sysDir += DIR_SEP;
	INFO_LOG(COMMON, "GetSysDirectory: Setting to %s:", sysDir.c_str());
	return sysDir;
}

// Returns a pointer to a string with a Dolphin data dir or file in the user's home
// directory. To be used in "multi-user" mode (that is, installed).
const char *GetUserPath(int DirIDX)
{
	static char UserDir[MAX_PATH] = {0};
	static char GCUserDir[MAX_PATH] = {0};
	static char WiiUserDir[MAX_PATH] = {0};
	static char WiiRootDir[MAX_PATH] = {0};
	static char ConfigDir[MAX_PATH] = {0};
	static char GameConfigDir[MAX_PATH] = {0};
	static char MapsDir[MAX_PATH] = {0};
	static char CacheDir[MAX_PATH] = {0};
	static char ShaderCacheDir[MAX_PATH] = {0};
	static char ShadersDir[MAX_PATH] = {0};
	static char StateSavesDir[MAX_PATH] = {0};
	static char ScreenShotsDir[MAX_PATH] = {0};
	static char HiresTexturesDir[MAX_PATH] = {0};
	static char DumpDir[MAX_PATH] = {0};
	static char DumpFramesDir[MAX_PATH] = {0};
	static char DumpTexturesDir[MAX_PATH] = {0};
	static char DumpDSPDir[MAX_PATH] = {0};
	static char LogsDir[MAX_PATH] = {0};
	static char MailLogsDir[MAX_PATH] = {0};
	static char WiiSYSCONFDir[MAX_PATH] = {0};
	static char WiiMenuDir[MAX_PATH] = {0};
	static char DolphinConfig[MAX_PATH] = {0};
	static char DebuggerConfig[MAX_PATH] = {0};
	static char LoggerConfig[MAX_PATH] = {0};
	static char MainLog[MAX_PATH] = {0};
	static char WiiSYSCONF[MAX_PATH] = {0};
	static char RamDump[MAX_PATH] = {0};
	static char ARamDump[MAX_PATH] = {0};
	static char GCSRam[MAX_PATH] = {0};

	// Set up all paths and files on the first run
	if (strlen(UserDir) == 0)
	{
#ifdef _WIN32
		// Keep the directory setup the way it was on windows
		snprintf(UserDir, sizeof(UserDir), ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP);
#elif defined (__linux__)
		if (File::Exists(ROOT_DIR DIR_SEP USERDATA_DIR))
			snprintf(UserDir, sizeof(UserDir), ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP);
		else
			snprintf(UserDir, sizeof(UserDir), "%s" DIR_SEP DOLPHIN_DATA_DIR DIR_SEP, getenv("HOME"));
#else
		snprintf(UserDir, sizeof(UserDir), "%s" DIR_SEP DOLPHIN_DATA_DIR DIR_SEP, getenv("HOME"));
#endif
		INFO_LOG(COMMON, "GetUserPath: Setting user directory to %s:", UserDir);

		snprintf(GCUserDir, sizeof(GCUserDir), "%s" GC_USER_DIR DIR_SEP, UserDir);
		snprintf(WiiUserDir, sizeof(WiiUserDir), "%s" WII_USER_DIR DIR_SEP, UserDir);
		snprintf(WiiRootDir, sizeof(WiiRootDir), "%s" WII_USER_DIR, UserDir);
		snprintf(ConfigDir, sizeof(ConfigDir), "%s" CONFIG_DIR DIR_SEP, UserDir);
		snprintf(GameConfigDir, sizeof(GameConfigDir), "%s" GAMECONFIG_DIR DIR_SEP, UserDir);
		snprintf(MapsDir, sizeof(MapsDir), "%s" MAPS_DIR DIR_SEP, UserDir);
		snprintf(CacheDir, sizeof(CacheDir), "%s" CACHE_DIR DIR_SEP, UserDir);
		snprintf(ShaderCacheDir, sizeof(ShaderCacheDir), "%s" SHADERCACHE_DIR DIR_SEP, UserDir);
		snprintf(ShadersDir, sizeof(ShadersDir), "%s" SHADERS_DIR DIR_SEP, UserDir);
		snprintf(StateSavesDir, sizeof(StateSavesDir), "%s" STATESAVES_DIR DIR_SEP, UserDir);
		snprintf(ScreenShotsDir, sizeof(ScreenShotsDir), "%s" SCREENSHOTS_DIR DIR_SEP, UserDir);
		snprintf(HiresTexturesDir, sizeof(HiresTexturesDir), "%s" HIRES_TEXTURES_DIR DIR_SEP, UserDir);
		snprintf(DumpDir, sizeof(DumpDir), "%s" DUMP_DIR DIR_SEP, UserDir);
		snprintf(DumpFramesDir, sizeof(DumpFramesDir), "%s" DUMP_FRAMES_DIR DIR_SEP, UserDir);
		snprintf(DumpTexturesDir, sizeof(DumpTexturesDir), "%s" DUMP_TEXTURES_DIR DIR_SEP, UserDir);
		snprintf(DumpDSPDir, sizeof(DumpDSPDir), "%s" DUMP_DSP_DIR DIR_SEP, UserDir);
		snprintf(LogsDir, sizeof(LogsDir), "%s" LOGS_DIR DIR_SEP, UserDir);
		snprintf(MailLogsDir, sizeof(MailLogsDir), "%s" MAIL_LOGS_DIR DIR_SEP, UserDir);
		snprintf(WiiSYSCONFDir, sizeof(WiiSYSCONFDir), "%s" WII_SYSCONF_DIR DIR_SEP, UserDir);
		snprintf(WiiMenuDir, sizeof(WiiMenuDir), "%s" WII_MENU_DIR DIR_SEP, UserDir);
		snprintf(DolphinConfig, sizeof(DolphinConfig), "%s" DOLPHIN_CONFIG, ConfigDir);
		snprintf(DebuggerConfig, sizeof(DebuggerConfig), "%s" DEBUGGER_CONFIG, ConfigDir);
		snprintf(LoggerConfig, sizeof(LoggerConfig), "%s" LOGGER_CONFIG, ConfigDir);
		snprintf(MainLog, sizeof(MainLog), "%s" MAIN_LOG, LogsDir);
		snprintf(WiiSYSCONF, sizeof(WiiSYSCONF), "%s" WII_SYSCONF, WiiSYSCONFDir);
		snprintf(RamDump, sizeof(RamDump), "%s" RAM_DUMP, DumpDir);
		snprintf(ARamDump, sizeof(ARamDump), "%s" ARAM_DUMP, DumpDir);
		snprintf(GCSRam, sizeof(GCSRam), "%s" GC_SRAM, GCUserDir);
	}
	switch (DirIDX)
	{
		case D_USER_IDX:
			return UserDir;
		case D_GCUSER_IDX:
			return GCUserDir;
		case D_WIIUSER_IDX:
			return WiiUserDir;
		case D_WIIROOT_IDX:
			return WiiRootDir;
		case D_CONFIG_IDX:
			return ConfigDir;
		case D_GAMECONFIG_IDX:
			return GameConfigDir;
		case D_MAPS_IDX:
			return MapsDir;
		case D_CACHE_IDX:
			return CacheDir;
		case D_SHADERCACHE_IDX:
			return ShaderCacheDir;
		case D_SHADERS_IDX:
			return ShadersDir;
		case D_STATESAVES_IDX:
			return StateSavesDir;
		case D_SCREENSHOTS_IDX:
			return ScreenShotsDir;
		case D_HIRESTEXTURES_IDX:
			return HiresTexturesDir;
		case D_DUMP_IDX:
			return DumpDir;
		case D_DUMPFRAMES_IDX:
			return DumpFramesDir;
		case D_DUMPTEXTURES_IDX:
			return DumpTexturesDir;
		case D_DUMPDSP_IDX:
			return DumpDSPDir;
		case D_LOGS_IDX:
			return LogsDir;
		case D_MAILLOGS_IDX:
			return MailLogsDir;
		case D_WIISYSCONF_IDX:
			return WiiSYSCONFDir;
		case D_WIIMENU_IDX:
			return WiiMenuDir;
		case F_DOLPHINCONFIG_IDX:
			return DolphinConfig;
		case F_DEBUGGERCONFIG_IDX:
			return DebuggerConfig;
		case F_LOGGERCONFIG_IDX:
			return LoggerConfig;
		case F_MAINLOG_IDX:
			return MainLog;
		case F_WIISYSCONF_IDX:
			return WiiSYSCONF;
		case F_RAMDUMP_IDX:
			return RamDump;
		case F_ARAMDUMP_IDX:
			return ARamDump;
		case F_GCSRAM_IDX:
			return GCSRam;
		default:
			return NULL;
	}
}

bool WriteStringToFile(bool text_file, const std::string &str, const char *filename)
{
	FILE *f = fopen(filename, text_file ? "w" : "wb");
	if (!f)
		return false;
	size_t len = str.size();
	if (len != fwrite(str.data(), 1, str.size(), f))
	{
		fclose(f);
		return false;
	}
	fclose(f);
	return true;
}

bool ReadFileToString(bool text_file, const char *filename, std::string &str)
{
	FILE *f = fopen(filename, text_file ? "r" : "rb");
	if (!f)
		return false;
	fseek(f, 0, SEEK_END);
	size_t len = (size_t)ftell(f);
	fseek(f, 0, SEEK_SET);
	char *buf = new char[len + 1];
	buf[fread(buf, 1, len, f)] = 0;
	str = std::string(buf, len);
	fclose(f);
	delete [] buf;
	return true;
}

} // namespace