joeylib2/tools/common/toolFile.h

37 lines
1.4 KiB
C

// Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
// See LICENSE.md for license information.
// SPDX-License-Identifier: MIT
//
// toolFile.h - whole-file reads and writes, numbers from the command line,
// and fatal errors for the JoeyLib host tools.
#ifndef TOOL_FILE_H
#define TOOL_FILE_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
// Print "<toolName>: <message>" to stderr and exit 1. toolName is set once
// by toolFileName (main's first act); printf-style arguments.
void toolFail(const char *fmt, ...) __attribute__((noreturn, format(printf, 1, 2)));
// toolFail with another exit code (2 for a bad command line, say).
void toolFailCode(int code, const char *fmt, ...) __attribute__((noreturn, format(printf, 2, 3)));
// The name toolFail prefixes its messages with.
void toolFileName(const char *toolName);
// An integer from text (len bytes of s) as Python's int() reads one:
// optional surrounding spaces, an optional sign, decimal digits; false when
// it is anything else or too big for a long.
bool toolParseInt(const char *s, size_t len, long *out);
// Read a whole file into a malloc'd buffer (one spare zero byte past the
// end, so text can be used as a C string); false if it cannot be read.
bool toolReadFile(const char *path, uint8_t **data, size_t *len);
// Write a whole file; false if it cannot be written.
bool toolWriteFile(const char *path, const uint8_t *data, size_t len);
#endif