summaryrefslogtreecommitdiff
path: root/cmake/platforms/windows/rawfiles.cpp
blob: 25bbcac2933c280186f01838f5dce0f61ac737f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/******************************************************************************
 *  rawfiles.cpp - code for class 'RawFiles'- a module that produces HTML HREFs
 *            pointing to actual text desired.  Uses standard
 *            files:    ot and nt using indexs ??.bks ??.cps ??.vss
 *
 *
 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
 *    CrossWire Bible Society
 *    P. O. Box 2528
 *    Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>

#include <rawverse.h>
#include <rawfiles.h>
#include <filemgr.h>
#include <versekey.h>
#include <sysdata.h>

SWORD_NAMESPACE_START

 /******************************************************************************
 * RawFiles Constructor - Initializes data for instance of RawFiles
 *
 * ENT:    iname - Internal name for module
 *         idesc - Name to display to user for module
 *         idisp - Display object to use for displaying
 */

RawFiles::RawFiles(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang) : RawVerse(ipath, FileMgr::RDWR), SWCom(iname, idesc, idisp, enc, dir, mark, ilang)
{
}


/******************************************************************************
 * RawFiles Destructor - Cleans up instance of RawFiles
 */

RawFiles::~RawFiles()
{
}


/** Is the module writable? :)
* @return yes or no
*/
bool RawFiles::isWritable() {
    return ((idxfp[0]->getFd() > 0) && ((idxfp[0]->mode & FileMgr::RDWR) == FileMgr::RDWR));
}


/******************************************************************************
 * RawFiles::getRawEntry    - Retrieve the unprocessed entry contents at
 *                            the current key position of this module
 *
 * RET: entry contents
 */

SWBuf &RawFiles::getRawEntryBuf() {
    FileDesc *datafile;
    long  start = 0;
    unsigned short size = 0;
    VerseKey *key = &getVerseKey();

    findOffset(key->Testament(), key->TestamentIndex(), &start, &size);

    entryBuf = "";
    if (size) {
        SWBuf tmpbuf = path;
        tmpbuf += '/';
        readText(key->Testament(), start, size, entryBuf);
        tmpbuf += entryBuf;
        entryBuf = "";
        datafile = FileMgr::getSystemFileMgr()->open(tmpbuf.c_str(), FileMgr::RDONLY);
        if (datafile->getFd() > 0) {
            size = datafile->seek(0, SEEK_END);
            char *tmpBuf = new char [ size + 1 ];
            memset(tmpBuf, 0, size + 1);
            datafile->seek(0, SEEK_SET);
            datafile->read(tmpBuf, size);
            entryBuf = tmpBuf;
            delete [] tmpBuf;
//            preptext(entrybuf);
        }
        FileMgr::getSystemFileMgr()->close(datafile);
    }
    return entryBuf;
}


/******************************************************************************
 * RawFiles::setEntry(char *)- Update the module's current key entry with
 *                             provided text
 */

void RawFiles::setEntry(const char *inbuf, long len) {
    FileDesc *datafile;
    long  start;
    unsigned short size;
    VerseKey *key = &getVerseKey();

    len = (len<0)?strlen(inbuf):len;

    findOffset(key->Testament(), key->TestamentIndex(), &start, &size);

    if (size) {
        SWBuf tmpbuf;
        entryBuf = path;
        entryBuf += '/';
        readText(key->Testament(), start, size, tmpbuf);
        entryBuf += tmpbuf;
    }
    else {
        SWBuf tmpbuf;
        entryBuf = path;
        entryBuf += '/';
        tmpbuf = getNextFilename();
        doSetText(key->Testament(), key->TestamentIndex(), tmpbuf);
        entryBuf += tmpbuf;
    }
    datafile = FileMgr::getSystemFileMgr()->open(entryBuf, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC);
    if (datafile->getFd() > 0) {
        datafile->write(inbuf, len);
    }
    FileMgr::getSystemFileMgr()->close(datafile);
}


/******************************************************************************
 * RawFiles::linkEntry(SWKey *)- Link the modules current key entry with
 *                               another module entry
 *
 * RET: *this
 */

void RawFiles::linkEntry(const SWKey *inkey) {

    long  start;
    unsigned short size;
    const VerseKey *key = &getVerseKey();

    findOffset(key->Testament(), key->TestamentIndex(), &start, &size);

    if (size) {
        SWBuf tmpbuf;
        readText(key->Testament(), start, size + 2, tmpbuf);

        key = &getVerseKey(inkey);
        doSetText(key->Testament(), key->TestamentIndex(), tmpbuf.c_str());
    }
}


/******************************************************************************
 * RawFiles::deleteEntry    - deletes this entry
 *
 * RET: *this
 */

void RawFiles::deleteEntry() {
    VerseKey *key = &getVerseKey();
    doSetText(key->Testament(), key->TestamentIndex(), "");
}


/******************************************************************************
 * RawFiles::getNextfilename - generates a valid filename in which to store
 *                             an entry
 *
 * RET: filename
 */

const char *RawFiles::getNextFilename() {
    static SWBuf incfile;
    __u32 number = 0;
    FileDesc *datafile;

    incfile.setFormatted("%s/incfile", path);
    datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::RDONLY);
    if (datafile->getFd() != -1) {
        if (datafile->read(&number, 4) != 4) number = 0;
        number = swordtoarch32(number);
    }
    number++;
    FileMgr::getSystemFileMgr()->close(datafile);

    datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC);
    incfile.setFormatted("%.7d", number-1);

    number = archtosword32(number);
    datafile->write(&number, 4);

    FileMgr::getSystemFileMgr()->close(datafile);
    return incfile;
}


char RawFiles::createModule(const char *path) {
    char *incfile = new char [ strlen (path) + 16 ];

    __u32 zero = 0;
    zero = archtosword32(zero);

    FileDesc *datafile;

    sprintf(incfile, "%s/incfile", path);
    datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC);
    delete [] incfile;
    datafile->write(&zero, 4);
    FileMgr::getSystemFileMgr()->close(datafile);

    return RawVerse::createModule (path);
}



SWORD_NAMESPACE_END