From cb829407f5eee3dafc05aa64bdcb308860ab2530 Mon Sep 17 00:00:00 2001 From: hades Date: Fri, 18 Aug 2000 13:01:40 +0000 Subject: [PATCH] *** empty log message *** --- allParam/ca/caParam.c | 213 ++++++++++++++++++++++++-------------- allParam/ca/caParam.h | 59 ++++++----- allParam/file/fileParam.c | 117 ++++++++++++--------- allParam/file/fileParam.h | 51 +++++---- allParam/ora/oraParam.h | 48 +++++---- allParam/ora/oraParam.pc | 38 ++++--- allParam/psql/psqlParam.c | 114 +++++++++++++++++--- allParam/psql/psqlParam.h | 53 ++++++---- allParam/tcl/tclParam.c | 98 ++++++++++-------- allParam/tcl/tclParam.h | 52 ++++++---- 10 files changed, 536 insertions(+), 307 deletions(-) diff --git a/allParam/ca/caParam.c b/allParam/ca/caParam.c index 36aff14..685f808 100644 --- a/allParam/ca/caParam.c +++ b/allParam/ca/caParam.c @@ -1,17 +1,19 @@ #define _POSIX_C_SOURCE 199509L -#include - -#include #include #include - -#include +#include #include "param.h" +static chid Param_openChannel(const Param *, const char *, int *); +static int Param_closeChannel(const Param *, chid, char *); +static char *Param_returnPVName(const char *, const char *, int); +static void Param_strerror(const Param *, const char *); + int conParam(Param *my) { + my->strerror = NULL; return 0; } @@ -19,106 +21,180 @@ void desParam(Param *my) { } -int Param_getInt(const Param *my, const char *name, const char *idx, unsigned long int *val) +int Param_getInt(const Param *my, const char *name, const char *idx, int *row, unsigned long int *val) { - return Param_getIntArray(my, name, idx, 1, val); + return Param_getIntArray(my, name, idx, 1, row, val); } -int Param_getString(const Param *my, const char *name, const char *idx, char *val) +int Param_getString(const Param *my, const char *name, const char *idx, int *row, char *val) { - int i; - int status; + int retVal = 0; + int found; chid chan; char *pPVName; + int i; - if((status = ca_task_initialize()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot initialize channel access.\n"); - exit (-1); + if(ca_task_initialize() != ECA_NORMAL) { + Param_strerror(my, "Cannot establish ca connection.\n"); + return -1; } pPVName = Param_returnPVName(name, idx, -1); - chan = Param_openChannel(pPVName); - if((status = ca_bget(chan, val)) != ECA_NORMAL) { - msglog(LOG_ERR, "Do not receive value of %s.\n", pPVName); - exit (-1); + chan = Param_openChannel(my, pPVName, &found); + switch (found) { + case(-1): + Param_strerror(my, "Cannot establish ca connection.\n"); + break; + case(0): + *row = 0; + break; + case(1): + *row = 1; + if(ca_bget(chan, val) != ECA_NORMAL) { + Param_strerror(my, "Did not receive correct data via ca.\n"); + return -1; + } + retVal = Param_closeChannel(my, chan, pPVName); + break; } - Param_closeChannel(chan, pPVName); - if((status = ca_task_exit()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot exit channel access.\n"); - exit (-1); + if(ca_task_exit() != ECA_NORMAL) { + Param_strerror(my, "Cannot close ca connection.\n"); + return -1; } - return 1; + return retVal; } -int Param_getIntArray(const Param *my, const char *name, const char *idx, int num, unsigned long int *val) +int Param_getIntArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, unsigned long int *val) { - int i; - int status; + int retVal = 0; + int found; chid chan; char *pPVName; + int i; - if((status = ca_task_initialize()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot initialize channel access.\n"); - exit (-1); + if(ca_task_initialize() != ECA_NORMAL) { + Param_strerror(my, "Cannot establish ca connection.\n"); + return -1; } pPVName = Param_returnPVName(name, idx, -1); - chan = Param_openChannel(pPVName); - if((status = ca_array_get(DBR_LONG, num, chan, val)) != ECA_NORMAL) { - msglog(LOG_ERR, "Do not receive value of %s.\n", pPVName); - exit (-1); + chan = Param_openChannel(my, pPVName, &found); + switch (found) { + case(-1): + Param_strerror(my, "Cannot establish ca connection.\n"); + break; + case(0): + *rows = 0; + break; + case(1): + *rows = maxrows; + if(ca_array_get(DBR_LONG, maxrows, chan, val) != ECA_NORMAL) { + Param_strerror(my, "Did not receive correct data via ca.\n"); + retVal = -1; + } + retVal|= Param_closeChannel(my, chan, pPVName); + break; } - Param_closeChannel(chan, pPVName); - if((status = ca_task_exit()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot exit channel access.\n"); - exit (-1); + if(ca_task_exit() != ECA_NORMAL) { + Param_strerror(my, "Cannot close ca connection.\n"); + return -1; } - return num; + return retVal; } -int Param_getStringArray(const Param *my, const char *name, const char *idx, int num, char **val) +int Param_getStringArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, char **val) { - int i; - int status; + int retVal = 0; + int found; chid chan; char *pPVName; + int i; - if((status = ca_task_initialize()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot initialize channel access.\n"); - exit (-1); + if(ca_task_initialize() != ECA_NORMAL) { + Param_strerror(my, "Cannot establish ca connection.\n"); + return -1; } - for (i = 0 ; i < num ; i++) { + *rows = 0; + for (i = 0 ; i < maxrows ; i++) { pPVName = Param_returnPVName(name, idx, i); - chan = Param_openChannel(pPVName); - if((status = ca_bget(chan, val)) != ECA_NORMAL) { - msglog(LOG_ERR, "Do not receive value of %s.\n", pPVName); - exit (-1); + chan = Param_openChannel(my, pPVName, &found); + switch (found) { + case(-1): + Param_strerror(my, "Cannot establish ca connection.\n"); + return -1; + break; + case(0): + i = maxrows; + break; + case(1): + if(ca_bget(chan, val) != ECA_NORMAL) { + Param_strerror(my, "Did not receive correct data via ca.\n"); + retVal = -1; + } else { + *rows++; + } + retVal|= Param_closeChannel(my, chan, pPVName); + break; } - Param_closeChannel(chan, pPVName); } - if((status = ca_task_exit()) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot exit channel access.\n"); - exit (-1); + if(ca_task_exit() != ECA_NORMAL) { + Param_strerror(my, "Cannot close ca connection.\n"); + return -1; } - return num; + return retVal; } -char *Param_returnPVName(const char *name, const char *idx, int num) +const char *Param_getErrStr(const Param *my) +{ + return my->strerror; +} + +static chid Param_openChannel(const Param *my, const char *pPVName, int *found) +{ + int status; + chid chan; + if((status = ca_search(pPVName, &chan)) == ECA_NORMAL) { + *found = 1; + } else if (status == ECA_GETFAIL) { + *found = 0; + } else { + *found = -1; + } + + if(ca_pend_io(TIMEOUT) != ECA_NORMAL) { + Param_strerror(my, "Cannot switch to asynchronous mode.\n"); + } + return chan; +} + +static int Param_closeChannel(const Param *my, chid chan, char *pPVName) +{ + if(ca_pend_io(TIMEOUT) != ECA_NORMAL) { + Param_strerror(my, "Cannot switch to asynchronous mode.\n"); + } + free(pPVName); + if(ca_clear_channel(chan) != ECA_NORMAL) { + return -1; + } + return 0; +} + +static char *Param_returnPVName(const char *name, const char *idx, int num) { int i; char *pPVName; if (num == -1) { - pPVName = allocMem((strlen("HAD:PARAM:") + strlen(name) + 1 + strlen(idx) + 1) * sizeof(char)); + pPVName = malloc((strlen("HAD:PARAM:") + strlen(name) + 1 + strlen(idx) + 1) * sizeof(char)); sprintf(pPVName ,"HAD:PARAM:%s:%s", name, idx); } else { - pPVName = allocMem((strlen("HAD:PARAM:") + strlen(name) + 1 + strlen(idx) + 1 + 4 + 1) * sizeof(char)); + pPVName = malloc((strlen("HAD:PARAM:") + strlen(name) + 1 + strlen(idx) + 1 + 4 + 1) * sizeof(char)); sprintf(pPVName ,"HAD:PARAM:%s:%s:%d", name, idx, num); } @@ -128,28 +204,9 @@ char *Param_returnPVName(const char *name, const char *idx, int num) return pPVName; } -chid Param_openChannel(const char *pPVName) +static void Param_strerror(const Param *my, const char *strerror) { - int status; - chid chan; - if((status = ca_search(pPVName, &chan)) != ECA_NORMAL) { - msglog(LOG_ERR, "Cannot open channel to %s: %s\n", pPVName, strerror(errno)); - exit (-1); - } - if((status = ca_pend_io(TIMEOUT)) != ECA_NORMAL) { - msglog(LOG_WARNING, "Cannot switch to asyncronous mode: %s\n", strerror(errno)); - } - return chan; -} - -int Param_closeChannel(chid chan, char *pPVName) -{ - int status; - if((status = ca_pend_io(TIMEOUT)) != ECA_NORMAL) { - msglog(LOG_WARNING, "Cannot switch to asyncronous mode: %s\n", strerror(errno)); - } - status = ca_clear_channel(chan); - freeMem(pPVName); - return status; + realloc(my->strerror, strlen(strerror)); + strcpy(my->strerror, strerror); } diff --git a/allParam/ca/caParam.h b/allParam/ca/caParam.h index 293eeba..878f717 100644 --- a/allParam/ca/caParam.h +++ b/allParam/ca/caParam.h @@ -5,19 +5,16 @@ * Section containing struct Param (different in the different param.h's) * **************************************************************************/ -typedef struct ParamS { -} Param; +#include #define PARAM_MAX_NVALS 1024 #define PARAM_MAX_NAME_LEN 128 #define TIMEOUT 1.0 -#include - -chid Param_openChannel(const char *); -int Param_closeChannel(chid, char *); -char *Param_returnPVName(const char *, const char *, int); +typedef struct ParamS { + char *strerror; +} Param; /****************************************************************** * Section containing the API for param (common to all param.h's) * @@ -27,45 +24,51 @@ int conParam(Param *); void desParam(Param *); /* - * All functions have a status as return value. If the return value is positive, - * it contains the number of array members returned (1 in case of - * Param_getString and Param_getInt, between 1 and num in case of - * Param_getStringArray and Param_getIntArray). + * All functions have a status as return value. The value i 0 if everything + * worked correctly and -1 if an error occured. In this case, you can get + * the address of an error String via the function Param_getErrStr(). * * Memory must be allocated for the result before the function is called. + * In the non-array-functions at &row is filled with 0 or 1 depending on + * whether the Parameter is found or not. * For example, calling Param_getInt needs * - * > int status; + * > int row; * > unsigned long int value; - * > if(status = Param_getInt(param, name, idx, value) != 1) { - * > printf("No parameter 'value' found. Exiting.\n"); + * > if(Param_getInt(param, name, idx, &row, &value) != 0) { + * > syslog(LOG_ERR, "Error retrieving value of %s(%s). Exiting.\n", name, idx); + * > exit(-1); + * > } else if(row == 0) { + * > syslog(LOG_ERR, "Parameter %s(%s) not found. Exiting.\n", name, idx); * > exit(-1); * > } - * + * + * In the array functions maxrows must supply the memory available for the + * result while the actual number of result rows is stored in rows. + * * For strings char[PARAM_MAX_VALUE_LEN] has to be allocated. Therefore the * above example expands to * - * > int status; - * > int num = 32; - * > char value[PARAM_MAX_VALUE_LEN][num]; - * > if(status = Param_getStringArray(param, name, idx, num, value) != num) { - * > printf("Not enough elements of 'value' found. Exiting.\n"); + * > int rows; + * > int maxrows = 32; + * > char value[PARAM_MAX_VALUE_LEN][maxrows]; + * > if(Param_getStringArray(param, name, idx, maxrows, &rows, value) != 0) { + * > syslog(LOG_ERR, "Error retrieving values of %s(%s). Exiting.\n", name, idx); * > exit(-1); + * > } else if(row != maxrows) { + * > syslog(LOG_WARNING, "%d Parameters %s(%s) found instead of %d.\n", rows, name, idx, maxrows); * > } * */ -#define PARAM_RV_NO_SUCH_PARAMETER 0 -#define PARAM_RV_FAILED -1 -#define PARAM_RV_NO_INT -2 - #define PARAM_MAX_VALUE_LEN 128 -int Param_getInt(const Param *, const char *, const char *, unsigned long int *); -int Param_getString(const Param *, const char *, const char *, char *); +int Param_getInt(const Param *, const char *, const char *, int *, unsigned long int *); +int Param_getString(const Param *, const char *, const char *, int *, char *); +int Param_getIntArray(const Param *, const char *, const char *, int, int *, unsigned long int *); +int Param_getStringArray(const Param *, const char *, const char *, int, int *, char **); -int Param_getIntArray(const Param *, const char *, const char *, int, unsigned long int *); -int Param_getStringArray(const Param *, const char *, const char *, int, char **); +const char *Param_getErrStr(const Param *); #endif diff --git a/allParam/file/fileParam.c b/allParam/file/fileParam.c index 774c8d8..8ceaef3 100644 --- a/allParam/file/fileParam.c +++ b/allParam/file/fileParam.c @@ -1,29 +1,33 @@ #define _POSIX_C_SOURCE 199509L +#include + #include +#include #include -#include - -#include #include "param.h" #define BUFFERSIZE 130 +static int Param_getParamNumber(const Param *, const char *, const char *); +static void Param_strerror(const Param *, const char *); + int conParam(Param *my) { + int retVal = 0; int i = 0; char *fileName; FILE *f; char buf[BUFFERSIZE]; + my->strerror = NULL; fileName = getenv("DAQSLOW_PARAM_FILE"); if (fileName == NULL) { strcpy(fileName, "param.tcl"); } if (NULL == (f = fopen(fileName, "r"))) { - msglog(LOG_ERR, "opening param file: %s\n", strerror(errno)); - exit (-1); + return -1; } else { int oneMore = 1; my->overFullParamFile = 0; @@ -39,97 +43,110 @@ int conParam(Param *my) } } else { oneMore = 0; - msglog(LOG_WARNING, "Too many parameters in file, ignoring the rest.\n"); my->overFullParamFile = 1; + retVal = -1; } } my->nVals = i; fclose(f); } - return 0; + return retVal; } void desParam(Param *my) { } -int Param_getParamNumber(const Param *my, const char *name, const char *idx) -{ - int retVal = -1; - int i; - char fullName[PARAM_MAX_NAME_LEN]; - - sprintf(fullName, "%s(%s)", name, idx); - for (i = 0; i < my->nVals; i++) { - if (strcmp(my->pname[i], fullName) == 0) { - retVal = i; - } - } - return retVal; -} - -int Param_getInt(const Param *my, const char *name, const char *idx, unsigned long int *val) +int Param_getInt(const Param *my, const char *name, const char *idx, int *row, unsigned long int *val) { - int status; + int retVal; char valstr[PARAM_MAX_VALUE_LEN]; char *endptr; - if ((status = Param_getString(my, name, idx, valstr)) != 1) { - msglog(LOG_ERR, "Error: cannot get Parameter %s as a string", idx); - return status; + if ((retVal = Param_getString(my, name, idx, row, valstr)) != 0) { + return retVal; } *val = strtoul(valstr, &endptr, 0); if (*endptr == '\0') { - return PARAM_RV_NO_INT; + Param_strerror(my, "Value seems to be no integer.\n"); + retVal = -1; } - return 1; + return retVal; } -int Param_getString(const Param *my, const char *name, const char *idx, char *val) +int Param_getString(const Param *my, const char *name, const char *idx, int *row, char *val) { int n; n = Param_getParamNumber(my, name, idx); if (n == -1) { - msglog(LOG_WARNING, "Parameter %s(%s) not found, returning NULL-pointer.\n", name, idx); - return 0; + strcpy(val, ""); + Param_strerror(my, "Parameter not found.\n"); + *row = 0; } else { strcpy(val, my->value[n]); + *row = 1; } - return 1; + return 0; } -int Param_getIntArray(const Param *my, const char *name, const char *idx, int num, unsigned long int *val) +int Param_getIntArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, unsigned long int *val) { - int status; - int tmp; + int row; + int retVal = 0; int i; char index[PARAM_MAX_NAME_LEN]; - for (i = 0 ; i < num ; i++) { + for (i = 0 ; i < maxrows ; i++) { sprintf(index,"%s%d", idx, i); - if ((tmp = Param_getInt(my, name, index, val + i) == 1) && (status >= 0)) { - status++; + if(retVal |= Param_getInt(my, name, index, &row, val + i) == 0) { + *rows+= row; } else { - status = tmp; + i = maxrows; } } - - return status; + + return retVal; } -int Param_getStringArray(const Param *my, const char *name, const char *idx, int num, char **val) +int Param_getStringArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, char **val) { - int status; - int tmp; + int row; + int retVal = 0; int i; char index[PARAM_MAX_NAME_LEN]; - for (i = 0 ; i < num ; i++) { + for (i = 0 ; i < maxrows ; i++) { sprintf(index,"%s%d", idx, i); - if ((tmp = Param_getString(my, name, index, val[i]) == 1) && (status >= 0)) { - status++; + if(retVal |= Param_getString(my, name, index, &row, val[i]) == 0) { + *rows+= row; } else { - status = tmp; + i = maxrows; + } + } + + return retVal; +} + +const char *Param_getErrStr(const Param *my) +{ + return my->strerror; +} + +static int Param_getParamNumber(const Param *my, const char *name, const char *idx) +{ + int retVal = -1; + int i; + char fullName[PARAM_MAX_NAME_LEN]; + + sprintf(fullName, "%s(%s)", name, idx); + for (i = 0; i < my->nVals; i++) { + if (strcmp(my->pname[i], fullName) == 0) { + retVal = i; } } + return retVal; +} - return status; +static void Param_strerror(const Param *my, const char *strerror) +{ + realloc(my->strerror, strlen(strerror)); + strcpy(my->strerror, strerror); } diff --git a/allParam/file/fileParam.h b/allParam/file/fileParam.h index d855094..d738cc0 100644 --- a/allParam/file/fileParam.h +++ b/allParam/file/fileParam.h @@ -9,14 +9,13 @@ #define PARAM_MAX_NVALS 1024 typedef struct ParamS { + char *strerror; int nVals; int overFullParamFile; char pname[PARAM_MAX_NVALS][PARAM_MAX_NAME_LEN]; char value[PARAM_MAX_NVALS][PARAM_MAX_NAME_LEN]; } Param; -int Param_getParamNumber(const Param *, const char *, const char *); - /****************************************************************** * Section containing the API for param (common to all param.h's) * ******************************************************************/ @@ -25,45 +24,51 @@ int conParam(Param *); void desParam(Param *); /* - * All functions have a status as return value. If the return value is positive, - * it contains the number of array members returned (1 in case of - * Param_getString and Param_getInt, between 1 and num in case of - * Param_getStringArray and Param_getIntArray). + * All functions have a status as return value. The value i 0 if everything + * worked correctly and -1 if an error occured. In this case, you can get + * the address of an error String via the function Param_getErrStr(). * * Memory must be allocated for the result before the function is called. + * In the non-array-functions at &row is filled with 0 or 1 depending on + * whether the Parameter is found or not. * For example, calling Param_getInt needs * - * > int status; + * > int row; * > unsigned long int value; - * > if(status = Param_getInt(param, name, idx, value) != 1) { - * > printf("No parameter 'value' found. Exiting.\n"); + * > if(Param_getInt(param, name, idx, &row, &value) != 0) { + * > syslog(LOG_ERR, "Error retrieving value of %s(%s). Exiting.\n", name, idx); + * > exit(-1); + * > } else if(row == 0) { + * > syslog(LOG_ERR, "Parameter %s(%s) not found. Exiting.\n", name, idx); * > exit(-1); * > } - * + * + * In the array functions maxrows must supply the memory available for the + * result while the actual number of result rows is stored in rows. + * * For strings char[PARAM_MAX_VALUE_LEN] has to be allocated. Therefore the * above example expands to * - * > int status; - * > int num = 32; - * > char value[PARAM_MAX_VALUE_LEN][num]; - * > if(status = Param_getStringArray(param, name, idx, num, value) != num) { - * > printf("Not enough elements of 'value' found. Exiting.\n"); + * > int rows; + * > int maxrows = 32; + * > char value[PARAM_MAX_VALUE_LEN][maxrows]; + * > if(Param_getStringArray(param, name, idx, maxrows, &rows, value) != 0) { + * > syslog(LOG_ERR, "Error retrieving values of %s(%s). Exiting.\n", name, idx); * > exit(-1); + * > } else if(row != maxrows) { + * > syslog(LOG_WARNING, "%d Parameters %s(%s) found instead of %d.\n", rows, name, idx, maxrows); * > } * */ -#define PARAM_RV_NO_SUCH_PARAMETER 0 -#define PARAM_RV_FAILED -1 -#define PARAM_RV_NO_INT -2 - #define PARAM_MAX_VALUE_LEN 128 -int Param_getInt(const Param *, const char *, const char *, unsigned long int *); -int Param_getString(const Param *, const char *, const char *, char *); +int Param_getInt(const Param *, const char *, const char *, int *, unsigned long int *); +int Param_getString(const Param *, const char *, const char *, int *, char *); +int Param_getIntArray(const Param *, const char *, const char *, int, int *, unsigned long int *); +int Param_getStringArray(const Param *, const char *, const char *, int, int *, char **); -int Param_getIntArray(const Param *, const char *, const char *, int, unsigned long int *); -int Param_getStringArray(const Param *, const char *, const char *, int, char **); +const char *Param_getErrStr(const Param *); #endif diff --git a/allParam/ora/oraParam.h b/allParam/ora/oraParam.h index ea8515c..9b4afe8 100644 --- a/allParam/ora/oraParam.h +++ b/allParam/ora/oraParam.h @@ -6,6 +6,7 @@ **************************************************************************/ typedef struct ParamS { + char *strerror; } Param; /****************************************************************** @@ -16,44 +17,51 @@ int conParam(Param *); void desParam(Param *); /* - * All functions have a status as return value. If the return value is positive, - * it contains the number of array members returned (1 in case of - * Param_getString and Param_getInt, between 1 and num in case of - * Param_getStringArray and Param_getIntArray). + * All functions have a status as return value. The value i 0 if everything + * worked correctly and -1 if an error occured. In this case, you can get + * the address of an error String via the function Param_getErrStr(). * * Memory must be allocated for the result before the function is called. + * In the non-array-functions at &row is filled with 0 or 1 depending on + * whether the Parameter is found or not. * For example, calling Param_getInt needs * - * > int status; + * > int row; * > unsigned long int value; - * > if(status = Param_getInt(param, name, idx, value) != 1) { - * > printf("No parameter 'value' found. Exiting.\n"); + * > if(Param_getInt(param, name, idx, &row, &value) != 0) { + * > syslog(LOG_ERR, "Error retrieving value of %s(%s). Exiting.\n", name, idx); + * > exit(-1); + * > } else if(row == 0) { + * > syslog(LOG_ERR, "Parameter %s(%s) not found. Exiting.\n", name, idx); * > exit(-1); * > } - * + * + * In the array functions maxrows must supply the memory available for the + * result while the actual number of result rows is stored in rows. + * * For strings char[PARAM_MAX_VALUE_LEN] has to be allocated. Therefore the * above example expands to * - * > int status; - * > int num = 32; - * > char value[PARAM_MAX_VALUE_LEN][num]; - * > if(status = Param_getStringArray(param, name, idx, num, value) != num) { - * > printf("Not enough elements of 'value' found. Exiting.\n"); + * > int rows; + * > int maxrows = 32; + * > char value[PARAM_MAX_VALUE_LEN][maxrows]; + * > if(Param_getStringArray(param, name, idx, maxrows, &rows, value) != 0) { + * > syslog(LOG_ERR, "Error retrieving values of %s(%s). Exiting.\n", name, idx); * > exit(-1); + * > } else if(row != maxrows) { + * > syslog(LOG_WARNING, "%d Parameters %s(%s) found instead of %d.\n", rows, name, idx, maxrows); * > } * */ -#define PARAM_RV_NO_SUCH_PARAMETER 0 -#define PARAM_RV_FAILED -1 - #define PARAM_MAX_VALUE_LEN 128 -int Param_getInt(const Param *, const char *, const char *, unsigned long int *); -int Param_getString(const Param *, const char *, const char *, char *); +int Param_getInt(const Param *, const char *, const char *, int *, unsigned long int *); +int Param_getString(const Param *, const char *, const char *, int *, char *); +int Param_getIntArray(const Param *, const char *, const char *, int, int *, unsigned long int *); +int Param_getStringArray(const Param *, const char *, const char *, int, int *, char **); -int Param_getIntArray(const Param *, const char *, const char *, int, unsigned long int *); -int Param_getStringArray(const Param *, const char *, const char *, int, char **); +const char *Param_getErrStr(const Param *); #endif diff --git a/allParam/ora/oraParam.pc b/allParam/ora/oraParam.pc index 29609a4..79f0e0d 100644 --- a/allParam/ora/oraParam.pc +++ b/allParam/ora/oraParam.pc @@ -1,14 +1,17 @@ #define _POSIX_C_SOURCE 199509L -/* Oracle communication area */ +#include + #include -/* SQL communication area */ #include #include "param.h" +static void Param_strerror(const Param *, const char *); + int conParam(Param *my) { + my->strerror = NULL; return 0; } @@ -16,31 +19,42 @@ void desParam(Param *my) { } -int Param_getInt(const Param *my, const char *name, const char *idx, unsigned long int *val) +int Param_getInt(const Param *my, const char *name, const char *idx, int *row, unsigned long int *val) { - return Param_getIntArray(my, name, idx, 1, val); + return Param_getIntArray(my, name, idx, 1, row, val); } -int Param_getString(const Param *my, const char *name, const char *idx, char *val) +int Param_getString(const Param *my, const char *name, const char *idx, int *row, char *val) { - return Param_getStringArray(my, name, idx, 1, &val); + return Param_getStringArray(my, name, idx, 1, row, &val); } -int Param_getIntArray(const Param *my, const char *name, const char *idx, int num, unsigned long int *val) +int Param_getIntArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, unsigned long int *val) { int i; - for (i = 0 ; istrerror; +} + +static void Param_strerror(const Param *my, const char *strerror) +{ + realloc(my->strerror, strlen(strerror)); + strcpy(my->strerror, strerror); } diff --git a/allParam/psql/psqlParam.c b/allParam/psql/psqlParam.c index f836850..8b3b7ba 100644 --- a/allParam/psql/psqlParam.c +++ b/allParam/psql/psqlParam.c @@ -1,11 +1,15 @@ #define _POSIX_C_SOURCE 199509L -#include +#include +#include #include "param.h" +static void Param_strerror(const Param *, const char *); + int conParam(Param *my) { + my->strerror = NULL; return 0; } @@ -13,31 +17,115 @@ void desParam(Param *my) { } -int Param_getInt(const Param *my, const char *name, const char *idx, unsigned long int *val) +int Param_getInt(const Param *my, const char *name, const char *idx, int *row, unsigned long int *val) { - return Param_getIntArray(my, name, idx, 1, val); + return Param_getIntArray(my, name, idx, 1, row, val); } -int Param_getString(const Param *my, const char *name, const char *idx, char *val) +int Param_getString(const Param *my, const char *name, const char *idx, int *row, char *val) { - return Param_getStringArray(my, name, idx, 1, &val); + return Param_getStringArray(my, name, idx, 1, row, &val); } -int Param_getIntArray(const Param *my, const char *name, const char *idx, int num, unsigned long int *val) +int Param_getIntArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, unsigned long int *val) { + int retVal; + int fnum; int i; - for (i = 0 ; i maxrows) { + Param_strerror(my, "Result has to many rows. Returning only 'maxrows' rows.\n"); + for (i = 0 ; i < maxrows ; i++) { + val[i] = strtoul(PQgetvalue(result, i, fnum), &endptr, 0); + if(*endptr != '\0') { + Param_strerror(my, "Value seems to be no integer.\n"); + retVal = -1; + } + } + retVal = -1; + } else { + for (i = 0 ; i < *rows ; i++) { + val[i] = strtoul(PQgetvalue(result, i, fnum), &endptr, 0); + if(*endptr != '\0') { + Param_strerror(my, "Value seems to be no integer.\n"); + retVal = -1; + } + } + for (i = *rows ; i < maxrows ; i++) { + val[i] = 0; + } + retVal = 0; + } + PQfinish(conn); + + return retVal; } -int Param_getStringArray(const Param *my, const char *name, const char *idx, int num, char **val) +int Param_getStringArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, char **val) { + int retVal; + int fnum; int i; - for (i = 0 ; i maxrows) { + Param_strerror(my, "Result has to many rows. Returning only 'maxrows' rows.\n"); + for (i = 0 ; i < maxrows ; i++) { + strcpy(val[i], PQgetvalue(result, i, fnum)); + } + retVal = -1; + } else { + for (i = 0 ; i < *rows ; i++) { + strcpy(val[i], PQgetvalue(result, i, fnum)); + } + for (i = *rows ; i < maxrows ; i++) { + val[i] = NULL; + } + retVal = 0; } - return num; + PQfinish(conn); + + return retVal; +} + +const char *Param_getErrStr(const Param *my) +{ + return my->strerror; +} + +static void Param_strerror(const Param *my, const char *strerror) +{ + realloc(my->strerror, strlen(strerror)); + strcpy(my->strerror, strerror); } diff --git a/allParam/psql/psqlParam.h b/allParam/psql/psqlParam.h index ea8515c..ef2cb6d 100644 --- a/allParam/psql/psqlParam.h +++ b/allParam/psql/psqlParam.h @@ -5,7 +5,13 @@ * Section containing struct Param (different in the different param.h's) * **************************************************************************/ +#include + +#define PG_MAX_QUERY_LEN 1024 +#define PG_MAX_CLAUSE_LEN 256 + typedef struct ParamS { + char *strerror; } Param; /****************************************************************** @@ -16,44 +22,51 @@ int conParam(Param *); void desParam(Param *); /* - * All functions have a status as return value. If the return value is positive, - * it contains the number of array members returned (1 in case of - * Param_getString and Param_getInt, between 1 and num in case of - * Param_getStringArray and Param_getIntArray). + * All functions have a status as return value. The value i 0 if everything + * worked correctly and -1 if an error occured. In this case, you can get + * the address of an error String via the function Param_getErrStr(). * * Memory must be allocated for the result before the function is called. + * In the non-array-functions at &row is filled with 0 or 1 depending on + * whether the Parameter is found or not. * For example, calling Param_getInt needs * - * > int status; + * > int row; * > unsigned long int value; - * > if(status = Param_getInt(param, name, idx, value) != 1) { - * > printf("No parameter 'value' found. Exiting.\n"); + * > if(Param_getInt(param, name, idx, &row, &value) != 0) { + * > syslog(LOG_ERR, "Error retrieving value of %s(%s). Exiting.\n", name, idx); + * > exit(-1); + * > } else if(row == 0) { + * > syslog(LOG_ERR, "Parameter %s(%s) not found. Exiting.\n", name, idx); * > exit(-1); * > } - * + * + * In the array functions maxrows must supply the memory available for the + * result while the actual number of result rows is stored in rows. + * * For strings char[PARAM_MAX_VALUE_LEN] has to be allocated. Therefore the * above example expands to * - * > int status; - * > int num = 32; - * > char value[PARAM_MAX_VALUE_LEN][num]; - * > if(status = Param_getStringArray(param, name, idx, num, value) != num) { - * > printf("Not enough elements of 'value' found. Exiting.\n"); + * > int rows; + * > int maxrows = 32; + * > char value[PARAM_MAX_VALUE_LEN][maxrows]; + * > if(Param_getStringArray(param, name, idx, maxrows, &rows, value) != 0) { + * > syslog(LOG_ERR, "Error retrieving values of %s(%s). Exiting.\n", name, idx); * > exit(-1); + * > } else if(row != maxrows) { + * > syslog(LOG_WARNING, "%d Parameters %s(%s) found instead of %d.\n", rows, name, idx, maxrows); * > } * */ -#define PARAM_RV_NO_SUCH_PARAMETER 0 -#define PARAM_RV_FAILED -1 - #define PARAM_MAX_VALUE_LEN 128 -int Param_getInt(const Param *, const char *, const char *, unsigned long int *); -int Param_getString(const Param *, const char *, const char *, char *); +int Param_getInt(const Param *, const char *, const char *, int *, unsigned long int *); +int Param_getString(const Param *, const char *, const char *, int *, char *); +int Param_getIntArray(const Param *, const char *, const char *, int, int *, unsigned long int *); +int Param_getStringArray(const Param *, const char *, const char *, int, int *, char **); -int Param_getIntArray(const Param *, const char *, const char *, int, unsigned long int *); -int Param_getStringArray(const Param *, const char *, const char *, int, char **); +const char *Param_getErrStr(const Param *); #endif diff --git a/allParam/tcl/tclParam.c b/allParam/tcl/tclParam.c index f1f0b53..df5c10b 100644 --- a/allParam/tcl/tclParam.c +++ b/allParam/tcl/tclParam.c @@ -2,36 +2,36 @@ #include -#include #include #include #include -#include - #include "param.h" +static void Param_strerror(const Param *, const char *); + int conParam(Param *my) { int retVal; char *fileName; int code; + my->strerror = NULL; fileName = getenv("DAQSLOW_PARAM_FILE"); if (fileName == NULL) { fileName = "param.tcl"; } my->interp = Tcl_CreateInterp(); code = Tcl_EvalFile(my->interp, fileName); - if (*my->interp->result != 0) { - fprintf(stderr, "opening param file: %s\n", my->interp->result); - retVal = -1; - } else { - if (code != TCL_OK) { - retVal = -1; + if (code != TCL_OK) { + if (*my->interp->result != 0) { + Param_strerror(my, my->interp->result); } else { - retVal = 0; + Param_strerror(my, "Tcl interpreter cannot read file correctly and does not deliver an error string.\n"); } + retVal = -1; + } else { + retVal = 0; } return retVal; } @@ -41,67 +41,83 @@ void desParam(Param *my) Tcl_DeleteInterp(my->interp); } -int Param_getInt(const Param *my, const char *name, const char *idx, unsigned long int *val) +int Param_getInt(const Param *my, const char *name, const char *idx, int *row, unsigned long int *val) { - int status; char valstr[PARAM_MAX_VALUE_LEN]; char *endptr; - if ((status = Param_getString(my, name, idx, valstr)) != 1) { - msglog(LOG_ERR, "Error: cannot get Parameter %s as a string", idx); - return status; + if (Param_getString(my, name, idx, row, valstr) != 0) { + return -1; } - *val = strtoul(valstr, &endptr, 0); - if (*endptr == '\0') { - return PARAM_RV_NO_INT; + if(valstr == NULL) { + *row = 0; + } else { + *val = strtoul(valstr, &endptr, 0); + if (*endptr == '\0') { + Param_strerror(my, "Value seems to be no integer.\n"); + return -1; + } + *row = 1; } - return 1; + return 0; } -int Param_getString(const Param *my, const char *name, const char *idx, char *val) +int Param_getString(const Param *my, const char *name, const char *idx, int *row, char *val) { strcpy(val, Tcl_GetVar2(my->interp, (char *) name, (char *) idx, 0)); if (val == NULL) { - val = ""; - fprintf(stderr, - "Parameter %s(%s) not found, default is \"\"\n", name, idx); - return 0; + *row = 0; + } else { + *row = 1; } - return 1; + return 0; } -int Param_getIntArray(const Param *my, const char *name, const char *idx, int num, unsigned long int *val) +int Param_getIntArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, unsigned long int *val) { - int status; - int tmp; + int retVal = 0; int i; + int row; char index[PARAM_MAX_NAME_LEN]; - for (i = 0 ; i < num ; i++) { + *rows = 0; + for (i = 0 ; i < maxrows ; i++) { sprintf(index,"%s%d", idx, i); - if ((tmp = Param_getInt(my, name, index, val[i]) == 1) && (status >= 0)) { - status++; + if(((retVal |= Param_getInt(my, name, index, &row, &val[i])) == 0) && (row == 1)) { + *rows++; } else { - status = tmp; + return retVal; } } - return status; + return retVal; } -int Param_getStringArray(const Param *my, const char *name, const char *idx, int num, char **val) +int Param_getStringArray(const Param *my, const char *name, const char *idx, int maxrows, int *rows, char **val) { - int status; - int tmp; + int retVal = 0; int i; + int row; char index[PARAM_MAX_NAME_LEN]; - for (i = 0 ; i < num ; i++) { + *rows = 0; + for (i = 0 ; i < maxrows ; i++) { sprintf(index,"%s%d", idx, i); - if ((tmp = Param_getString(my, name, index, val[i]) == 1) && (status >= 0)) { - status++; + if(((retVal |= Param_getString(my, name, index, &row, val[i])) == 0) && (row == 1)) { + *rows++; } else { - status = tmp; + return retVal; } } - return status; + return retVal; +} + +const char *Param_getErrStr(const Param *my) +{ + return my->strerror; +} + +void Param_strerror(const Param *my, const char *strerror) +{ + realloc(my->strerror, strlen(strerror)); + strcpy(my->strerror, strerror); } diff --git a/allParam/tcl/tclParam.h b/allParam/tcl/tclParam.h index 1c9913d..2e11006 100644 --- a/allParam/tcl/tclParam.h +++ b/allParam/tcl/tclParam.h @@ -7,7 +7,10 @@ #include +#define PARAM_MAX_NAME_LEN 128 + typedef struct ParamS { + char *strerror; Tcl_Interp *interp; } Param; @@ -19,46 +22,51 @@ int conParam(Param *); void desParam(Param *); /* - * All functions have a status as return value. If the return value is positive, - * it contains the number of array members returned (1 in case of - * Param_getString and Param_getInt, between 1 and num in case of - * Param_getStringArray and Param_getIntArray). + * All functions have a status as return value. The value i 0 if everything + * worked correctly and -1 if an error occured. In this case, you can get + * the address of an error String via the function Param_getErrStr(). * * Memory must be allocated for the result before the function is called. + * In the non-array-functions at &row is filled with 0 or 1 depending on + * whether the Parameter is found or not. * For example, calling Param_getInt needs * - * > int status; + * > int row; * > unsigned long int value; - * > if(status = Param_getInt(param, name, idx, value) != 1) { - * > printf("No parameter 'value' found. Exiting.\n"); + * > if(Param_getInt(param, name, idx, &row, &value) != 0) { + * > syslog(LOG_ERR, "Error retrieving value of %s(%s). Exiting.\n", name, idx); + * > exit(-1); + * > } else if(row == 0) { + * > syslog(LOG_ERR, "Parameter %s(%s) not found. Exiting.\n", name, idx); * > exit(-1); * > } - * + * + * In the array functions maxrows must supply the memory available for the + * result while the actual number of result rows is stored in rows. + * * For strings char[PARAM_MAX_VALUE_LEN] has to be allocated. Therefore the * above example expands to * - * > int status; - * > int num = 32; - * > char value[PARAM_MAX_VALUE_LEN][num]; - * > if(status = Param_getStringArray(param, name, idx, num, value) != num) { - * > printf("Not enough elements of 'value' found. Exiting.\n"); + * > int rows; + * > int maxrows = 32; + * > char value[PARAM_MAX_VALUE_LEN][maxrows]; + * > if(Param_getStringArray(param, name, idx, maxrows, &rows, value) != 0) { + * > syslog(LOG_ERR, "Error retrieving values of %s(%s). Exiting.\n", name, idx); * > exit(-1); + * > } else if(row != maxrows) { + * > syslog(LOG_WARNING, "%d Parameters %s(%s) found instead of %d.\n", rows, name, idx, maxrows); * > } * */ -#define PARAM_RV_NO_SUCH_PARAMETER 0 -#define PARAM_RV_FAILED -1 -#define PARAM_RV_NO_INT -2 - -#define PARAM_MAX_NAME_LEN 128 #define PARAM_MAX_VALUE_LEN 128 -int Param_getInt(const Param *, const char *, const char *, unsigned long int *); -int Param_getString(const Param *, const char *, const char *, char *); +int Param_getInt(const Param *, const char *, const char *, int *, unsigned long int *); +int Param_getString(const Param *, const char *, const char *, int *, char *); +int Param_getIntArray(const Param *, const char *, const char *, int, int *, unsigned long int *); +int Param_getStringArray(const Param *, const char *, const char *, int, int *, char **); -int Param_getIntArray(const Param *, const char *, const char *, int, unsigned long int *); -int Param_getStringArray(const Param *, const char *, const char *, int, char **); +const char *Param_getErrStr(const Param *); #endif -- 2.43.0