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 #define ARGON_INT 0
00052 #define ARGON_STRING  1
00053 #define ARGON_DOUBLE  2
00054 #define WMP_ERROR fprintf
00055 #define STR strdup
00056 
00057 typedef struct {
00058         char sw[16];
00059         char type;
00060         void * var;
00061         int need_value;
00062         int mandatory;
00063         int set;
00064         char comment[64];
00065 } argo_data_t;
00066 
00067 static struct {
00068         argo_data_t at[ARGO_MAX_SWITCH];
00069         char idx;
00070 } regs;
00071 
00072 static char example[256];
00073 
00074 static void init(){
00075         static int initied = 0;
00076         if (!initied){
00077                 memset(&regs,0,sizeof(regs));
00078                 initied = 1;
00079                 example[0] = 0;
00080         }
00081 
00082 }
00083 
00084 int argo_addInt(int * var, char* sw, int dfl, int need_value) {
00085         init();
00086         *var = dfl;
00087         regs.at[(int)regs.idx].var = (void*) var;
00088         strcpy(regs.at[(int)regs.idx].sw, sw);
00089         regs.at[(int)regs.idx].type = ARGON_INT;
00090         regs.at[(int)regs.idx].need_value = need_value;
00091         sprintf(regs.at[(int)regs.idx].comment, "--%s: type int", sw);
00092         regs.idx++;
00093         return regs.idx - 1;
00094 }
00095 int argo_addIntMandatory(int * var, char* sw, int dfl, int need_value) {
00096         init();
00097         *var = dfl;
00098         regs.at[(int)regs.idx].var = (void*) var;
00099         strcpy(regs.at[(int)regs.idx].sw, sw);
00100         regs.at[(int)regs.idx].type = ARGON_INT;
00101         regs.at[(int)regs.idx].need_value = need_value;
00102         regs.at[(int)regs.idx].mandatory = 1;
00103         sprintf(regs.at[(int)regs.idx].comment, "--%s: type int", sw);
00104         regs.idx++;
00105         return regs.idx - 1;
00106 }
00107 int argo_addDouble(double * var, char* sw, double dfl) {
00108         init();
00109         *var = dfl;
00110         regs.at[(int)regs.idx].var = (double*) var;
00111         strcpy(regs.at[(int)regs.idx].sw, sw);
00112         regs.at[(int)regs.idx].type = ARGON_DOUBLE;
00113         sprintf(regs.at[(int)regs.idx].comment, "--%s: type double", sw);
00114         regs.idx++;
00115         return regs.idx - 1;
00116 }
00117 
00118 int argo_addString(char * var, char* sw, char * dfl) {
00119         init();
00120         strcpy(var, dfl);
00121         regs.at[(int)regs.idx].var = (char*) var;
00122         strcpy(regs.at[(int)regs.idx].sw, sw);
00123         regs.at[(int)regs.idx].type = ARGON_STRING;
00124         sprintf(regs.at[(int)regs.idx].comment, "--%s: type string", sw);
00125         regs.idx++;
00126         return regs.idx - 1;
00127 }
00128 
00129 void argo_setComment(char * sw, char * text) {
00130         int i;
00131         for (i = 0; i < regs.idx; i++) {
00132                 if (strcmp(sw, regs.at[i].sw) == 0) {
00133                         sprintf(regs.at[i].comment, "-%s: %s", sw, text);
00134                 }
00135         }
00136 }
00137 
00138 void argo_setCommentId(int id, char * text){
00139         sprintf(regs.at[id].comment, "--%s: %s", regs.at[id].sw, text);
00140 }
00141 void argo_setExample(char * exe, char * switchs){
00142         sprintf(example, "Example: %s %s",exe, switchs);
00143 }
00144 void argo_doProcess(int argc, char * argv[], int start) {
00145         int i, j;
00146    int done;
00147         int error = 0;
00148         for (i = start + 1; i < argc; i++) {
00149 
00150                 /* switch format */
00151                 if (argv[i][0] != '-' || (argv[i][1] != '-')) {
00152                         continue;
00153                 }
00154 
00155                 /* switch existence */
00156                 done = 0;
00157                 for (j = 0; j < regs.idx; j++) {
00158                         if (strcmp(&argv[i][2], "h") == 0) {
00159                                 error = 1;
00160                                 done = 1;
00161                                 break;
00162                         }
00163 
00164                         if (strcmp(&argv[i][2], regs.at[j].sw) == 0) {
00165                                 done = 1;
00166                                 if (regs.at[j].type == ARGON_INT) {
00167                                         if (regs.at[j].need_value) {
00168                                                 if (i == argc - 1) {
00169                                                         error = 4;
00170                                                         break;
00171                                                 } else {
00172                                                         *((int *) regs.at[j].var) = atoi(argv[i + 1]);
00173                                                         regs.at[j].set = 1;
00174                                                         i++;
00175                                                 }
00176                                         } else {
00177                                                 *((int *) regs.at[j].var) = 1;
00178                                                 regs.at[j].set = 1;
00179                                         }
00180                                 } else if (regs.at[j].type == ARGON_DOUBLE) {
00181                                         if (i == argc - 1) {
00182                                                 error = 4;
00183                                                 break;
00184                                         } else {
00185                                                 (*((double *) regs.at[j].var))=atof(argv[i + 1]);
00186                                                 regs.at[j].set = 1;
00187                                                 i++;
00188                                         }
00189                                 } else if (regs.at[j].type == ARGON_STRING) {
00190                                         if (i == argc - 1) {
00191                                                 error = 4;
00192                                                 break;
00193                                         } else {
00194                                                 strcpy(((char *) regs.at[j].var), argv[i + 1]);
00195                                                 regs.at[j].set = 1;
00196                                                 i++;
00197                                         }
00198                                 }
00199                         }
00200                 }
00201 
00202                 if (!done) {
00203          WMP_ERROR(stderr, "*** Inexistent switch %s specified\n",argv[i]);
00204                         error = 3;
00205                         break;
00206                 }
00207 
00208         }
00209         for (i = 0; i< regs.idx ; i++){
00210                 if (regs.at[i].mandatory &&  !regs.at[i].set){
00211                         WMP_ERROR(stderr, "*** Parameter %s is mandatory\n", regs.at[i].sw);
00212                         error = 8;
00213                 }
00214         }
00215 
00216         if (error == 4) {
00217       WMP_ERROR(stderr, "*** Incorrect number of parameters\n");
00218         }
00219         if (error != 0) {
00220                 WMP_ERROR(stderr, "List of switches:\n");
00221                 for (i = 0; i < regs.idx; i++) {
00222                         WMP_ERROR(stderr, "%s\n", regs.at[i].comment);
00223                 }
00224                 if (strlen(example) > 0){
00225                         fprintf(stderr,"%s\n",example);
00226                 }
00227                 exit(1);
00228         }
00229 }
00230 #endif /* ARGON_H_ */


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