3 * Get path to config dir/file.
6 * Returns the pointer to the ALLOCATED buffer containing the
7 * zero terminated path string. This buffer has to be FREED
12 #include <CoreFoundation/CoreFoundation.h>
13 #include <sys/types.h>
21 char *get_path(char *filename
){
24 #if defined(__MINGW32__)
25 static char *config_dir
= "/mplayer";
27 static char *config_dir
= "/.mplayer";
33 CFURLRef res_url_ref
=NULL
;
34 CFURLRef bdl_url_ref
=NULL
;
35 char *res_url_path
= NULL
;
36 char *bdl_url_path
= NULL
;
39 if ((homedir
= getenv("HOME")) == NULL
)
40 #if defined(__MINGW32__)||defined(__CYGWIN__) /*hack to get fonts etc. loaded outside of cygwin environment*/
44 GetModuleFileNameA(NULL
, exedir
, 260);
45 for(i
=0; i
< strlen(exedir
);i
++)if(exedir
[i
] =='\\'){exedir
[i
]='/';imax
=i
;}
52 len
= strlen(homedir
) + strlen(config_dir
) + 1;
53 if (filename
== NULL
) {
54 if ((buff
= (char *) malloc(len
)) == NULL
)
56 sprintf(buff
, "%s%s", homedir
, config_dir
);
58 len
+= strlen(filename
) + 1;
59 if ((buff
= (char *) malloc(len
)) == NULL
)
61 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
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
)) {
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
)) {
83 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
85 CFRelease(bdl_url_ref
);
87 if( strcmp(res_url_path
, bdl_url_path
) == 0)
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;
96 buff
= (char *) malloc(maxlen
);
97 strcpy(buff
, res_url_path
);
100 strcat(buff
, filename
);
104 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);