comparison mac/dw.m @ 1381:3ba4853d5b78

Initial attempt at dw_app_dir() for OS/2, Windows and Mac... This function will return the best attempt at determining the application data directory for the current platform. The executable directory on OS/2 or Windows. The application bundle directory on Mac. The "share" folder at the same level as the binary (or /usr/local/share if that can't be determined) plus the binary name on Unix.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 25 Nov 2011 17:36:59 +0000
parents a54720b10821
children ead81c3d9e4e
comparison
equal deleted inserted replaced
1380:cc66b6d4b74e 1381:3ba4853d5b78
119 pthread_key_t _dw_pool_key; 119 pthread_key_t _dw_pool_key;
120 #endif 120 #endif
121 pthread_key_t _dw_fg_color_key; 121 pthread_key_t _dw_fg_color_key;
122 pthread_key_t _dw_bg_color_key; 122 pthread_key_t _dw_bg_color_key;
123 SInt32 DWOSMajor, DWOSMinor, DWOSBuild; 123 SInt32 DWOSMajor, DWOSMinor, DWOSBuild;
124 static char _dw_bundle_path[PATH_MAX+1] = { 0 };
124 125
125 /* Create a default colors for a thread */ 126 /* Create a default colors for a thread */
126 void _init_colors(void) 127 void _init_colors(void)
127 { 128 {
128 NSColor *fgcolor = [[NSColor grayColor] retain]; 129 NSColor *fgcolor = [[NSColor grayColor] retain];
3030 * current user directory. Or the root directory (C:\ on 3031 * current user directory. Or the root directory (C:\ on
3031 * OS/2 and Windows). 3032 * OS/2 and Windows).
3032 */ 3033 */
3033 char *dw_user_dir(void) 3034 char *dw_user_dir(void)
3034 { 3035 {
3035 static char _user_dir[1024] = ""; 3036 static char _user_dir[PATH_MAX+1] = { 0 };
3036 3037
3037 if(!_user_dir[0]) 3038 if(!_user_dir[0])
3038 { 3039 {
3039 char *home = getenv("HOME"); 3040 char *home = getenv("HOME");
3040 3041
3041 if(home) 3042 if(home)
3042 strcpy(_user_dir, home); 3043 strncpy(_user_dir, home, PATH_MAX);
3043 else 3044 else
3044 strcpy(_user_dir, "/"); 3045 strcpy(_user_dir, "/");
3045 } 3046 }
3046 return _user_dir; 3047 return _user_dir;
3048 }
3049
3050 /*
3051 * Returns a pointer to a static buffer which containes the
3052 * private application data directory.
3053 */
3054 char *dw_app_dir(void)
3055 {
3056 return _dw_bundle_path;
3047 } 3057 }
3048 3058
3049 /* 3059 /*
3050 * Displays a debug message on the console... 3060 * Displays a debug message on the console...
3051 * Parameters: 3061 * Parameters:
9602 char *pathcopy = strdup(argv[0]); 9612 char *pathcopy = strdup(argv[0]);
9603 char *app = strstr(pathcopy, ".app/"); 9613 char *app = strstr(pathcopy, ".app/");
9604 9614
9605 if(app) 9615 if(app)
9606 { 9616 {
9607 char pathbuf[PATH_MAX]; 9617 char pathbuf[PATH_MAX+1] = { 0 };
9608 9618 size_t len = (size_t)(app - pathcopy);
9619
9620 if(len > 0)
9621 strncpy(_dw_bundle_path, pathcopy, len + 5);
9609 *app = 0; 9622 *app = 0;
9610 9623
9611 getcwd(pathbuf, PATH_MAX); 9624 getcwd(pathbuf, PATH_MAX);
9612 9625
9613 /* If run from a bundle the path seems to be / */ 9626 /* If run from a bundle the path seems to be / */
9614 if(strcmp(pathbuf, "/") == 0) 9627 if(strcmp(pathbuf, "/") == 0)
9615 { 9628 {
9616 size_t len; 9629 char *pos = strrchr(pathcopy, '/');
9617 9630
9618 strncpy(pathbuf, pathcopy, PATH_MAX); 9631 if(pos)
9619
9620 len = strlen(pathbuf);
9621
9622 while(len > 0 && pathbuf[len] != '/')
9623 { 9632 {
9624 len--; 9633 strncpy(pathbuf, pathcopy, (size_t)(pos - pathcopy));
9634 chdir(pathbuf);
9625 } 9635 }
9626 pathbuf[len] = '\0';
9627 chdir(pathbuf);
9628 } 9636 }
9629 } 9637 }
9638 }
9639 if(pathcopy)
9630 free(pathcopy); 9640 free(pathcopy);
9631 } 9641
9642 /* Just in case we can't obtain a path */
9643 if(!_dw_bundle_path[0])
9644 getcwd(_dw_bundle_path, PATH_MAX);
9645
9632 /* Get the operating system version */ 9646 /* Get the operating system version */
9633 Gestalt(gestaltSystemVersionMajor, &DWOSMajor); 9647 Gestalt(gestaltSystemVersionMajor, &DWOSMajor);
9634 Gestalt(gestaltSystemVersionMinor, &DWOSMinor); 9648 Gestalt(gestaltSystemVersionMinor, &DWOSMinor);
9635 Gestalt(gestaltSystemVersionBugFix, &DWOSBuild); 9649 Gestalt(gestaltSystemVersionBugFix, &DWOSBuild);
9636 /* Create the application object */ 9650 /* Create the application object */