The R Project SVN R

Rev

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

Rev 88273 Rev 90200
Line 29... Line 29...
29
#include <tre/tre.h>
29
#include <tre/tre.h>
30
 
30
 
31
static SEXP allocMatrixNA(SEXPTYPE, int, int);
31
static SEXP allocMatrixNA(SEXPTYPE, int, int);
32
static void transferVector(SEXP s, SEXP t);
32
static void transferVector(SEXP s, SEXP t);
33
 
33
 
-
 
34
/* Build a CHARSXP marked as UTF-8 from the NUL-terminated string 's'.
-
 
35
   DCF files are required to be UTF-8, so 's' is interpreted as UTF-8
-
 
36
   regardless of its actual encoding; any invalid byte sequences are
-
 
37
   repaired by escaping them as "<xx>", exactly as
-
 
38
   iconv(from = "UTF-8", to = "UTF-8", sub = "byte") does (which the R
-
 
39
   code paths in read.dcf()/write.dcf() also use).  The common case of
-
 
40
   already-valid input is handled without conversion. */
-
 
41
static SEXP mkCharUTF8sub(const char *s)
-
 
42
{
-
 
43
    if (utf8Valid(s))
-
 
44
	return mkCharCE(s, CE_UTF8);
-
 
45
 
-
 
46
    /* reEnc3() performs the iconv() repair via Riconv(); subst = 1 selects
-
 
47
       the "<xx>" hexadecimal substitution for invalid bytes.  It returns a
-
 
48
       string allocated with R_alloc() (and freed at the vmaxset() in
-
 
49
       do_readDCF()), or 's' itself if iconv is unavailable.  Either way
-
 
50
       mkCharCE() copies the bytes into the CHARSXP below. */
-
 
51
    const char *repaired = reEnc3(s, "UTF-8", "UTF-8", 1);
-
 
52
    return mkCharCE(repaired, CE_UTF8);
-
 
53
}
-
 
54
 
34
static void con_cleanup(void *data)
55
static void con_cleanup(void *data)
35
{
56
{
36
    Rconnection con = data;
57
    Rconnection con = data;
37
    if(con->isopen) con->close(con);
58
    if(con->isopen) con->close(con);
38
}
59
}
Line 201... Line 222...
201
			    }
222
			    }
202
			    n_eblanklines = 0;
223
			    n_eblanklines = 0;
203
			}
224
			}
204
			strcat(buf, line + offset);
225
			strcat(buf, line + offset);
205
		    }
226
		    }
206
		    SET_STRING_ELT(retval, lastm + nwhat * k, mkChar(buf));
227
		    SET_STRING_ELT(retval, lastm + nwhat * k, mkCharUTF8sub(buf));
207
		}
228
		}
208
	    } else {
229
	    } else {
209
		if(tre_regexecb(&regline, line, 1, regmatch, 0) == 0) {
230
		if(tre_regexecb(&regline, line, 1, regmatch, 0) == 0) {
210
		    for(m = 0; m < nwhat; m++){
231
		    for(m = 0; m < nwhat; m++){
211
			whatlen = (int) strlen(CHAR(STRING_ELT(what, m)));
232
			whatlen = (int) strlen(CHAR(STRING_ELT(what, m)));
Line 228... Line 249...
228
				if((tre_regexecb(&trailblank, line, 1,
249
				if((tre_regexecb(&trailblank, line, 1,
229
						 regmatch, 0) == 0))
250
						 regmatch, 0) == 0))
230
				    line[regmatch[0].rm_so] = '\0';
251
				    line[regmatch[0].rm_so] = '\0';
231
			    }
252
			    }
232
			    SET_STRING_ELT(retval, m + nwhat * k,
253
			    SET_STRING_ELT(retval, m + nwhat * k,
233
					   mkChar(line + offset));
254
					   mkCharUTF8sub(line + offset));
234
			    break;
255
			    break;
235
			} else {
256
			} else {
236
			    /* This is a field, but not one prespecified */
257
			    /* This is a field, but not one prespecified */
237
			    lastm = -1;
258
			    lastm = -1;
238
			    field_skip = true;
259
			    field_skip = true;
Line 272... Line 293...
272
			    } else buf = tmp;
293
			    } else buf = tmp;
273
			    buflen = need;
294
			    buflen = need;
274
			}
295
			}
275
			strncpy(buf, line, Rf_strchr(line, ':') - line);
296
			strncpy(buf, line, Rf_strchr(line, ':') - line);
276
			buf[Rf_strchr(line, ':') - line] = '\0';
297
			buf[Rf_strchr(line, ':') - line] = '\0';
277
			SET_STRING_ELT(what, nwhat, mkChar(buf));
298
			SET_STRING_ELT(what, nwhat, mkCharUTF8sub(buf));
278
			nwhat++;
299
			nwhat++;
279
			/* lastm uses C indexing, hence nwhat - 1 */
300
			/* lastm uses C indexing, hence nwhat - 1 */
280
			lastm = nwhat - 1;
301
			lastm = nwhat - 1;
281
			field_name = CHAR(STRING_ELT(what, lastm));
302
			field_name = CHAR(STRING_ELT(what, lastm));
282
			if(has_fold_excludes) {
303
			if(has_fold_excludes) {
Line 290... Line 311...
290
			    if((tre_regexecb(&trailblank, line, 1,
311
			    if((tre_regexecb(&trailblank, line, 1,
291
					     regmatch, 0) == 0))
312
					     regmatch, 0) == 0))
292
				line[regmatch[0].rm_so] = '\0';
313
				line[regmatch[0].rm_so] = '\0';
293
			}
314
			}
294
			SET_STRING_ELT(retval, lastm + nwhat * k,
315
			SET_STRING_ELT(retval, lastm + nwhat * k,
295
				       mkChar(line + offset));
316
				       mkCharUTF8sub(line + offset));
296
		    }
317
		    }
297
		} else {
318
		} else {
298
		    /* Must be a regular line with no tag ... */
319
		    /* Must be a regular line with no tag ... */
299
		    line[20] = '\0';
320
		    line[20] = '\0';
300
		    error(_("Line starting '%s ...' is malformed!"), line);
321
		    error(_("Line starting '%s ...' is malformed!"), line);