The R Project SVN R

Rev

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

Rev 45475 Rev 46045
Line 36... Line 36...
36
#include <config.h>
36
#include <config.h>
37
#endif
37
#endif
38
 
38
 
39
#include <Defn.h>
39
#include <Defn.h>
40
#include <stdlib.h>
40
#include <stdlib.h>
-
 
41
#include <Rinternals.h>
-
 
42
#include <R_ext/Parse.h>  /* parsing is used in handling escape codes */
-
 
43
 
41
 
44
 
42
#ifndef _Xconst
45
#ifndef _Xconst
43
#define _Xconst const
46
#define _Xconst const
44
#endif
47
#endif
45
#include <X11/X.h>
48
#include <X11/X.h>
Line 1041... Line 1044...
1041
	SET_VECTOR_ELT(DE->work, wcol - 1, tmp2);
1044
	SET_VECTOR_ELT(DE->work, wcol - 1, tmp2);
1042
    }
1045
    }
1043
    return newcol;
1046
    return newcol;
1044
}
1047
}
1045
 
1048
 
-
 
1049
static SEXP lang5(SEXP s, SEXP t, SEXP u, SEXP v, SEXP w)
-
 
1050
{
-
 
1051
    PROTECT(s);
-
 
1052
    s = LCONS(s, list4(t, u, v, w));
-
 
1053
    UNPROTECT(1);
-
 
1054
    return s;
-
 
1055
}
-
 
1056
 
-
 
1057
static SEXP processEscapes(SEXP x)
-
 
1058
{
-
 
1059
    SEXP newval, pattern, replacement, expr;
-
 
1060
    ParseStatus status;
-
 
1061
    
-
 
1062
    /* We process escape sequences in a scalar string by escaping
-
 
1063
       unescaped quotes, then quoting the whole thing and parsing it.  This
-
 
1064
       is supposed to be equivalent to the R code
-
 
1065
 
-
 
1066
       newval <- gsub(perl=TRUE, "(?<!\\\\)((\\\\\\\\)*)\"", "\\1\\\\\"", x)
-
 
1067
       newval <- sub('(^.*$)', '"\1"', newval)
-
 
1068
       newval <- eval(parse(text=newval))
-
 
1069
 
-
 
1070
       We do it this way to avoid extracting the escape handling
-
 
1071
       code from the parser.  We need it in C code because this may be executed
-
 
1072
       numerous times from C in dataentry.c */
-
 
1073
    	
-
 
1074
    PROTECT( pattern = mkString("(?<!\\\\)((\\\\\\\\)*)\"") );
-
 
1075
    PROTECT( replacement = mkString("\\1\\\\\"") );
-
 
1076
    PROTECT( expr = lang5(install("gsub"), ScalarLogical(1), pattern, replacement, x) );
-
 
1077
    SET_TAG( CDR(expr), install("perl") );
-
 
1078
 
-
 
1079
    PROTECT( newval = eval(expr, R_BaseEnv) );
-
 
1080
    PROTECT( pattern = mkString("(^.*$)") );
-
 
1081
    PROTECT( replacement = mkString("\"\\1\"") );
-
 
1082
    PROTECT( expr = lang4(install("sub"), pattern, replacement, newval) );
-
 
1083
    PROTECT( newval = eval(expr, R_BaseEnv) );
-
 
1084
    PROTECT( expr = R_ParseVector( newval, 1, &status, R_NilValue) );
-
 
1085
    
-
 
1086
    /* We only handle the first entry. If this were available more generally,
-
 
1087
       we'd probably want to loop over all of expr */
-
 
1088
       
-
 
1089
    if (status == PARSE_OK && length(expr))
-
 
1090
	PROTECT( newval = eval(VECTOR_ELT(expr, 0), R_BaseEnv) );
-
 
1091
    else
-
 
1092
	PROTECT( newval = R_NilValue );  /* protect just so the count doesn't change */
-
 
1093
    UNPROTECT(10);
-
 
1094
    return newval;
-
 
1095
}
-
 
1096
 
1046
/* close up the entry to a cell, put the value that has been entered
1097
/* close up the entry to a cell, put the value that has been entered
1047
   into the correct place and as the correct type */
1098
   into the correct place and as the correct type */
1048
 
1099
 
1049
static void closerect(DEstruct DE)
1100
static void closerect(DEstruct DE)
1050
{
1101
{
Line 1088... Line 1139...
1088
	    if (clength != 0) {
1139
	    if (clength != 0) {
1089
		/* do it this way to ensure NA, Inf, ...  can get set */
1140
		/* do it this way to ensure NA, Inf, ...  can get set */
1090
		char *endp;
1141
		char *endp;
1091
		double new = R_strtod(buf, &endp);
1142
		double new = R_strtod(buf, &endp);
1092
		Rboolean warn = !isBlankString(endp);
1143
		Rboolean warn = !isBlankString(endp);
1093
		if (TYPEOF(cvec) == STRSXP)
1144
		if (TYPEOF(cvec) == STRSXP) {
-
 
1145
		    SEXP newval;
-
 
1146
		    PROTECT( newval = mkString(buf) );
-
 
1147
		    PROTECT( newval = processEscapes(newval) );
-
 
1148
		    if (TYPEOF(newval) == STRSXP && length(newval) == 1)
1094
		    SET_STRING_ELT(cvec, wrow - 1, mkChar(buf));
1149
			SET_STRING_ELT(cvec, wrow - 1, STRING_ELT(newval, 0));
-
 
1150
		    else
-
 
1151
			warning("dataentry: parse error on string");
-
 
1152
		    UNPROTECT(2);
1095
		else
1153
		} else
1096
		    REAL(cvec)[wrow - 1] = new;
1154
		    REAL(cvec)[wrow - 1] = new;
1097
		if (newcol && warn) {
1155
		if (newcol && warn) {
1098
		    /* change mode to character */
1156
		    /* change mode to character */
1099
		    SEXP tmp = coerceVector(cvec, STRSXP);
1157
		    SEXP tmp = coerceVector(cvec, STRSXP);
1100
		    SET_STRING_ELT(tmp, wrow - 1, mkChar(buf));
1158
		    SET_STRING_ELT(tmp, wrow - 1, mkChar(buf));