1 /* 2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 10 #include "Arguments.h" 11 12 13 Arguments::Arguments(int defaultArgsNum, const char * const *defaultArgs) 14 : fUsageRequested(false), 15 fBounds(50, 50, 630, 435), 16 fStandardShell(true), 17 fFullScreen(false), 18 fShellArgumentCount(0), 19 fShellArguments(NULL), 20 fTitle(NULL) 21 { 22 _SetShellArguments(defaultArgsNum, defaultArgs); 23 } 24 25 26 Arguments::~Arguments() 27 { 28 _SetShellArguments(0, NULL); 29 } 30 31 32 void 33 Arguments::Parse(int argc, const char *const *argv) 34 { 35 int argi = 1; 36 while (argi < argc) { 37 const char *arg = argv[argi++]; 38 39 if (*arg == '-') { 40 if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { 41 fUsageRequested = true; 42 43 /*} else if (strcmp(arg, "-l") == 0) { 44 // location 45 float x, y; 46 if (argi + 1 >= argc 47 || sscanf(argv[argi++], "%f", &x) != 1 48 || sscanf(argv[argi++], "%f", &y) != 1) { 49 print_usage_and_exit(true); 50 } 51 52 fBounds.OffsetTo(x, y); 53 54 } else if (strcmp(arg, "-s") == 0) { 55 // size 56 float width, height; 57 if (argi + 1 >= argc 58 || sscanf(argv[argi++], "%f", &width) != 1 59 || sscanf(argv[argi++], "%f", &height) != 1) { 60 print_usage_and_exit(true); 61 } 62 63 fBounds.right = fBounds.left + width; 64 fBounds.bottom = fBounds.top + height; 65 */ 66 } else if (strcmp(arg, "-t") == 0 || strcmp(arg, "--title") == 0) { 67 // title 68 if (argi >= argc) 69 fUsageRequested = true; 70 else 71 fTitle = argv[argi++]; 72 73 } else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fullscreen") == 0) { 74 fFullScreen = true; 75 argi++; 76 } else { 77 // illegal option 78 fprintf(stderr, "Unrecognized option \"%s\"\n", arg); 79 fUsageRequested = true; 80 } 81 82 } else { 83 // no option, so the remainder is the shell program with arguments 84 _SetShellArguments(argc - argi + 1, argv + argi - 1); 85 argi = argc; 86 fStandardShell = false; 87 } 88 } 89 } 90 91 92 void 93 Arguments::GetShellArguments(int &argc, const char *const *&argv) const 94 { 95 argc = fShellArgumentCount; 96 argv = fShellArguments; 97 } 98 99 100 void 101 Arguments::_SetShellArguments(int argc, const char *const *argv) 102 { 103 // delete old arguments 104 for (int32 i = 0; i < fShellArgumentCount; i++) 105 free((void *)fShellArguments[i]); 106 delete[] fShellArguments; 107 108 fShellArguments = NULL; 109 fShellArgumentCount = 0; 110 111 // copy new ones 112 if (argc > 0 && argv) { 113 fShellArguments = new const char*[argc + 1]; 114 for (int i = 0; i < argc; i++) 115 fShellArguments[i] = strdup(argv[i]); 116 117 fShellArguments[argc] = NULL; 118 fShellArgumentCount = argc; 119 } 120 } 121 122