-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathCompleteFile.cpp
77 lines (69 loc) · 2.18 KB
/
CompleteFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "util/CompleteFile.h"
#include <cstring> // std::strcmp
#include <stdio.h> // FILE, fileno, fopen, getc, getc_unlocked, _getc_nolock
#include <sys/stat.h> // stat, fstat
#include "jsapi.h" // JS_ReportErrorNumberLatin1
#include "jsfriendapi.h" // js::GetErrorMessage, JSMSG_CANT_OPEN
bool js::ReadCompleteFile(JSContext* cx, FILE* fp, FileContents& buffer) {
/* Get the complete length of the file, if possible. */
struct stat st;
int ok = fstat(fileno(fp), &st);
if (ok != 0) {
return false;
}
if (st.st_size > 0) {
if (!buffer.reserve(st.st_size)) {
return false;
}
}
/* Use the fastest available getc. */
auto fast_getc =
#if defined(HAVE_GETC_UNLOCKED)
getc_unlocked
#elif defined(HAVE__GETC_NOLOCK)
_getc_nolock
#else
getc
#endif
;
// Read in the whole file. Note that we can't assume the data's length
// is actually st.st_size, because 1) some files lie about their size
// (/dev/zero and /dev/random), and 2) reading files in text mode on
// Windows collapses "\r\n" pairs to single \n characters.
for (;;) {
int c = fast_getc(fp);
if (c == EOF) {
break;
}
if (!buffer.append(c)) {
return false;
}
}
return true;
}
/*
* Open a source file for reading. Supports "-" and nullptr to mean stdin. The
* return value must be fclosed unless it is stdin.
*/
bool js::AutoFile::open(JSContext* cx, const char* filename) {
if (!filename || std::strcmp(filename, "-") == 0) {
fp_ = stdin;
} else {
fp_ = fopen(filename, "r");
if (!fp_) {
/*
* Use Latin1 variant here because the encoding of filename is
* platform dependent.
*/
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_CANT_OPEN,
filename, "No such file or directory");
return false;
}
}
return true;
}