| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <R.h> |
| 25 |
#include <Rdefines.h> |
| 26 |
#include <R_ext/Rdynload.h> |
| 27 |
|
| 28 |
#include <biosig.h> |
| 29 |
|
| 30 |
SEXP sload(SEXP filename, SEXP channels) { |
| 31 |
|
| 32 |
|
| 33 |
if(!isString(filename) || length(filename) != 1) |
| 34 |
error("filename is not a single string"); |
| 35 |
|
| 36 |
|
| 37 |
HDRTYPE *hdr = sopen(CHAR(asChar(filename)), "r", NULL); |
| 38 |
if (serror2(hdr)) return R_NilValue; |
| 39 |
|
| 40 |
long NS = biosig_get_number_of_channels(hdr); |
| 41 |
size_t SPR = biosig_get_number_of_samples(hdr); |
| 42 |
|
| 43 |
|
| 44 |
SEXP result = PROTECT(allocMatrix(REALSXP, SPR, NS)); |
| 45 |
|
| 46 |
|
| 47 |
int status = sread(REAL(result), 0, SPR, hdr); |
| 48 |
if (serror2(hdr)) { |
| 49 |
destructHDR(hdr); |
| 50 |
free(result); |
| 51 |
UNPROTECT(1); |
| 52 |
return R_NilValue; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
destructHDR(hdr); |
| 57 |
UNPROTECT(1); |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
SEXP jsonHeader (SEXP filename) { |
| 62 |
|
| 63 |
if(!isString(filename) || length(filename) != 1) |
| 64 |
error("filename is not a single string"); |
| 65 |
|
| 66 |
|
| 67 |
HDRTYPE *hdr = sopen(CHAR(asChar(filename)), "r", NULL); |
| 68 |
if (serror2(hdr)) return R_NilValue; |
| 69 |
|
| 70 |
|
| 71 |
SEXP result = R_NilValue; |
| 72 |
char *str = NULL; |
| 73 |
asprintf_hdr2json(&str, hdr); |
| 74 |
if (str != NULL) { |
| 75 |
result = PROTECT(allocVector(STRSXP, strlen(str)+1)); |
| 76 |
SET_STRING_ELT(result, 0, mkChar(str)); |
| 77 |
free(str); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
destructHDR(hdr); |
| 82 |
UNPROTECT(1); |
| 83 |
return result; |
| 84 |
} |