[Rcpp-commits] r3677 - pkg/RcppCNPy/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jul 7 15:55:47 CEST 2012
Author: edd
Date: 2012-07-07 15:55:47 +0200 (Sat, 07 Jul 2012)
New Revision: 3677
Modified:
pkg/RcppCNPy/src/cnpy.cpp
pkg/RcppCNPy/src/cnpy.h
pkg/RcppCNPy/src/cnpyMod.cpp
Log:
o on error, use Rf_error to return and not just REprintf
Modified: pkg/RcppCNPy/src/cnpy.cpp
===================================================================
--- pkg/RcppCNPy/src/cnpy.cpp 2012-07-07 01:28:30 UTC (rev 3676)
+++ pkg/RcppCNPy/src/cnpy.cpp 2012-07-07 13:55:47 UTC (rev 3677)
@@ -58,7 +58,7 @@
void cnpy::parse_npy_header(FILE* fp, unsigned int& word_size, unsigned int*& shape, unsigned int& ndims) {
char buffer[256];
- if (fread(buffer,sizeof(char),11,fp) != 11) REprintf("cnpy::parse_npy_header read discprepancy");
+ if (fread(buffer,sizeof(char),11,fp) != 11) Rf_error("cnpy::parse_npy_header read discprepancy");
std::string header = fgets(buffer,256,fp);
Rassert(header[header.size()-1] == '\n', "header ended improperly");
@@ -99,7 +99,7 @@
{
std::vector<char> footer(22);
fseek(fp,-22,SEEK_END);
- if (fread(&footer[0],sizeof(char),22,fp) != 22) REprintf("cnpy::parse_zip_footer read discprepancy");
+ if (fread(&footer[0],sizeof(char),22,fp) != 22) Rf_error("cnpy::parse_zip_footer read discprepancy");
unsigned short disk_no, disk_start, nrecs_on_disk, comment_len;
disk_no = *(unsigned short*) &footer[4];
@@ -129,7 +129,7 @@
arr.shape = std::vector<unsigned int>(shape,shape+ndims);
arr.data = new char[size*word_size];
//int nread = fread(arr.data,word_size,size,fp);
- if (fread(arr.data,word_size,size,fp) != size) REprintf("cnpy::load_the_npy_file read size discrepancy");
+ if (fread(arr.data,word_size,size,fp) != size) Rf_error("cnpy::load_the_npy_file read size discrepancy");
return arr;
}
@@ -146,7 +146,7 @@
arr.shape = std::vector<unsigned int>(shape,shape+ndims);
arr.data = new char[size*word_size];
//int nread = fread(arr.data,word_size,size,fp);
- //if (gzread(fp,arr.data,word_size*size) < 0) REprintf("cnpy::gzload_the_npy_file error");
+ //if (gzread(fp,arr.data,word_size*size) < 0) Rf_error("cnpy::gzload_the_npy_file error");
gzread(fp,arr.data,word_size*size);
return arr;
}
@@ -154,14 +154,14 @@
cnpy::npz_t cnpy::npz_load(std::string fname) {
FILE* fp = fopen(fname.c_str(),"rb");
- if(!fp) REprintf("npz_load: Error! Unable to open file %s!\n",fname.c_str());
+ if(!fp) Rf_error("npz_load: Error! Unable to open file %s!\n",fname.c_str());
Rassert(fp, "fp error");
cnpy::npz_t arrays;
while(1) {
std::vector<char> local_header(30);
- if (fread(&local_header[0],sizeof(char),30,fp) != 30) REprintf("cnpy::npz_load read discprepancy on header");
+ if (fread(&local_header[0],sizeof(char),30,fp) != 30) Rf_error("cnpy::npz_load read discprepancy on header");
//if we've reached the global header, stop reading
if(local_header[2] != 0x03 || local_header[3] != 0x04) break;
@@ -169,7 +169,7 @@
//read in the variable name
unsigned short name_len = *(unsigned short*) &local_header[26];
std::string varname(name_len,' ');
- if (fread(&varname[0],sizeof(char),name_len,fp) != name_len) REprintf("cnpy::npz_load read discprepancy on name_len");
+ if (fread(&varname[0],sizeof(char),name_len,fp) != name_len) Rf_error("cnpy::npz_load read discprepancy on name_len");
//erase the lagging .npy
varname.erase(varname.end()-4,varname.end());
@@ -178,7 +178,7 @@
unsigned short extra_field_len = *(unsigned short*) &local_header[28];
if(extra_field_len > 0) {
std::vector<char> buff(extra_field_len);
- if (fread(&buff[0],sizeof(char),extra_field_len,fp) != extra_field_len) REprintf("cnpy::npz_load read discprepancy on extra_field_len");
+ if (fread(&buff[0],sizeof(char),extra_field_len,fp) != extra_field_len) Rf_error("cnpy::npz_load read discprepancy on extra_field_len");
}
arrays[varname] = load_the_npy_file(fp);
@@ -192,12 +192,12 @@
FILE* fp = fopen(fname.c_str(),"rb");
if(!fp) {
- REprintf("npz_load: Error! Unable to open file %s!\n",fname.c_str());
+ Rf_error("npz_load: Error! Unable to open file %s!\n",fname.c_str());
}
while(1) {
std::vector<char> local_header(30);
- if (fread(&local_header[0],sizeof(char),30,fp) != 30) REprintf("cnpy::npz_load read discprepancy on header");
+ if (fread(&local_header[0],sizeof(char),30,fp) != 30) Rf_error("cnpy::npz_load read discprepancy on header");
//if we've reached the global header, stop reading
if(local_header[2] != 0x03 || local_header[3] != 0x04) break;
@@ -205,7 +205,7 @@
//read in the variable name
unsigned short name_len = *(unsigned short*) &local_header[26];
std::string vname(name_len,' ');
- if (fread(&vname[0],sizeof(char),name_len,fp) != name_len) REprintf("cnpy::npz_load read discprepancy on name_len");
+ if (fread(&vname[0],sizeof(char),name_len,fp) != name_len) Rf_error("cnpy::npz_load read discprepancy on name_len");
vname.erase(vname.end()-4,vname.end()); //erase the lagging .npy
//read in the extra field
@@ -225,7 +225,7 @@
}
fclose(fp);
- REprintf("npz_load: Error! Variable name %s not found in %s!\n",varname.c_str(),fname.c_str());
+ Rf_error("npz_load: Error! Variable name %s not found in %s!\n",varname.c_str(),fname.c_str());
// never reached -- not satisfying -Wall -pedantic
}
@@ -234,7 +234,7 @@
FILE* fp = fopen(fname.c_str(), "rb");
if(!fp) {
- REprintf("npy_load: Error! Unable to open file %s!\n",fname.c_str());
+ Rf_error("npy_load: Error! Unable to open file %s!\n",fname.c_str());
}
NpyArray arr = load_the_npy_file(fp);
@@ -246,7 +246,7 @@
cnpy::NpyArray cnpy::npy_gzload(std::string fname) {
gzFile fp = gzopen(fname.c_str(), "rb");
if(!fp) {
- REprintf("npy_gzload: Error! Unable to open file %s!\n",fname.c_str());
+ Rf_error("npy_gzload: Error! Unable to open file %s!\n",fname.c_str());
}
NpyArray arr = gzload_the_npy_file(fp);
gzclose(fp);
@@ -255,7 +255,7 @@
void cnpy::parse_npy_gzheader(gzFile fp, unsigned int& word_size, unsigned int*& shape, unsigned int& ndims) {
char buffer[256];
- if (gzread(fp,buffer,sizeof(char)*11) != 11) REprintf("cnpy::parse_npy_gzheader read discprepancy");
+ if (gzread(fp,buffer,sizeof(char)*11) != 11) Rf_error("cnpy::parse_npy_gzheader read discprepancy");
std::string header = gzgets(fp, buffer,256);
Rassert(header[header.size()-1] == '\n', "header ended improperly");
Modified: pkg/RcppCNPy/src/cnpy.h
===================================================================
--- pkg/RcppCNPy/src/cnpy.h 2012-07-07 01:28:30 UTC (rev 3676)
+++ pkg/RcppCNPy/src/cnpy.h 2012-07-07 13:55:47 UTC (rev 3677)
@@ -18,12 +18,12 @@
#ifdef RCPP_HAS_LONG_LONG_TYPES
#include <cstdint> // for std::int64_t, needs c++11 switch
#endif
-#include <R_ext/Print.h> // for REprintf
+#include <Rinternals.h> // for Rf_error
namespace cnpy {
inline void Rassert(bool val, std::string txt) {
- if ( ! val) REprintf(txt.c_str());
+ if ( ! val) Rf_error(txt.c_str());
}
struct NpyArray {
@@ -84,17 +84,17 @@
parse_npy_header(fp,word_size,tmp_shape,tmp_dims);
if(word_size != sizeof(T)) {
- REprintf("cnpy error: %s has word size %d but npy_save appending data sized %d\n", fname.c_str(), word_size, sizeof(T));
+ Rf_error("cnpy error: %s has word size %d but npy_save appending data sized %d\n", fname.c_str(), word_size, sizeof(T));
//assert( word_size == sizeof(T) );
}
if(tmp_dims != ndims) {
- REprintf("cnpy error: npy_save attempting to append misdimensioned data to %s\n", fname.c_str());
+ Rf_error("cnpy error: npy_save attempting to append misdimensioned data to %s\n", fname.c_str());
//assert(tmp_dims == ndims);
}
for(unsigned int i = 1; i < ndims; i++) {
if(shape[i] != tmp_shape[i]) {
- REprintf("cnpy error: npy_save attempting to append misshaped data to %s\n", fname.c_str());
+ Rf_error("cnpy error: npy_save attempting to append misshaped data to %s\n", fname.c_str());
//assert(shape[i] == tmp_shape[i]);
}
}
Modified: pkg/RcppCNPy/src/cnpyMod.cpp
===================================================================
--- pkg/RcppCNPy/src/cnpyMod.cpp 2012-07-07 01:28:30 UTC (rev 3676)
+++ pkg/RcppCNPy/src/cnpyMod.cpp 2012-07-07 13:55:47 UTC (rev 3677)
@@ -68,7 +68,7 @@
#endif
} else {
arr.destruct();
- REprintf("Unsupported type in npyLoad");
+ Rf_error("Unsupported type in npyLoad");
}
} else if (shape.size() == 2) {
if (type == "numeric") {
@@ -81,7 +81,7 @@
#endif
} else {
arr.destruct();
- REprintf("Unsupported type in npyLoad");
+ Rf_error("Unsupported type in npyLoad");
}
} else {
Rf_error("Unsupported dimension in npyLoad");
@@ -105,7 +105,7 @@
cnpy::npy_save(filename, mat.begin(), &(shape[0]), 2, mode);
#endif
} else {
- REprintf("Unsupported matrix type\n");
+ Rf_error("Unsupported matrix type\n");
}
} else if (::Rf_isVector(x)) {
if (::Rf_isNumeric(x)) {
@@ -121,10 +121,10 @@
cnpy::npy_save(filename, vec.begin(), &(shape[0]), 1, mode);
#endif
} else {
- REprintf("Unsupported vector type\n");
+ Rf_error("Unsupported vector type\n");
}
} else {
- REprintf("Unsupported type\n");
+ Rf_error("Unsupported type\n");
}
}
More information about the Rcpp-commits
mailing list