AmendHub

Download

vkoskiv

/

MacNTP

/

util.h

 

(View History)

vkoskiv   Large rewrite, restructure the project into an INIT Latest amendment: 11 on 2023-09-07

1 /*
2 * Copyright (c) 2020-2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef __UTIL_H__
18 #define __UTIL_H__
19
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <time.h>
23
24 #ifndef SIZE_MAX
25 #define SIZE_MAX ULONG_MAX
26 #endif
27
28 #define nitems(what) (sizeof((what)) / sizeof((what)[0]))
29 #define member_size(type, member) sizeof(((type *)0)->member)
30
31 #define MALLOC_NOTE_SIZE 32
32
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
34 #define MAX(a, b) ((a) > (b) ? (a) : (b))
35 #define BOUND(a, min, max) ((a) > (max) ? (max) : ((a) < (min) ? (min) : (a)))
36
37 #define EXPAND_TO_FIT(var, var_size, used_size, add, grow_amount) { \
38 if ((used_size) + (add) >= (var_size)) { \
39 while ((used_size) + (add) >= (var_size)) { \
40 (var_size) += (grow_amount); \
41 } \
42 (var) = xrealloc((var), (var_size)); \
43 } \
44 }
45
46 #define CHARS_TO_LONG(a,b,c,d) (unsigned long)(\
47 ((unsigned long)((unsigned char)(a)) << 24) | \
48 ((unsigned long)((unsigned char)(b)) << 16) | \
49 ((unsigned long)((unsigned char)(c)) << 8) | \
50 (unsigned long)((unsigned char)(d)) )
51 #define CHARS_TO_SHORT(a,b) (unsigned short)(\
52 ((unsigned short)((unsigned char)(a)) << 8) | \
53 (unsigned short)((unsigned char)(b)) )
54
55 #define SCROLLBAR_WIDTH 16
56
57 /* GetMBarHeight() is not very useful */
58 #define MENUBAR_HEIGHT 20
59
60 #define TICKS_PER_SEC 60L
61
62 #define MAX_TEXTEDIT_SIZE 32767L
63
64 #ifndef bool
65 typedef Boolean bool;
66 #endif
67 typedef signed long off_t;
68 typedef signed long ssize_t;
69 typedef unsigned char u_char;
70 typedef unsigned long u_int;
71 typedef unsigned char u_int8_t;
72 typedef char int8_t;
73 typedef unsigned short u_int16_t;
74 typedef unsigned long u_int32_t;
75
76 #define BYTE_ORDER BIG_ENDIAN
77
78 typedef struct {
79 short push[2], rts;
80 void *addr;
81 } tCodeStub;
82
83 struct stat {
84 short st_mode;
85 ssize_t st_size;
86 time_t st_ctime;
87 time_t st_mtime;
88 unsigned char st_flags;
89 };
90
91 void util_init(void);
92
93 void * xmalloc(size_t, char *note);
94 void xfree(void *ptrptr);
95 void xfree_verify(void);
96 void * xmalloczero(size_t, char *note);
97 void * xcalloc(size_t, size_t, char *note);
98 void * xrealloc(void *src, size_t size);
99 void * xreallocarray(void *, size_t, size_t);
100 char * xstrdup(const char *, char *note);
101 char * xstrndup(const char *str, size_t maxlen, char *note);
102
103 short getline(char *str, size_t len, char **ret);
104 const char * ordinal(unsigned short n);
105 size_t rtrim(char *str, char *chars);
106 long strpos_quoted(char *str, char c);
107 char * OSTypeToString(OSType type);
108
109 unsigned long xorshift32(void);
110
111 void panic(const char *format, ...);
112 void err(short ret, const char *format, ...);
113 void warnx(const char *format, ...);
114 void warn(const char *format, ...);
115 void note(const char *format, ...);
116 void appicon_note(const char *format, ...);
117 short ask(const char *format, ...);
118 #define ASK_YES 1
119 #define ASK_NO 2
120 void about(char *program_name);
121 void progress(char *format, ...);
122 void window_rect(WindowPtr win, Rect *ret);
123 void center_in_screen(short width, short height, bool titlebar, Rect *b);
124 Point centered_sf_dialog(void);
125
126 Handle xNewHandle(size_t size);
127 Handle xGetResource(ResType type, short id);
128 StringHandle xGetString(short id);
129 char * xGetStringAsChar(short id);
130 long xGetStringAsLong(short id);
131 void xSetHandleSize(Handle h, Size s);
132
133 short getpath(short vRefNum, Str255 fileName, Str255 *ret, bool include_file);
134 bool FIsDir(Str255 path);
135 short stat(char *path, struct stat *sb);
136 short FStat(Str255 path, struct stat *sb);
137 OSErr copy_file(Str255 source, Str255 dest, bool overwrite);
138 OSErr copy_file_contents(short source_ref, short dest_ref);
139 OSErr FSReadLine(short frefnum, char *buf, size_t buflen);
140
141 char * gestalt_machine_type(void);
142 char * get_version(bool long_version);
143
144 short FontHeight(short font_id, short size);
145 void DrawGrowIconOnly(WindowPtr win);
146 short TEGetWidth(short off, TEHandle te);
147 void UpdateScrollbarForTE(GrafPtr win, ControlHandle scroller, TEHandle te,
148 bool reset);
149 void SetTrackControlTE(TEHandle te);
150 pascal void TrackMouseDownInControl(ControlHandle control, short part);
151 pascal bool ModalDialogFilter(DialogPtr dlg, EventRecord *event,
152 short *hit);
153 void PasswordDialogFieldFilterSetup(short ditl_id, char *storage,
154 size_t len);
155 pascal bool PasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event,
156 short *hit);
157 void PasswordDialogFieldFinish(void);
158 pascal void NullCaretHook(void);
159 void HideMenuBar(void);
160 void RestoreHiddenMenuBar(void);
161
162 size_t strlcat(char *dst, const char *src, size_t dsize);
163 size_t strlcpy(char *dst, const char *src, size_t dsize);
164 char * strndup(const char *str, size_t maxlen);
165 char * strsep(char **stringp, const char *delim);
166 int snprintf(char *s, size_t size, const char *fmt, ...);
167 int vsnprintf(char *s, size_t size, const char *fmt, void *p);
168
169 #endif