00001 #include "drms_cmdparams.h"
00002 #include "drms.h"
00003 #include "cmdparams_priv.h"
00004
00005
00006 static char *blacklist[] =
00007 {
00008 "DRMS_RETENTION",
00009 "DRMS_ARCHIVE",
00010 NULL
00011 };
00012
00013
00014
00015 static int OnBlackList(const char *argname)
00016 {
00017 int onlist = 0;
00018 char **ps = blacklist;
00019
00020 while(*ps)
00021 {
00022 if (strcmp(*ps, argname) == 0)
00023 {
00024 onlist = 1;
00025 break;
00026 }
00027
00028 ps++;
00029 }
00030
00031 return onlist;
00032 }
00033
00034
00035 int drms_cmdparams_exists(CmdParams_t *parms, const char *name)
00036 {
00037 int exists = 0;
00038
00039
00040 exists = (cmdparams_getargstruct(parms, name) != NULL);
00041
00042 if (!exists && !OnBlackList(name) && cmdparams_exists(parms, name))
00043 {
00044
00045 exists = 1;
00046 }
00047
00048 return exists;
00049 }
00050
00051 const char *drms_cmdparams_get_str(CmdParams_t *parms, const char *name, int *status)
00052 {
00053 const char *str = NULL;
00054 int statint = DRMS_SUCCESS;
00055
00056
00057
00058 if (drms_cmdparams_exists(parms, name))
00059 {
00060 str = cmdparams_get_str(parms, name, &statint);
00061 }
00062 else
00063 {
00064 statint = DRMS_ERROR_UNKNOWNCMDARG;
00065 }
00066
00067 if (status)
00068 {
00069 *status = statint;
00070 }
00071
00072 return str;
00073 }
00074
00075 int drms_cmdparams_get_int(CmdParams_t *parms, const char *name, int *status)
00076 {
00077 int statint = DRMS_SUCCESS;
00078 DRMS_Value_t value;
00079 const char *str = drms_cmdparams_get_str(parms, name, &statint);
00080
00081 if (statint == DRMS_SUCCESS)
00082 {
00083 if (drms_sscanf2(str, NULL, 0, DRMS_TYPE_INT, &value) == -1)
00084 {
00085 statint = DRMS_ERROR_INVALIDCMDARGCONV;
00086 }
00087 }
00088
00089 if (status)
00090 {
00091 *status = statint;
00092 }
00093
00094 if (statint == DRMS_SUCCESS)
00095 {
00096 return value.value.int_val;
00097 }
00098 else
00099 {
00100 return DRMS_MISSING_INT;
00101 }
00102 }
00103
00104 int16_t drms_cmdparams_get_int16(CmdParams_t *parms, const char *name, int *status)
00105 {
00106 int statint = DRMS_SUCCESS;
00107 DRMS_Value_t value;
00108 const char *str = drms_cmdparams_get_str(parms, name, &statint);
00109
00110 if (statint == DRMS_SUCCESS)
00111 {
00112 if (drms_sscanf2(str, NULL, 0, DRMS_TYPE_SHORT, &value) == -1)
00113 {
00114 statint = DRMS_ERROR_INVALIDCMDARGCONV;
00115 }
00116 }
00117
00118 if (status)
00119 {
00120 *status = statint;
00121 }
00122
00123 if (statint == DRMS_SUCCESS)
00124 {
00125 return (int16_t)(value.value.int_val);
00126 }
00127 else
00128 {
00129 return DRMS_MISSING_SHORT;
00130 }
00131 }
00132
00133
00134 DRMS_Value_t *drms_cmdparams_get(CmdParams_t *parms, const char *name, DRMS_Type_t type, int *status)
00135 {
00136 int statint = DRMS_SUCCESS;
00137 DRMS_Value_t value;
00138 const char *str = drms_cmdparams_get_str(parms, name, &statint);
00139 DRMS_Value_t *rv = NULL;
00140 char *tmp = NULL;
00141
00142 if (type == DRMS_TYPE_STRING)
00143 {
00144
00145
00146 tmp = malloc(strlen(str) + 3);
00147
00148 if (tmp)
00149 {
00150 snprintf(tmp, strlen(str) + 3, "\"%s\"", str);
00151 str = tmp;
00152 }
00153 else
00154 {
00155 statint = DRMS_ERROR_OUTOFMEMORY;
00156 }
00157 }
00158
00159 if (statint == DRMS_SUCCESS)
00160 {
00161 if (drms_sscanf2(str, NULL, 0, type, &value) == -1)
00162 {
00163 statint = DRMS_ERROR_INVALIDCMDARGCONV;
00164 }
00165 }
00166
00167 if (statint == DRMS_SUCCESS)
00168 {
00169 rv = malloc(sizeof(DRMS_Value_t));
00170
00171 if (!rv)
00172 {
00173 statint = DRMS_ERROR_OUTOFMEMORY;
00174 }
00175 else
00176 {
00177 *rv = value;
00178 }
00179 }
00180
00181 if (tmp)
00182 {
00183 free(tmp);
00184 tmp = NULL;
00185 }
00186
00187 if (status)
00188 {
00189 *status = statint;
00190 }
00191
00192 return rv;
00193 }
00194
00195
00196 CmdParams_Arg_t *drms_cmdparams_getnext(CmdParams_t *parms, HIterator_t **last, int *status)
00197 {
00198 int istat = DRMS_SUCCESS;
00199 HIterator_t *hit = NULL;
00200 CmdParams_Arg_t *arg = NULL;
00201 HContainer_t *args = NULL;
00202 CmdParams_Arg_t *argret = NULL;
00203
00204 if (last)
00205 {
00206 if (*last)
00207 {
00208
00209 hit = *last;
00210 }
00211 else
00212 {
00213 hit = *last = (HIterator_t *)malloc(sizeof(HIterator_t));
00214 if (hit != NULL)
00215 {
00216 args = cmdparams_get_argscont(parms);
00217
00218 if (args)
00219 {
00220 hiter_new(hit, args);
00221 }
00222 else
00223 {
00224
00225 istat = DRMS_ERROR_INVALIDDATA;
00226 }
00227 }
00228 else
00229 {
00230 istat = DRMS_ERROR_OUTOFMEMORY;
00231 }
00232 }
00233
00234 if (istat == DRMS_SUCCESS)
00235 {
00236 arg = hiter_getnext(hit);
00237
00238 if (arg)
00239 {
00240 argret = arg;
00241 }
00242 }
00243 }
00244
00245 if (status)
00246 {
00247 *status = istat;
00248 }
00249
00250 return argret;
00251 }