annotated_binary_text_gen.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2021 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
  17. #define FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include "binary_annotator.h"
  22. namespace flatbuffers {
  23. class AnnotatedBinaryTextGenerator {
  24. public:
  25. struct Options {
  26. // The maximum number of raw bytes to print per line in the output. 8 is a
  27. // good default due to the largest type (double) being 8 bytes long.
  28. size_t max_bytes_per_line = 8;
  29. // The output file postfix, appended between the filename and the extension.
  30. // Example binary1.bin -> binary1_annotated.bin
  31. std::string output_postfix = "";
  32. // The output file extension, replacing any extension given. If empty, don't
  33. // change the provided extension. AFB = Annotated Flatbuffer Binary
  34. //
  35. // Example: binary1.bin -> binary1.afb
  36. std::string output_extension = "afb";
  37. // Controls.
  38. bool include_vector_contents = true;
  39. };
  40. explicit AnnotatedBinaryTextGenerator(
  41. const Options &options, std::map<uint64_t, BinarySection> annotations,
  42. const uint8_t *const binary, const int64_t binary_length)
  43. : annotations_(std::move(annotations)),
  44. binary_(binary),
  45. binary_length_(binary_length),
  46. options_(options) {}
  47. // Generate the annotated binary for the given `filename`. Returns true if the
  48. // annotated binary was successfully saved.
  49. bool Generate(const std::string &filename,
  50. const std::string &schema_filename);
  51. private:
  52. const std::map<uint64_t, BinarySection> annotations_;
  53. // The binary data itself.
  54. const uint8_t *binary_;
  55. const int64_t binary_length_;
  56. // Output configuration
  57. const Options options_;
  58. };
  59. } // namespace flatbuffers
  60. #endif // FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_