forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirs.c
170 lines (149 loc) · 4.68 KB
/
dirs.c
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
/*****************************************************************************
* dirs.c: directories configuration
*****************************************************************************
* Copyright (C) 2001-2010 VLC authors and VideoLAN
* Copyright © 2007-2012 Rémi Denis-Courmont
*
* Authors: Gildas Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <vlc_common.h>
#ifdef __MINGW32__
# include <w32api.h>
#endif
#include <direct.h>
#include <shlobj.h>
#include "../libvlc.h"
#include <vlc_charset.h>
#include <vlc_configuration.h>
#include "config/configuration.h"
#include <assert.h>
#include <limits.h>
char *config_GetLibDir (void)
{
/* Get our full path */
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQuery (config_GetLibDir, &mbi, sizeof(mbi)))
goto error;
wchar_t wpath[MAX_PATH];
if (!GetModuleFileName ((HMODULE) mbi.AllocationBase, wpath, MAX_PATH))
goto error;
wchar_t *file = wcsrchr (wpath, L'\\');
if (file == NULL)
goto error;
*file = L'\0';
return FromWide (wpath);
error:
abort ();
}
static char *config_GetDataDir(void)
{
const char *path = getenv ("VLC_DATA_PATH");
return (path != NULL) ? strdup (path) : config_GetLibDir ();
}
char *config_GetSysPath(vlc_sysdir_t type, const char *filename)
{
char *dir = NULL;
switch (type)
{
case VLC_PKG_DATA_DIR:
dir = config_GetDataDir();
break;
case VLC_PKG_LIB_DIR:
case VLC_PKG_LIBEXEC_DIR:
dir = config_GetLibDir();
break;
case VLC_SYSDATA_DIR:
break;
case VLC_LOCALE_DIR:
dir = config_GetSysPath(VLC_PKG_DATA_DIR, "locale");
break;
default:
vlc_assert_unreachable();
}
if (filename == NULL || unlikely(dir == NULL))
return dir;
char *path;
if (unlikely(asprintf(&path, "%s\\%s", dir, filename) == -1))
path = NULL;
free(dir);
return path;
}
static char *config_GetShellDir (int csidl)
{
wchar_t wdir[MAX_PATH];
if (SHGetFolderPathW (NULL, csidl | CSIDL_FLAG_CREATE,
NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
return FromWide (wdir);
return NULL;
}
static char *config_GetAppDir (void)
{
/* if portable directory exists, use it */
WCHAR path[MAX_PATH];
if (GetModuleFileName (NULL, path, MAX_PATH))
{
WCHAR *lastDir = wcsrchr (path, TEXT('\\'));
if (lastDir)
{
wcscpy (lastDir + 1, TEXT("portable"));
DWORD attrib = GetFileAttributes (path);
if (attrib != INVALID_FILE_ATTRIBUTES &&
(attrib & FILE_ATTRIBUTE_DIRECTORY))
return FromWide (path);
}
}
char *psz_dir;
char *psz_parent = config_GetShellDir (CSIDL_APPDATA);
if (psz_parent == NULL
|| asprintf (&psz_dir, "%s\\vlc", psz_parent) == -1)
psz_dir = NULL;
free (psz_parent);
return psz_dir;
}
#warning FIXME Use known folders on Vista and above
char *config_GetUserDir (vlc_userdir_t type)
{
switch (type)
{
case VLC_HOME_DIR:
return config_GetShellDir (CSIDL_PERSONAL);
case VLC_CONFIG_DIR:
case VLC_USERDATA_DIR:
return config_GetAppDir ();
case VLC_CACHE_DIR:
return config_GetAppDir ();
case VLC_DESKTOP_DIR:
case VLC_DOWNLOAD_DIR:
case VLC_TEMPLATES_DIR:
case VLC_PUBLICSHARE_DIR:
case VLC_DOCUMENTS_DIR:
return config_GetUserDir(VLC_HOME_DIR);
case VLC_MUSIC_DIR:
return config_GetShellDir (CSIDL_MYMUSIC);
case VLC_PICTURES_DIR:
return config_GetShellDir (CSIDL_MYPICTURES);
case VLC_VIDEOS_DIR:
return config_GetShellDir (CSIDL_MYVIDEO);
}
vlc_assert_unreachable ();
}