src/fios.cpp
branchNewGRF_ports
changeset 6719 4cc327ad39d5
parent 6577 ee768d653dd0
child 6720 35756db7e577
equal deleted inserted replaced
6718:5a8b295aa345 6719:4cc327ad39d5
    55 }
    55 }
    56 
    56 
    57 /**
    57 /**
    58  * Compare two FiosItem's. Used with qsort when sorting the file list.
    58  * Compare two FiosItem's. Used with qsort when sorting the file list.
    59  * @param a A pointer to the first FiosItem to compare.
    59  * @param a A pointer to the first FiosItem to compare.
    60  * @param a A pointer to the second FiosItem to compare.
    60  * @param b A pointer to the second FiosItem to compare.
    61  * @return -1, 0 or 1, depending on how the two items should be sorted.
    61  * @return -1, 0 or 1, depending on how the two items should be sorted.
    62  */
    62  */
    63 int CDECL compare_FiosItems(const void *a, const void *b)
    63 int CDECL compare_FiosItems(const void *a, const void *b)
    64 {
    64 {
    65 	const FiosItem *da = (const FiosItem *)a;
    65 	const FiosItem *da = (const FiosItem *)a;
   112 	case FIOS_TYPE_DRIVE: sprintf(path, "%c:" PATHSEP, item->title[0]); break;
   112 	case FIOS_TYPE_DRIVE: sprintf(path, "%c:" PATHSEP, item->title[0]); break;
   113 #endif
   113 #endif
   114 
   114 
   115 	case FIOS_TYPE_PARENT:
   115 	case FIOS_TYPE_PARENT:
   116 		/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
   116 		/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
   117 		if ((s = strrchr(path, PATHSEPCHAR)) != NULL) {
   117 		s = strrchr(path, PATHSEPCHAR);
   118 			s[1] = '\0'; // go up a directory
   118 		if (s != NULL && s != path) {
   119 			if (!FiosIsRoot(path)) s[0] = '\0';
   119 			s[0] = '\0'; // Remove last path separator character, so we can go up one level.
   120 		}
   120 		}
       
   121 		s = strrchr(path, PATHSEPCHAR);
       
   122 		if (s != NULL) s[1] = '\0'; // go up a directory
   121 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
   123 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
   122 		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
   124 		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
   123 		else if ((s = strrchr(path, ':')) != NULL) s[1] = '\0';
   125 		else if ((s = strrchr(path, ':')) != NULL) s[1] = '\0';
   124 #endif
   126 #endif
   125 		break;
   127 		break;
   126 
   128 
   127 	case FIOS_TYPE_DIR:
   129 	case FIOS_TYPE_DIR:
   128 		if (!FiosIsRoot(path)) strcat(path, PATHSEP);
       
   129 		strcat(path, item->name);
   130 		strcat(path, item->name);
       
   131 		strcat(path, PATHSEP);
   130 		break;
   132 		break;
   131 
   133 
   132 	case FIOS_TYPE_DIRECT:
   134 	case FIOS_TYPE_DIRECT:
   133 		sprintf(path, "%s" PATHSEP, item->name);
   135 		sprintf(path, "%s", item->name);
   134 		s = strrchr(path, PATHSEPCHAR);
       
   135 		if (s != NULL && s[1] == '\0') s[0] = '\0'; // strip trailing slash
       
   136 		break;
   136 		break;
   137 
   137 
   138 	case FIOS_TYPE_FILE:
   138 	case FIOS_TYPE_FILE:
   139 	case FIOS_TYPE_OLDFILE:
   139 	case FIOS_TYPE_OLDFILE:
   140 	case FIOS_TYPE_SCENARIO:
   140 	case FIOS_TYPE_SCENARIO:
   148 		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
   148 		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
   149 		if (FiosIsRoot(path)) {
   149 		if (FiosIsRoot(path)) {
   150 			snprintf(str_buffr, lengthof(str_buffr), "%s:%s", path, item->name);
   150 			snprintf(str_buffr, lengthof(str_buffr), "%s:%s", path, item->name);
   151 		} else // XXX - only next line!
   151 		} else // XXX - only next line!
   152 #endif
   152 #endif
   153 		snprintf(str_buffr, lengthof(str_buffr), "%s" PATHSEP "%s", path, item->name);
   153 		snprintf(str_buffr, lengthof(str_buffr), "%s%s", path, item->name);
   154 
   154 
   155 		return str_buffr;
   155 		return str_buffr;
   156 	}
   156 	}
   157 	}
   157 	}
   158 
   158 
   198 }
   198 }
   199 
   199 
   200 typedef byte fios_getlist_callback_proc(int mode, const char *filename, const char *ext, char *title);
   200 typedef byte fios_getlist_callback_proc(int mode, const char *filename, const char *ext, char *title);
   201 
   201 
   202 /** Create a list of the files in a directory, according to some arbitrary rule.
   202 /** Create a list of the files in a directory, according to some arbitrary rule.
   203  *  @param num Will be filled with the amount of items.
       
   204  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   203  *  @param mode The mode we are in. Some modes don't allow 'parent'.
   205  *  @param callback The function that is called where you need to do the filtering.
   204  *  @param callback_proc The function that is called where you need to do the filtering.
   206  *  @return Return the list of files. */
   205  *  @return Return the list of files. */
   207 static FiosItem *FiosGetFileList(int mode, fios_getlist_callback_proc *callback_proc)
   206 static FiosItem *FiosGetFileList(int mode, fios_getlist_callback_proc *callback_proc)
   208 {
   207 {
   209 	struct stat sb;
   208 	struct stat sb;
   210 	struct dirent *dirent;
   209 	struct dirent *dirent;