Commit 27e8f896 authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

refactored Options::cdoVarnames to std::vector<std::string>

parent 9a52b1f7
Pipeline #4309 passed with stages
in 21 minutes and 6 seconds
......@@ -38,14 +38,14 @@ fill_vars(const VarList &varList, std::vector<bool> &vars)
{
const int nvars = vars.size();
if (Options::cdoNumVarnames)
if (Options::cdoNumVarnames() > 0)
{
auto found = false;
for (int varID = 0; varID < nvars; ++varID)
{
vars[varID] = false;
for (int i = 0; i < Options::cdoNumVarnames; ++i)
if (strcmp(varList[varID].name, Options::cdoVarnames[i]) == 0)
for (int i = 0; i < Options::cdoNumVarnames(); ++i)
if (strcmp(varList[varID].name, Options::cdoVarnames[i].c_str()) == 0)
{
vars[varID] = true;
found = true;
......@@ -53,7 +53,7 @@ fill_vars(const VarList &varList, std::vector<bool> &vars)
}
}
if (!found) cdoAbort("Variable %s%s not found!", Options::cdoVarnames[0], Options::cdoNumVarnames > 1 ? ",..." : "");
if (!found) cdoAbort("Variable %s%s not found!", Options::cdoVarnames[0], Options::cdoNumVarnames() > 1 ? ",..." : "");
}
else
{
......
......@@ -661,38 +661,26 @@ defineChunktype(const std::string &arg)
// clang-format on
}
static void
std::vector<std::string>
defineVarnames(const char *const arg)
{
size_t len = strlen(arg);
size_t istart = 0;
while (istart < len && (arg[istart] == ' ' || arg[istart] == ',')) istart++;
len -= istart;
if (len)
{
Options::cdoVarnames = (char **) Malloc(MAX_NUM_VARNAMES * sizeof(char *));
std::string strArgs = std::string(arg);
std::vector<std::string> newVarnames;
char *pbuf = strdup(arg + istart);
Options::cdoVarnames[Options::cdoNumVarnames++] = pbuf;
const char delim = ',';
size_t previous = 0;
size_t current = strArgs.find(delim);
char *commapos = pbuf;
while ((commapos = strchr(commapos, ',')) != nullptr)
{
*commapos++ = '\0';
if (strlen(commapos))
{
if (Options::cdoNumVarnames >= MAX_NUM_VARNAMES) cdoAbort("Too many variable names (limit=%d)!", MAX_NUM_VARNAMES);
Options::cdoVarnames[Options::cdoNumVarnames++] = commapos;
}
}
/*
for ( int i = 0; i < Options::cdoNumVarnames; ++i )
printf("varname %d: %s\n", i+1, Options::cdoVarnames[i]);
*/
while (current != std::string::npos)
{
newVarnames.push_back(strArgs.substr(previous, current - previous));
previous = current + 1;
current = strArgs.find(delim, previous);
}
newVarnames.push_back(strArgs.substr(previous, current - previous));
Options::cdoVarnames = newVarnames;
}
static void
......@@ -1264,7 +1252,7 @@ parseOptionsLong(int argc, char *argv[])
case 'l': defineZaxis(CDO_optarg); break;
case 'm': cdiDefMissval(atof(CDO_optarg)); break;
case 'M': cdiDefGlobal("HAVE_MISSVAL", true); break;
case 'n': defineVarnames(CDO_optarg); break;
case 'n': Options::cdoVarnames = defineVarnames(CDO_optarg); break;
case 'O': Options::cdoOverwriteMode = true; break;
case 'P': numThreads = parameter2int(CDO_optarg); break;
case 'p':
......@@ -1894,12 +1882,6 @@ main(int argc, char *argv[])
if (Options::Timer) timer_report();
}
if (Options::cdoVarnames)
{
if (Options::cdoNumVarnames) Free(Options::cdoVarnames[0]);
Free(Options::cdoVarnames);
}
if (CDO_Rusage) cdo_rusage();
if (!status) status = Options::cdoExitStatus;
......
......@@ -72,8 +72,10 @@ int cdoChunkType = CDI_UNDEFID;
bool cdoOverwriteMode = false;
bool cdoParIO = false;
bool cdoRegulargrid = false;
int cdoNumVarnames = 0;
char **cdoVarnames = nullptr;
std::vector<std::string> cdoVarnames;
size_t cdoNumVarnames(){
return cdoVarnames.size();
}
bool REMAP_genweights = true;
......
......@@ -17,6 +17,9 @@
#ifndef CDO_OPTIONS_H
#define CDO_OPTIONS_H
#include <vector>
#include <string>
namespace cdo
{
extern const char *progname;
......@@ -74,8 +77,8 @@ extern int cdoChunkType;
extern bool cdoOverwriteMode;
extern bool cdoParIO;
extern bool cdoRegulargrid;
extern int cdoNumVarnames;
extern char **cdoVarnames;
size_t cdoNumVarnames();
extern std::vector<std::string> cdoVarnames;
extern bool REMAP_genweights;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment