AmendHub

Download

jcs

/

amend

/

util.h

 

(View History)

jcs   bile: Sync with other projects Latest amendment: 66 on 2022-06-02

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