File: hex_output.cpp

package info (click to toggle)
pico-sdk 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 26,552 kB
  • sloc: ansic: 146,841; asm: 13,423; python: 2,417; cpp: 2,171; yacc: 381; lex: 270; makefile: 32; sh: 13; javascript: 13
file content (41 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "output_format.h"
#include <iostream>

struct hex_output : public output_format {
    struct factory {
        factory() {
            output_format::add(new hex_output());
        }
    };

    hex_output() : output_format("hex") {}

    std::string get_description() {
        return "Raw hex output (only valid for single program inputs)";
    }

    virtual int output(std::string destination, std::vector<std::string> output_options,
                       const compiled_source &source) {
        FILE *out = open_single_output(destination);
        if (!out) return 1;

        if (source.programs.size() > 1) {
            // todo don't have locations any more!
            std::cerr << "error: hex output only supports a single program input\n";
            return 1;
        }
        for (const auto &i : source.programs[0].instructions) {
            fprintf(out, "%04x\n", i);
        }
        if (out != stdout) { fclose(out); }
        return 0;
    }
};

static hex_output::factory creator;