AmendHub

Download

jcs

/

subtext

/

util.h

 

(View History)

jcs   util: Add a comment explaining EXPAND_TO_FIT Latest amendment: 278 on 2022-11-11

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 /*
38 * If var of var_size (of which used_size is used) is not big enough to
39 * hold add, expand it by grow_amount (to give headroom for subsequent
40 * expansion).
41 */
42 #define EXPAND_TO_FIT(var, var_size, used_size, add, grow_amount) { \
43 if ((used_size) + (add) >= (var_size)) { \
44 while ((used_size) + (add) >= (var_size)) { \
45 (var_size) += (grow_amount); \
46 } \
47 (var) = xrealloc((var), (var_size)); \
48 } \
49 }
50
51 #define CHARS_TO_LONG(a,b,c,d) (unsigned long)(\
52 ((unsigned long)((unsigned char)(a)) << 24) | \
53 ((unsigned long)((unsigned char)(b)) << 16) | \
54 ((unsigned long)((unsigned char)(c)) << 8) | \
55 (unsigned long)((unsigned char)(d)) )
56 #define CHARS_TO_SHORT(a,b) (unsigned short)(\
57 ((unsigned short)((unsigned char)(a)) << 8) | \
58 (unsigned short)((unsigned char)(b)) )
59
60 #define SCROLLBAR_WIDTH 16
61
62 /* GetMBarHeight() is not very useful */
63 #define MENUBAR_HEIGHT 20
64
65 #define TICKS_PER_SEC 60L
66
67 #define MAX_TEXTEDIT_SIZE 32767L
68
69 #ifndef bool
70 typedef Boolean bool;
71 #endif
72 typedef signed long off_t;
73 typedef signed long ssize_t;
74 typedef unsigned char u_char;
75 typedef unsigned long u_int;
76 typedef unsigned char u_int8_t;
77 typedef unsigned short u_int16_t;
78 typedef unsigned long u_int32_t;
79
80 #define BYTE_ORDER BIG_ENDIAN
81
82 typedef struct {
83 short push[2], rts;
84 void *addr;
85 } tCodeStub;
86
87 struct stat {
88 short st_mode;
89 ssize_t st_size;
90 time_t st_ctime;
91 time_t st_mtime;
92 unsigned char st_flags;
93 };
94
95 void util_init(void);
96
97 void * xmalloc(size_t, char *note);
98 void xfree(void *ptrptr);
99 void xfree_verify(void);
100 void * xmalloczero(size_t, char *note);
101 void * xcalloc(size_t, size_t, char *note);
102 void * xrealloc(void *src, size_t size);
103 void * xreallocarray(void *, size_t, size_t);
104 char * xstrdup(const char *, char *note);
105 char * xstrndup(const char *str, size_t maxlen, char *note);
106
107 short getline(char *str, size_t len, char **ret);
108 const char * ordinal(unsigned short n);
109 size_t rtrim(char *str, char *chars);
110 long strpos_quoted(char *str, char c);
111 char * OSTypeToString(OSType type);
112
113 unsigned long xorshift32(void);
114
115 void panic(const char *format, ...);
116 void err(short ret, const char *format, ...);
117 void warnx(const char *format, ...);
118 void warn(const char *format, ...);
119 void note(const char *format, ...);
120 void appicon_note(const char *format, ...);
121 short ask(const char *format, ...);
122 #define ASK_YES 1
123 #define ASK_NO 2
124 void about(char *program_name);
125 void progress(char *format, ...);
126 void window_rect(WindowPtr win, Rect *ret);
127 void center_in_screen(short width, short height, bool titlebar, Rect *b);
128 Point centered_sf_dialog(void);
129
130 Handle xNewHandle(size_t size);
131 Handle xGetResource(ResType type, short id);
132 StringHandle xGetString(short id);
133 char * xGetStringAsChar(short id);
134 long xGetStringAsLong(short id);
135 void xSetHandleSize(Handle h, Size s);
136
137 short getpath(short vRefNum, Str255 fileName, Str255 *ret, bool include_file);
138 bool FIsDir(Str255 path);
139 short stat(char *path, struct stat *sb);
140 short FStat(Str255 path, struct stat *sb);
141 OSErr copy_file(Str255 source, Str255 dest, bool overwrite);
142 OSErr copy_file_contents(short source_ref, short dest_ref);
143 OSErr FSReadLine(short frefnum, char *buf, size_t buflen);
144
145 char * gestalt_machine_type(void);
146 char * get_version(bool long_version);
147
148 short FontHeight(short font_id, short size);
149 void DrawGrowIconOnly(WindowPtr win);
150 short TEGetWidth(short off, TEHandle te);
151 void UpdateScrollbarForTE(GrafPtr win, ControlHandle scroller, TEHandle te,
152 bool reset);
153 void SetTrackControlTE(TEHandle te);
154 pascal void TrackMouseDownInControl(ControlHandle control, short part);
155 pascal bool ModalDialogFilter(DialogPtr dlg, EventRecord *event,
156 short *hit);
157 void PasswordDialogFieldFilterSetup(short ditl_id, char *storage,
158 size_t len);
159 pascal bool PasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event,
160 short *hit);
161 void PasswordDialogFieldFinish(void);
162 pascal void NullCaretHook(void);
163 void HideMenuBar(void);
164 void RestoreHiddenMenuBar(void);
165
166 size_t strlcat(char *dst, const char *src, size_t dsize);
167 size_t strlcpy(char *dst, const char *src, size_t dsize);
168 char * strndup(const char *str, size_t maxlen);
169 char * strsep(char **stringp, const char *delim);
170 int snprintf(char *s, size_t size, const char *fmt, ...);
171 int vsnprintf(char *s, size_t size, const char *fmt, void *p);
172
173 #endif