AmendHub

Download

jcs

/

amend

/

settings.c

 

(View History)

jcs   *: Bring in util from Subtext with malloc debugging, integrate it Latest amendment: 92 on 2022-08-18

1 /*
2 * Copyright (c) 2021 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 #include <stdio.h>
18 #include <string.h>
19
20 #include "amend.h"
21 #include "settings.h"
22 #include "tetab.h"
23 #include "util.h"
24
25 struct settings settings = { 0 };
26
27 void
28 settings_load(void)
29 {
30 char *author;
31
32 author = xGetStringAsChar(STR_AUTHOR_ID);
33 memcpy(settings.author, author, sizeof(settings.author));
34 settings.author[sizeof(settings.author)] = '\0';
35 xfree(&author);
36 if (settings.author[0] == '\0')
37 snprintf(settings.author, sizeof(settings.author), "unknown");
38
39 settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID);
40 if (settings.tabwidth < 1 || settings.tabwidth > 20) {
41 warn("Bogus tabwidth resource %d", settings.tabwidth);
42 settings.tabwidth = 4;
43 }
44
45 TETabWidth = settings.tabwidth;
46 }
47
48 void
49 settings_save(void)
50 {
51 Handle res;
52 size_t l;
53 char tmp[8];
54
55 res = xGetResource('STR ', STR_AUTHOR_ID);
56 HLock(res);
57 l = strlen(settings.author);
58 xSetHandleSize(res, l + 1);
59 memcpy(*res, settings.author, l + 1);
60 CtoPstr(*res);
61 ChangedResource(res);
62 ReleaseResource(res);
63
64 res = xGetResource('STR ', STR_TABWIDTH_ID);
65 HLock(res);
66 snprintf(tmp, sizeof(tmp), "%d", settings.tabwidth);
67 l = strlen(tmp);
68 xSetHandleSize(res, l + 1);
69 memcpy(*res, tmp, l + 1);
70 CtoPstr(*res);
71 ChangedResource(res);
72 ReleaseResource(res);
73
74 UpdateResFile(CurResFile());
75 if (ResError())
76 warn("UpdateResFile failed! %d", ResError());
77
78 TETabWidth = settings.tabwidth;
79 }
80
81 void
82 settings_edit(void)
83 {
84 Str255 txt;
85 Handle ihandle;
86 size_t len;
87 short itype, hit;
88 bool done;
89 DialogPtr dlg;
90 Rect irect;
91
92 dlg = GetNewDialog(SETTINGS_DLOG_ID, nil, (WindowPtr)-1);
93 if (dlg == NULL)
94 err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID);
95
96 GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect);
97 memcpy(txt, settings.author, strlen(settings.author) + 1);
98 CtoPstr(txt);
99 SetIText(ihandle, txt);
100
101 GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect);
102 snprintf((char *)&txt, sizeof(txt), "%d", settings.tabwidth);
103 CtoPstr(txt);
104 SetIText(ihandle, txt);
105
106 ShowWindow(dlg);
107
108 dialog_wait:
109 done = false;
110 while (!done) {
111 ModalDialog(nil, &hit);
112 switch (hit) {
113 case SETTINGS_SAVE_ID:
114 done = true;
115 break;
116 case SETTINGS_CANCEL_ID:
117 goto dialog_close;
118 case SETTINGS_AUTHOR_ID:
119 break;
120 case SETTINGS_TABWIDTH_ID:
121 break;
122 }
123 }
124
125 GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect);
126 GetIText(ihandle, txt);
127 PtoCstr(txt);
128 if (strlen((char *)&txt) >= sizeof(settings.author)) {
129 warn("Author setting is too long (%ld max)",
130 sizeof(settings.author) - 1);
131 goto dialog_wait;
132 }
133
134 memcpy(settings.author, txt, strlen((char *)&txt) + 1);
135
136 GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect);
137 GetIText(ihandle, txt);
138 PtoCstr(txt);
139 settings.tabwidth = atoi((char *)&txt);
140 HUnlock(ihandle);
141
142 settings_save();
143
144 dialog_close:
145 DisposeDialog(dlg);
146 }