Commit f0fcfdcd authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

added file for cpp module integration

parent 49723c88
...@@ -71,6 +71,8 @@ ...@@ -71,6 +71,8 @@
#define MAX_NUM_VARNAMES 256 #define MAX_NUM_VARNAMES 256
#include <string>
static int Debug = 0; static int Debug = 0;
static int Version = 0; static int Version = 0;
static int Help = 0; static int Help = 0;
...@@ -324,50 +326,47 @@ void cdo_init_is_tty(void) ...@@ -324,50 +326,47 @@ void cdo_init_is_tty(void)
} }
static static
void cdoPrintHelp(const char *phelp[]/*, char *xoperator*/) void cdoPrintHelp(std::vector<std::string> help/*, char *xoperator*/)
{ {
if ( phelp == NULL ) if (help.empty())
fprintf(stderr, "No help available for this operator!\n"); fprintf(stderr, "No help available for this operator!\n");
else else
{ {
bool lprint; bool lprint;
while ( *phelp ) for(unsigned long i = 0; i < help.size(); i++)
{ {
lprint = !(*phelp[0] == '\0' && *(phelp+1) && *(phelp+1)[0] == ' '); lprint = !(help[i][0] == '\0' && help[i+1][0] == ' ');
if ( lprint ) if ( lprint )
{ {
if ( COLOR_STDOUT ) if ( COLOR_STDOUT )
{ {
if ( (strcmp(*phelp, "NAME") == 0) || if ( (help[i].compare( "NAME") == 0) ||
(strcmp(*phelp, "SYNOPSIS") == 0) || (help[i].compare( "SYNOPSIS") == 0) ||
(strcmp(*phelp, "DESCRIPTION") == 0) || (help[i].compare( "DESCRIPTION") == 0) ||
(strcmp(*phelp, "OPERATORS") == 0) || (help[i].compare( "OPERATORS") == 0) ||
(strcmp(*phelp, "NAMELIST") == 0) || (help[i].compare( "NAMELIST") == 0) ||
(strcmp(*phelp, "PARAMETER") == 0) || (help[i].compare( "PARAMETER") == 0) ||
(strcmp(*phelp, "ENVIRONMENT") == 0) || (help[i].compare( "ENVIRONMENT") == 0) ||
(strcmp(*phelp, "NOTE") == 0) || (help[i].compare( "NOTE") == 0) ||
(strcmp(*phelp, "EXAMPLES") == 0) ) (help[i].compare( "EXAMPLES") == 0) )
{ {
set_text_color(stdout, BRIGHT, BLACK); set_text_color(stdout, BRIGHT, BLACK);
fprintf(stdout, "%s", *phelp); fprintf(stdout, "%s", help[i].c_str());
reset_text_color(stdout); reset_text_color(stdout);
fprintf(stdout, "\n"); fprintf(stdout, "\n");
} }
else else
fprintf(stdout, "%s\n", *phelp); fprintf(stdout, "%s\n", help[i].c_str());
} }
else else
{ {
fprintf(stdout, "%s\n", *phelp); fprintf(stdout, "%s\n", help[i].c_str());
} }
} }
phelp++;
} }
} }
} }
static static
void cdoSetDebug(int level) void cdoSetDebug(int level)
{ {
...@@ -1448,7 +1447,7 @@ int main(int argc, char *argv[]) ...@@ -1448,7 +1447,7 @@ int main(int argc, char *argv[])
int lstop = FALSE; int lstop = FALSE;
int noff = 0; int noff = 0;
int status = 0; int status = 0;
char *operatorArg = NULL; const char *operatorArg = NULL;
argument_t *argument = NULL; argument_t *argument = NULL;
cdo_init_is_tty(); cdo_init_is_tty();
...@@ -1469,7 +1468,7 @@ int main(int argc, char *argv[]) ...@@ -1469,7 +1468,7 @@ int main(int argc, char *argv[])
if ( noff ) setDefaultFileType(Progname+noff, 0); if ( noff ) setDefaultFileType(Progname+noff, 0);
get_env_vars(); get_env_vars();
init_modules();
status = parse_options_long(argc, argv); status = parse_options_long(argc, argv);
if ( status != 0 ) return -1; if ( status != 0 ) return -1;
...@@ -1591,7 +1590,14 @@ int main(int argc, char *argv[]) ...@@ -1591,7 +1590,14 @@ int main(int argc, char *argv[])
timer_start(timer_total); timer_start(timer_total);
#ifdef CUSTOM_MODULES
load_custom_modules("custom_modules");
operatorModule(operatorName)(argument); operatorModule(operatorName)(argument);
close_library_handles();
#else
operatorModule(operatorName)(argument);
#endif
timer_stop(timer_total); timer_stop(timer_total);
......
This diff is collapsed.
This diff is collapsed.
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de> /***
See COPYING file for copying and redistribution conditions.
This program is free software; you can redistribute it and/or modify Copyright (C) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
it under the terms of the GNU General Public License as published by See COPYING file for copying and redistribution conditions.
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, This program is free software; you can redistribute it and/or modify
but WITHOUT ANY WARRANTY; without even the implied warranty of it under the terms of the GNU General Public License as published by
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the the Free Software Foundation; version 2 of the License.
GNU General Public License for more details.
*/ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef _MODULES_H #ifndef _MODULES_H
#define _MODULES_H #define _MODULES_H
#define MAX_MOD_OPERATORS 128 // maximum number of operators for a module
#include <iostream>
#include <map>
#include <stdbool.h> #include <stdbool.h>
#include <vector>
/***
type definition for module functions loaded from a custom module
*/
typedef void (*dyn_oper_t)(void *arg);
typedef struct {
void *(*func)(void *); // Module
std::vector<std::string> help; // Help
std::vector<const char *> operators; // Operator names
short mode; // Module mode: 0:intern 1:extern
short number; // Allowed number type
short streamInCnt; // Number of input streams
short streamOutCnt; // Number of output streams
} modules_t;
extern int NumModules;
extern int NumAliases;
/***
vector for library handles for loaded custom modules
*/
static std::vector<void *> custom_modules_lib_handles;
/**
* Maps operator names to their module names
*/
static std::map<std::string, std::string> modules_map;
/**
* Contains added modules as values and their names as key
*/
static std::map<std::string, modules_t> modules;
/***
* Key: operator alias / Value: operator original name
*/
static std::map<std::string, std::string> aliases;
void *(*operatorModule(std::string operatorName))(void *);
void *(*operatorModule(const char *operatorName))(void *); void *(*operatorModule(const char *operatorName))(void *);
void init_modules();
const char **operatorHelp(const char *operatorName); void init_aliases();
int add_alias(std::string alias, std::string original);
void add_module(std::string module_name, modules_t new_module);
std::string get_module_name_to(std::string operatorName);
std::vector<std::string> operatorHelp(std::string operatorName);
int operatorStreamInCnt(const char *operatorName); int operatorStreamInCnt(const char *operatorName);
int operatorStreamOutCnt(const char *operatorName); int operatorStreamOutCnt(const char *operatorName);
int operatorStreamNumber(const char *operatorName); int operatorStreamNumber(const char *operatorName);
void operatorPrintAll(void); void operatorPrintAll(void);
void operatorPrintList(bool print_no_output); void operatorPrintList(bool print_no_output);
#ifdef CUSTOM_MODULES
void load_custom_module(std::string path);
void load_custom_modules(std::string folder_path);
void close_library_handles();
#endif
#endif /* _MODULES_H */ #endif /* _MODULES_H */
/***
Copyright (C) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
See COPYING file for copying and redistribution conditions.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef _MODULES_H
#define _MODULES_H
#define MAX_MOD_OPERATORS 128 // maximum number of operators for a module
#include <iostream>
#include <map>
#include <stdbool.h>
#include <vector>
/***
type definition for module functions loaded from a custom module
*/
typedef void (*dyn_oper_t)(void *arg);
typedef struct {
void *(*func)(void *); // Module
std::vector<std::string> help; // Help
std::vector<const char *> operators; // Operator names
short mode; // Module mode: 0:intern 1:extern
short number; // Allowed number type
short streamInCnt; // Number of input streams
short streamOutCnt; // Number of output streams
} modules_t;
extern int NumModules;
extern int NumAliases;
/***
vector for library handles for loaded custom modules
*/
static std::vector<void *> custom_modules_lib_handles;
/**
* Maps operator names to their module names
*/
static std::map<std::string, std::string> modules_map;
/**
* Contains added modules as values and their names as key
*/
static std::map<std::string, modules_t> modules;
/***
* Key: operator alias / Value: operator original name
*/
static std::map<std::string, std::string> aliases;
void *(*operatorModule(std::string operatorName))(void *);
void *(*operatorModule(const char *operatorName))(void *);
void init_modules();
void init_aliases();
int add_alias(std::string alias, std::string original);
void add_module(std::string module_name, modules_t new_module);
std::string get_module_name_to(std::string operatorName);
std::vector<std::string> operatorHelp(std::string operatorName);
int operatorStreamInCnt(const char *operatorName);
int operatorStreamOutCnt(const char *operatorName);
int operatorStreamNumber(const char *operatorName);
void operatorPrintAll(void);
void operatorPrintList(bool print_no_output);
#ifdef CUSTOM_MODULES
void load_custom_module(std::string path);
void load_custom_modules(std::string folder_path);
void close_library_handles();
#endif
#endif /* _MODULES_H */
This diff is collapsed.
...@@ -221,7 +221,6 @@ char *getOperator(const char *argument) ...@@ -221,7 +221,6 @@ char *getOperator(const char *argument)
return operatorArg; return operatorArg;
} }
const char *operatorAlias(const char *operatorName);
const char *getOperatorName(const char *operatorArg) const char *getOperatorName(const char *operatorArg)
{ {
...@@ -241,7 +240,10 @@ const char *getOperatorName(const char *operatorArg) ...@@ -241,7 +240,10 @@ const char *getOperatorName(const char *operatorArg)
} }
/* return operatorName; */ /* return operatorName; */
return operatorAlias(operatorName); std::string opName = aliases[std::string(operatorName)];
memcpy(operatorName,opName.c_str(),opName.size() );
return operatorName;
} }
...@@ -390,7 +392,15 @@ void input_int(char *arg, int intarr[], int maxint, int *nintfound) ...@@ -390,7 +392,15 @@ void input_int(char *arg, int intarr[], int maxint, int *nintfound)
*nintfound = nint; *nintfound = nint;
} }
std::string string2lower(std::string str)
{
std::string lower_case_string = str;
for(char c : str)
{
c = tolower(c);
}
return lower_case_string;
}
void strtolower(char *str) void strtolower(char *str)
{ {
if ( str ) if ( str )
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#define UNCHANGED_RECORD (processSelf() == 0 && cdoStreamName(0)->argv[0][0] != '-' && cdoRegulargrid == FALSE && cdoDefaultFileType == -1 && cdoDefaultDataType == -1 && cdoDefaultByteorder == -1 ) #define UNCHANGED_RECORD (processSelf() == 0 && cdoStreamName(0)->argv[0][0] != '-' && cdoRegulargrid == FALSE && cdoDefaultFileType == -1 && cdoDefaultDataType == -1 && cdoDefaultByteorder == -1 )
#include <string>
extern char *Progname; extern char *Progname;
extern char *cdoGridSearchDir; extern char *cdoGridSearchDir;
extern int CDO_Reduce_Dim; extern int CDO_Reduce_Dim;
...@@ -221,6 +222,7 @@ int zaxisFromName(const char *zaxisname); ...@@ -221,6 +222,7 @@ int zaxisFromName(const char *zaxisname);
/* refactor: moved here from cdo.h */ /* refactor: moved here from cdo.h */
int cdo_omp_get_thread_num(void); int cdo_omp_get_thread_num(void);
void cdo_omp_set_num_threads(int nthreads); void cdo_omp_set_num_threads(int nthreads);
std::string string2lower(std::string str);
void strtolower(char *str); void strtolower(char *str);
void strtoupper(char *str); void strtoupper(char *str);
......
Markdown is supported
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