argon.h
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           ARGON               --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V0.1B  15/09/10
00005  *
00006  *
00007  *  File: argon.h
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2010, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 
00037 #ifndef ARGON_H_
00038 #define ARGON_H_
00039 
00040 #define ARGO_MAX_SWITCH 10
00041 
00042 int argo_addInt(int * var, char* sw, int dfl, int need_value);
00043 int argo_addDouble(double * var, char* sw, double dfl);
00044 int argo_addString(char * var, char* sw, char * dfl);
00045 void argo_setComment(char * sw, char * text);
00046 void argo_setCommentId(int id, char * text);
00047 void argo_doProcess(int argc, char * argv[], int start);
00048 
00049 /* CODE >>>>>>> */
00050 
00051 #include "config/compiler.h"
00052 
00053 #define ARGON_INT 0
00054 #define ARGON_STRING  1
00055 #define ARGON_DOUBLE  2
00056 
00057 typedef struct {
00058         char sw[16];
00059         char type;
00060         void * var;
00061         int need_value;
00062         char comment[64];
00063 } argo_data_t;
00064 
00065 static struct {
00066         argo_data_t at[ARGO_MAX_SWITCH];
00067         char idx;
00068 } regs;
00069 
00070 int argo_addInt(int * var, char* sw, int dfl, int need_value) {
00071         *var = dfl;
00072         regs.at[(int)regs.idx].var = (void*) var;
00073         strcpy(regs.at[(int)regs.idx].sw, sw);
00074         regs.at[(int)regs.idx].type = ARGON_INT;
00075         regs.at[(int)regs.idx].need_value = need_value;
00076         sprintf(regs.at[(int)regs.idx].comment, "--%s: type int", sw);
00077         regs.idx++;
00078         return regs.idx - 1;
00079 }
00080 
00081 int argo_addDouble(double * var, char* sw, double dfl) {
00082         *var = dfl;
00083         regs.at[(int)regs.idx].var = (double*) var;
00084         strcpy(regs.at[(int)regs.idx].sw, sw);
00085         regs.at[(int)regs.idx].type = ARGON_DOUBLE;
00086         sprintf(regs.at[(int)regs.idx].comment, "--%s: type double", sw);
00087         regs.idx++;
00088         return regs.idx - 1;
00089 }
00090 
00091 int argo_addString(char * var, char* sw, char * dfl) {
00092         strcpy(var, dfl);
00093         regs.at[(int)regs.idx].var = (char*) var;
00094         strcpy(regs.at[(int)regs.idx].sw, sw);
00095         regs.at[(int)regs.idx].type = ARGON_STRING;
00096         sprintf(regs.at[(int)regs.idx].comment, "--%s: type string", sw);
00097         regs.idx++;
00098         return regs.idx - 1;
00099 }
00100 
00101 void argo_setComment(char * sw, char * text) {
00102         int i;
00103         for (i = 0; i < regs.idx; i++) {
00104                 if (strcmp(sw, regs.at[i].sw) == 0) {
00105                         sprintf(regs.at[i].comment, "-%s: %s", sw, text);
00106                 }
00107         }
00108 }
00109 
00110 void argo_setCommentId(int id, char * text){
00111         sprintf(regs.at[id].comment, "--%s: %s", regs.at[id].sw, text);
00112 }
00113 
00114 void argo_doProcess(int argc, char * argv[], int start) {
00115         int i, j;
00116    int done;
00117         int error = 0;
00118         for (i = start + 1; i < argc; i++) {
00119 
00120                 /* switch format */
00121                 if (argv[i][0] != '-' || (argv[i][1] != '-')) {
00122                         continue;
00123                 }
00124 
00125                 /* switch existence */
00126                 done = 0;
00127                 for (j = 0; j < regs.idx; j++) {
00128                         if (strcmp(&argv[i][2], "h") == 0) {
00129                                 error = 1;
00130                                 done = 1;
00131                                 break;
00132                         }
00133 
00134                         if (strcmp(&argv[i][2], regs.at[j].sw) == 0) {
00135                                 done = 1;
00136                                 if (regs.at[j].type == ARGON_INT) {
00137                                         if (regs.at[j].need_value) {
00138                                                 if (i == argc - 1) {
00139                                                         error = 4;
00140                                                         break;
00141                                                 } else {
00142                                                         *((int *) regs.at[j].var) = atoi(argv[i + 1]);
00143                                                         i++;
00144                                                 }
00145                                         } else {
00146                                                 *((int *) regs.at[j].var) = 1;
00147                                         }
00148                                 } else if (regs.at[j].type == ARGON_DOUBLE) {
00149                                         if (i == argc - 1) {
00150                                                 error = 4;
00151                                                 break;
00152                                         } else {
00153                   ATOF(argv[i + 1], ((double *) regs.at[j].var));
00154                                                 i++;
00155                                         }
00156                                 } else if (regs.at[j].type == ARGON_STRING) {
00157                                         if (i == argc - 1) {
00158                                                 error = 4;
00159                                                 break;
00160                                         } else {
00161                                                 strcpy(((char *) regs.at[j].var), argv[i + 1]);
00162                                                 i++;
00163                                         }
00164                                 }
00165                         }
00166                 }
00167 
00168                 if (!done) {
00169          WMP_ERROR(stderr, "*** Inexistent switch %s specified\n",argv[i]);
00170                         error = 3;
00171                         break;
00172                 }
00173 
00174         }
00175         if (error == 4) {
00176       WMP_ERROR(stderr, "*** Incorrect number of parameters\n");
00177         }
00178         if (error != 0) {
00179       WMP_ERROR(stderr, "List of switch:\n");
00180                 for (i = 0; i < regs.idx; i++) {
00181          WMP_ERROR(stderr, "%s\n", regs.at[i].comment);
00182                 }
00183 #ifndef __KERNEL__
00184                 exit(0);
00185 #else
00186       return;  // .................... ???????????????
00187 #endif
00188         }
00189 }
00190 #endif /* ARGON_H_ */


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:09