00001
00002
00003
00004
00005
00006
00007
00008 #include <ctype.h>
00009 #include <stdio.h>
00010 #include <fcntl.h>
00011
00012 #ifndef __GNUC__
00013 #include <io.h>
00014 #else
00015 #include <unistd.h>
00016 #endif
00017
00018 #include <string.h>
00019 #include <utilfuns.h>
00020 #include <rawverse.h>
00021 #include <rawfiles.h>
00022 #include <filemgr.h>
00023
00024 #ifndef O_BINARY // O_BINARY is needed in Borland C++ 4.53
00025 #define O_BINARY 0 // If it hasn't been defined than we probably
00026 #endif // don't need it.
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 RawFiles::RawFiles(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang) : RawVerse(ipath, O_RDWR), SWCom(iname, idesc, idisp, enc, dir, mark, ilang)
00038 {
00039 }
00040
00041
00042
00043
00044
00045
00046 RawFiles::~RawFiles()
00047 {
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 char *RawFiles::getRawEntry() {
00059 FileDesc *datafile;
00060 long start = 0;
00061 unsigned short size = 0;
00062 char *tmpbuf;
00063 VerseKey *key = 0;
00064
00065 #ifndef _WIN32_WCE
00066 try {
00067 #endif
00068 key = SWDYNAMIC_CAST(VerseKey, this->key);
00069 #ifndef _WIN32_WCE
00070 }
00071 catch ( ... ) {}
00072 #endif
00073 if (!key)
00074 key = new VerseKey(this->key);
00075
00076 findoffset(key->Testament(), key->Index(), &start, &size);
00077
00078 if (entrybuf)
00079 delete [] entrybuf;
00080
00081 if (size) {
00082 tmpbuf = new char [ (size + 2) + strlen(path) + 5 ];
00083 sprintf(tmpbuf,"%s/",path);
00084 gettext(key->Testament(), start, (size + 2), tmpbuf+strlen(tmpbuf));
00085 datafile = FileMgr::systemFileMgr.open(tmpbuf, O_RDONLY|O_BINARY);
00086 delete [] tmpbuf;
00087 if (datafile->getFd() > 0) {
00088 size = lseek(datafile->getFd(), 0, SEEK_END);
00089 entrybuf = new char [ size * FILTERPAD ];
00090 memset(entrybuf, 0, size * FILTERPAD);
00091 lseek(datafile->getFd(), 0, SEEK_SET);
00092 read(datafile->getFd(), entrybuf, size);
00093
00094 }
00095 else {
00096 entrybuf = new char [2];
00097 entrybuf[0] = 0;
00098 entrybuf[1] = 0;
00099 }
00100 FileMgr::systemFileMgr.close(datafile);
00101 }
00102 else {
00103 entrybuf = new char [2];
00104 entrybuf[0] = 0;
00105 entrybuf[1] = 0;
00106 }
00107
00108 if (key != this->key)
00109 delete key;
00110
00111 return entrybuf;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 SWModule &RawFiles::operator <<(const char *inbuf) {
00123 FileDesc *datafile;
00124 long start;
00125 unsigned short size;
00126 char *tmpbuf;
00127 VerseKey *key = 0;
00128
00129 #ifndef _WIN32_WCE
00130 try {
00131 #endif
00132 key = SWDYNAMIC_CAST(VerseKey, this->key);
00133 #ifndef _WIN32_WCE
00134 }
00135 catch ( ... ) {}
00136 #endif
00137 if (!key)
00138 key = new VerseKey(this->key);
00139
00140 findoffset(key->Testament(), key->Index(), &start, &size);
00141
00142 if (size) {
00143 tmpbuf = new char [ (size + 2) + strlen(path) + 1 ];
00144 sprintf(tmpbuf, "%s/", path);
00145 gettext(key->Testament(), start, (size + 2), tmpbuf+strlen(tmpbuf));
00146 }
00147 else {
00148 tmpbuf = new char [ 16 + strlen(path) + 1 ];
00149 sprintf(tmpbuf, "%s/%s", path, getnextfilename());
00150 settext(key->Testament(), key->Index(), tmpbuf+strlen(path)+1);
00151 }
00152 datafile = FileMgr::systemFileMgr.open(tmpbuf, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
00153 delete [] tmpbuf;
00154 if (datafile->getFd() > 0) {
00155 write(datafile->getFd(), inbuf, strlen(inbuf));
00156 }
00157 FileMgr::systemFileMgr.close(datafile);
00158
00159 if (key != this->key)
00160 delete key;
00161
00162 return *this;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 SWModule &RawFiles::operator <<(const SWKey *inkey) {
00174
00175 long start;
00176 unsigned short size;
00177 char *tmpbuf;
00178 const VerseKey *key = 0;
00179
00180 #ifndef _WIN32_WCE
00181 try {
00182 #endif
00183 key = SWDYNAMIC_CAST(VerseKey, inkey);
00184 #ifndef _WIN32_WCE
00185 }
00186 catch ( ... ) {}
00187 #endif
00188 if (!key)
00189 key = new VerseKey(this->key);
00190
00191 findoffset(key->Testament(), key->Index(), &start, &size);
00192
00193 if (size) {
00194 tmpbuf = new char [ size + 2];
00195 gettext(key->Testament(), start, size + 2, tmpbuf);
00196
00197 if (key != inkey)
00198 delete key;
00199 key = 0;
00200
00201 #ifndef _WIN32_WCE
00202 try {
00203 #endif
00204 key = SWDYNAMIC_CAST(VerseKey, inkey);
00205 #ifndef _WIN32_WCE
00206 }
00207 catch ( ... ) {}
00208 #endif
00209 if (!key)
00210 key = new VerseKey(this->key);
00211 settext(key->Testament(), key->Index(), tmpbuf);
00212 }
00213
00214 if (key != inkey)
00215 delete key;
00216
00217 return *this;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227 void RawFiles::deleteEntry() {
00228
00229 VerseKey *key = 0;
00230
00231 #ifndef _WIN32_WCE
00232 try {
00233 #endif
00234 key = SWDYNAMIC_CAST(VerseKey, this->key);
00235 #ifndef _WIN32_WCE
00236 }
00237 catch ( ... ) {}
00238 #endif
00239 if (!key)
00240 key = new VerseKey(this->key);
00241
00242 settext(key->Testament(), key->Index(), "");
00243
00244 if (key != this->key)
00245 delete key;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 char *RawFiles::getnextfilename() {
00257 static char incfile[255];
00258 long number;
00259 FileDesc *datafile;
00260
00261 sprintf(incfile, "%s/incfile", path);
00262 datafile = FileMgr::systemFileMgr.open(incfile, O_RDONLY|O_BINARY);
00263 if (read(datafile->getFd(), &number, 4) != 4)
00264 number = 0;
00265 number++;
00266 FileMgr::systemFileMgr.close(datafile);
00267
00268 datafile = FileMgr::systemFileMgr.open(incfile, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
00269 write(datafile->getFd(), &number, 4);
00270 FileMgr::systemFileMgr.close(datafile);
00271 sprintf(incfile, "%.7ld", number-1);
00272 return incfile;
00273 }
00274
00275
00276 char RawFiles::createModule (const char *path) {
00277 char *incfile = new char [ strlen (path) + 16 ];
00278 static long zero = 0;
00279 FileDesc *datafile;
00280
00281 sprintf(incfile, "%s/incfile", path);
00282 datafile = FileMgr::systemFileMgr.open(incfile, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
00283 delete [] incfile;
00284 write(datafile->getFd(), &zero, 4);
00285 FileMgr::systemFileMgr.close(datafile);
00286
00287 return RawVerse::createModule (path);
00288 }
00289
00290
00291