00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <ctype.h>
00010 #include <stdio.h>
00011 #include <fcntl.h>
00012 #include <errno.h>
00013
00014 #ifndef __GNUC__
00015 #include <io.h>
00016 #include <sys/stat.h>
00017 #else
00018 #include <unistd.h>
00019 #endif
00020
00021 #include <string.h>
00022 #include <utilfuns.h>
00023 #include <rawverse.h>
00024 #include <versekey.h>
00025 #include <sysdata.h>
00026
00027 #ifndef O_BINARY // O_BINARY is needed in Borland C++ 4.53
00028 #define O_BINARY 0 // If it hasn't been defined than we probably
00029 #endif // don't need it.
00030
00031
00032
00033
00034
00035
00036 int RawVerse::instance = 0;
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 RawVerse::RawVerse(const char *ipath, int fileMode)
00048 {
00049 char *buf;
00050
00051 nl = '\n';
00052 path = 0;
00053 stdstr(&path, ipath);
00054 buf = new char [ strlen(path) + 80 ];
00055 if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00056 path[strlen(path)-1] = 0;
00057
00058 if (fileMode == -1) {
00059 fileMode = O_RDWR;
00060 }
00061
00062 sprintf(buf, "%s/ot.vss", path);
00063 idxfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00064
00065 sprintf(buf, "%s/nt.vss", path);
00066 idxfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00067
00068 sprintf(buf, "%s/ot", path);
00069 textfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00070
00071 sprintf(buf, "%s/nt", path);
00072 textfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00073
00074 delete [] buf;
00075 instance++;
00076 }
00077
00078
00079
00080
00081
00082
00083 RawVerse::~RawVerse()
00084 {
00085 int loop1;
00086
00087 if (path)
00088 delete [] path;
00089
00090 --instance;
00091
00092 for (loop1 = 0; loop1 < 2; loop1++) {
00093 FileMgr::systemFileMgr.close(idxfp[loop1]);
00094 FileMgr::systemFileMgr.close(textfp[loop1]);
00095 }
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void RawVerse::findoffset(char testmt, long idxoff, long *start, unsigned short *size) {
00109 idxoff *= 6;
00110 if (!testmt)
00111 testmt = ((idxfp[1]) ? 1:2);
00112
00113 if (idxfp[testmt-1]->getFd() >= 0) {
00114 lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
00115 read(idxfp[testmt-1]->getFd(), start, 4);
00116 long len = read(idxfp[testmt-1]->getFd(), size, 2);
00117
00118 *start = swordtoarch32(*start);
00119 *size = swordtoarch16(*size);
00120
00121 if (len < 2) {
00122 *size = (unsigned short)((*start) ? (lseek(textfp[testmt-1]->getFd(), 0, SEEK_END) - (long)*start) : 0);
00123 }
00124 }
00125 else {
00126 *start = 0;
00127 *size = 0;
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 void RawVerse::preptext(char *buf)
00141 {
00142 char *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
00143
00144 for (to = from = buf; *from; from++) {
00145 switch (*from) {
00146 case 10:
00147 if (!realdata)
00148 continue;
00149 space = (cr) ? 0 : 1;
00150 cr = 0;
00151 nlcnt++;
00152 if (nlcnt > 1) {
00153
00154 *to++ = nl;
00155
00156 }
00157 continue;
00158 case 13:
00159 if (!realdata)
00160 continue;
00161 *to++ = nl;
00162 space = 0;
00163 cr = 1;
00164 continue;
00165 }
00166 realdata = 1;
00167 nlcnt = 0;
00168 if (space) {
00169 space = 0;
00170 if (*from != ' ') {
00171 *to++ = ' ';
00172 from--;
00173 continue;
00174 }
00175 }
00176 *to++ = *from;
00177 }
00178 *to = 0;
00179
00180 while (to > (buf+1)) {
00181 to--;
00182 if ((*to == 10) || (*to == ' '))
00183 *to = 0;
00184 else break;
00185 }
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 void RawVerse::gettext(char testmt, long start, unsigned short size, char *buf) {
00200 memset(buf, 0, size+1);
00201 if (!testmt)
00202 testmt = ((idxfp[1]) ? 1:2);
00203 if (size) {
00204 if (textfp[testmt-1]->getFd() >= 0) {
00205 lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
00206 read(textfp[testmt-1]->getFd(), buf, (int)size - 2);
00207 }
00208 }
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 void RawVerse::settext(char testmt, long idxoff, const char *buf, long len)
00222 {
00223 long start, outstart;
00224 unsigned short size;
00225 unsigned short outsize;
00226 static const char nl[] = {13, 10};
00227
00228 idxoff *= 6;
00229 if (!testmt)
00230 testmt = ((idxfp[1]) ? 1:2);
00231
00232 size = outsize = len ? len : strlen(buf);
00233
00234 start = outstart = lseek(textfp[testmt-1]->getFd(), 0, SEEK_END);
00235 lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
00236
00237 if (size) {
00238 lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
00239 write(textfp[testmt-1]->getFd(), buf, (int)size);
00240
00241
00242 write(textfp[testmt-1]->getFd(), &nl, 2);
00243 }
00244 else {
00245 start = 0;
00246 }
00247
00248 outstart = archtosword32(start);
00249 outsize = archtosword16(size);
00250
00251 write(idxfp[testmt-1]->getFd(), &outstart, 4);
00252 write(idxfp[testmt-1]->getFd(), &outsize, 2);
00253
00254
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 void RawVerse::linkentry(char testmt, long destidxoff, long srcidxoff) {
00267 long start;
00268 unsigned short size;
00269
00270 destidxoff *= 6;
00271 srcidxoff *= 6;
00272
00273 if (!testmt)
00274 testmt = ((idxfp[1]) ? 1:2);
00275
00276
00277 lseek(idxfp[testmt-1]->getFd(), srcidxoff, SEEK_SET);
00278 read(idxfp[testmt-1]->getFd(), &start, 4);
00279 read(idxfp[testmt-1]->getFd(), &size, 2);
00280
00281
00282 lseek(idxfp[testmt-1]->getFd(), destidxoff, SEEK_SET);
00283 write(idxfp[testmt-1]->getFd(), &start, 4);
00284 write(idxfp[testmt-1]->getFd(), &size, 2);
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 char RawVerse::createModule(const char *ipath)
00296 {
00297 char *path = 0;
00298 char *buf = new char [ strlen (ipath) + 20 ];
00299 FileDesc *fd, *fd2;
00300
00301 stdstr(&path, ipath);
00302
00303 if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00304 path[strlen(path)-1] = 0;
00305
00306 sprintf(buf, "%s/ot", path);
00307 unlink(buf);
00308 fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00309 fd->getFd();
00310 FileMgr::systemFileMgr.close(fd);
00311
00312 sprintf(buf, "%s/nt", path);
00313 unlink(buf);
00314 fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00315 fd->getFd();
00316 FileMgr::systemFileMgr.close(fd);
00317
00318 sprintf(buf, "%s/ot.vss", path);
00319 unlink(buf);
00320 fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00321 fd->getFd();
00322
00323 sprintf(buf, "%s/nt.vss", path);
00324 unlink(buf);
00325 fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00326 fd2->getFd();
00327
00328 VerseKey vk;
00329 vk.Headings(1);
00330 long offset = 0;
00331 short size = 0;
00332 for (vk = TOP; !vk.Error(); vk++) {
00333 write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &offset, 4);
00334 write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &size, 2);
00335 }
00336
00337 FileMgr::systemFileMgr.close(fd);
00338 FileMgr::systemFileMgr.close(fd2);
00339
00340 delete [] path;
00341
00342
00343
00344
00345
00346 return 0;
00347 }