avi subtitle stream dumper
[mplayer/greg.git] / get_path.c
blobbde2b79fc23c70e9efa5b47c574dfc49d134f48b
2 /*
3 * Get path to config dir/file.
5 * Return Values:
6 * Returns the pointer to the ALLOCATED buffer containing the
7 * zero terminated path string. This buffer has to be FREED
8 * by the caller.
11 #ifdef MACOSX_BUNDLE
12 #include <CoreFoundation/CoreFoundation.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #endif
21 char *get_path(char *filename){
22 char *homedir;
23 char *buff;
24 #if defined(__MINGW32__)
25 static char *config_dir = "/mplayer";
26 #else
27 static char *config_dir = "/.mplayer";
28 #endif
29 int len;
30 #ifdef MACOSX_BUNDLE
31 struct stat dummy;
32 CFIndex maxlen=256;
33 CFURLRef res_url_ref=NULL;
34 CFURLRef bdl_url_ref=NULL;
35 char *res_url_path = NULL;
36 char *bdl_url_path = NULL;
37 #endif
39 if ((homedir = getenv("HOME")) == NULL)
40 #if defined(__MINGW32__)||defined(__CYGWIN__) /*hack to get fonts etc. loaded outside of cygwin environment*/
42 int i,imax=0;
43 char exedir[260];
44 GetModuleFileNameA(NULL, exedir, 260);
45 for(i=0; i< strlen(exedir);i++)if(exedir[i] =='\\'){exedir[i]='/';imax=i;}
46 exedir[imax]='\0';
47 homedir = exedir;
49 #else
50 return NULL;
51 #endif
52 len = strlen(homedir) + strlen(config_dir) + 1;
53 if (filename == NULL) {
54 if ((buff = (char *) malloc(len)) == NULL)
55 return NULL;
56 sprintf(buff, "%s%s", homedir, config_dir);
57 } else {
58 len += strlen(filename) + 1;
59 if ((buff = (char *) malloc(len)) == NULL)
60 return NULL;
61 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
64 #ifdef MACOSX_BUNDLE
65 if(stat(buff, &dummy)) {
67 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
68 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
70 if(res_url_ref&&bdl_url_ref) {
72 res_url_path=malloc(maxlen);
73 bdl_url_path=malloc(maxlen);
75 while(!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
76 maxlen*=2;
77 res_url_path=realloc(res_url_path, maxlen);
79 CFRelease(res_url_ref);
81 while(!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
82 maxlen*=2;
83 bdl_url_path=realloc(bdl_url_path, maxlen);
85 CFRelease(bdl_url_ref);
87 if( strcmp(res_url_path, bdl_url_path) == 0)
88 res_url_path = NULL;
91 if(res_url_path&&filename) {
92 if((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
93 maxlen=strlen(filename)+strlen(res_url_path)+2;
95 free(buff);
96 buff = (char *) malloc(maxlen);
97 strcpy(buff, res_url_path);
99 strcat(buff,"/");
100 strcat(buff, filename);
103 #endif
104 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
105 return buff;