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; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 33 | Error visitAcceleratorsResource(const RCResource *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 34 | Error visitHTMLResource(const RCResource *) override; |
| 35 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 36 | Error visitCharacteristicsStmt(const CharacteristicsStmt *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 37 | Error visitLanguageStmt(const LanguageResource *) override; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 38 | Error visitVersionStmt(const VersionStmt *) override; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 39 | |
| 40 | struct ObjectInfo { |
| 41 | uint16_t LanguageInfo; |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 42 | uint32_t Characteristics; |
| 43 | uint32_t VersionInfo; |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 44 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 45 | ObjectInfo() : LanguageInfo(0), Characteristics(0), VersionInfo(0) {} |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 46 | } ObjectData; |
| 47 | |
| 48 | private: |
| 49 | Error handleError(Error &&Err, const RCResource *Res); |
| 50 | |
| 51 | Error |
| 52 | writeResource(const RCResource *Res, |
| 53 | Error (ResourceFileWriter::*BodyWriter)(const RCResource *)); |
| 54 | |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 55 | // NullResource |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 56 | Error writeNullBody(const RCResource *); |
Marek Sokolowski | 22fccd6 | 2017-09-29 19:07:44 +0000 | [diff] [blame^] | 57 | |
| 58 | // AcceleratorsResource |
| 59 | Error writeSingleAccelerator(const AcceleratorsResource::Accelerator &, |
| 60 | bool IsLastItem); |
| 61 | Error writeAcceleratorsBody(const RCResource *); |
| 62 | |
| 63 | // HTMLResource |
Marek Sokolowski | 8f19343 | 2017-09-29 17:14:09 +0000 | [diff] [blame] | 64 | Error writeHTMLBody(const RCResource *); |
| 65 | |
| 66 | // Output stream handling. |
| 67 | std::unique_ptr<raw_fd_ostream> FS; |
| 68 | |
| 69 | uint64_t tell() const { return FS->tell(); } |
| 70 | |
| 71 | uint64_t writeObject(const ArrayRef<uint8_t> Data); |
| 72 | |
| 73 | template <typename T> uint64_t writeInt(const T &Value) { |
| 74 | support::detail::packed_endian_specific_integral<T, support::little, |
| 75 | support::unaligned> |
| 76 | Object(Value); |
| 77 | return writeObject(Object); |
| 78 | } |
| 79 | |
| 80 | template <typename T> uint64_t writeObject(const T &Value) { |
| 81 | return writeObject(ArrayRef<uint8_t>( |
| 82 | reinterpret_cast<const uint8_t *>(&Value), sizeof(T))); |
| 83 | } |
| 84 | |
| 85 | template <typename T> void writeObjectAt(const T &Value, uint64_t Position) { |
| 86 | FS->pwrite((const char *)&Value, sizeof(T), Position); |
| 87 | } |
| 88 | |
| 89 | Error writeCString(StringRef Str, bool WriteTerminator = true); |
| 90 | |
| 91 | Error writeIdentifier(const IntOrString &Ident); |
| 92 | Error writeIntOrString(const IntOrString &Data); |
| 93 | |
| 94 | Error appendFile(StringRef Filename); |
| 95 | |
| 96 | void padStream(uint64_t Length); |
| 97 | }; |
| 98 | |
| 99 | } // namespace rc |
| 100 | } // namespace llvm |
| 101 | |
| 102 | #endif |