Skip to content

Commit 5b4cfc7

Browse files
committed
copied commit from local branch
1 parent 6f05e4a commit 5b4cfc7

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

src/fread.c

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,21 @@ static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper) {
205205
* is constructed manually (using say snprintf) that warning(), stop()
206206
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
207207
*/
208-
static const char* strlim(const char *ch, size_t limit) {
209-
static char buf[1002];
210-
static int flip = 0;
211-
char *ptr = buf + 501 * flip;
212-
flip = 1 - flip;
213-
char *ch2 = ptr;
214-
limit = imin(limit, 500);
208+
static const char* strlim(const char *ch, char buf[static 500], size_t limit) {
209+
char *ch2 = buf;
215210
size_t width = 0;
216211
while ((*ch>'\r' || (*ch!='\0' && *ch!='\r' && *ch!='\n')) && width++<limit) {
217212
*ch2++ = *ch++;
218213
}
219214
*ch2 = '\0';
220-
return ptr;
215+
return buf;
221216
}
222217

223218
static const char *typeLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
224219

225-
static char *typesAsString(int ncol) {
220+
static char *typesAsString(char str[static 101], int ncol) {
226221
int nLetters = strlen(typeLetter);
227222
if (NUMTYPE>nLetters) INTERNAL_STOP("NUMTYPE(%d) > nLetters(%d)", NUMTYPE, nLetters); // # nocov
228-
static char str[101];
229223
int i=0;
230224
if (ncol<=100) {
231225
for (; i<ncol; i++) str[i] = typeLetter[IGNORE_BUMP(type[i])];
@@ -413,10 +407,9 @@ double wallclock(void)
413407
* multiple threads at the same time, or hold on to the value returned for
414408
* extended periods of time.
415409
*/
416-
static const char* filesize_to_str(const size_t fsize)
410+
static const char* filesize_to_str(char output[static 100], const size_t fsize)
417411
{
418412
static const char suffixes[] = {'T', 'G', 'M', 'K'};
419-
static char output[100];
420413
for (int i = 0; i <= sizeof(suffixes); i++) {
421414
int shift = (sizeof(suffixes) - i) * 10;
422415
if ((fsize >> shift) == 0) continue;
@@ -426,18 +419,18 @@ static const char* filesize_to_str(const size_t fsize)
426419
}
427420
if (ndigits == 0 || (fsize == (fsize >> shift << shift))) {
428421
if (i < sizeof(suffixes)) {
429-
snprintf(output, sizeof(output), "%"PRIu64"%cB (%"PRIu64" bytes)", // # notranslate
422+
snprintf(output, 100, "%"PRIu64"%cB (%"PRIu64" bytes)", // # notranslate
430423
(fsize >> shift), suffixes[i], fsize);
431424
return output;
432425
}
433426
} else {
434-
snprintf(output, sizeof(output), "%.*f%cB (%"PRIu64" bytes)", // # notranslate
427+
snprintf(output, 100, "%.*f%cB (%"PRIu64" bytes)", // # notranslate
435428
ndigits, (double)fsize / (1LL << shift), suffixes[i], fsize);
436429
return output;
437430
}
438431
}
439432
if (fsize == 1) return "1 byte";
440-
snprintf(output, sizeof(output), "%"PRIu64" bytes", fsize); // # notranslate
433+
snprintf(output, 100, "%"PRIu64" bytes", fsize); // # notranslate
441434
return output;
442435
}
443436

@@ -1423,7 +1416,7 @@ int freadMain(freadMainArgs _args) {
14231416
}
14241417
fileSize = (size_t) stat_buf.st_size;
14251418
if (fileSize == 0) {close(fd); STOP(_("File is empty: %s"), fnam);}
1426-
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str(fileSize));
1419+
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str((char[100]) {}, fileSize));
14271420

14281421
// No MAP_POPULATE for faster nrows=10 and to make possible earlier progress bar in row count stage
14291422
// Mac doesn't appear to support MAP_POPULATE anyway (failed on CRAN when I tried).
@@ -1455,7 +1448,7 @@ int freadMain(freadMainArgs _args) {
14551448
if (GetFileSizeEx(hFile,&liFileSize)==0) { CloseHandle(hFile); STOP(_("GetFileSizeEx failed (returned 0) on file: %s"), fnam); }
14561449
fileSize = (size_t)liFileSize.QuadPart;
14571450
if (fileSize<=0) { CloseHandle(hFile); STOP(_("File is empty: %s"), fnam); }
1458-
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str(fileSize));
1451+
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str((char[100]) {}, fileSize));
14591452
HANDLE hMap=CreateFileMapping(hFile, NULL, PAGE_WRITECOPY, 0, 0, NULL);
14601453
if (hMap==NULL) { CloseHandle(hFile); STOP(_("This is Windows, CreateFileMapping returned error %lu for file %s"), GetLastError(), fnam); }
14611454
mmp = MapViewOfFile(hMap,FILE_MAP_COPY,0,0,fileSize); // fileSize must be <= hilo passed to CreateFileMapping above.
@@ -1464,7 +1457,7 @@ int freadMain(freadMainArgs _args) {
14641457
if (mmp == NULL) {
14651458
#endif
14661459
int nbit = 8*sizeof(char *); // #nocov
1467-
STOP(_("Opened %s file ok but could not memory map it. This is a %dbit process. %s."), filesize_to_str(fileSize), nbit, // # nocov
1460+
STOP(_("Opened %s file ok but could not memory map it. This is a %dbit process. %s."), filesize_to_str((char[100]) {}, fileSize), nbit, // # nocov
14681461
nbit<=32 ? _("Please upgrade to 64bit") : _("There is probably not enough contiguous virtual memory available")); // # nocov
14691462
}
14701463
sof = (const char*) mmp;
@@ -1561,7 +1554,7 @@ int freadMain(freadMainArgs _args) {
15611554
// # nocov start
15621555
if (!verbose)
15631556
DTPRINT(_("%s. Attempt to copy file in RAM failed."), msg);
1564-
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1557+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str((char[100]) {}, fileSize));
15651558
// # nocov end
15661559
}
15671560
if (verbose)
@@ -1642,7 +1635,7 @@ int freadMain(freadMainArgs _args) {
16421635
if (ch>=eof) STOP(_("Input is either empty, fully whitespace, or skip has been set after the last non-whitespace."));
16431636
if (verbose) {
16441637
if (lineStart>ch) DTPRINT(_(" Moved forward to first non-blank line (%d)\n"), row1line);
1645-
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, 30));
1638+
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[500]) {}, 30));
16461639
}
16471640
ch = pos = lineStart;
16481641
}
@@ -1832,7 +1825,7 @@ int freadMain(freadMainArgs _args) {
18321825
if (!fill && tt!=ncol) INTERNAL_STOP("first line has field count %d but expecting %d", tt, ncol); // # nocov
18331826
if (verbose) {
18341827
DTPRINT(_(" Detected %d columns on line %d. This line is either column names or first data row. Line starts as: <<%s>>\n"),
1835-
tt, row1line, strlim(pos, 30));
1828+
tt, row1line, strlim(pos, (char[500]) {}, 30));
18361829
DTPRINT(_(" Quote rule picked = %d\n"), quoteRule);
18371830
DTPRINT(_(" fill=%s and the most number of columns found is %d\n"), fill?"true":"false", ncol);
18381831
}
@@ -1849,7 +1842,7 @@ int freadMain(freadMainArgs _args) {
18491842
// # nocov start
18501843
if (!verbose)
18511844
DTPRINT(_("%s. Attempt to copy file in RAM failed."), msg);
1852-
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1845+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str((char[100]) {}, fileSize));
18531846
// # nocov end
18541847
}
18551848
if (verbose)
@@ -1995,7 +1988,7 @@ int freadMain(freadMainArgs _args) {
19951988
memcpy(type, tmpType, ncol);
19961989
}
19971990
if (verbose && (bumped || jump==0 || jump==nJumps-1)) {
1998-
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString(ncol), quoteRule);
1991+
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString((char[101]) {}, ncol), quoteRule);
19991992
}
20001993
}
20011994

@@ -2090,7 +2083,7 @@ int freadMain(freadMainArgs _args) {
20902083
type[j] = tmpType[j];
20912084
}
20922085
}
2093-
if (verbose && bumped) DTPRINT(_(" Type codes (first row) : %s Quote rule %d\n"), typesAsString(ncol), quoteRule);
2086+
if (verbose && bumped) DTPRINT(_(" Type codes (first row) : %s Quote rule %d\n"), typesAsString((char[101]) {}, ncol), quoteRule);
20942087
}
20952088

20962089
estnrow=1;
@@ -2222,7 +2215,7 @@ int freadMain(freadMainArgs _args) {
22222215
rowSize8 += (size[j] & 8);
22232216
if (type[j] == CT_STRING) nStringCols++; else nNonStringCols++;
22242217
}
2225-
if (verbose) DTPRINT(_(" After %d type and %d drop user overrides : %s\n"), nUserBumped, ndrop, typesAsString(ncol));
2218+
if (verbose) DTPRINT(_(" After %d type and %d drop user overrides : %s\n"), nUserBumped, ndrop, typesAsString((char[101]) {}, ncol));
22262219
tColType = wallclock();
22272220
}
22282221

@@ -2689,7 +2682,7 @@ int freadMain(freadMainArgs _args) {
26892682
for (int i=0; i<ncol; i++) typeCounts[ IGNORE_BUMP(type[i]) ]++;
26902683

26912684
if (nTypeBump) {
2692-
if (verbose) DTPRINT(_(" %d out-of-sample type bumps: %s\n"), nTypeBump, typesAsString(ncol));
2685+
if (verbose) DTPRINT(_(" %d out-of-sample type bumps: %s\n"), nTypeBump, typesAsString((char[101]) {}, ncol));
26932686
rowSize1 = rowSize4 = rowSize8 = 0;
26942687
nStringCols = 0;
26952688
nNonStringCols = 0;
@@ -2725,7 +2718,7 @@ int freadMain(freadMainArgs _args) {
27252718

27262719
double tTot = tReread-t0; // tReread==tRead when there was no reread
27272720
if (verbose) DTPRINT(_("Read %"PRIu64" rows x %d columns from %s file in %02d:%06.3f wall clock time\n"),
2728-
(uint64_t)DTi, ncol-ndrop, filesize_to_str(fileSize), (int)tTot/60, fmod(tTot,60.0));
2721+
(uint64_t)DTi, ncol-ndrop, filesize_to_str((char[100]) {}, fileSize), (int)tTot/60, fmod(tTot,60.0));
27292722

27302723
//*********************************************************************************************
27312724
// [12] Finalize the datatable
@@ -2750,23 +2743,23 @@ int freadMain(freadMainArgs _args) {
27502743
while (ch<eof && *ch!='\n' && *ch!='\r') ch++;
27512744
while (ch<eof && isspace(*ch)) ch++;
27522745
if (ch==eof) {
2753-
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter,500));
2746+
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[500]) {}, 500));
27542747
}
27552748
else {
27562749
ch = headPos;
27572750
int tt = countfields(&ch);
27582751
if (fill>0) {
27592752
DTWARN(_("Stopped early on line %"PRIu64". Expected %d fields but found %d. Consider fill=%d or even more based on your knowledge of the input file. Use fill=Inf for reading the whole file for detecting the number of fields. First discarded non-empty line: <<%s>>"),
2760-
(uint64_t)DTi+row1line, ncol, tt, tt, strlim(skippedFooter,500));
2753+
(uint64_t)DTi+row1line, ncol, tt, tt, strlim(skippedFooter, (char[500]) {}, 500));
27612754
} else {
27622755
DTWARN(_("Stopped early on line %"PRIu64". Expected %d fields but found %d. Consider fill=TRUE. First discarded non-empty line: <<%s>>"),
2763-
(uint64_t)DTi+row1line, ncol, tt, strlim(skippedFooter,500));
2756+
(uint64_t)DTi+row1line, ncol, tt, strlim(skippedFooter, (char[500]) {}, 500));
27642757
}
27652758
}
27662759
}
27672760
}
27682761
if (quoteRuleBumpedCh!=NULL && quoteRuleBumpedCh<headPos) {
2769-
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRIu64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), (uint64_t)quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, 500));
2762+
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRIu64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), (uint64_t)quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, (char[500]) {}, 500));
27702763
}
27712764

27722765
if (verbose) {

0 commit comments

Comments
 (0)