1 //  This may look like C code, but it is really -*- C++ -*-
2 
3 //  ------------------------------------------------------------------
4 //  The Goldware Library
5 //  Copyright (C) 1990-1999 Odinn Sorensen
6 //  ------------------------------------------------------------------
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Library General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 of the License, or (at your option) any later version.
11 //
12 //  This library is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //  Library General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Library General Public
18 //  License along with this program; if not, write to the Free
19 //  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 //  MA 02111-1307, USA
21 //  ------------------------------------------------------------------
22 //  $Id: gmowcat1.cpp,v 1.7 2006/05/14 11:45:05 ssianky Exp $
23 //  ------------------------------------------------------------------
24 //  WildCat! 4.x messagebase engine.
25 //  ------------------------------------------------------------------
26 
27 #include <gdbgerr.h>
28 #include <gmemdbg.h>
29 #include <gdbgtrk.h>
30 #include <gmowcat.h>
31 
32 
33 //  ------------------------------------------------------------------
34 
35 WCatWide* wcatwide = NULL;
36 WCatData* wcatdata = NULL;
37 int       wcatdatano = 0;
38 
39 
40 //  ------------------------------------------------------------------
41 
data_open()42 void WCatArea::data_open() {
43 
44   wide = wcatwide;
45   data = wcatdata + (wcatdatano++);
46 }
47 
48 
49 //  ------------------------------------------------------------------
50 
data_close()51 void WCatArea::data_close() {
52 
53   wcatdatano--;
54 }
55 
56 
57 //  ------------------------------------------------------------------
58 
raw_close()59 void WCatArea::raw_close() {
60 
61   GFTRK("WCatRawClose");
62 
63   if(data->fhix != -1)   ::close(data->fhix);   data->fhix = -1;
64   if(data->fhdat != -1)  ::close(data->fhdat);  data->fhdat = -1;
65 
66   GFTRK(0);
67 }
68 
69 
70 //  ------------------------------------------------------------------
71 
test_open(const char * __file)72 int WCatArea::test_open(const char* __file) {
73 
74   GFTRK("WCatTestOpen");
75 
76   int _fh;
77   long _tries = 0;
78 
79   do {
80 
81     _fh = ::sopen(__file, O_RDWR|O_BINARY|O_CREAT, WideSharemode, S_STDRW);
82     if(_fh == -1) {
83 
84       // Tell the world
85       if((errno != EACCES) or (PopupLocked(++_tries, false, __file) == false)) {
86 
87         // User requested to exit
88         WideLog->ErrOpen();
89         raw_close();
90         WideLog->printf("! A WildCat! msgbase file could not be opened.");
91         WideLog->printf(": %s.", __file);
92         WideLog->ErrOSInfo();
93         OpenErrorExit();
94       }
95     }
96   } while(_fh == -1);
97 
98   // Remove the popup window
99   if(_tries)
100     PopupLocked(0, 0, NULL);
101 
102   GFTRK(0);
103 
104   return _fh;
105 }
106 
107 
108 //  ------------------------------------------------------------------
109 
raw_open()110 void WCatArea::raw_open() {
111 
112   GFTRK("WCatRawOpen");
113 
114   data->fhix  = test_open(AddPath(real_path(), ".ix"));
115   data->fhdat = test_open(AddPath(real_path(), ".dat"));
116 
117   GFTRK(0);
118 }
119 
120 
121 //  ------------------------------------------------------------------
122 
WCatExit()123 void WCatExit() {
124 
125   throw_xrelease(wcatwide);
126   throw_xrelease(wcatdata);
127 }
128 
129 
130 //  ------------------------------------------------------------------
131 
WCatInit(int userno)132 void WCatInit(int userno) {
133 
134   wcatdata = (WCatData*)throw_calloc(3, sizeof(WCatData));
135   wcatwide = (WCatWide*)throw_calloc(1, sizeof(WCatWide));
136 
137   wcatwide->userno = userno;
138 }
139 
140 
141 //  ------------------------------------------------------------------
142 
open()143 void WCatArea::open() {
144 
145   GFTRK("WCatOpen");
146 
147   isopen++;
148   if(isopen > 2) {
149     WideLog->ErrTest();
150     WideLog->printf("! Trying to open a WildCat! msgbase more than twice.");
151     WideLog->printf(": %s, %s.", echoid(), path());
152     WideLog->printf("+ Info: This indicates a serious bug.");
153     WideLog->printf("+ Advice: Report to the Author immediately.");
154     TestErrorExit();
155   }
156   if(isopen == 1) {
157     if(ispacked()) {
158       isopen--;
159       const char* newpath = Unpack(path());
160       if(newpath == NULL)
161         packed(false);
162       set_real_path(newpath ? newpath : path());
163       isopen++;
164     }
165     data_open();
166     raw_open();
167     refresh();
168     scan();
169   }
170 
171   GFTRK(0);
172 }
173 
174 
175 //  ------------------------------------------------------------------
176 
save_lastread()177 void WCatArea::save_lastread() {
178 
179   GFTRK("WCatSaveLastread");
180 
181   int _fh = ::sopen(AddPath(real_path(), ".lrd"), O_RDWR|O_CREAT|O_BINARY, WideSharemode, S_STDRW);
182   if(_fh != -1) {
183     word _lastread = (word)Msgn->CvtReln(lastread);
184     lseekset(_fh, wcatwide->userno, sizeof(word));
185     write(_fh, &_lastread, sizeof(word));
186     ::close(_fh);
187   }
188 
189   GFTRK(0);
190 }
191 
192 
193 //  ------------------------------------------------------------------
194 
close()195 void WCatArea::close() {
196 
197   GFTRK("WCatClose");
198 
199   if(isopen) {
200     if(isopen == 1) {
201       save_lastread();
202       raw_close();
203       Msgn->Reset();
204       throw_release(data->idx);
205       data_close();
206       if(ispacked()) {
207         CleanUnpacked(real_path());
208       }
209     }
210     isopen--;
211   }
212   else {
213     WideLog->ErrTest();
214     WideLog->printf("! Trying to close an already closed WildCat! msgbase.");
215     WideLog->printf(": %s, %s.", echoid(), path());
216     WideLog->printf("+ Info: This indicates a potentially serious bug.");
217     WideLog->printf("+ Advice: Report to the Author immediately.");
218     TestErrorExit();
219   }
220 
221   GFTRK(0);
222 }
223 
224 
225 //  ------------------------------------------------------------------
226 
suspend()227 void WCatArea::suspend() {
228 
229   GFTRK("WCatSuspend");
230 
231   save_lastread();
232 
233   GFTRK(0);
234 }
235 
236 
237 //  ------------------------------------------------------------------
238 
resume()239 void WCatArea::resume() {
240 
241   GFTRK("WCatResume");
242 
243 
244 
245   GFTRK(0);
246 }
247 
248 
249 //  ------------------------------------------------------------------
250