Backed out changeset 4191b252db9b (bug 1886734) for causing build bustages @netwerk...
[gecko.git] / js / src / util / CompleteFile.h
blobb13fe30b2886f39843f181a16c2ebd3e1cc6c5e2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef util_CompleteFile_h
8 #define util_CompleteFile_h
10 #include "mozilla/Assertions.h" // MOZ_ASSERT
12 #include <stdint.h> // uint8_t
13 #include <stdio.h> // fclose, FILE, stdin
15 #include "jstypes.h" // JS_PUBLIC_API
16 #include "js/AllocPolicy.h" // js::TempAllocPolicy
17 #include "js/Vector.h" // js::Vector
19 struct JS_PUBLIC_API JSContext;
21 namespace js {
23 using FileContents = Vector<uint8_t, 8, TempAllocPolicy>;
25 extern bool ReadCompleteFile(JSContext* cx, FILE* fp, FileContents& buffer);
27 class AutoFile {
28 FILE* fp_ = nullptr;
30 public:
31 AutoFile() = default;
33 ~AutoFile() {
34 if (fp_ && fp_ != stdin) {
35 fclose(fp_);
39 FILE* fp() const { return fp_; }
41 bool open(JSContext* cx, const char* filename);
43 bool readAll(JSContext* cx, FileContents& buffer) {
44 MOZ_ASSERT(fp_);
45 return ReadCompleteFile(cx, fp_, buffer);
49 } // namespace js
51 #endif /* util_CompleteFile_h */