AmendHub

Download

jcs

/

wallops

/

util.h

 

(View History)

jcs   irc: Handle /me and CTCP VERSION, move vers resource handling to util Latest amendment: 14 on 2022-02-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 MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define MAX(a, b) ((a) > (b) ? (a) : (b))
33 #define BOUND(a, min, max) ((a) > (max) ? (max) : ((a) < (min) ? (min) : (a)))
34
35 #define EXPAND_TO_FIT(var, var_size, used_size, add, grow_amount) { \
36 if ((used_size) + (add) >= (var_size)) { \
37 while ((used_size) + (add) >= (var_size)) { \
38 (var_size) += (grow_amount); \
39 } \
40 (var) = xrealloc((var), (var_size)); \
41 } \
42 }
43
44 #define SCROLLBAR_WIDTH 16
45
46 /* GetMBarHeight() is not very useful */
47 #define MENUBAR_HEIGHT 20
48
49 #define MAX_TEXTEDIT_SIZE 32767L
50
51 #ifndef bool
52 typedef Boolean bool;
53 #endif
54
55 typedef signed long off_t;
56 typedef signed long ssize_t;
57 typedef unsigned char u_char;
58 typedef unsigned long u_int;
59
60 typedef struct {
61 short push[2], rts;
62 void *addr;
63 } tCodeStub;
64
65 typedef struct stat {
66 short st_mode;
67 ssize_t st_size;
68 time_t st_ctime;
69 time_t st_mtime;
70 unsigned char st_flags;
71 };
72
73 void * xmalloc(size_t);
74 void * xmalloczero(size_t);
75 void * xcalloc(size_t, size_t);
76 void * xrealloc(void *src, size_t size);
77 void * xmallocarray(size_t nmemb, size_t size);
78 void * xreallocarray(void *, size_t, size_t);
79 char * xstrdup(const char *);
80
81 short getline(char *str, size_t len, char **ret);
82 size_t strlcpy(char *dst, const char *src, size_t dsize);
83 size_t strlcat(char *dst, const char *src, size_t dsize);
84 const char * ordinal(unsigned short n);
85 long strpos_quoted(char *str, char c);
86 char * OSTypeToString(OSType type);
87
88 unsigned long xorshift32(void);
89
90 void panic(const char *format, ...);
91 void err(short ret, const char *format, ...);
92 void warnx(const char *format, ...);
93 void warn(const char *format, ...);
94 void note(const char *format, ...);
95 short ask(const char *format, ...);
96 #define ASK_YES 1
97 #define ASK_NO 2
98 void progress(char *format, ...);
99
100 Handle xNewHandle(size_t size);
101 Handle xGetResource(ResType type, short id);
102 StringHandle xGetString(short id);
103 char * xGetStringAsChar(short id);
104 long xGetStringAsLong(short id);
105 void xSetHandleSize(Handle h, Size s);
106
107 short getpath(short vRefNum, Str255 fileName, Str255 *ret, bool include_file);
108 bool FIsDir(Str255 path);
109 short stat(char *path, struct stat *sb);
110 short FStat(Str255 path, struct stat *sb);
111 OSErr copy_file(Str255 source, Str255 dest, bool overwrite);
112 OSErr copy_file_contents(short source_ref, short dest_ref);
113 OSErr FSReadLine(short frefnum, char *buf, size_t buflen);
114
115 char * gestalt_machine_type(void);
116 char * get_version(bool long_version);
117
118 short FontHeight(short font_id, short size);
119 void DrawGrowIconOnly(WindowPtr win);
120 short TEGetWidth(short off, TEHandle te);
121 void UpdateScrollbarForTE(ControlHandle scroller, TEHandle te, bool reset);
122 void SetTrackControlTE(TEHandle te);
123 pascal void TrackMouseDownInControl(ControlHandle control, short part);
124 pascal bool ModalDialogFilter(DialogPtr dlg, EventRecord *event,
125 short *hit);
126 void PasswordDialogFieldFilterSetup(short ditl_id, char *storage,
127 size_t len);
128 pascal bool PasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event,
129 short *hit);
130 void PasswordDialogFieldFinish(void);
131 pascal void NullCaretHook(void);
132
133 #endif