1 /*
2   This file is part of CDO. CDO is a collection of Operators to manipulate and analyse Climate model Data.
3 
4   Author: Uwe Schulzweida
5 
6 */
7 
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include <cdi.h>
12 #include "cdo_output.h"
13 #include "util_files.h"
14 
15 int
define_table(const char * tablearg)16 define_table(const char *tablearg)
17 {
18   const char *tablename = tablearg;
19 
20   int tableID = FileUtils::file_exists(tablename) ? tableRead(tablename) : CDI_UNDEFID;
21 
22   if (tableID == CDI_UNDEFID)
23     {
24       char *tablepath = getenv("CD_TABLEPATH");
25       if (tablepath)
26         {
27           int len = sizeof(tablepath) + sizeof(tablename) + 3;
28           char *tablefile = (char *) malloc(len * sizeof(char));
29           strcpy(tablefile, tablepath);
30           strcat(tablefile, "/");
31           strcat(tablefile, tablename);
32           if (FileUtils::file_exists(tablename)) tableID = tableRead(tablefile);
33           free(tablefile);
34         }
35     }
36 
37   if (tableID == CDI_UNDEFID) tableID = tableInq(-1, 0, tablename);
38 
39   if (tableID == CDI_UNDEFID) cdo_abort("table <%s> not found", tablename);
40 
41   return tableID;
42 }
43