video: remove lowres support, cut "too slow" message
[mplayer.git] / path.c
blobe51c4c9c345f9413fbca72dbc0f3525208c69c1d
1 /*
2 * Get path to config dir/file.
4 * Return Values:
5 * Returns the pointer to the ALLOCATED buffer containing the
6 * zero terminated path string. This buffer has to be FREED
7 * by the caller.
9 * This file is part of MPlayer.
11 * MPlayer is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * MPlayer is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include "config.h"
34 #include "mp_msg.h"
35 #include "path.h"
37 #ifdef CONFIG_MACOSX_BUNDLE
38 #include <CoreFoundation/CoreFoundation.h>
39 #include <unistd.h>
40 #elif defined(__MINGW32__)
41 #include <windows.h>
42 #elif defined(__CYGWIN__)
43 #include <windows.h>
44 #include <sys/cygwin.h>
45 #endif
47 #include "talloc.h"
49 #include "osdep/io.h"
51 char *get_path(const char *filename){
52 char *homedir;
53 char *buff;
54 #ifdef __MINGW32__
55 static char *config_dir = "/mplayer";
56 #else
57 static char *config_dir = "/.mplayer";
58 #endif
59 #if defined(__MINGW32__) || defined(__CYGWIN__)
60 char exedir[260];
61 #endif
62 int len;
63 #ifdef CONFIG_MACOSX_BUNDLE
64 struct stat dummy;
65 CFIndex maxlen=256;
66 CFURLRef res_url_ref=NULL;
67 CFURLRef bdl_url_ref=NULL;
68 char *res_url_path = NULL;
69 char *bdl_url_path = NULL;
70 #endif
72 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
73 config_dir = "";
74 else if ((homedir = getenv("HOME")) == NULL)
75 #if defined(__MINGW32__) || defined(__CYGWIN__)
76 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
78 int i,imax=0;
79 len = (int)GetModuleFileNameA(NULL, exedir, 260);
80 for (i=0; i < len; i++)
81 if (exedir[i] =='\\')
82 {exedir[i]='/'; imax=i;}
83 exedir[imax]='\0';
84 homedir = exedir;
86 #else
87 return NULL;
88 #endif
89 len = strlen(homedir) + strlen(config_dir) + 1;
90 if (filename == NULL) {
91 if ((buff = malloc(len)) == NULL)
92 return NULL;
93 sprintf(buff, "%s%s", homedir, config_dir);
94 } else {
95 len += strlen(filename) + 1;
96 if ((buff = malloc(len)) == NULL)
97 return NULL;
98 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
101 #ifdef CONFIG_MACOSX_BUNDLE
102 if (stat(buff, &dummy)) {
104 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
105 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
107 if (res_url_ref&&bdl_url_ref) {
109 res_url_path=malloc(maxlen);
110 bdl_url_path=malloc(maxlen);
112 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
113 maxlen*=2;
114 res_url_path=realloc(res_url_path, maxlen);
116 CFRelease(res_url_ref);
118 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
119 maxlen*=2;
120 bdl_url_path=realloc(bdl_url_path, maxlen);
122 CFRelease(bdl_url_ref);
124 if (strcmp(res_url_path, bdl_url_path) == 0)
125 res_url_path = NULL;
128 if (res_url_path&&filename) {
129 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
130 maxlen=strlen(filename)+strlen(res_url_path)+2;
132 free(buff);
133 buff = malloc(maxlen);
134 strcpy(buff, res_url_path);
136 strcat(buff,"/");
137 strcat(buff, filename);
140 #endif
141 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
142 return buff;
145 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
146 void set_path_env(void)
148 /*make our codec dirs available for LoadLibraryA()*/
149 char win32path[MAX_PATH];
150 #ifdef __CYGWIN__
151 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
152 #else /*__CYGWIN__*/
153 /* Expand to absolute path unless it's already absolute */
154 if (!strstr(BINARY_CODECS_PATH,":") && BINARY_CODECS_PATH[0] != '\\') {
155 GetModuleFileNameA(NULL, win32path, MAX_PATH);
156 strcpy(strrchr(win32path, '\\') + 1, BINARY_CODECS_PATH);
158 else strcpy(win32path, BINARY_CODECS_PATH);
159 #endif /*__CYGWIN__*/
160 mp_msg(MSGT_WIN32, MSGL_V, "Setting PATH to %s\n", win32path);
161 if (!SetEnvironmentVariableA("PATH", win32path))
162 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
164 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
166 char *codec_path = BINARY_CODECS_PATH;
168 char *mp_basename(const char *path)
170 char *s;
172 #if HAVE_DOS_PATHS
173 s = strrchr(path, '\\');
174 if (s)
175 path = s + 1;
176 s = strrchr(path, ':');
177 if (s)
178 path = s + 1;
179 #endif
180 s = strrchr(path, '/');
181 return s ? s + 1 : (char *)path;
184 struct bstr mp_dirname(const char *path)
186 struct bstr ret = {(uint8_t *)path, mp_basename(path) - path};
187 if (ret.len == 0)
188 return bstr(".");
189 return ret;
192 char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
194 if (p1.len == 0)
195 return bstrdup0(talloc_ctx, p2);
196 if (p2.len == 0)
197 return bstrdup0(talloc_ctx, p1);
199 #if HAVE_DOS_PATHS
200 if (p2.len >= 2 && p2.start[1] == ':'
201 || p2.start[0] == '\\' || p2.start[0] == '/')
202 #else
203 if (p2.start[0] == '/')
204 #endif
205 return bstrdup0(talloc_ctx, p2); // absolute path
207 bool have_separator;
208 int endchar1 = p1.start[p1.len - 1];
209 #if HAVE_DOS_PATHS
210 have_separator = endchar1 == '/' || endchar1 == '\\'
211 || p1.len == 2 && endchar1 == ':'; // "X:" only
212 #else
213 have_separator = endchar1 == '/';
214 #endif
216 return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
217 have_separator ? "" : "/", BSTR_P(p2));
220 bool mp_path_exists(const char *path)
222 struct stat st;
223 return mp_stat(path, &st) == 0;
226 bool mp_path_isdir(const char *path)
228 struct stat st;
229 return mp_stat(path, &st) == 0 && S_ISDIR(st.st_mode);