The R Project SVN R

Rev

Rev 85771 | Rev 87180 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 85771 Rev 86452
Line 2734... Line 2734...
2734
    }
2734
    }
2735
    UNPROTECT(1);
2735
    UNPROTECT(1);
2736
    vmaxset(vmax);
2736
    vmaxset(vmax);
2737
    return ans;
2737
    return ans;
2738
}
2738
}
2739
 
-
 
2740
#define NO_CALL_R
-
 
2741
 
-
 
2742
#ifndef NO_CALL_R
-
 
2743
// call_R was deprecated in R 2.15.0 and removed from RS.h in R 4.2.0
-
 
2744
 
-
 
2745
static const struct {
-
 
2746
    const char *name;
-
 
2747
    const SEXPTYPE type;
-
 
2748
}
-
 
2749
 
-
 
2750
typeinfo[] = {
-
 
2751
    {"logical",	  LGLSXP },
-
 
2752
    {"integer",	  INTSXP },
-
 
2753
    {"double",	  REALSXP},
-
 
2754
    {"complex",	  CPLXSXP},
-
 
2755
    {"character", STRSXP },
-
 
2756
    {"list",	  VECSXP },
-
 
2757
    {NULL,	  0      }
-
 
2758
};
-
 
2759
 
-
 
2760
static int string2type(char *s)
-
 
2761
{
-
 
2762
    int i;
-
 
2763
    for (i = 0 ; typeinfo[i].name ; i++) {
-
 
2764
	if(!strcmp(typeinfo[i].name, s)) {
-
 
2765
	    return typeinfo[i].type;
-
 
2766
	}
-
 
2767
    }
-
 
2768
    error(_("type \"%s\" not supported in interlanguage calls"), s);
-
 
2769
    return 1; /* for -Wall */
-
 
2770
}
-
 
2771
 
-
 
2772
/* This is entirely legacy, with no known users (Mar 2012).
-
 
2773
   So we freeze the code involved.
-
 
2774
 */
-
 
2775
 
-
 
2776
static void *RObjToCPtr2(SEXP s)
-
 
2777
{
-
 
2778
    int n;
-
 
2779
 
-
 
2780
    switch(TYPEOF(s)) {
-
 
2781
    case LGLSXP:
-
 
2782
    case INTSXP:
-
 
2783
	n = LENGTH(s);
-
 
2784
	int *iptr = INTEGER(s);
-
 
2785
	iptr = (int*) R_alloc(n, sizeof(int));
-
 
2786
	for (int i = 0 ; i < n ; i++) iptr[i] = INTEGER(s)[i];
-
 
2787
	return (void*) iptr;
-
 
2788
	break;
-
 
2789
    case REALSXP:
-
 
2790
	n = LENGTH(s);
-
 
2791
	double *rptr = REAL(s);
-
 
2792
	rptr = (double*) R_alloc(n, sizeof(double));
-
 
2793
	for (int i = 0 ; i < n ; i++) rptr[i] = REAL(s)[i];
-
 
2794
	return (void*) rptr;
-
 
2795
	break;
-
 
2796
    case CPLXSXP:
-
 
2797
	n = LENGTH(s);
-
 
2798
	Rcomplex *zptr = COMPLEX(s);
-
 
2799
	zptr = (Rcomplex*) R_alloc(n, sizeof(Rcomplex));
-
 
2800
	for (int i = 0 ; i < n ; i++) zptr[i] = COMPLEX(s)[i];
-
 
2801
	return (void*) zptr;
-
 
2802
	break;
-
 
2803
    case STRSXP:
-
 
2804
	n = LENGTH(s);
-
 
2805
	char **cptr = (char**) R_alloc(n, sizeof(char*));
-
 
2806
	for (int i = 0 ; i < n ; i++) {
-
 
2807
	    const char *ss = translateChar(STRING_ELT(s, i));
-
 
2808
	    cptr[i] = (char*) R_alloc(strlen(ss) + 1, sizeof(char));
-
 
2809
	    strcpy(cptr[i], ss);
-
 
2810
	}
-
 
2811
	return (void*) cptr;
-
 
2812
	break;
-
 
2813
	/* From here down, probably not right */
-
 
2814
    case VECSXP:
-
 
2815
	n = length(s);
-
 
2816
	SEXP *lptr = (SEXP *) R_alloc(n, sizeof(SEXP));
-
 
2817
	for (int i = 0 ; i < n ; i++) lptr[i] = VECTOR_ELT(s, i);
-
 
2818
	return (void*) lptr;
-
 
2819
	break;
-
 
2820
    default:
-
 
2821
	return (void*) s;
-
 
2822
    }
-
 
2823
}
-
 
2824
 
-
 
2825
void call_R(char *func, long nargs, void **arguments, char **modes,
-
 
2826
	    long *lengths, char **names, long nres, char **results)
-
 
2827
{
-
 
2828
    SEXP call, pcall, s;
-
 
2829
    SEXPTYPE type;
-
 
2830
    int i, j, n;
-
 
2831
 
-
 
2832
    if (!isFunction((SEXP)func))
-
 
2833
	error("invalid function in call_R");
-
 
2834
    if (nargs < 0)
-
 
2835
	error("invalid argument count in call_R");
-
 
2836
    if (nres < 0)
-
 
2837
	error("invalid return value count in call_R");
-
 
2838
    PROTECT(pcall = call = allocList((int) nargs + 1));
-
 
2839
    SET_TYPEOF(call, LANGSXP);
-
 
2840
    SETCAR(pcall, (SEXP)func);
-
 
2841
    s = R_NilValue;		/* -Wall */
-
 
2842
    for (i = 0 ; i < nargs ; i++) {
-
 
2843
	pcall = CDR(pcall);
-
 
2844
	type = string2type(modes[i]);
-
 
2845
	switch(type) {
-
 
2846
	case LGLSXP:
-
 
2847
	case INTSXP:
-
 
2848
	    n = (int) lengths[i];
-
 
2849
	    SETCAR(pcall, allocVector(type, n));
-
 
2850
	    memcpy(INTEGER(CAR(pcall)), arguments[i], n * sizeof(int));
-
 
2851
	    break;
-
 
2852
	case REALSXP:
-
 
2853
	    n = (int) lengths[i];
-
 
2854
	    SETCAR(pcall, allocVector(REALSXP, n));
-
 
2855
	    memcpy(REAL(CAR(pcall)), arguments[i], n * sizeof(double));
-
 
2856
	    break;
-
 
2857
	case CPLXSXP:
-
 
2858
	    n = (int) lengths[i];
-
 
2859
	    SETCAR(pcall, allocVector(CPLXSXP, n));
-
 
2860
	    memcpy(REAL(CAR(pcall)), arguments[i], n * sizeof(Rcomplex));
-
 
2861
	    break;
-
 
2862
	case STRSXP:
-
 
2863
	    n = (int) lengths[i];
-
 
2864
	    SETCAR(pcall, allocVector(STRSXP, n));
-
 
2865
	    for (j = 0 ; j < n ; j++) {
-
 
2866
		char *str = (char*)(arguments[i]);
-
 
2867
		SET_STRING_ELT(CAR(pcall), i, mkChar(str));
-
 
2868
	    }
-
 
2869
	    break;
-
 
2870
	default:
-
 
2871
	    error(_("mode '%s' is not supported in call_R"), modes[i]);
-
 
2872
	}
-
 
2873
	if(names && names[i])
-
 
2874
	    SET_TAG(pcall, install(names[i]));
-
 
2875
	ENSURE_NAMEDMAX(CAR(pcall));
-
 
2876
    }
-
 
2877
    PROTECT(s = eval(call, R_GlobalEnv));
-
 
2878
    switch(TYPEOF(s)) {
-
 
2879
    case LGLSXP:
-
 
2880
    case INTSXP:
-
 
2881
    case REALSXP:
-
 
2882
    case CPLXSXP:
-
 
2883
    case STRSXP:
-
 
2884
	if(nres > 0)
-
 
2885
	    results[0] = (char *) RObjToCPtr2(s);
-
 
2886
	break;
-
 
2887
    case VECSXP:
-
 
2888
	n = length(s);
-
 
2889
	if (nres < n) n = (int) nres;
-
 
2890
	for (i = 0 ; i < n ; i++)
-
 
2891
	    results[i] = (char *) RObjToCPtr2(VECTOR_ELT(s, i));
-
 
2892
	break;
-
 
2893
    case LISTSXP:
-
 
2894
	n = length(s);
-
 
2895
	if(nres < n) n = (int) nres;
-
 
2896
	for(i = 0 ; i < n ; i++) {
-
 
2897
	    results[i] = (char *) RObjToCPtr2(s);
-
 
2898
	    s = CDR(s);
-
 
2899
	}
-
 
2900
	break;
-
 
2901
    }
-
 
2902
    UNPROTECT(2);
-
 
2903
    return;
-
 
2904
}
-
 
2905
 
-
 
2906
void call_S(char *func, long nargs, void **arguments, char **modes,
-
 
2907
	    long *lengths, char **names, long nres, char **results)
-
 
2908
{
-
 
2909
    call_R(func, nargs, arguments, modes, lengths, names, nres, results);
-
 
2910
}
-
 
2911
#endif
-