Commit 9b2b6200 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replace kvlist_t by KVList.

parent 415ff4d6
......@@ -39,19 +39,19 @@ diffGetParameter(double &abslim, double &rellim, int &mapflag)
const int pargc = operatorArgc();
if (pargc)
{
char **pargv = operatorArgv();
const char **pargv = (const char **) operatorArgv();
list_t *kvlist = list_new(sizeof(keyValues_t *), free_keyval, "DIFF");
if (kvlist_parse_cmdline(kvlist, pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "DIFF";
if (kvlist.parseArguments(pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
const char *key = kv->key;
if (kv->nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv->nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv->values[0];
const char *key = kv.key.c_str();
if (kv.nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv.nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv.values[0].c_str();
// clang-format off
if (cstrIsEqual(key, "abslim")) abslim = parameter2double(value);
......@@ -66,8 +66,6 @@ diffGetParameter(double &abslim, double &rellim, int &mapflag)
else cdoAbort("Invalid parameter key >%s<!", key);
// clang-format on
}
list_destroy(kvlist);
}
}
......
......@@ -88,27 +88,25 @@ fldstatGetParameter(bool *weights)
int pargc = operatorArgc();
if (pargc)
{
char **pargv = operatorArgv();
const char **pargv = (const char **) operatorArgv();
list_t *kvlist = list_new(sizeof(keyValues_t *), free_keyval, "FLDSTAT");
if (kvlist_parse_cmdline(kvlist, pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "FLDSTAT";
if (kvlist.parseArguments(pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
const char *key = kv->key;
if (kv->nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv->nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv->values[0];
const char *key = kv.key.c_str();
if (kv.nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv.nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv.values[0].c_str();
if (cstrIsEqual(key, "weights"))
*weights = parameter2bool(value);
else
cdoAbort("Invalid parameter key >%s<!", key);
}
list_destroy(kvlist);
}
}
......
......@@ -127,22 +127,22 @@ set_parameter(void)
strcpy(name_u, "u");
strcpy(name_v, "v");
int pargc = operatorArgc();
const int pargc = operatorArgc();
if (pargc)
{
char **pargv = operatorArgv();
const char **pargv = (const char **) operatorArgv();
list_t *kvlist = list_new(sizeof(keyValues_t *), free_keyval, "PARAMETER");
if (kvlist_parse_cmdline(kvlist, pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "PARAMETER";
if (kvlist.parseArguments(pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
const char *key = kv->key;
if (kv->nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv->nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv->values[0];
const char *key = kv.key.c_str();
if (kv.nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv.nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv.values[0].c_str();
if (cstrIsEqual(key, "u"))
strcpy(name_u, value);
......@@ -164,8 +164,6 @@ set_parameter(void)
else
cdoAbort("Invalid parameter key >%s<!", key);
}
list_destroy(kvlist);
}
if (Options::cdoVerbose) print_parameter();
......
......@@ -58,34 +58,38 @@ write_const_vars(const int streamID2, const int vlistID2, const int nvars, std::
}
static void
eval_timestepmask(const char *maskfile, list_t *kvlist)
eval_timestepmask(const char *maskfile, KVList &kvlist)
{
int n = 0;
bool *imask = cdo_read_timestepmask(maskfile, &n);
if (imask == NULL) cdoAbort("Read of timestep mask failed!");
int nvals = 0;
int nvalues = 0;
for (int i = 0; i < n; ++i)
if (imask[i]) nvals++;
if (nvals == 0)
if (imask[i]) nvalues++;
if (nvalues == 0)
cdoPrint("timestepmask has no values!");
else
{
char **values = (char **) Malloc(nvals * sizeof(char *));
KeyValues kv;
kv.key = "timestep";
kv.nvalues = nvalues;
kv.values.resize(nvalues);
std::vector<char> value;
int j = 0;
for (int i = 0; i < n; ++i)
{
if (imask[i])
{
const size_t length = (size_t) log10(j + 1) + 2;
values[j] = (char *) Malloc(length * sizeof(char));
sprintf(values[j++], "%d", i + 1);
value.resize(length);
sprintf(value.data(), "%d", i + 1);
kv.values[j++] = value.data();
}
}
kvlist_append(kvlist, "timestep", (const char **) values, nvals);
for (int i = 0; i < nvals; ++i) Free(values[i]);
Free(values);
kvlist.push_back(kv);
}
Free(imask);
......@@ -127,22 +131,24 @@ Select(void *process)
operatorInputArg(cdoOperatorEnter(operatorID));
const int nsel = operatorArgc();
char **argnames = operatorArgv();
const char **argnames = (const char **) operatorArgv();
if (nsel == 0) cdoAbort("Parameter missing!");
list_t *kvlist = kvlist_new("SELECT");
if (kvlist_parse_cmdline(kvlist, nsel, argnames) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "SELECT";
if (kvlist.parseArguments(nsel, argnames) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
const keyValues_t *kv = kvlist_search(kvlist, "timestepmask");
const KeyValues *kv = kvlist.search("timestepmask");
if (kv && kv->nvalues > 0)
{
if (kvlist_search(kvlist, "timestep")) cdoAbort("Parameter timestep and timestepmask can't be combined!");
eval_timestepmask(kv->values[0], kvlist);
if (kvlist.search("timestep")) cdoAbort("Parameter timestep and timestepmask can't be combined!");
eval_timestepmask(kv->values[0].c_str(), kvlist);
}
sellist_t *sellist = sellist_create(kvlist);
SelectList sellist;
sellist_init(sellist, kvlist);
// clang-format off
SELLIST_ADD_INT(timestep_of_year, "Timestep of year");
......@@ -701,7 +707,6 @@ END_LABEL:
vlistDestroy(vlistID2);
sellist_destroy(sellist);
kvlist_destroy(kvlist);
if (tsID2 == 0) cdoAbort("No timesteps selected!");
......
......@@ -145,22 +145,22 @@ getLayerThickness(bool useweights, bool genbounds, int index, int zaxisID, int n
static void
vertstatGetParameter(bool *weights, bool *genbounds)
{
int pargc = operatorArgc();
const int pargc = operatorArgc();
if (pargc)
{
char **pargv = operatorArgv();
const char **pargv = (const char **) operatorArgv();
list_t *kvlist = list_new(sizeof(keyValues_t *), free_keyval, "VERTSTAT");
if (kvlist_parse_cmdline(kvlist, pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "VERTSTAT";
if (kvlist.parseArguments(pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
const char *key = kv->key;
if (kv->nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv->nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv->values[0];
const char *key = kv.key.c_str();
if (kv.nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv.nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv.values[0].c_str();
if (cstrIsEqual(key, "weights"))
*weights = parameter2bool(value);
......@@ -169,8 +169,6 @@ vertstatGetParameter(bool *weights, bool *genbounds)
else
cdoAbort("Invalid parameter key >%s<!", key);
}
list_destroy(kvlist);
}
}
......
......@@ -44,30 +44,28 @@ int yearMode = 0;
static void
set_parameter(void)
{
int pargc = operatorArgc();
const int pargc = operatorArgc();
if (pargc)
{
char **pargv = operatorArgv();
const char **pargv = (const char **) operatorArgv();
list_t *kvlist = list_new(sizeof(keyValues_t *), free_keyval, "PARAMETER");
if (kvlist_parse_cmdline(kvlist, pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist_print(kvlist);
KVList kvlist;
kvlist.name = "YDAYSTAT";
if (kvlist.parseArguments(pargc, pargv) != 0) cdoAbort("Parse error!");
if (Options::cdoVerbose) kvlist.print();
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
const char *key = kv->key;
if (kv->nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv->nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv->values[0];
const char *key = kv.key.c_str();
if (kv.nvalues > 1) cdoAbort("Too many values for parameter key >%s<!", key);
if (kv.nvalues < 1) cdoAbort("Missing value for parameter key >%s<!", key);
const char *value = kv.values[0].c_str();
if (cstrIsEqual(key, "yearMode"))
yearMode = parameter2int(value);
else
cdoAbort("Invalid parameter key >%s<!", key);
}
list_destroy(kvlist);
}
}
......
......@@ -24,7 +24,7 @@ cdo_read_timestepmask(const char *maskfile, int *n)
*n = 0;
int streamID = streamOpenRead(maskfile);
if (streamID == CDI_UNDEFID) cdoAbort("Open failed on %s!", maskfile);
if (streamID < 0) cdoAbort("Open failed on %s!", maskfile);
int vlistID = streamInqVlist(streamID);
......@@ -82,7 +82,7 @@ cdo_read_mask(const char *maskfile, size_t *n)
*n = 0;
int streamID = streamOpenRead(maskfile);
if (streamID == CDI_UNDEFID) cdoAbort("Open failed on %s!", maskfile);
if (streamID < 0) cdoAbort("Open failed on %s!", maskfile);
int vlistID = streamInqVlist(streamID);
......@@ -118,7 +118,7 @@ cdo_read_index(const char *indexfile, size_t *n)
*n = 0;
int streamID = streamOpenRead(indexfile);
if (streamID == CDI_UNDEFID) cdoAbort("Open failed on %s!", indexfile);
if (streamID < 0) cdoAbort("Open failed on %s!", indexfile);
int vlistID = streamInqVlist(streamID);
......
......@@ -14,8 +14,8 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef _LIST_H
#define _LIST_H
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include <stdbool.h>
......
......@@ -78,6 +78,16 @@ KVList::parseArguments(const int argc, const char **argv)
return 0;
}
const KeyValues *
KVList::search(const std::string &key)
{
for (const auto &kv : *this)
{
if (kv.key.compare(key) == 0) return &kv;
}
return NULL;
}
keyValues_t *
kvlist_search(list_t *kvlist, const char *key)
......
......@@ -36,6 +36,7 @@ public:
void print();
int parseArguments(const int argc, const char **argv);
const KeyValues *search(const std::string &key);
};
......
......@@ -21,9 +21,9 @@
#include "parameterConversion.h"
#include "sellist.h"
//#define SELDEBUG 1
// #define SELDEBUG 1
int
static int
get_ival(const char *intstr, int idefault, int istart, int iend, int *ilast)
{
int i;
......@@ -64,231 +64,213 @@ split_intstring(const char *intstr, int *first, int *last, int *inc)
}
}
sellist_t *
sellist_create(list_t *kvlist)
void
sellist_init(SelectList &sellist, KVList &kvlist)
{
sellist_t *sellist = (sellist_t *) Malloc(sizeof(sellist_t));
sellist->size = list_size(kvlist);
sellist->entry = (selentry_t *) Malloc(sellist->size * sizeof(selentry_t));
sellist.resize(kvlist.size());
int i = 0;
for (listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next)
for (const auto &kv : kvlist)
{
keyValues_t *kv = *(keyValues_t **) kvnode->data;
selentry_t *e = &(sellist->entry[i]);
e->key = kv->key;
e->values = kv->values;
e->nvalues = kv->nvalues;
selentry_t &e = sellist[i];
e.key = kv.key;
e.nvalues = kv.nvalues;
e.values.resize(kv.nvalues);
for (int i = 0; i < kv.nvalues; ++i) e.values[i] = kv.values[i];
#ifdef SELDEBUG
printf("%s =", e->key);
for (int ii = 0; ii < e->nvalues; ++ii) printf(" '%s'", e->values[ii]);
printf("%s =", e.key.c_str());
for (int ii = 0; ii < e.nvalues; ++ii) printf(" '%s'", e.values[ii].c_str());
printf("\n");
#endif
++i;
}
for (int i = 0; i < sellist->size; ++i)
for (int i = 0; i < (int)sellist.size(); ++i)
{
selentry_t *e = &(sellist->entry[i]);
e->flag = NULL;
e->cvalues = NULL;
selentry_t &e = sellist[i];
e.flag = NULL;
e.cvalues = NULL;
#ifdef SELDEBUG
printf("%s =", e->key);
for (int ii = 0; ii < e->nvalues; ++ii) printf(" '%s'", e->values[ii]);
printf("%s =", e.key.c_str());
for (int ii = 0; ii < e.nvalues; ++ii) printf(" '%s'", e.values[ii].c_str());
printf("\n");
#endif
}
return sellist;
}
void
sellist_destroy(sellist_t *sellist)
sellist_destroy(SelectList &sellist)
{
if (sellist)
for (int i = 0; i < (int)sellist.size(); ++i)
{
for (int i = 0; i < sellist->size; ++i)
{
selentry_t *e = &(sellist->entry[i]);
if (e->txt) Free(e->txt);
if (e->flag) Free(e->flag);
if (e->cvalues) Free(e->cvalues);
}
Free(sellist);
selentry_t &e = sellist[i];
if (e.txt) Free(e.txt);
if (e.flag) Free(e.flag);
if (e.cvalues) Free(e.cvalues);
}
}
void
sellist_verify(sellist_t *sellist)
sellist_verify(SelectList &sellist)
{
if (sellist)
for (int i = 0; i < (int)sellist.size(); ++i)
{
for (int i = 0; i < sellist->size; ++i)
{
selentry_t *e = &(sellist->entry[i]);
if (e->type == 0) cdoAbort("Unsupported selection keyword: '%s'!", e->key);
}
selentry_t &e = sellist[i];
if (e.type == 0) cdoAbort("Unsupported selection keyword: '%s'!", e.key.c_str());
}
}
int
sellist_add(sellist_t *sellist, const char *txt, const char *name, int type)
sellist_add(SelectList &sellist, const char *txt, const char *name, int type)
{
int idx = -1;
if (sellist)
for (int i = 0; i < (int)sellist.size(); ++i)
{
const char *key = sellist[i].key.c_str();
if (cstrIsEqual(key, name))
{
idx = i;
break;
}
}
if (idx >= 0 && idx < (int)sellist.size())
{
for (int i = 0; i < sellist->size; ++i)
selentry_t &e = sellist[idx];
e.type = type;
e.txt = strdup(txt);
if (e.nvalues && e.cvalues == NULL)
{
const char *key = sellist->entry[i].key;
if (cstrIsEqual(key, name))
switch (type)
{
idx = i;
break;
case SELLIST_INT: e.cvalues = Malloc(e.nvalues * sizeof(int)); break;
case SELLIST_FLT: e.cvalues = Malloc(e.nvalues * sizeof(double)); break;
case SELLIST_WORD: e.cvalues = Malloc(e.nvalues * sizeof(char *)); break;
}
}
if (idx >= 0 && idx < sellist->size)
{
selentry_t *e = &(sellist->entry[idx]);
e->type = type;
e->txt = strdup(txt);
if (e->nvalues && e->cvalues == NULL)
int j = 0;
int nvalues = e.nvalues;
for (int i = 0; i < nvalues; ++i)
switch (type)
{
case SELLIST_INT:
{
switch (type)
int first, last, inc;
split_intstring(e.values[i].c_str(), &first, &last, &inc);
if (first == last)
{
case SELLIST_INT: e->cvalues = Malloc(e->nvalues * sizeof(int)); break;
case SELLIST_FLT: e->cvalues = Malloc(e->nvalues * sizeof(double)); break;
case SELLIST_WORD: e->cvalues = Malloc(e->nvalues * sizeof(char *)); break;
((int *) e.cvalues)[j++] = first;
}
}
int j = 0;
int nvalues = e->nvalues;
for (int i = 0; i < nvalues; ++i) switch (type)
{
case SELLIST_INT:
else
{
int first, last, inc;
split_intstring(e->values[i], &first, &last, &inc);
if (first == last)
{
((int *) e->cvalues)[j++] = first;
}
int k = 0;
if (inc >= 0)
for (int ival = first; ival <= last; ival += inc) k++;
else
for (int ival = first; ival >= last; ival += inc) k++;
e.nvalues += k - 1;
if (e.nvalues)
{
int k = 0;
e.cvalues = Realloc(e.cvalues, e.nvalues * sizeof(int));
if (inc >= 0)
for (int ival = first; ival <= last; ival += inc) k++;
{
for (int ival = first; ival <= last; ival += inc) ((int *) e.cvalues)[j++] = ival;
}
else
for (int ival = first; ival >= last; ival += inc) k++;
e->nvalues += k - 1;
if (e->nvalues)
{
e->cvalues = Realloc(e->cvalues, e->nvalues * sizeof(int));
if (inc >= 0)
{
for (int ival = first; ival <= last; ival += inc) ((int *) e->cvalues)[j++] = ival;
}
else
{
for (int ival = first; ival >= last; ival += inc) ((int *) e->cvalues)[j++] = ival;
}
for (int ival = first; ival >= last; ival += inc) ((int *) e.cvalues)[j++] = ival;
}
}
break;
}
case SELLIST_FLT: ((double *) e->cvalues)[i] = parameter2double(e->values[i]); break;
case SELLIST_WORD: ((const char **) e->cvalues)[i] = parameter2word(e->values[i]); break;
}