1 /*****************************************************************************/
2 /*                                                                           */
3 /*                 (C) Copyright 1995-1996  Alberto Pasquale                 */
4 /*                 Portions (C) Copyright 1999 Per Lundberg                  */
5 /*                                                                           */
6 /*                   A L L   R I G H T S   R E S E R V E D                   */
7 /*                                                                           */
8 /*****************************************************************************/
9 /*                                                                           */
10 /* How to contact the author:  Alberto Pasquale of 2:332/504@fidonet         */
11 /*                             Viale Verdi 106                               */
12 /*                             41100 Modena                                  */
13 /*                             Italy                                         */
14 /*                                                                           */
15 /*****************************************************************************/
16 
17 #include "apgenlib.hpp"
18 #include <string.h>
19 #include "bbsgenlb.hpp"
20 
21 
FAREADAT()22 FAREADAT::FAREADAT ()
23 {
24     fareaname = NULL;
25     f = NULL;
26     fahp = NULL;
27     arean = (word) -1;
28     filesbbsbuf = new char[PATH_MAX];
29     filesbbsptr = NULL;
30 }
31 
32 
~FAREADAT()33 FAREADAT::~FAREADAT ()
34 {
35     delete[] filesbbsbuf;
36     if (fahp) {
37         delete[] fahp->pov;
38         delete[] fahp->heap;
39         delete fahp;
40     }
41     if (f)
42         fclose (f);
43     if (fareaname)
44         delete[] fareaname;
45 }
46 
47 
OpenFAreaDat(const char * FAreaDat)48 int FAREADAT::OpenFAreaDat (const char *FAreaDat)
49 {
50     char fareadatname[PATH_MAX];
51     long areaid;
52 
53     fareaname = newcpy (FAreaDat);
54 
55     strcpy (fareadatname, FAreaDat);
56     addext (fareadatname, ".DAT");
57 
58     f = fopen (fareadatname, "rb");
59     if (!f)
60         return -1;
61     setvbuf (f, NULL, _IOFBF, 8192);
62     if (fread (&areaid, sizeof (areaid), 1, f) != 1)
63         return -2;
64     if (areaid != FAREA_ID)
65         return -3;
66 
67     fahp = new FAH;
68     fahp->pov = new OVERRIDE[FAD_OVR_MAX];
69     fahp->heap = new char[FAD_HEAP_SIZE];
70 
71     return 0;
72 }
73 
74 
LoadArea()75 FAH *FAREADAT::LoadArea ()
76 {
77     if (fread (&fahp->fa, sizeof (FAREA), 1, f) != 1)
78         return NULL;
79     if (fahp->fa.cbArea != sizeof (FAREA)) {
80         if (fahp->fa.cbArea < sizeof (FAREA))
81             return NULL;
82         if (fseek (f, fahp->fa.cbArea - sizeof (FAREA), SEEK_CUR))
83             return NULL;
84     }
85     if (fahp->fa.num_override > FAD_OVR_MAX)
86         return NULL;
87     if (fahp->fa.cbHeap > FAD_HEAP_SIZE)
88         return NULL;
89     if (fread (fahp->pov, sizeof (OVERRIDE), fahp->fa.num_override, f) != fahp->fa.num_override)
90         return NULL;
91     if (fread (fahp->heap, fahp->fa.cbHeap, 1, f) != 1)
92         return NULL;
93     fahp->heap_size = fahp->fa.cbHeap;
94     fahp->bi.use_barpriv = 0;
95 
96     if (*PFAS (fahp, filesbbs) == '\0') {
97         strcpy (fl_stpcpy (filesbbsbuf, PFAS (fahp, downpath)), "files.bbs");
98         filesbbsptr = filesbbsbuf;
99     } else
100         filesbbsptr = PFAS (fahp, filesbbs);
101 
102     return fahp;
103 }
104 
105 
NextArea(int act)106 FAH *FAREADAT::NextArea (int act)
107 {
108     FAH *fahp;
109 
110     while (1) {
111         arean ++;
112         fahp = LoadArea ();
113         if (!fahp)
114             break;
115         if (act == FAD_Normal)
116             break;
117         if  (!(fahp->fa.attribs & (FA_DIVBEGIN | FA_DIVEND)))
118             break;
119     }
120 
121     return fahp;
122 }
123 
124 
Area(word num)125 FAH *FAREADAT::Area (word num)
126 {
127     char idxname[PATH_MAX];
128     strcpy (idxname, fareaname);
129     setext (idxname, ".IDX");
130 
131     FILE *fi = fopen (idxname, "rb");
132     if (!fi)
133         return NULL;
134     if (fseek (fi, num * sizeof (MFIDX), SEEK_SET)) {
135         fclose (fi);
136         return NULL;
137     }
138     MFIDX mfidx;
139     if (fread (&mfidx, sizeof (mfidx), 1, fi) != 1) {
140         fclose (fi);
141         return NULL;
142     }
143     fclose (fi);
144 
145     if (fseek (f, mfidx.ofs, SEEK_SET))
146         return NULL;
147 
148     arean = num;
149 
150     FAH *fahp = LoadArea ();
151     if (!fahp)
152         return NULL;
153     if  (fahp->fa.attribs & (FA_DIVBEGIN | FA_DIVEND))
154         return NULL;
155 
156     return fahp;
157 }
158 
159 
Area(const char * name)160 FAH *FAREADAT::Area (const char *name)
161 {
162     char idxname[PATH_MAX];
163     strcpy (idxname, fareaname);
164     setext (idxname, ".IDX");
165 
166     FILE *fi = fopen (idxname, "rb");
167     if (!fi)
168         return NULL;
169 
170     MFIDX mfidx;
171     arean = (word) -1;
172     FAH *fahp = NULL;
173 
174     while (fread (&mfidx, sizeof (mfidx), 1, fi) == 1) {
175         arean ++;
176         if (strncasecmp (name, mfidx.name, 15) == 0) {
177             if (fseek (f, mfidx.ofs, SEEK_SET)) {
178                 fclose (fi);
179                 return NULL;
180             }
181             fahp = LoadArea ();
182             if (!fahp) {
183                 fclose (fi);
184                 return NULL;
185             }
186             if (stricmp (name, PFAS (fahp, name)) == 0) {
187                 fclose (fi);
188                 if  (fahp->fa.attribs & (FA_DIVBEGIN | FA_DIVEND))
189                     return NULL;
190                 return fahp;
191             }
192         }
193     }
194 
195     fclose (fi);
196     return NULL;
197 }
198 
199 
AreaNum()200 word FAREADAT::AreaNum ()
201 {
202     return arean;
203 }
204 
205 
filesbbs()206 char *FAREADAT::filesbbs ()
207 {
208     return filesbbsptr;
209 }
210