summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2005-03-27 12:48:25 +0000
committertron <tron@openttd.org>2005-03-27 12:48:25 +0000
commit002dd026f0bc396b5fbdb9553e0c7f834a001fa9 (patch)
tree752c36cae7c2c9932025977b24a8a8fdc38e12ba
parent79530aa7fe26c673dc7c7a0ea757d1e24629670a (diff)
downloadopenttd-002dd026f0bc396b5fbdb9553e0c7f834a001fa9.tar.xz
(svn r2085) Improve browsing via console:
- change directory/load map via filename (number is also possible) - add command "pwd" to Print the current Working Directory - rename "list_files" to "ls" (and add alias "dir") - rename "goto_dir" to "cd" - loading of TTD maps via "load" is now possible - switching of drives via "cd" (only applies to systems which have a drive concept) is now possible - "scan_files" is now obsolete and was removed This is still far from perfect, mostly because the way OTTD handles files is Broken By Design(tm), but should make file navigation via console a bit easier
-rw-r--r--console_cmds.c189
1 files changed, 82 insertions, 107 deletions
diff --git a/console_cmds.c b/console_cmds.c
index e3184892c..e8ddcb051 100644
--- a/console_cmds.c
+++ b/console_cmds.c
@@ -174,152 +174,127 @@ DEF_CONSOLE_CMD(ConSave)
return NULL;
}
-/* Load a file-number from current dir */
-static void LoadMap(uint no)
+
+static const FiosItem* GetFiosItem(const char* file)
{
- /* Build file list */
- BuildFileList();
+ int i;
- /* Check if in range */
- if (no != 0 && no <= (uint)_fios_num) {
- const FiosItem *item = &_fios_list[no - 1];
+ BuildFileList();
- if (item->type == FIOS_TYPE_FILE) {
- /* Load the file */
- _switch_mode = SM_LOAD;
- SetFiosType(item->type);
- strcpy(_file_to_saveload.name, FiosBrowseTo(item));
+ for (i = 0; i < _fios_num; i++) {
+ if (strcmp(file, _fios_list[i].name) == 0) break;
+ }
- IConsolePrint(_iconsole_color_default, "Loading map...");
- } else
- IConsolePrint(_iconsole_color_error, "That is not a map.");
+ if (i == _fios_num) { /* If no name matches, try to parse it as number */
+ char* endptr;
- } else /* Show usages */
- IConsolePrint(_iconsole_color_error, "Unknown map. Use 'list_files' and 'goto_dir' to find the numbers of the savegame.");
+ i = strtol(file, &endptr, 10);
+ if (file == endptr || *endptr != '\0') i = -1;
+ }
- /* Free the file-list */
- FiosFreeSavegameList();
+ return IS_INT_INSIDE(i, 0, _fios_num) ? &_fios_list[i] : NULL;
}
-/* Load a file from a map */
+
DEF_CONSOLE_CMD(ConLoad)
{
- /* We need 1 argument */
- if (argc == 2) {
- /* Load the map */
- LoadMap(atoi(argv[1]));
+ const FiosItem* item;
+ const char* file;
+
+ if (argc != 2) {
+ IConsolePrint(_iconsole_color_default, "Usage: load <file | number>");
return NULL;
}
- /* Give usage */
- IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: load <file-no>");
- return NULL;
-}
-
-/* List all the files in the current dir via console */
-DEF_CONSOLE_CMD(ConListFiles)
-{
- const FiosItem *item;
- int pos = 0;
-
- /* Build the file-list */
- BuildFileList();
+ file = argv[1];
+ item = GetFiosItem(file);
+ if (item != NULL) {
+ switch (item->type) {
+ case FIOS_TYPE_FILE:
+ case FIOS_TYPE_OLDFILE:
+ _switch_mode = SM_LOAD;
+ SetFiosType(item->type);
+ strcpy(_file_to_saveload.name, FiosBrowseTo(item));
+ break;
- /* As long as we have files */
- while (pos < _fios_num) {
- item = _fios_list + pos;
- pos++;
- /* Show them */
- IConsolePrintF(_iconsole_color_default, "%d) %s", pos, item->title[0] ? item->title : item->name);
+ default:
+ IConsolePrintF(_iconsole_color_error, "%s: Not a map.", file);
+ break;
+ }
+ } else {
+ IConsolePrintF(_iconsole_color_error, "%s: No such file or directory.",
+ file);
}
- /* Destroy the file list */
FiosFreeSavegameList();
-
return NULL;
}
-/* Get an Specific file */
-DEF_CONSOLE_CMD(ConScanFiles)
-{
- const FiosItem *item;
- int pos = 0;
- _iconsole_var* result;
+/* List all the files in the current dir via console */
+DEF_CONSOLE_CMD(ConListFiles)
+{
+ int i;
- result = IConsoleVarAlloc(ICONSOLE_VAR_STRING);
-
- if (argc <= 1) {
- IConsoleVarSetString(result, "0");
- return result; // return an zero
- }
-
- /* Build the file-list */
BuildFileList();
- /* As long as we have files */
- while (pos < _fios_num) {
- item = _fios_list + pos;
- pos++;
- if (strcmp(argv[1], "..") == 0) {
- if (item->type == FIOS_TYPE_PARENT) {
- // huh we are searching for the parent directory
- char buffer[10];
- sprintf(buffer, "%d", pos);
- IConsoleVarSetString(result, buffer);
- return result;
- }
- } else
- // file records ?
- if (item->type == FIOS_TYPE_FILE) {
- if (strcmp(argv[1], item->name) == 0) {
- char buffer[10];
- sprintf(buffer, "%d", pos);
- IConsoleVarSetString(result, buffer);
- return result;
- }
- }
+ for (i = 0; i < _fios_num; i++) {
+ const FiosItem* item = &_fios_list[i];
+
+ IConsolePrintF(_iconsole_color_default, "%d) %s",
+ i, item->title[0] != '\0' ? item->title : item->name);
}
- /* Destroy the file list */
FiosFreeSavegameList();
-
return NULL;
}
+
/* Change the dir via console */
-DEF_CONSOLE_CMD(ConGotoDir)
+DEF_CONSOLE_CMD(ConChangeDirectory)
{
- char *file;
- int no;
+ const FiosItem* item;
+ const char* file;
- /* We need 1 argument */
if (argc != 2) {
- IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: goto_dir <dir-no>");
+ IConsolePrint(_iconsole_color_default, "Usage: cd <directory | number>");
return NULL;
}
- no = atoi(argv[1]);
-
- /* Make the file list */
- BuildFileList();
+ file = argv[1];
+ item = GetFiosItem(file);
+ if (item != NULL) {
+ switch (item->type) {
+ case FIOS_TYPE_DIR:
+ case FIOS_TYPE_DRIVE:
+ case FIOS_TYPE_PARENT:
+ FiosBrowseTo(item);
+ break;
- /* Check if we are in range */
- if (no != 0 && no <= _fios_num) {
- const FiosItem *item = &_fios_list[no - 1];
- /* Only DIR and PARENT we do allow here */
- if (item->type == FIOS_TYPE_DIR || item->type == FIOS_TYPE_PARENT) {
- /* Goto the map */
- file = FiosBrowseTo(item);
- FiosFreeSavegameList();
- return NULL;
+ default:
+ IConsolePrintF(_iconsole_color_error, "%s: Not a directory.", file);
+ break;
}
+ } else {
+ IConsolePrintF(_iconsole_color_error, "%s: No such file or directory.",
+ file);
}
- /* Report error */
- IConsolePrint(_iconsole_color_default, "That number is no directory.");
+ FiosFreeSavegameList();
+ return NULL;
+}
+
+
+DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
+{
+ const char* path;
+
+ // XXX Workaround for broken file handling
+ FiosGetSavegameList(&_fios_num, SLD_LOAD_GAME);
FiosFreeSavegameList();
+ FiosGetDescText(&path);
+ IConsolePrint(_iconsole_color_default, path);
return NULL;
}
@@ -1270,13 +1245,13 @@ void IConsoleStdLibRegister(void)
IConsoleCmdRegister("alias", ConAlias);
IConsoleCmdRegister("load", ConLoad);
IConsoleCmdRegister("save", ConSave);
- IConsoleCmdRegister("list_files", ConListFiles);
- IConsoleCmdRegister("scan_files", ConScanFiles);
- IConsoleCmdRegister("goto_dir", ConGotoDir);
+ IConsoleCmdRegister("ls", ConListFiles);
+ IConsoleCmdRegister("cd", ConChangeDirectory);
+ IConsoleCmdRegister("pwd", ConPrintWorkingDirectory);
+ IConsoleAliasRegister("dir", "ls");
IConsoleAliasRegister("new_game", "newgame");
IConsoleAliasRegister("newmap", "newgame");
IConsoleAliasRegister("new_map", "newgame");
- IConsoleAliasRegister("load_game", "temp_string << scan_files %!;load temp_string");
IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE);