~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/genbook/rawgenbook/rawgenbook.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *  rawtext.cpp - code for class 'RawGenBook'- a module that reads raw text files:
 
3
 *                ot and nt using indexs ??.bks ??.cps ??.vss
 
4
 */
 
5
 
 
6
 
 
7
#include <stdio.h>
 
8
#include <fcntl.h>
 
9
 
 
10
#ifndef __GNUC__
 
11
#include <io.h>
 
12
#else
 
13
#include <unistd.h>
 
14
#endif
 
15
 
 
16
#include <utilfuns.h>
 
17
#include <rawgenbook.h>
 
18
#include <rawstr.h>
 
19
 
 
20
#ifndef O_BINARY
 
21
#define O_BINARY 0
 
22
#endif
 
23
 
 
24
SWORD_NAMESPACE_START
 
25
 
 
26
/******************************************************************************
 
27
 * RawGenBook Constructor - Initializes data for instance of RawGenBook
 
28
 *
 
29
 * ENT: iname - Internal name for module
 
30
 *      idesc - Name to display to user for module
 
31
 *      idisp    - Display object to use for displaying
 
32
 */
 
33
 
 
34
RawGenBook::RawGenBook(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang)
 
35
                : SWGenBook(iname, idesc, idisp, enc, dir, mark, ilang) {
 
36
        int fileMode = O_RDWR;
 
37
        char *buf = new char [ strlen (ipath) + 20 ];
 
38
 
 
39
        path = 0;
 
40
        stdstr(&path, ipath);
 
41
 
 
42
 
 
43
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
44
                path[strlen(path)-1] = 0;
 
45
 
 
46
        delete key;
 
47
        key = CreateKey();
 
48
 
 
49
 
 
50
        sprintf(buf, "%s.bdt", path);
 
51
        bdtfd = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
 
52
 
 
53
        delete [] buf;
 
54
 
 
55
}
 
56
 
 
57
 
 
58
/******************************************************************************
 
59
 * RawGenBook Destructor - Cleans up instance of RawGenBook
 
60
 */
 
61
 
 
62
RawGenBook::~RawGenBook() {
 
63
 
 
64
        FileMgr::systemFileMgr.close(bdtfd);
 
65
 
 
66
        if (path)
 
67
                delete [] path;
 
68
 
 
69
}
 
70
 
 
71
 
 
72
/******************************************************************************
 
73
 * RawGenBook::getRawEntry      - Returns the correct verse when char * cast
 
74
 *                                      is requested
 
75
 *
 
76
 * RET: string buffer with verse
 
77
 */
 
78
 
 
79
SWBuf &RawGenBook::getRawEntryBuf() {
 
80
 
 
81
        __u32 offset = 0;
 
82
        __u32 size = 0;
 
83
 
 
84
        TreeKeyIdx *key = 0;
 
85
        try {
 
86
                key = SWDYNAMIC_CAST(TreeKeyIdx, (this->key));
 
87
        }
 
88
        catch ( ... ) {}
 
89
 
 
90
        if (!key) {
 
91
                key = (TreeKeyIdx *)CreateKey();
 
92
                (*key) = *(this->key);
 
93
        }
 
94
 
 
95
        int dsize;
 
96
        key->getUserData(&dsize);
 
97
        entryBuf = "";
 
98
        if (dsize > 7) {
 
99
                memcpy(&offset, key->getUserData(), 4);
 
100
                offset = swordtoarch32(offset);
 
101
 
 
102
                memcpy(&size, key->getUserData() + 4, 4);
 
103
                size = swordtoarch32(size);
 
104
 
 
105
                entrySize = size;        // support getEntrySize call
 
106
 
 
107
                entryBuf.setFillByte(0);
 
108
                entryBuf.setSize(size);
 
109
                lseek(bdtfd->getFd(), offset, SEEK_SET);
 
110
                read(bdtfd->getFd(), entryBuf.getRawData(), size);
 
111
 
 
112
                rawFilter(entryBuf, 0); // hack, decipher
 
113
                rawFilter(entryBuf, key);
 
114
 
 
115
//                 if (!isUnicode())
 
116
                        RawStr::prepText(entryBuf);
 
117
        }
 
118
 
 
119
        if (key != this->key) // free our key if we created a VerseKey
 
120
                delete key;
 
121
 
 
122
        return entryBuf;
 
123
}
 
124
 
 
125
 
 
126
void RawGenBook::setEntry(const char *inbuf, long len) {
 
127
 
 
128
        __u32 offset = archtosword32(lseek(bdtfd->getFd(), 0, SEEK_END));
 
129
        __u32 size = 0;
 
130
        TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
 
131
 
 
132
        char userData[8];
 
133
 
 
134
        if (!len)
 
135
                len = strlen(inbuf);
 
136
 
 
137
        write(bdtfd->getFd(), inbuf, len);
 
138
 
 
139
        size = archtosword32(len);
 
140
        memcpy(userData, &offset, 4);
 
141
        memcpy(userData+4, &size, 4);
 
142
        key->setUserData(userData, 8);
 
143
        key->save();
 
144
}
 
145
 
 
146
 
 
147
void RawGenBook::linkEntry(const SWKey *inkey) {
 
148
        TreeKeyIdx *srckey = 0;
 
149
        TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
 
150
        // see if we have a VerseKey * or decendant
 
151
        try {
 
152
                srckey = SWDYNAMIC_CAST(TreeKeyIdx, inkey);
 
153
        }
 
154
        catch ( ... ) {}
 
155
        // if we don't have a VerseKey * decendant, create our own
 
156
        if (!srckey) {
 
157
                srckey = (TreeKeyIdx *)CreateKey();
 
158
                (*srckey) = *inkey;
 
159
        }
 
160
 
 
161
        key->setUserData(srckey->getUserData(), 8);
 
162
        key->save();
 
163
 
 
164
        if (inkey != srckey) // free our key if we created a VerseKey
 
165
                delete srckey;
 
166
}
 
167
 
 
168
 
 
169
/******************************************************************************
 
170
 * RawGenBook::deleteEntry      - deletes this entry
 
171
 *
 
172
 * RET: *this
 
173
 */
 
174
 
 
175
void RawGenBook::deleteEntry() {
 
176
        TreeKeyIdx *key = ((TreeKeyIdx *)this->key);
 
177
        key->remove();
 
178
}
 
179
 
 
180
 
 
181
char RawGenBook::createModule(const char *ipath) {
 
182
        char *path = 0;
 
183
        char *buf = new char [ strlen (ipath) + 20 ];
 
184
        FileDesc *fd;
 
185
        signed char retval;
 
186
 
 
187
        stdstr(&path, ipath);
 
188
 
 
189
        if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
 
190
                path[strlen(path)-1] = 0;
 
191
 
 
192
        sprintf(buf, "%s.bdt", path);
 
193
        unlink(buf);
 
194
        fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
 
195
        fd->getFd();
 
196
        FileMgr::systemFileMgr.close(fd);
 
197
 
 
198
        retval = TreeKeyIdx::create(path);
 
199
        delete [] path;
 
200
        return retval;  
 
201
}
 
202
 
 
203
 
 
204
SWKey *RawGenBook::CreateKey() {
 
205
        TreeKeyIdx *newKey = new TreeKeyIdx(path);
 
206
        return newKey;
 
207
}
 
208
 
 
209
SWORD_NAMESPACE_END