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