The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawfiles.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawfiles.cpp - code for class 'RawFiles'- a module that reads and writes
4  * entries each to separate files on the filesystem
5  *
6  * $Id: rawfiles.cpp 3821 2020-11-02 18:33:02Z scribe $
7  *
8  * Copyright 1998-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 
28 #include <rawverse.h>
29 #include <rawfiles.h>
30 #include <filemgr.h>
31 #include <versekey.h>
32 #include <sysdata.h>
33 
35 
36  /******************************************************************************
37  * RawFiles Constructor - Initializes data for instance of RawFiles
38  *
39  * ENT: iname - Internal name for module
40  * idesc - Name to display to user for module
41  * idisp - Display object to use for displaying
42  */
43 
44 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)
45 {
46 }
47 
48 
49 /******************************************************************************
50  * RawFiles Destructor - Cleans up instance of RawFiles
51  */
52 
54 {
55 }
56 
57 
61 bool RawFiles::isWritable() const {
62  return ((idxfp[0]->getFd() > 0) && ((idxfp[0]->mode & FileMgr::RDWR) == FileMgr::RDWR));
63 }
64 
65 
66 /******************************************************************************
67  * RawFiles::getRawEntry - Retrieve the unprocessed entry contents at
68  * the current key position of this module
69  *
70  * RET: entry contents
71  */
72 
74  FileDesc *datafile;
75  long start = 0;
76  unsigned short size = 0;
77  const VerseKey *key = &getVerseKey();
78 
79  findOffset(key->getTestament(), key->getTestamentIndex(), &start, &size);
80 
81  entryBuf = "";
82  if (size) {
83  SWBuf tmpbuf = path;
84  tmpbuf += '/';
85  readText(key->getTestament(), start, size, entryBuf);
86  tmpbuf += entryBuf;
87  entryBuf = "";
88  datafile = FileMgr::getSystemFileMgr()->open(tmpbuf.c_str(), FileMgr::RDONLY);
89  if (datafile->getFd() > 0) {
90  size = datafile->seek(0, SEEK_END);
91  char *tmpBuf = new char [ size + 1 ];
92  memset(tmpBuf, 0, size + 1);
93  datafile->seek(0, SEEK_SET);
94  datafile->read(tmpBuf, size);
95  entryBuf = tmpBuf;
96  delete [] tmpBuf;
97 // preptext(entrybuf);
98  }
99  FileMgr::getSystemFileMgr()->close(datafile);
100  }
101  return entryBuf;
102 }
103 
104 
105 /******************************************************************************
106  * RawFiles::setEntry(char *)- Update the module's current key entry with
107  * provided text
108  */
109 
110 void RawFiles::setEntry(const char *inbuf, long len) {
111  FileDesc *datafile;
112  long start;
113  unsigned short size;
114  VerseKey *key = &getVerseKey();
115 
116  len = (len<0)?strlen(inbuf):len;
117 
118  findOffset(key->getTestament(), key->getTestamentIndex(), &start, &size);
119 
120  if (size) {
121  SWBuf tmpbuf;
122  entryBuf = path;
123  entryBuf += '/';
124  readText(key->getTestament(), start, size, tmpbuf);
125  entryBuf += tmpbuf;
126  }
127  else {
128  SWBuf tmpbuf;
129  entryBuf = path;
130  entryBuf += '/';
131  tmpbuf = getNextFilename();
132  doSetText(key->getTestament(), key->getTestamentIndex(), tmpbuf);
133  entryBuf += tmpbuf;
134  }
136  if (datafile->getFd() > 0) {
137  datafile->write(inbuf, len);
138  }
139  FileMgr::getSystemFileMgr()->close(datafile);
140 }
141 
142 
143 /******************************************************************************
144  * RawFiles::linkEntry(SWKey *)- Link the modules current key entry with
145  * another module entry
146  *
147  * RET: *this
148  */
149 
150 void RawFiles::linkEntry(const SWKey *inkey) {
151 
152  long start;
153  unsigned short size;
154  const VerseKey *key = &getVerseKey();
155 
156  findOffset(key->getTestament(), key->getTestamentIndex(), &start, &size);
157 
158  if (size) {
159  SWBuf tmpbuf;
160  readText(key->getTestament(), start, size + 2, tmpbuf);
161 
162  key = &getVerseKeyConst(inkey);
163  doSetText(key->getTestament(), key->getTestamentIndex(), tmpbuf.c_str());
164  }
165 }
166 
167 
168 /******************************************************************************
169  * RawFiles::deleteEntry - deletes this entry
170  *
171  * RET: *this
172  */
173 
175  VerseKey *key = &getVerseKey();
176  doSetText(key->getTestament(), key->getTestamentIndex(), "");
177 }
178 
179 
180 /******************************************************************************
181  * RawFiles::getNextfilename - generates a valid filename in which to store
182  * an entry
183  *
184  * RET: filename
185  */
186 
188  static SWBuf incfile;
189  SW_u32 number = 0;
190  FileDesc *datafile;
191 
192  incfile.setFormatted("%s/incfile", path);
193  datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::RDONLY);
194  if (datafile->getFd() != -1) {
195  if (datafile->read(&number, 4) != 4) number = 0;
196  number = swordtoarch32(number);
197  }
198  number++;
199  FileMgr::getSystemFileMgr()->close(datafile);
200 
202  incfile.setFormatted("%.7d", number-1);
203 
204  number = archtosword32(number);
205  datafile->write(&number, 4);
206 
207  FileMgr::getSystemFileMgr()->close(datafile);
208  return incfile;
209 }
210 
211 
212 char RawFiles::createModule(const char *path) {
213  char *incfile = new char [ strlen (path) + 16 ];
214 
215  SW_u32 zero = 0;
216  zero = archtosword32(zero);
217 
218  FileDesc *datafile;
219 
220  sprintf(incfile, "%s/incfile", path);
222  delete [] incfile;
223  datafile->write(&zero, 4);
224  FileMgr::getSystemFileMgr()->close(datafile);
225 
226  return RawVerse::createModule (path);
227 }
228 
229 
230 
static char createModule(const char *path)
Definition: rawfiles.cpp:212
virtual bool isWritable() const
Definition: rawfiles.cpp:61
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
long seek(long offset, int whence)
Definition: filemgr.cpp:143
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
const VerseKey & getVerseKeyConst(const SWKey *key=0) const
Definition: swcom.cpp:90
static unsigned int RDONLY
Definition: filemgr.h:75
#define archtosword32(x)
Definition: sysdata.h:97
virtual void deleteEntry()
Definition: rawfiles.cpp:174
#define SEEK_END
Definition: zconf.h:246
static unsigned int RDWR
Definition: filemgr.h:76
static unsigned int TRUNC
Definition: filemgr.h:74
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawfiles.cpp:110
void findOffset(char testmt, long idxoff, long *start, unsigned short *end) const
Definition: rawverse.cpp:118
virtual void linkEntry(const SWKey *linkKey)
Definition: rawfiles.cpp:150
int getFd()
Definition: filemgr.h:231
virtual SWBuf & getRawEntryBuf() const
Definition: rawfiles.cpp:73
const char * getNextFilename()
Definition: rawfiles.cpp:187
long write(const void *buf, long count)
Definition: filemgr.cpp:153
char * path
Definition: rawverse.h:45
FileDesc * idxfp[2]
Definition: rawverse.h:42
void close(FileDesc *file)
Definition: filemgr.cpp:196
const char * c_str() const
Definition: swbuf.h:158
#define swordtoarch32(x)
Definition: sysdata.h:94
void readText(char testmt, long start, unsigned short size, SWBuf &buf) const
Definition: rawverse.cpp:154
Definition: swcom.h:49
static char createModule(const char *path, const char *v11n="KJV")
Definition: rawverse.cpp:248
RawFiles(const char *ipath, const char *iname=0, const char *idesc=0, SWDisplay *idisp=0, SWTextEncoding encoding=ENC_UNKNOWN, SWTextDirection dir=DIRECTION_LTR, SWTextMarkup markup=FMT_UNKNOWN, const char *ilang=0)
Definition: rawfiles.cpp:44
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
SWBuf entryBuf
Definition: swmodule.h:140
static unsigned int CREAT
Definition: filemgr.h:72
virtual ~RawFiles()
Definition: rawfiles.cpp:53
unsigned int SW_u32
Definition: sysdata.h:41
virtual long getTestamentIndex() const
Definition: versekey.cpp:1704
static unsigned int WRONLY
Definition: filemgr.h:77
long read(void *buf, long count)
Definition: filemgr.cpp:148
void doSetText(char testmt, long idxoff, const char *buf, long len=-1)
Definition: rawverse.cpp:178
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
#define SWORD_NAMESPACE_END
Definition: defs.h:40
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
Definition: swkey.h:77
#define SWTextMarkup
Definition: swmodule.h:79
VerseKey & getVerseKey(SWKey *key=0)
Definition: swcom.cpp:123
virtual char getTestament() const
Definition: versekey.cpp:1498
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101