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