mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-25 17:42:03 +08:00
testprograms: parse arguments using SDLTest_CommonState
This commit is contained in:
committed by
Anonymous Maarten
parent
8bea41f737
commit
4a6528e3f0
+50
-13
@@ -17,22 +17,67 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL_test.h>
|
||||
|
||||
typedef int (*fntype)(const char *);
|
||||
|
||||
static void log_usage(char *progname, SDLTest_CommonState *state) {
|
||||
static const char *options[] = { "library", "functionname|--hello", NULL };
|
||||
SDLTest_CommonLogUsage(state, progname, options);
|
||||
SDL_Log("USAGE: %s <library> <functionname>\n", progname);
|
||||
SDL_Log(" %s <lib with puts()> --hello\n", progname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int retval = 0;
|
||||
int hello = 0;
|
||||
const char *libname = NULL;
|
||||
const char *symname = NULL;
|
||||
void *lib = NULL;
|
||||
fntype fn = NULL;
|
||||
SDLTest_CommonState *state;
|
||||
|
||||
if (argc != 3) {
|
||||
const char *app = argv[0];
|
||||
SDL_Log("USAGE: %s <library> <functionname>\n", app);
|
||||
SDL_Log(" %s --hello <lib with puts()>\n", app);
|
||||
/* Initialize test framework */
|
||||
state = SDLTest_CommonCreateState(argv, 0);
|
||||
if (state == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Enable standard application logging */
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
/* Parse commandline */
|
||||
for (i = 1; i < argc;) {
|
||||
int consumed;
|
||||
|
||||
consumed = SDLTest_CommonArg(state, i);
|
||||
if (!consumed) {
|
||||
if (SDL_strcmp(argv[i], "--hello") == 0) {
|
||||
if (!symname || SDL_strcmp(symname, "puts") == 0) {
|
||||
symname = "puts";
|
||||
consumed = 1;
|
||||
hello = 1;
|
||||
}
|
||||
} else if (!libname) {
|
||||
libname = argv[i];
|
||||
consumed = 1;
|
||||
} else if (!symname) {
|
||||
symname = argv[i];
|
||||
consumed = 1;
|
||||
}
|
||||
}
|
||||
if (consumed <= 0) {
|
||||
log_usage(argv[0], state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
i += consumed;
|
||||
}
|
||||
|
||||
if (!libname || !symname) {
|
||||
log_usage(argv[0], state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -42,15 +87,6 @@ int main(int argc, char *argv[])
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (SDL_strcmp(argv[1], "--hello") == 0) {
|
||||
hello = 1;
|
||||
libname = argv[2];
|
||||
symname = "puts";
|
||||
} else {
|
||||
libname = argv[1];
|
||||
symname = argv[2];
|
||||
}
|
||||
|
||||
lib = SDL_LoadObject(libname);
|
||||
if (lib == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadObject('%s') failed: %s\n",
|
||||
@@ -74,5 +110,6 @@ int main(int argc, char *argv[])
|
||||
SDL_UnloadObject(lib);
|
||||
}
|
||||
SDL_Quit();
|
||||
SDLTest_CommonDestroyState(state);
|
||||
return retval;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user