AmendHub

Download

jcs

/

wikipedia

/

util.h

 

(View History)

jcs   util: Sync with upstream Latest amendment: 41 on 2023-08-28

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