The R Project SVN R

Rev

Rev 86821 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 86821 Rev 87891
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 2001--2021  The R Core Team
3
 *  Copyright (C) 2001--2025  The R Core Team
4
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
4
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
5
 *
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  the Free Software Foundation; either version 2 of the License, or
Line 174... Line 174...
174
    UNPROTECT(1);
174
    UNPROTECT(1);
175
}
175
}
176
 
176
 
177
typedef struct parse_info {
177
typedef struct parse_info {
178
    Rconnection con;
178
    Rconnection con;
179
    Rboolean old_latin1;
179
    bool old_latin1;
180
    Rboolean old_utf8;
180
    bool old_utf8;
181
}  parse_cleanup_info;
181
}  parse_cleanup_info;
182
 
182
 
183
static void parse_cleanup(void *data)
183
static void parse_cleanup(void *data)
184
{
184
{
185
    parse_cleanup_info *pci = (parse_cleanup_info *)data;
185
    parse_cleanup_info *pci = (parse_cleanup_info *)data;
Line 205... Line 205...
205
    R_ParseError = 0;
205
    R_ParseError = 0;
206
    R_ParseErrorMsg[0] = '\0';
206
    R_ParseErrorMsg[0] = '\0';
207
 
207
 
208
    int ifile = asInteger(CAR(args));                   args = CDR(args);
208
    int ifile = asInteger(CAR(args));                   args = CDR(args);
209
    Rconnection con = getConnection(ifile);
209
    Rconnection con = getConnection(ifile);
210
    Rboolean wasopen = con->isopen;
210
    bool wasopen = con->isopen;
211
    int num = asInteger(CAR(args));			args = CDR(args);
211
    int num = asInteger(CAR(args));			args = CDR(args);
212
    if (num == 0)
212
    if (num == 0)
213
	return(allocVector(EXPRSXP, 0));
213
	return(allocVector(EXPRSXP, 0));
214
 
214
 
215
    SEXP text = PROTECT(coerceVector(CAR(args), STRSXP));
215
    SEXP text = PROTECT(coerceVector(CAR(args), STRSXP));
Line 232... Line 232...
232
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
232
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
233
		 R_NilValue, R_NilValue);
233
		 R_NilValue, R_NilValue);
234
    cntxt.cend = &parse_cleanup;
234
    cntxt.cend = &parse_cleanup;
235
    cntxt.cenddata = &pci;
235
    cntxt.cenddata = &pci;
236
 
236
 
237
    known_to_be_latin1 = known_to_be_utf8 = FALSE;
237
    known_to_be_latin1 = known_to_be_utf8 = false;
238
    Rboolean allKnown = TRUE;
238
    bool allKnown = true;
239
    /* allow 'encoding' to override declaration on 'text'. */
239
    /* allow 'encoding' to override declaration on 'text'. */
240
    if(streql(encoding, "latin1")) {
240
    if(streql(encoding, "latin1")) {
241
	if (!mbcslocale) {
241
	if (!mbcslocale) {
242
	    known_to_be_latin1 = TRUE;
242
	    known_to_be_latin1 = true;
243
	    allKnown = FALSE;
243
	    allKnown = false;
244
	} else
244
	} else
245
	    warning(_("argument encoding=\"latin1\" is ignored in MBCS locales"));
245
	    warning(_("argument encoding=\"latin1\" is ignored in MBCS locales"));
246
    } else if(streql(encoding, "UTF-8"))  {
246
    } else if(streql(encoding, "UTF-8"))  {
247
	if (!mbcslocale || utf8locale) {
247
	if (!mbcslocale || utf8locale) {
248
	    known_to_be_utf8 = TRUE;
248
	    known_to_be_utf8 = true;
249
	    allKnown = FALSE;
249
	    allKnown = false;
250
	} else
250
	} else
251
	    /* the input may be invalid or not parseable when interpreted as
251
	    /* the input may be invalid or not parseable when interpreted as
252
	       in different multi-byte encoding; related to PR#16819 */
252
	       in different multi-byte encoding; related to PR#16819 */
253
	    warning(_("argument encoding=\"UTF-8\" is ignored in MBCS locales"));
253
	    warning(_("argument encoding=\"UTF-8\" is ignored in MBCS locales"));
254
    } else if(!streql(encoding, "unknown") && !streql(encoding, "native.enc"))
254
    } else if(!streql(encoding, "unknown") && !streql(encoding, "native.enc"))
Line 273... Line 273...
273
	*/
273
	*/
274
	if(allKnown)
274
	if(allKnown)
275
	  for(int i = 0; i < length(text); i++)
275
	  for(int i = 0; i < length(text); i++)
276
	    if(!ENC_KNOWN(STRING_ELT(text, i)) &&
276
	    if(!ENC_KNOWN(STRING_ELT(text, i)) &&
277
	       ! IS_ASCII(STRING_ELT(text, i))) {
277
	       ! IS_ASCII(STRING_ELT(text, i))) {
278
		allKnown = FALSE;
278
		allKnown = false;
279
		break;
279
		break;
280
	    }
280
	    }
281
	if(allKnown) {
281
	if(allKnown) {
282
	    known_to_be_latin1 = pci.old_latin1;
282
	    known_to_be_latin1 = pci.old_latin1;
283
	    known_to_be_utf8 = pci.old_utf8;
283
	    known_to_be_utf8 = pci.old_utf8;