00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <fcntl.h>
00009
00010 #ifndef __GNUC__
00011 #include <io.h>
00012 #else
00013 #include <unistd.h>
00014 #endif
00015
00016 #include <string.h>
00017 #include <utilfuns.h>
00018 #include <rawgenbook.h>
00019 #include <rawstr.h>
00020
00021 #ifndef O_BINARY
00022 #define O_BINARY 0
00023 #endif
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 RawGenBook::RawGenBook(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang)
00034 : SWGenBook(iname, idesc, idisp, enc, dir, mark, ilang) {
00035 int fileMode = O_RDWR;
00036 char *buf = new char [ strlen (ipath) + 20 ];
00037
00038 entryBuf = 0;
00039 path = 0;
00040 stdstr(&path, ipath);
00041
00042
00043 if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00044 path[strlen(path)-1] = 0;
00045
00046 delete key;
00047 key = CreateKey();
00048
00049
00050 sprintf(buf, "%s.bdt", path);
00051 bdtfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00052
00053 delete [] buf;
00054
00055 }
00056
00057
00058
00059
00060
00061
00062 RawGenBook::~RawGenBook() {
00063
00064 FileMgr::systemFileMgr.close(bdtfd);
00065
00066 if (path)
00067 delete [] path;
00068
00069 if (entryBuf)
00070 delete [] entryBuf;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 char *RawGenBook::getRawEntry() {
00082
00083 __u32 offset = 0;
00084 __u32 size = 0;
00085
00086 TreeKeyIdx *key = 0;
00087 #ifndef _WIN32_WCE
00088 try {
00089 #endif
00090 key = SWDYNAMIC_CAST(TreeKeyIdx, (this->key));
00091 #ifndef _WIN32_WCE
00092 }
00093 catch ( ... ) {}
00094 #endif
00095
00096 if (!key) {
00097 key = (TreeKeyIdx *)CreateKey();
00098 (*key) = *(this->key);
00099 }
00100
00101 if (entryBuf)
00102 delete [] entryBuf;
00103
00104 int dsize;
00105 key->getUserData(&dsize);
00106 if (dsize > 7) {
00107 memcpy(&offset, key->getUserData(), 4);
00108 offset = swordtoarch32(offset);
00109
00110 memcpy(&size, key->getUserData() + 4, 4);
00111 size = swordtoarch32(size);
00112
00113 entrySize = size;
00114
00115 entryBuf = new char [ (size + 2) * FILTERPAD ];
00116 *entryBuf = 0;
00117 lseek(bdtfd->getFd(), offset, SEEK_SET);
00118 read(bdtfd->getFd(), entryBuf, size);
00119
00120 rawFilter(entryBuf, size, key);
00121
00122 if (!isUnicode())
00123 RawStr::preptext(entryBuf);
00124 }
00125 else {
00126 entryBuf = new char [2];
00127 entryBuf[0] = 0;
00128 entryBuf[1] = 0;
00129 entrySize = 0;
00130 }
00131
00132 if (key != this->key)
00133 delete key;
00134
00135 return entryBuf;
00136 }
00137
00138
00139
00140 #ifdef _MSC_VER
00141 SWModule &RawGenBook::operator =(SW_POSITION p) {
00142 #else
00143 RawGenBook &RawGenBook::operator =(SW_POSITION p) {
00144 #endif
00145 SWModule::operator =(p);
00146 return *this;
00147 }
00148
00149 SWModule &RawGenBook::setentry(const char *inbuf, long len) {
00150
00151 __u32 offset = archtosword32(lseek(bdtfd->getFd(), 0, SEEK_END));
00152 __u32 size = 0;
00153 TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
00154
00155 char userData[8];
00156
00157 if (!len)
00158 len = strlen(inbuf);
00159
00160 write(bdtfd->getFd(), inbuf, len);
00161
00162 size = archtosword32(len);
00163 memcpy(userData, &offset, 4);
00164 memcpy(userData+4, &size, 4);
00165 key->setUserData(userData, 8);
00166 key->save();
00167
00168 return *this;
00169 }
00170
00171 SWModule &RawGenBook::operator <<(const char *inbuf) {
00172 return setentry(inbuf, 0);
00173 }
00174
00175
00176 SWModule &RawGenBook::operator <<(const SWKey *inkey) {
00177 TreeKeyIdx *srckey = 0;
00178 TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
00179
00180 #ifndef _WIN32_WCE
00181 try {
00182 #endif
00183 srckey = SWDYNAMIC_CAST(TreeKeyIdx, inkey);
00184 #ifndef _WIN32_WCE
00185 }
00186 catch ( ... ) {}
00187 #endif
00188
00189 if (!srckey) {
00190 srckey = (TreeKeyIdx *)CreateKey();
00191 (*srckey) = *inkey;
00192 }
00193
00194 key->setUserData(srckey->getUserData(), 8);
00195 key->save();
00196
00197 if (inkey != srckey)
00198 delete srckey;
00199
00200 return *this;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210 void RawGenBook::deleteEntry() {
00211 TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
00212 key->remove();
00213 }
00214
00215
00216 char RawGenBook::createModule(const char *ipath) {
00217 char *path = 0;
00218 char *buf = new char [ strlen (ipath) + 20 ];
00219 FileDesc *fd;
00220 signed char retval;
00221
00222 stdstr(&path, ipath);
00223
00224 if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00225 path[strlen(path)-1] = 0;
00226
00227 sprintf(buf, "%s.bdt", path);
00228 unlink(buf);
00229 fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00230 fd->getFd();
00231 FileMgr::systemFileMgr.close(fd);
00232
00233 retval = TreeKeyIdx::create(path);
00234 delete [] path;
00235 return retval;
00236 }
00237
00238
00239 SWKey *RawGenBook::CreateKey() {
00240 TreeKeyIdx *newKey = new TreeKeyIdx(path);
00241 return newKey;
00242 }