text: --=-NkVkUG8T6fRojPA+2c+R Content-Type: text/plain Content-Transfer-Encoding: 7bit. On Wed, 2001-12-26 at 03:59, Bobby Nations wrote: > Guys, > > > --=-NkVkUG8T6fRojPA+2c+R Content-Disposition: attachment; filename=RawVerse.java Content-Transfer-Encoding: quoted-printable Content-Type: text/x-java; charset=ISO-8859-1 package com.eireneh.bible.book.sword; import java.io.*; import com.eireneh.util.*; /** * Code for class 'RawVerse'- a module that reads raw text files * ot and nt using indexs ??.bks ??.cps ??.vss and provides lookup and pars= ing * functions based on class VerseKey */ public class RawVerse { /** constant for the introduction */ public static final int TESTAMENT_INTRO =3D 0; /** constant for the old testament */ public static final int TESTAMENT_OLD =3D 1; /** constant for the new testament */ public static final int TESTAMENT_NEW =3D 2; /** * RawVerse Constructor - Initializes data for instance of RawVerse * @param path - path of the directory where data and index files are l= ocated. * be sure to include the trailing separator (e.g. '/' or '\') * (e.g. 'modules/texts/rawtext/webster/') */ public RawVerse(String path) throws FileNotFoundException { idx_raf[TESTAMENT_OLD] =3D new RandomAccessFile(path + "ot.vss", "r= "); idx_raf[TESTAMENT_NEW] =3D new RandomAccessFile(path + "nt.vss", "r= "); txt_raf[TESTAMENT_OLD] =3D new RandomAccessFile(path + "ot", "r"); txt_raf[TESTAMENT_NEW] =3D new RandomAccessFile(path + "nt", "r"); // The original had a dtor that did the equiv of .close()ing the ab= ove // I'm not sure that there is a delete type ability in Book.java an= d // the finalizer for RandomAccessFile will do it anyway so for the // moment I'm going to ignore this. // The original also stored the path, but I don't think it ever use= dExcepti= on { Location loc =3D new Location(); // There was a bodge here to move testament around if someone wante= d // to read the intro? We just have the set of static finals above // if (testament =3D=3D 0) // testament =3D idx_raf[1] =3D=3D null ? 1 : 2; // There was a test here to check ensure that is idx_raf[testament-= 1] // was null then we returned an default Location (of 0,0). However // This seems like papering over any errors so I have left it out f= or // the time being // I've now totally re-written this because we did have byte-sex // problems. The file is little endian, and we read big endianly. // read the next 6 byes. idx_raf[testament].seek(idxoff*6); byte[] read =3D new byte[6]; idx_raf[testament].readFully(read); int[] temp =3D new int[6]; for (int i=3D0; i<temp.length; i++) { temp[i] =3D read[i] >=3D 0 ? read[i] : 256 + read[i]; log.fine("temp["+i+"]=3D"+temp[i]); } loc.start =3D (temp[3] << 24) | (temp[2] << 16) | (temp[1] << 8) | = temp[0]; loc.size =3D (temp[5] << 8) | temp[4]; // the original lseek used SEEK_SET. This is the only option in Jav= a // The *6 is because we use 4 bytes for the offset, and 2 for the l= ength // There used to be some code at the start of the method like: // idxoff *=3D 6; // But itn't good to alter parameters and here is the only place th= at // it is used. // There was some BIGENDIAN swapping stuff here. To be honest I // can't be bothered to think about whether or not this is needed // right now. // *start =3D lelong(*start); // *size =3D =3D =3D new StringBuffer(text); boolean space =3D false; boolean cr =3D false; boolean realdata =3D false; char nlcnt =3D 0; int to =3D 0; for (int from=3D0; from<buf.length(); from++) { switch (buf.charAt(from)) { case 10: if (!realdata) continue; space =3D (cr) ? false : true; cr =3D false; nlcnt++; if (nlcnt > 1) { // buf.setCharAt(to++, nl); buf.setCharAt(to++, '\n'); // nlcnt =3D 0; } continue; case 13: if (!realdata) continue; buf.setCharAt(to++, '\n'); space =3D false; cr =3D true; continue; } realdata =3D true; nlcnt =3D 0; if (space) { space =3D false; if (buf.charAt(from) !=3D ' ') { I= OException { // As in getText() we don't alter the formal parameter // idxoff *=3D 6; // As in getText() There was some messing around with testament // if (testament =3D=3D 0) // testament =3D idx_raf[1] =3D=3D null ? 1 : 2; // outsize started off being unsigned // and it looks like "unsigned short size;" is not used short outsize =3D (short) buf.length(); // There was some more BIGENDIAN nonsense here. Again ignoring the // MACOSX bits it looked like: // start =3D lelong(start); // outsize =3D leshort(size); // I've also moved things around very slightly, the endian bits cam= e // just before the writeShort(); idx_raf[testament].seek(idxoff*6); long start =3D idx_raf[testament].readLong(); idx_raf[testament].writeShort(outsize); // There is some encoding stuff to be thought about here byte[] data =3D basicall= y // deleted and recreated (empty) the named file. // unlink(buf); // fd =3D FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BIN= ARY, S_IREAD|S_IWRITE); // FileMgr::systemFileMgr.close(fd); File file =3D new File(filename); file.delete(); file.createNewFile(); } /** * There has to be a better method than this. findoffset() returned a s= tart * and and offset, and multiple return values are not possible in Java. * It seems to me that returning start and size from a public i/f repre= sents * showing our callers more than we should and I expect that the soluti= on * lies in a thorough sorting out if the interface, but I want to keep * the methods unchanged as reasonable right now. */ public class Location { /** Where does the data start */ public long start =3D 0; /** The data length. Is short long enough? the original was unsigne= d short */ public int size =3D 0; /** * Debug only */ public String toString() { return "start=3D"+start+", size=3D"+size; } } /** * A test program */ public static void main(String[] args) { try { // To start with I'm going to hard code the path String path =3D "/usr/apps/sword/modules/texts/rawtext/kjv/"; RawVerse verse =3D new RawVerse(path); Location loc =3D verse.findOffset(RawVerse.TESTAMENT_NEW, 6); String pre =3D verse.getText(RawVerse.TESTAMENT_NEW, loc); log.fine("loc=3D"+loc); log.fine("pre=3D"+pre); log.fine("post=3D"+verse.prepText(pre)); } catch (Exception ex) { log.log(Level.INFO, "Failure", ex); } } /** The array of index files */ private RandomAccessFile[] idx_raf =3D new RandomAccessFile[3]; /** The array of data files */ private RandomAccessFile[] txt_raf =3D new RandomAccessFile[3]; /** The log stream */ protected static Logger log =3D Logger.getLogger("bible.book"); } --=-NkVkUG8T6fRojPA+2c+R--