Skip to content

Commit

Permalink
Separated headers; CRLF handling; Win .onLoad()
Browse files Browse the repository at this point in the history
Needed to fix CRLG again. GRIB API functions that are made public in
this package have been split out into their own header. Added onLoad
function for Windows to handle GRIB_DEFINITION_PATH
  • Loading branch information
Nathan Wendt committed Jan 4, 2017
1 parent a114801 commit d8c8380
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^configure\.ac$
^README\.md$
^\.DS_Store$
^\.Rhistory$
10 changes: 8 additions & 2 deletions R/grib_open.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@

grib_open <- function(file) {

# test to make sure file is actuall GRIB format
if (.Call("gribr_grib_test", path.expand(file))) {
# does it exist and is it a file
normFile <- normalizePath(file, mustWork = FALSE)
if (!file_test("-f", normFile)) {
stop("%s is not a valid file", normFile)
}

# test to make sure file is actually in GRIB format
if (.Call("gribr_grib_test", file)) {
stop("File is not GRIB format (0 messages found)")
}

Expand Down
7 changes: 7 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.onLoad <- function(library, package) {
if(.Platform$OS.type == "windows") {
if (Sys.getenv("GRIB_DEFINITION_PATH") == "") {
Sys.setenv(GRIB_DEFINITION_PATH = "")
}
}
}
5 changes: 5 additions & 0 deletions src/grib_api_extra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* This header contains functions/structs in the GRIB API that have not been
made public via the main header but are used by gribr */

void grib_index_rewind(grib_index* index);
char *grib_context_full_defs_path(grib_context* c, const char* basename);
15 changes: 15 additions & 0 deletions src/grib_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
#include <Rinternals.h>

#include "gribr.h"
#include "grib_api_extra.h"

SEXP gribr_grib_open(SEXP gribr_fileName) {

const char *p_fileName = NULL;
SEXP gribr_fileHandle;
FILE *input = NULL;
grib_context *c = NULL;
char *def_path = NULL;
const char *basename = "boot.def";

/* This important step makes sure that the GRIB
definition files for the GRIB API are found. If
they are not found and some actions are performed
on a GRIB file, the GRIB API will throw an
exception and R will crash. */
c = grib_context_get_default();
def_path = grib_context_full_defs_path(c, basename);
if (!def_path) {
error("gribr: GRIB definition files not found; Please set the GRIB_DEFINITION_PATH");
}

p_fileName = CHAR(STRING_ELT(gribr_fileName, 0));
input = fopen(p_fileName, "rb");
Expand Down
1 change: 1 addition & 0 deletions src/grib_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>

#include "gribr.h"
#include "grib_api_extra.h"

SEXP gribr_select(SEXP gribr_filePath, SEXP gribr_keyList, SEXP gribr_isMulti) {
int err;
Expand Down
3 changes: 0 additions & 3 deletions src/gribr.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,3 @@ void grewind(FILE* file);
void gerror(const char *str, int err);
SEXP getListElement(SEXP list, const char *str);
SEXP gribr_message_from_handle(grib_handle *h, int isMulti);

/* Extra GRIB API prototypes */
void grib_index_rewind(grib_index* index);

0 comments on commit d8c8380

Please sign in to comment.