home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
linuxmafia.com 2016
/
linuxmafia.com.tar
/
linuxmafia.com
/
pub
/
palmos
/
astro-src-2.1.0beta.tar.gz
/
astro-src-2.1.0beta.tar
/
astro-src-2.1.0beta
/
java
/
WritePDB.java
< prev
Wrap
Text File
|
2000-08-13
|
6KB
|
205 lines
import palmutils.pdb.*;
import java.io.*;
import java.util.*;
public class WritePDB
{
static void encode(String source,
String destination,
boolean noSeconds)
{
FileReader fr = null;
BufferedReader br = null;
RandomAccessFile raf = null;
DatabaseHeader databaseHeader = new DatabaseHeader();
AstroAppInfo astroAppInfo = new AstroAppInfo();
RecordIndex recordIndex = new RecordIndex();
Vector records = new Vector();
try
{
/*
* Open the files
*/
fr = new FileReader(source);
br = new BufferedReader(fr);
raf = new RandomAccessFile(destination,"rw");
}
catch(FileNotFoundException fnfe)
{
System.err.println("Could not open "+source+".");
System.err.println(fnfe.getMessage());
return;
}
catch(IOException ioe)
{
System.err.println("Could not open "+destination+".");
System.err.println(ioe.getMessage());
return;
}
try
{
/*
* Initialize the header.
*/
databaseHeader.setName(br.readLine());
databaseHeader.setVersion((short)0);
databaseHeader.setTypeID("DATA");
databaseHeader.setCreatorID("MWH2");
/*
* Read the records, loading them into the vector
*/
while (br.ready())
{
AstroRecord ar = new AstroRecord();
String buffer = br.readLine();
StringTokenizer st = null;
try
{
st = new StringTokenizer(buffer,"\t",false);
}
catch (Exception e)
{
throw new EOFException();
}
if (st.countTokens() == 0)
throw new EOFException();
ar.description = st.nextToken();
ar.raH = Short.valueOf(st.nextToken()).shortValue();
if (noSeconds)
{
double d = Double.valueOf(st.nextToken()).doubleValue();
ar.raM = (short)d;
d = Math.round(d*60.0 - (double)ar.raM*(double)60.0);
ar.raS = (short)d;
}
else
{
ar.raM = Short.valueOf(st.nextToken()).shortValue();
ar.raS = Double.valueOf(st.nextToken()).shortValue();
}
ar.decD = Short.valueOf(st.nextToken()).shortValue();
if (noSeconds)
{
double d = Double.valueOf(st.nextToken()).doubleValue();
ar.decM = (short)d;
d = Math.round(d*60.0 - (double)ar.decM*(double)60.0);
ar.decS = (short)d;
}
else
{
ar.decM = Short.valueOf(st.nextToken()).shortValue();
ar.decS = Double.valueOf(st.nextToken()).shortValue();
}
ar.magnitude = st.nextToken();
ar.size = st.nextToken();
System.out.println(ar.toString());
records.addElement(ar);
recordIndex.addOffset(new RecordOffset());
}
}
catch (EOFException ee)
{
// do nothing
}
catch (Exception e)
{
System.err.println(e);
e.printStackTrace();
return;
}
try
{
/*
* Calculate the size of the header & index.
*/
int offset = databaseHeader.getSize() +
recordIndex.getSize();
/*
* Set the location of the app info.
*/
databaseHeader.setAppInfoID(offset);
offset += astroAppInfo.getSize();
/*
* Up date the app info structure.
*/
astroAppInfo.numRecords = recordIndex.getNumRecords();
astroAppInfo.currRecord = 0;
/*
* Build the record index
*/
for (int i=0; i < astroAppInfo.numRecords; i++)
{
RecordOffset ro = recordIndex.getOffset(i);
AstroRecord ar = (AstroRecord)records.elementAt(i);
ro.setOffset(offset);
offset = offset + ar.getSize();
}
br.close();
/*
* Write the header, index and app info.
*/
databaseHeader.write(raf);
recordIndex.write(raf);
astroAppInfo.write(raf);
/*
* Write the records
*/
for (int i=0; i < astroAppInfo.numRecords; i++)
{
AstroRecord ar = (AstroRecord)records.elementAt(i);
ar.write(raf);
}
raf.close();
}
catch (Exception e)
{
System.err.println(e);
e.printStackTrace();
}
}
static public void main(String args[]) throws IOException
{
int argsOffset = 0;
boolean noSeconds = true;
while (argsOffset <= args.length && args[argsOffset].charAt(0)=='-')
try {
switch (args[argsOffset].charAt(1))
{
case 's':
noSeconds = false;
break;
default:
System.err.println("Invalid option.");
return;
}
argsOffset++;
}
catch (Exception e)
{
System.err.println("Failed parsing arguments.");
return;
}
encode(args[argsOffset],args[argsOffset+1],noSeconds);
}
}