Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 1 | //===-- ResourceSerializator.h ----------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines a visitor serializing resources to a .res stream. |
| 11 | // |
| 12 | //===---------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 15 | #define LLVM_TOOLS_LLVMRC_RESOURCESERIALIZATOR_H |
| 16 | |
| 17 | #include "ResourceScriptStmt.h" |
| 18 | #include "ResourceVisitor.h" |
| 19 | |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace rc { |
| 24 | |
| 25 | class ResourceFileWriter : public Visitor { |
| 26 | public: |
| 27 | ResourceFileWriter(std::unique_ptr<raw_fd_ostream> Stream) |
| 28 | : FS(std::move(Stream)) { |
| 29 | assert(FS && "Output stream needs to be provided to the serializator"); |
| 30 | } |
| 31 | |
| 32 | Error visitNullResource(const RCResource *) override; |
| 33 | Error visitHTMLResource(const RCResource *) override; |
| 34 | |
| 35 | Error visitLanguageStmt(const LanguageResource *) override; |
| 36 | |
| 37 | struct ObjectInfo { |
| 38 | uint16_t LanguageInfo; |
| 39 | |
| 40 | ObjectInfo() : LanguageInfo(0) {} |
| 41 | } ObjectData; |
| 42 | |
| 43 | private: |
| 44 | Error handleError(Error &&Err, const RCResource *Res); |
| 45 | |
| 46 | Error |
| 47 | writeResource(const RCResource *Res, |
| 48 | Error (ResourceFileWriter::*BodyWriter)(const RCResource *)); |
| 49 | |
| 50 | Error writeNullBody(const RCResource *); |
| 51 | Error writeHTMLBody(const RCResource *); |
| 52 | |
| 53 | // Output stream handling. |
| 54 | std::unique_ptr<raw_fd_ostream> FS; |
| 55 | |
| 56 | uint64_t tell() const { return FS->tell(); } |
| 57 | |
| 58 | uint64_t writeObject(const ArrayRef<uint8_t> Data); |
| 59 | |
| 60 | template <typename T> uint64_t writeInt(const T &Value) { |
| 61 | support::detail::packed_endian_specific_integral<T, support::little, |
| 62 | support::unaligned> |
| 63 | Object(Value); |
| 64 | return writeObject(Object); |
| 65 | } |
| 66 | |
| 67 | template <typename T> uint64_t writeObject(const T &Value) { |
| 68 | return writeObject(ArrayRef<uint8_t>( |
| 69 | reinterpret_cast<const uint8_t *>(&Value), sizeof(T))); |
| 70 | } |
| 71 | |
| 72 | template <typename T> void writeObjectAt(const T &Value, uint64_t Position) { |
| 73 | FS->pwrite((const char *)&Value, sizeof(T), Position); |
| 74 | } |
| 75 | |
| 76 | Error writeCString(StringRef Str, bool WriteTerminator = true); |
| 77 | |
| 78 | Error writeIdentifier(const IntOrString &Ident); |
| 79 | Error writeIntOrString(const IntOrString &Data); |
| 80 | |
| 81 | Error appendFile(StringRef Filename); |
| 82 | |
| 83 | void padStream(uint64_t Length); |
| 84 | }; |
| 85 | |
| 86 | } // namespace rc |
| 87 | } // namespace llvm |
| 88 | |
| 89 | #endif |