Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/tools/utils.h
7854 views
1
#ifndef UTILS_H_
2
#define UTILS_H_
3
4
#include <stdio.h>
5
#include <stdint.h>
6
7
// defines
8
9
// printing size_t varies by compiler
10
#if defined(_MSC_VER) || defined(__MINGW32__)
11
#define SIZE_T_FORMAT "%Iu"
12
#else
13
#define SIZE_T_FORMAT "%zu"
14
#endif
15
16
#define KB 1024
17
#define MB (1024 * KB)
18
19
// number of elements in statically declared array
20
#define DIM(S_ARR_) (sizeof(S_ARR_) / sizeof(S_ARR_[0]))
21
22
#define MIN(A_, B_) ((A_) < (B_) ? (A_) : (B_))
23
#define MAX(A_, B_) ((A_) > (B_) ? (A_) : (B_))
24
25
// align value to N-byte boundary
26
#define ALIGN(VAL_, ALIGNMENT_) (((VAL_) + ((ALIGNMENT_) - 1)) & ~((ALIGNMENT_) - 1))
27
28
// read/write u32/16 big/little endian
29
#define read_u32_be(buf) (unsigned int)(((buf)[0] << 24) + ((buf)[1] << 16) + ((buf)[2] << 8) + ((buf)[3]))
30
#define read_u32_le(buf) (unsigned int)(((buf)[1] << 24) + ((buf)[0] << 16) + ((buf)[3] << 8) + ((buf)[2]))
31
#define write_u32_be(buf, val) do { \
32
(buf)[0] = ((val) >> 24) & 0xFF; \
33
(buf)[1] = ((val) >> 16) & 0xFF; \
34
(buf)[2] = ((val) >> 8) & 0xFF; \
35
(buf)[3] = (val) & 0xFF; \
36
} while(0)
37
#define read_u16_be(buf) (((buf)[0] << 8) + ((buf)[1]))
38
#define write_u16_be(buf, val) do { \
39
(buf)[0] = ((val) >> 8) & 0xFF; \
40
(buf)[1] = ((val)) & 0xFF; \
41
} while(0)
42
43
// Windows compatibility
44
#if defined(_MSC_VER) || defined(__MINGW32__)
45
#include <direct.h>
46
#define mkdir(DIR_, PERM_) _mkdir(DIR_)
47
#ifndef strcasecmp
48
#define strcasecmp(A, B) stricmp(A, B)
49
#endif
50
#endif
51
52
// typedefs
53
54
#define MAX_DIR_FILES 128
55
typedef struct
56
{
57
char *files[MAX_DIR_FILES];
58
int count;
59
} dir_list;
60
61
typedef enum
62
{
63
ENCODING_RAW,
64
ENCODING_U8,
65
ENCODING_U16,
66
ENCODING_U32,
67
ENCODING_U64,
68
} write_encoding;
69
70
// global verbosity setting
71
extern int g_verbosity;
72
73
#define ERROR(...) fprintf(stderr, __VA_ARGS__)
74
#define INFO(...) if (g_verbosity) printf(__VA_ARGS__)
75
76
// functions
77
78
// convert two bytes in big-endian to signed int
79
int read_s16_be(unsigned char *buf);
80
81
// convert four bytes in big-endian to float
82
float read_f32_be(unsigned char *buf);
83
84
// determine if value is power of 2
85
// returns 1 if val is power of 2, 0 otherwise
86
int is_power2(unsigned int val);
87
88
// print buffer as raw binary, hex u8, hex u16, hex u32, hex u64. pads to encoding size
89
// fp: file pointer
90
// encoding: encoding type to use (see write_encoding)
91
// buf: buffer to read bytes from
92
// length: length of buffer to print
93
int fprint_write_output(FILE *fp, write_encoding encoding, const uint8_t *buf, int length);
94
95
// perform byteswapping to convert from v64 to z64 ordering
96
void swap_bytes(unsigned char *data, long length);
97
98
// reverse endian to convert from n64 to z64 ordering
99
void reverse_endian(unsigned char *data, long length);
100
101
// get size of file without opening it;
102
// returns file size or negative on error
103
long filesize(const char *file_name);
104
105
// update file timestamp to now, creating it if it doesn't exist
106
void touch_file(const char *filename);
107
108
// read entire contents of file into buffer
109
// returns file size or negative on error
110
long read_file(const char *file_name, unsigned char **data);
111
112
// write buffer to file
113
// returns number of bytes written out or -1 on failure
114
long write_file(const char *file_name, unsigned char *data, long length);
115
116
// generate an output file name from input name by replacing file extension
117
// in_name: input file name
118
// out_name: buffer to write output name in
119
// extension: new file extension to use
120
void generate_filename(const char *in_name, char *out_name, char *extension);
121
122
// extract base filename from file path
123
// name: path to file
124
// returns just the file name after the last '/'
125
char *basename(const char *name);
126
127
// make a directory if it doesn't exist
128
// dir_name: name of the directory
129
void make_dir(const char *dir_name);
130
131
// copy a file from src_name to dst_name. will not make directories
132
// src_name: source file name
133
// dst_name: destination file name
134
long copy_file(const char *src_name, const char *dst_name);
135
136
// list a directory, optionally filtering files by extension
137
// dir: directory to list files in
138
// extension: extension to filter files by (NULL if no filtering)
139
// list: output list and count
140
void dir_list_ext(const char *dir, const char *extension, dir_list *list);
141
142
// free associated date from a directory list
143
// list: directory list filled in by dir_list_ext() call
144
void dir_list_free(dir_list *list);
145
146
// determine if a string ends with another string
147
// str: string to check if ends with 'suffix'
148
// suffix: string to see if 'str' ends with
149
// returns 1 if 'str' ends with 'suffix'
150
int str_ends_with(const char *str, const char *suffix);
151
152
#endif // UTILS_H_
153
154