zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_sysfilesystem.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #ifdef SDL_FILESYSTEM_WINDOWS
24 
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 /* System dependent filesystem routines */
27 
28 #include "../../core/windows/SDL_windows.h"
29 #include <shlobj.h>
30 
31 #include "SDL_assert.h"
32 #include "SDL_error.h"
33 #include "SDL_stdinc.h"
34 #include "SDL_filesystem.h"
35 
36 char *
37 SDL_GetBasePath(void)
38 {
39  TCHAR path[MAX_PATH];
40  const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
41  size_t i;
42 
43  SDL_assert(len < SDL_arraysize(path));
44 
45  if (len == 0) {
46  WIN_SetError("Couldn't locate our .exe");
47  return NULL;
48  }
49 
50  for (i = len-1; i > 0; i--) {
51  if (path[i] == '\\') {
52  break;
53  }
54  }
55 
56  SDL_assert(i > 0); /* Should have been an absolute path. */
57  path[i+1] = '\0'; /* chop off filename. */
58  return WIN_StringToUTF8(path);
59 }
60 
61 char *
62 SDL_GetPrefPath(const char *org, const char *app)
63 {
64  /*
65  * Vista and later has a new API for this, but SHGetFolderPath works there,
66  * and apparently just wraps the new API. This is the new way to do it:
67  *
68  * SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
69  * NULL, &wszPath);
70  */
71 
72  TCHAR path[MAX_PATH];
73  char *utf8 = NULL;
74  char *retval = NULL;
75 
76  if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
77  WIN_SetError("Couldn't locate our prefpath");
78  return NULL;
79  }
80 
81  utf8 = WIN_StringToUTF8(path);
82  if (utf8) {
83  const size_t len = SDL_strlen(utf8) + SDL_strlen(org) + SDL_strlen(app) + 4;
84  retval = (char *) SDL_malloc(len);
85  if (!retval) {
86  SDL_free(utf8);
88  return NULL;
89  }
90  SDL_snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
91  SDL_free(utf8);
92  }
93 
94  return retval;
95 }
96 
97 #endif /* SDL_FILESYSTEM_WINDOWS */
98 
99 /* vi: set ts=4 sw=4 expandtab: */
#define NULL
Definition: ftobjs.h:61
DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt,...)
Definition: SDL_string.c:1277
DECLSPEC void SDLCALL SDL_free(void *mem)
DECLSPEC char *SDLCALL SDL_GetPrefPath(const char *org, const char *app)
Get the user-and-app-specific path where files can be written.
GLenum GLsizei len
Definition: glew.h:7035
GLsizei const GLchar *const * path
Definition: glew.h:5828
#define WIN_StringToUTF8(S)
Definition: SDL_windows.h:41
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
#define SDL_assert(condition)
Definition: SDL_assert.h:159
DECLSPEC size_t SDLCALL SDL_strlen(const char *str)
Definition: SDL_string.c:389
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:83
int WIN_SetError(const char *prefix)
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
Include file for filesystem SDL API functions.
int i
Definition: pngrutil.c:1377
DECLSPEC char *SDLCALL SDL_GetBasePath(void)
Get the path where the application resides.