From f3a0332bc3a290b612f3e8251944a2233b0e0705 Mon Sep 17 00:00:00 2001 From: zeromus Date: Fri, 29 May 2009 08:27:03 +0000 Subject: [PATCH] i wish i couldve done it right the first time --- desmume/src/commandline.cpp | 91 +++++++++++++++++++++++++++++++++++++ desmume/src/commandline.h | 60 ++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 desmume/src/commandline.cpp create mode 100644 desmume/src/commandline.h diff --git a/desmume/src/commandline.cpp b/desmume/src/commandline.cpp new file mode 100644 index 000000000..e74ef3d6b --- /dev/null +++ b/desmume/src/commandline.cpp @@ -0,0 +1,91 @@ +/* commandline.cpp + + Copyright (C) 2009 DeSmuME team + + This file is part of DeSmuME + + DeSmuME is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DeSmuME is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DeSmuME; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#include +#include "commandline.h" + +CommandLine::CommandLine() +: error(NULL) +, ctx(g_option_context_new ("")) +{ + load_slot = 0; +} + +CommandLine::~CommandLine() +{ + if(error) g_error_free (error); + g_option_context_free (ctx); +} + +void CommandLine::loadCommonOptions() +{ + //these options should be available in every port. + //my advice is, do not be afraid of using #ifdef here if it makes sense. + //but also see the gtk port for an example of how to combine this with other options + //(you may need to use ifdefs to cause options to be entered in the desired order) + static const GOptionEntry options[] = { + { "load-slot", 0, 0, G_OPTION_ARG_INT, &load_slot, "Loads savegame from slot NUM", "NUM"}, + { NULL } + }; + + g_option_context_add_main_entries (ctx, options, "options"); +} + +bool CommandLine::parse(int argc,char **argv) +{ + g_option_context_parse (ctx, &argc, &argv, &error); + if (error) + { + g_printerr("Error parsing command line arguments: %s\n", error->message); + return false; + } + + if (argc == 2) + nds_file = argv[1]; + if (argc > 2) + return false; + + return true; +} + +bool CommandLine::validate() +{ + if (load_slot < 0 || load_slot > 10) { + g_printerr("I only know how to load from slots 1-10, 0 means 'do not load savegame' and is default\n"); + return false; + } + + return true; +} + +void CommandLine::errorHelp(const char* binName) +{ + //TODO - strip this down to just the filename + g_printerr("USAGE: %s [options] [nds-file]\n", binName); + g_printerr("USAGE: %s --help - for help\n", binName); +} + +void foo() +{ + g_option_context_free(NULL); +} diff --git a/desmume/src/commandline.h b/desmume/src/commandline.h new file mode 100644 index 000000000..9bc574aa5 --- /dev/null +++ b/desmume/src/commandline.h @@ -0,0 +1,60 @@ +/* commandline.h + + Copyright (C) 2009 DeSmuME team + + This file is part of DeSmuME + + DeSmuME is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DeSmuME is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DeSmuME; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _COMMANDLINE_H_ +#define _COMMANDLINE_H_ + +#include + +//I hate C. we have to forward declare these with more detail than I like +typedef struct _GOptionContext GOptionContext; +typedef struct _GError GError; + +//this class will also eventually try to take over the responsibility of using the args that it handles +//for example: preparing the emulator run by loading the rom, savestate, and/or movie in the correct pattern. +//it should also populate CommonSettings with its initial values + +class CommandLine +{ +public: + //actual options: these may move to another sturct + int load_slot; + std::string nds_file; + + //load up the common commandline options + void loadCommonOptions(); + + bool parse(int argc,char **argv); + + //validate the common commandline options + bool validate(); + + //print a little help message for cases when erroneous commandlines are entered + void errorHelp(const char* binName); + + CommandLine(); + ~CommandLine(); + + GOptionContext *ctx; + GError *error; +}; + +#endif