1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "output_format.h"
8 #include <iostream>
9 
10 struct hex_output : public output_format {
11     struct factory {
factoryhex_output::factory12         factory() {
13             output_format::add(new hex_output());
14         }
15     };
16 
hex_outputhex_output17     hex_output() : output_format("hex") {}
18 
get_descriptionhex_output19     std::string get_description() {
20         return "Raw hex output (only valid for single program inputs)";
21     }
22 
outputhex_output23     virtual int output(std::string destination, std::vector<std::string> output_options,
24                        const compiled_source &source) {
25         FILE *out = open_single_output(destination);
26         if (!out) return 1;
27 
28         if (source.programs.size() > 1) {
29             // todo don't have locations any more!
30             std::cerr << "error: hex output only supports a single program input\n";
31             return 1;
32         }
33         for (const auto &i : source.programs[0].instructions) {
34             fprintf(out, "%04x\n", i);
35         }
36         if (out != stdout) { fclose(out); }
37         return 0;
38     }
39 };
40 
41 static hex_output::factory creator;
42