Commit 5a45ed36 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added operator kvldump.

parent 1282e4e6
......@@ -20,17 +20,79 @@
*/
#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
static
void print_values(int nvalues, char **values)
{
if ( nvalues && values )
{
int dtype = literals_find_datatype(nvalues, values);
for ( int i = 0; i < nvalues; ++i )
{
if ( i ) printf(", ");
switch (dtype)
{
case CDI_DATATYPE_INT8: printf("%db", literal_to_int(values[i])); break;
case CDI_DATATYPE_INT16: printf("%ds", literal_to_int(values[i])); break;
case CDI_DATATYPE_INT32: printf("%d", literal_to_int(values[i])); break;
case CDI_DATATYPE_FLT32: printf("%gf", literal_to_double(values[i])); break;
case CDI_DATATYPE_FLT64: printf("%g", literal_to_double(values[i])); break;
default: printf("\"%s\"", values[i]);
}
}
}
}
void kvldump(list_t *pmlist)
{
if ( pmlist )
{
for ( listNode_t *pmnode = pmlist->head; pmnode; pmnode = pmnode->next )
{
if ( pmnode->data )
{
list_t *kvlist = *(list_t **)pmnode->data;
if ( kvlist )
{
const char *listname = list_name(kvlist);
if ( listname ) printf("&%s\n", list_name(kvlist));
for ( listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next )
{
keyValues_t *kv = *(keyValues_t **)kvnode->data;
const char *key = kv->key;
if ( listname ) printf(" ");
printf("%s = ", key);
print_values(kv->nvalues, kv->values);
printf("\n");
}
if ( listname ) printf("/\n");
}
}
}
}
}
void *Nmldump(void *argument)
{
cdoInitialize(argument);
int NMLDUMP = cdoOperatorAdd("nmldump", 0, 0, NULL);
int KVLDUMP = cdoOperatorAdd("kvldump", 0, 0, NULL);
int operatorID = cdoOperatorID();
list_t *pmlist = namelist_to_pmlist(stdin, "STDIN");
list_for_each(pmlist, pmlist_print_iter);
if ( operatorID == NMLDUMP )
list_for_each(pmlist, pmlist_print_iter);
else if ( operatorID == KVLDUMP )
kvldump(pmlist);
list_destroy(pmlist);
......
......@@ -371,7 +371,7 @@ void *Maggraph(void *argument);
#define MrotuvOperators {"mrotuv"}
#define MrotuvbOperators {"mrotuvb"}
#define NinfoOperators {"nyear", "nmon", "ndate", "ntime", "ncode", "npar", "nlevel", "ngridpoints", "ngrids"}
#define NmldumpOperators {"nmldump"}
#define NmldumpOperators {"nmldump", "kvldump"}
#define OutputOperators {"output", "outputint", "outputsrv", "outputext", "outputf", "outputts", \
"outputfld", "outputarr", "outputxyz"}
#define OutputtabOperators {"outputtab"}
......
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#include "cdo_int.h"
#include "cdi.h"
int literal_get_datatype(const char *literal)
{
if ( literal && *literal )
......@@ -14,7 +12,7 @@ int literal_get_datatype(const char *literal)
errno = 0;
long lval = strtol(literal, &endptr, 10);
if ( errno == 0 && *endptr == 0 ) return CDI_DATATYPE_INT32;
else if ( errno == 0 && *(endptr+1) == 0 )
else if ( errno == 0 && *(endptr+1) == 0 && (*endptr == 's' || *endptr == 'b') )
{
if ( *endptr == 's' && lval >= SHRT_MIN && lval <= SHRT_MAX )
return CDI_DATATYPE_INT16;
......@@ -107,7 +105,7 @@ double literal_to_double(const char *literal)
int main(void)
{
const char *literals[] = {"127b", "-32768s", "-2147483647", "-1.e+36f", "1.e+308", "temperature", "surface pressure"};
const char *literals[] = {"127b", "-32768s", "-2147483647", "-1.e+36f", "1.e+308", "temperature", "surface pressure", "1000."};
int nliterals = sizeof(literals) / sizeof(literals[0]);
for ( int i = 0; i < nliterals; ++i )
......
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