Complete.Org: Mailing Lists: Archives: freeciv-dev: July 1999:
[Freeciv-Dev] GTK options
Home

[Freeciv-Dev] GTK options

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] GTK options
From: Nicolas Brunel <brunel@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 11 Jul 1999 01:49:18 +0000 (GMT)

Hello,

  This is a little clean up of the gtk options. It's shorter. I removed
the code which put the unused arguments at the beginning of the array
argv[].I added some ligns to complain and dye if an unknown or badly formatted
option is found.

Best regards,

Nicolas,
Index: gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.c,v
retrieving revision 1.25
diff -u -r1.25 gui_main.c
--- gui_main.c  1999/07/03 04:41:29     1.25
+++ gui_main.c  1999/07/11 00:37:13
@@ -194,11 +194,40 @@
 }
 
 /**************************************************************************
+  return a char * to the parameter of the option or NULL.
+  *i can be increased to get next string in the array argv[]. 
+**************************************************************************/
+
+char * get_option(const char *option_name,char **argv,int *i)
+{
+  size_t len = strlen(option_name);
+  if (!strcmp(option_name,argv[*i]) || 
+      (!strncmp(option_name,argv[*i],len) &&
+       argv[*i][len]=='=') ||
+     !strncmp(option_name+1,argv[*i],2))
+     {
+     argv[*i] += (argv[*i][1] != '-' ? 0 : len); 
+     
+     if (argv[*i][0] == '=')
+       argv[*i]++;
+       else
+       (*i)++;
+       
+     check_arg(argv[*i], option_name, argv[0]);
+
+     return argv[*i];
+     }
+  
+  return NULL;
+}
+
+/**************************************************************************
 ...
 **************************************************************************/
-void parse_options(int *argc, char **argv[])
+void parse_options(int argc, char *argv[])
 {
   char *logfile=NULL;
+  char *option=NULL; 
   int loglevel;
 
   /* set default argument values */
@@ -223,156 +252,58 @@
 
   if (argc && argv)
   {
-    int i, j, k;
+    int i;
     
-    for (i = 1; i < *argc;)
+    for (i = 1; i < argc;)
     {
-      if (!strcmp ("--help", (*argv)[i]) ||
-              !strncmp ("-h", (*argv)[i], 2))
+      if (!strcmp ("--help", argv[i]) ||
+              !strncmp ("-h", argv[i], 2))
       {
-       print_usage((*argv)[0]);
+       print_usage(argv[0]);
        exit(0);
       }
-      else if (!strcmp ("--version", (*argv)[i]) ||
-              !strncmp ("-v", (*argv)[i], 2))
+      else if (!strcmp ("--version", argv[i]) ||
+              !strncmp ("-v", argv[i], 2))
       {
         fprintf(stderr, "%s\n", FREECIV_NAME_VERSION);
         exit(0);
       }
-      else if (!strcmp ("--log",  (*argv)[i]) ||
-              !strncmp ("--log=", (*argv)[i], 6) ||
-              !strncmp ("-l", (*argv)[i], 2))
+      else if ((option = get_option("--log",argv,&i)) != NULL)
       {
-       char *opt_log = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 5);
-       
-       if (*opt_log == '=')
-         opt_log++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_log = (*argv)[i];
-       }
-       check_arg(opt_log, "--log", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       logfile=opt_log;
+       logfile=option;
       }
-      else if (!strcmp ("--name",  (*argv)[i]) ||
-              !strncmp ("--name=", (*argv)[i], 7) ||
-              !strncmp ("-n", (*argv)[i], 2))
+      else if ((option = get_option("--name",argv,&i)) != NULL)
       {
-       char *opt_name = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 6);
-       
-       if (*opt_name == '=')
-         opt_name++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_name = (*argv)[i];
-       }
-       check_arg(opt_name, "--name", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       strcpy(name, opt_name);
+       strcpy(name, option);
       }
-      else if (!strcmp ("--port",  (*argv)[i]) ||
-              !strncmp ("--port=", (*argv)[i], 7) ||
-              !strncmp ("-p", (*argv)[i], 2))
+      else if ((option = get_option("--port",argv,&i)) != NULL)
       {
-       char *opt_port = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 6);
-       
-       if (*opt_port == '=')
-         opt_port++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_port = (*argv)[i];
-       }
-       check_arg(opt_port, "--port", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       server_port=atoi(opt_port);
+       server_port=atoi(option);
       }
-      else if (!strcmp ("--server",  (*argv)[i]) ||
-              !strncmp ("--server=", (*argv)[i], 9) ||
-              !strncmp ("-s", (*argv)[i], 2))
+      else if ((option = get_option("--server",argv,&i)) != NULL)
       {
-       char *opt_server = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 8);
-       
-       if (*opt_server == '=')
-         opt_server++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_server = (*argv)[i];
-       }
-       check_arg(opt_server, "--server", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       strcpy(server_host, opt_server);
+       strcpy(server_host, option);
       }
-      else if (!strcmp ("--debug",  (*argv)[i]) ||
-              !strncmp ("--debug=", (*argv)[i], 8) ||
-              !strncmp ("-d", (*argv)[i], 2))
+      else if ((option = get_option("--debug",argv,&i)) != NULL)
       {
-       char *opt_debug = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 7);
-       
-       if (*opt_debug == '=')
-         opt_debug++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_debug = (*argv)[i];
-       }
-       check_arg(opt_debug, "--debug", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       loglevel=log_parse_level_str(opt_debug);
+       loglevel=log_parse_level_str(option);
        if (loglevel==-1) {
          exit(1);
        }
       }
-      else if (!strcmp ("--tiles",  (*argv)[i]) ||
-              !strncmp ("--tiles=", (*argv)[i], 8) ||
-              !strncmp ("-t", (*argv)[i], 2))
+      else if ((option = get_option("--tiles",argv,&i)) != NULL)
       {
-       char *opt_tiles = (*argv)[i] + ((*argv)[i][1] != '-' ? 0 : 7);
-       
-       if (*opt_tiles == '=')
-         opt_tiles++;
-       else
-       {
-         (*argv)[i] = NULL;
-         i += 1;
-         opt_tiles = (*argv)[i];
-       }
-       check_arg(opt_tiles, "--tiles", (*argv)[0]);
-        (*argv)[i] = NULL;
-
-       tile_set_dir=opt_tiles;
+       tile_set_dir=option;
       }
+      else 
+        {
+       fprintf(stderr,"unrecognized option or wrong parameter %s.\n",argv[i]);
+        print_usage(argv[0]);
+       exit(1);
+       }
       i += 1;
     }
 
-    for (i = 1; i < *argc; i++)
-    {
-      for (k = i; k < *argc; k++)
-        if ((*argv)[k] != NULL)
-          break;
-  
-      if (k > i)
-      {
-        k -= i;
-        for (j = i + k; j < *argc; j++)
-          (*argv)[j-k] = (*argv)[j];
-        *argc -= k;
-      }
-    }
   }
   log_init(logfile, loglevel, NULL);
 }
@@ -743,7 +674,7 @@
 {
   GdkBitmap          *icon_bitmap;
 
-  parse_options(&argc, &argv);
+  parse_options(argc, argv);
 
   /* Process GTK arguments */
   gtk_init(&argc, &argv);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] GTK options, Nicolas Brunel <=