unix.c
changeset 2334 8986c80be5be
parent 2323 eadae7d611e7
child 2338 8fadf1e5c5ea
equal deleted inserted replaced
2333:2adba268db9f 2334:8986c80be5be
   101 		strcpy(fios->name, "..");
   101 		strcpy(fios->name, "..");
   102 		strcpy(fios->title, ".. (Parent directory)");
   102 		strcpy(fios->title, ".. (Parent directory)");
   103 	}
   103 	}
   104 
   104 
   105 	// Show subdirectories first
   105 	// Show subdirectories first
   106 	dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/");
   106 	dir = opendir(_fios_path);
   107 	if (dir != NULL) {
   107 	if (dir != NULL) {
   108 		while ((dirent = readdir(dir)) != NULL) {
   108 		while ((dirent = readdir(dir)) != NULL) {
   109 			snprintf(filename, lengthof(filename), "%s/%s",
   109 			snprintf(filename, lengthof(filename), "%s/%s",
   110 				_fios_path, dirent->d_name);
   110 				_fios_path, dirent->d_name);
   111 			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
   111 			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
   136 	 * .SAV OpenTTD saved game
   136 	 * .SAV OpenTTD saved game
   137 	 * .SS1 Transport Tycoon Deluxe preset game
   137 	 * .SS1 Transport Tycoon Deluxe preset game
   138 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
   138 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
   139 	 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
   139 	 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
   140 	 */
   140 	 */
   141 	dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/");
   141 	dir = opendir(_fios_path);
   142 	if (dir != NULL) {
   142 	if (dir != NULL) {
   143 		while ((dirent = readdir(dir)) != NULL) {
   143 		while ((dirent = readdir(dir)) != NULL) {
   144 			char *t;
   144 			char *t;
   145 
   145 
   146 			snprintf(filename, lengthof(filename), "%s/%s",
   146 			snprintf(filename, lengthof(filename), "%s/%s",
   191 
   191 
   192 	if (_fios_scn_path == NULL) {
   192 	if (_fios_scn_path == NULL) {
   193 		_fios_scn_path = malloc(MAX_PATH);
   193 		_fios_scn_path = malloc(MAX_PATH);
   194 		strcpy(_fios_scn_path, _path.scenario_dir);
   194 		strcpy(_fios_scn_path, _path.scenario_dir);
   195 	}
   195 	}
       
   196 
   196 	_fios_path = _fios_scn_path;
   197 	_fios_path = _fios_scn_path;
   197 
   198 
   198 	// Parent directory, only if not of the type C:\.
   199 	// Parent directory, only if not of the type C:\.
   199 	if (_fios_path[1] != '\0' && mode != SLD_NEW_GAME) {
   200 	if (_fios_path[1] != '\0' && mode != SLD_NEW_GAME) {
   200 		fios = FiosAlloc();
   201 		fios = FiosAlloc();
   202 		fios->mtime = 0;
   203 		fios->mtime = 0;
   203 		strcpy(fios->title, ".. (Parent directory)");
   204 		strcpy(fios->title, ".. (Parent directory)");
   204 	}
   205 	}
   205 
   206 
   206 	// Show subdirectories first
   207 	// Show subdirectories first
   207 	dir = opendir(_fios_path[0] ? _fios_path : "/");
   208 	dir = opendir(_fios_path);
   208 	if (dir != NULL) {
   209 	if (dir != NULL) {
   209 		while ((dirent = readdir(dir)) != NULL) {
   210 		while ((dirent = readdir(dir)) != NULL) {
   210 			snprintf(filename, lengthof(filename), "%s/%s",
   211 			snprintf(filename, lengthof(filename), "%s/%s",
   211 				_fios_path, dirent->d_name);
   212 				_fios_path, dirent->d_name);
   212 			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
   213 			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
   219 			}
   220 			}
   220 		}
   221 		}
   221 		closedir(dir);
   222 		closedir(dir);
   222 	}
   223 	}
   223 
   224 
       
   225 	{
       
   226 		/* XXX ugly global variables ... */
       
   227 		byte order = _savegame_sort_order;
       
   228 		_savegame_sort_order = 2; // sort ascending by name
       
   229 		qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
       
   230 		_savegame_sort_order = order;
       
   231 	}
       
   232 
   224 	// this is where to start sorting
   233 	// this is where to start sorting
   225 	sort_start = _fios_count;
   234 	sort_start = _fios_count;
   226 
   235 
   227 	/* Show scenario files
   236 	/* Show scenario files
   228 	 * .SCN OpenTTD style scenario file
   237 	 * .SCN OpenTTD style scenario file
   229 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
   238 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
   230 	 * .SS0 Transport Tycoon Deluxe preset scenario
   239 	 * .SS0 Transport Tycoon Deluxe preset scenario
   231 	 */
   240 	 */
   232 	dir = opendir(_fios_path[0] ? _fios_path : "/");
   241 	dir = opendir(_fios_path);
   233 	if (dir != NULL) {
   242 	if (dir != NULL) {
   234 		while ((dirent = readdir(dir)) != NULL) {
   243 		while ((dirent = readdir(dir)) != NULL) {
   235 			char *t;
   244 			char *t;
   236 
   245 
   237 			snprintf(filename, lengthof(filename), "%s/%s", _fios_path, dirent->d_name);
   246 			snprintf(filename, lengthof(filename), "%s/%s", _fios_path, dirent->d_name);
   242 				fios = FiosAlloc();
   251 				fios = FiosAlloc();
   243 				fios->type = FIOS_TYPE_SCENARIO;
   252 				fios->type = FIOS_TYPE_SCENARIO;
   244 				fios->mtime = sb.st_mtime;
   253 				fios->mtime = sb.st_mtime;
   245 				ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
   254 				ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
   246 
   255 
   247 				*t  = '\0'; // strip extension
   256 				*t = '\0'; // strip extension
   248 				ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title));
   257 				ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title));
   249 			} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
   258 			} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
   250 					mode == SLD_NEW_GAME) {
   259 					mode == SLD_NEW_GAME) {
   251 				if (t != NULL && (
   260 				if (t != NULL && (
   252 							strcasecmp(t, ".sv0") == 0 ||
   261 							strcasecmp(t, ".sv0") == 0 ||
   284 	char *s;
   293 	char *s;
   285 
   294 
   286 	switch (item->type) {
   295 	switch (item->type) {
   287 		case FIOS_TYPE_PARENT:
   296 		case FIOS_TYPE_PARENT:
   288 			s = strrchr(path, '/');
   297 			s = strrchr(path, '/');
   289 			if (s != NULL) *s = '\0';
   298 			if (s != path)
       
   299 				s[0] = '\0';
       
   300 			else
       
   301 				s[1] = '\0';
   290 			break;
   302 			break;
   291 
   303 
   292 		case FIOS_TYPE_DIR:
   304 		case FIOS_TYPE_DIR:
   293 			s = strchr(item->name, '/');
   305 			if (path[1] != '\0') strcat(path, "/");
   294 			if (s != NULL) *s = '\0';
       
   295 			strcat(path, "/");
       
   296 			strcat(path, item->name);
   306 			strcat(path, item->name);
   297 			break;
   307 			break;
   298 
   308 
   299 		case FIOS_TYPE_FILE:
   309 		case FIOS_TYPE_FILE:
   300 		case FIOS_TYPE_OLDFILE:
   310 		case FIOS_TYPE_OLDFILE: