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