1 /*
2 * (C) Copyright 2005- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 *
7 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9 */
10
11 #include "grib_api_internal.h"
12
13 /* Input lon must be in degrees not radians */
14 /* Not to be used for latitudes as they can be -ve */
normalise_longitude_in_degrees(double lon)15 double normalise_longitude_in_degrees(double lon)
16 {
17 while (lon < 0)
18 lon += 360;
19 while (lon > 360)
20 lon -= 360;
21 return lon;
22 }
23
24
25 #ifdef ECCODES_ON_WINDOWS
26 /* Replace C99/Unix rint() for Windows Visual C++ (only before VC++ 2013 versions) */
27 #if defined _MSC_VER && _MSC_VER < 1800
rint(double x)28 double rint(double x)
29 {
30 char* buf = 0;
31 int decimal = 0, sign = 0, err = 0;
32 double result = 0;
33 buf = (char*)malloc(_CVTBUFSIZE);
34 err = _fcvt_s(buf, _CVTBUFSIZE, x, 0, &decimal, &sign);
35 Assert(err == 0);
36 result = atof(buf);
37 if (sign == 1) {
38 result = result * -1;
39 }
40 free(buf);
41 return result;
42 }
43 #endif
44 #endif
45
46
get_dir_separator_char(void)47 char get_dir_separator_char(void)
48 {
49 #ifdef ECCODES_ON_WINDOWS
50 #define DIR_SEPARATOR_CHAR '\\'
51 #else
52 #define DIR_SEPARATOR_CHAR '/'
53 #endif
54 return DIR_SEPARATOR_CHAR;
55 }
56
57 /* Return 1 if the filepath is a regular file, 0 otherwise */
path_is_regular_file(const char * path)58 int path_is_regular_file(const char* path)
59 {
60 struct stat s;
61 int stat_val = stat(path, &s);
62 if (stat_val != 0)
63 return 0; /*error doing stat*/
64 return S_ISREG(s.st_mode); /* 1 if it's a regular file */
65 }
66
67 /* Return 1 if the filepath is a directory, 0 otherwise */
path_is_directory(const char * path)68 int path_is_directory(const char* path)
69 {
70 struct stat s;
71 int stat_val = stat(path, &s);
72 if (stat_val == 0) {
73 if (S_ISDIR(s.st_mode)) {
74 return 1;
75 }
76 }
77 return 0;
78 }
79
codes_getenv(const char * name)80 char* codes_getenv(const char* name)
81 {
82 /* Look for the new ecCodes environment variable names */
83 /* if not found, then look for old grib_api ones for backward compatibility */
84 char* result = getenv(name);
85 if (result == NULL) {
86 const char* old_name = name;
87
88 /* Test the most commonly used variables first */
89 if (STR_EQ(name, "ECCODES_SAMPLES_PATH"))
90 old_name = "GRIB_SAMPLES_PATH";
91 else if (STR_EQ(name, "ECCODES_DEFINITION_PATH"))
92 old_name = "GRIB_DEFINITION_PATH";
93 else if (STR_EQ(name, "ECCODES_DEBUG"))
94 old_name = "GRIB_API_DEBUG";
95
96 else if (STR_EQ(name, "ECCODES_FAIL_IF_LOG_MESSAGE"))
97 old_name = "GRIB_API_FAIL_IF_LOG_MESSAGE";
98 else if (STR_EQ(name, "ECCODES_GRIB_WRITE_ON_FAIL"))
99 old_name = "GRIB_API_WRITE_ON_FAIL";
100 else if (STR_EQ(name, "ECCODES_GRIB_LARGE_CONSTANT_FIELDS"))
101 old_name = "GRIB_API_LARGE_CONSTANT_FIELDS";
102 else if (STR_EQ(name, "ECCODES_NO_ABORT"))
103 old_name = "GRIB_API_NO_ABORT";
104 else if (STR_EQ(name, "ECCODES_GRIBEX_MODE_ON"))
105 old_name = "GRIB_GRIBEX_MODE_ON";
106 else if (STR_EQ(name, "ECCODES_GRIB_IEEE_PACKING"))
107 old_name = "GRIB_IEEE_PACKING";
108 else if (STR_EQ(name, "ECCODES_IO_BUFFER_SIZE"))
109 old_name = "GRIB_API_IO_BUFFER_SIZE";
110 else if (STR_EQ(name, "ECCODES_LOG_STREAM"))
111 old_name = "GRIB_API_LOG_STREAM";
112 else if (STR_EQ(name, "ECCODES_GRIB_NO_BIG_GROUP_SPLIT"))
113 old_name = "GRIB_API_NO_BIG_GROUP_SPLIT";
114 else if (STR_EQ(name, "ECCODES_GRIB_NO_SPD"))
115 old_name = "GRIB_API_NO_SPD";
116 else if (STR_EQ(name, "ECCODES_GRIB_KEEP_MATRIX"))
117 old_name = "GRIB_API_KEEP_MATRIX";
118 else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_DEFINITION_PATH"))
119 old_name = "_GRIB_API_ECMWF_TEST_DEFINITION_PATH";
120 else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_SAMPLES_PATH"))
121 old_name = "_GRIB_API_ECMWF_TEST_SAMPLES_PATH";
122 else if (STR_EQ(name, "ECCODES_GRIB_JPEG"))
123 old_name = "GRIB_JPEG";
124 else if (STR_EQ(name, "ECCODES_GRIB_DUMP_JPG_FILE"))
125 old_name = "GRIB_DUMP_JPG_FILE";
126 else if (STR_EQ(name, "ECCODES_PRINT_MISSING"))
127 old_name = "GRIB_PRINT_MISSING";
128
129 result = getenv(old_name);
130 }
131 return result;
132 }
133