comparison mac/dw.m @ 1298:79db0563c7c2

Code to split the file and path apart on Mac... Also fixed issues with how we construct the folder URL.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 01 Nov 2011 13:05:26 +0000
parents 40d62db77c90
children 7527fb644bc0
comparison
equal deleted inserted replaced
1297:b8f6e7dd4544 1298:79db0563c7c2
3124 * the file path on success. 3124 * the file path on success.
3125 * 3125 *
3126 */ 3126 */
3127 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags) 3127 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags)
3128 { 3128 {
3129 char temp[PATH_MAX+1];
3130 char *file = NULL, *path = NULL;
3131
3132 /* Figure out path information */
3133 if(defpath)
3134 {
3135 struct stat buf;
3136
3137 /* Get an absolute path */
3138 if(!realpath(defpath, temp))
3139 strcpy(temp, defpath);
3140
3141 /* Check if the defpath exists */
3142 if(stat(temp, &buf) == 0)
3143 {
3144 /* Can be a directory or file */
3145 if(buf.st_mode & S_IFDIR)
3146 path = temp;
3147 else
3148 file = temp;
3149 }
3150 /* If it wasn't a directory... check if there is a path */
3151 if(!path && strchr(temp, '/'))
3152 {
3153 unsigned long x = strlen(temp) - 1;
3154
3155 /* Trim off the filename */
3156 while(x > 0 && temp[x] != '/')
3157 {
3158 x--;
3159 }
3160 if(temp[x] == '/')
3161 {
3162 temp[x] = 0;
3163 /* Check to make sure the trimmed piece is a directory */
3164 if(stat(temp, &buf) == 0)
3165 {
3166 if(buf.st_mode & S_IFDIR)
3167 {
3168 /* We now have it split */
3169 path = temp;
3170 file = &temp[x+1];
3171 }
3172 }
3173 }
3174 }
3175 }
3176
3129 if(flags == DW_FILE_OPEN || flags == DW_DIRECTORY_OPEN) 3177 if(flags == DW_FILE_OPEN || flags == DW_DIRECTORY_OPEN)
3130 { 3178 {
3131 /* Create the File Open Dialog class. */ 3179 /* Create the File Open Dialog class. */
3132 NSOpenPanel* openDlg = [NSOpenPanel openPanel]; 3180 NSOpenPanel* openDlg = [NSOpenPanel openPanel];
3133 struct stat buf;
3134 3181
3135 if(defpath && stat(defpath, &buf) == 0) 3182 if(path)
3136 { 3183 [openDlg setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:path]]];
3137 if(buf.st_mode & S_IFDIR)
3138 [openDlg setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]];
3139 }
3140 3184
3141 /* Enable the selection of files in the dialog. */ 3185 /* Enable the selection of files in the dialog. */
3142 if(flags == DW_FILE_OPEN) 3186 if(flags == DW_FILE_OPEN)
3143 { 3187 {
3144 [openDlg setCanChooseFiles:YES]; 3188 [openDlg setCanChooseFiles:YES];
3176 } 3220 }
3177 else 3221 else
3178 { 3222 {
3179 /* Create the File Save Dialog class. */ 3223 /* Create the File Save Dialog class. */
3180 NSSavePanel* saveDlg = [NSSavePanel savePanel]; 3224 NSSavePanel* saveDlg = [NSSavePanel savePanel];
3181 struct stat buf;
3182 3225
3183 if(defpath && stat(defpath, &buf) == 0) 3226 if(path)
3184 { 3227 [saveDlg setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:path]]];
3185 if(buf.st_mode & S_IFDIR) 3228 if(file)
3186 [saveDlg setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]]; 3229 [saveDlg setNameFieldStringValue:[NSString stringWithUTF8String:file]];
3187 else
3188 [saveDlg setNameFieldStringValue:[NSString stringWithUTF8String:defpath]];
3189 }
3190 3230
3191 /* Enable the creation of directories in the dialog. */ 3231 /* Enable the creation of directories in the dialog. */
3192 [saveDlg setCanCreateDirectories:YES]; 3232 [saveDlg setCanCreateDirectories:YES];
3193 3233
3194 /* Handle file types */ 3234 /* Handle file types */