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   
Text File  |  2000-08-13  |  6KB  |  205 lines

  1. import palmutils.pdb.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class WritePDB 
  6. {
  7.     static void encode(String source,
  8.                        String destination,
  9.                        boolean noSeconds)
  10.     {
  11.         FileReader fr = null;
  12.         BufferedReader br = null;
  13.         RandomAccessFile raf = null;
  14.         DatabaseHeader databaseHeader = new DatabaseHeader();
  15.         AstroAppInfo astroAppInfo = new AstroAppInfo();
  16.         RecordIndex recordIndex = new RecordIndex();
  17.         Vector records = new Vector();
  18.         
  19.         try 
  20.         {
  21.             /*
  22.              * Open the files
  23.              */
  24.             fr = new FileReader(source);
  25.             br = new BufferedReader(fr);
  26.             raf = new RandomAccessFile(destination,"rw");
  27.         }
  28.         catch(FileNotFoundException fnfe) 
  29.         {
  30.             System.err.println("Could not open "+source+".");
  31.             System.err.println(fnfe.getMessage());
  32.             return;
  33.         }
  34.         catch(IOException ioe) 
  35.         {
  36.             System.err.println("Could not open "+destination+".");
  37.             System.err.println(ioe.getMessage());
  38.             return;
  39.         }
  40.  
  41.         try 
  42.         {
  43.             /*
  44.              * Initialize the header.
  45.              */
  46.             databaseHeader.setName(br.readLine());
  47.             databaseHeader.setVersion((short)0);
  48.             databaseHeader.setTypeID("DATA");
  49.             databaseHeader.setCreatorID("MWH2");
  50.  
  51.             /*
  52.              * Read the records, loading them into the vector
  53.              */
  54.             while (br.ready()) 
  55.             {
  56.                 AstroRecord ar = new AstroRecord();
  57.                 String buffer = br.readLine();
  58.                 StringTokenizer st = null;
  59.  
  60.                 try 
  61.                 {
  62.                     st = new StringTokenizer(buffer,"\t",false);
  63.                 }
  64.                 catch (Exception e) 
  65.                 {
  66.                     throw new EOFException();
  67.                 }
  68.  
  69.                 if (st.countTokens() == 0)
  70.                     throw new EOFException();
  71.  
  72.                 ar.description = st.nextToken();
  73.                 ar.raH = Short.valueOf(st.nextToken()).shortValue();
  74.                 if (noSeconds) 
  75.                 {
  76.                     double d = Double.valueOf(st.nextToken()).doubleValue();
  77.                     ar.raM = (short)d;
  78.                     d = Math.round(d*60.0 - (double)ar.raM*(double)60.0);
  79.                     ar.raS = (short)d;
  80.                 }
  81.                 else 
  82.                 {
  83.                     ar.raM = Short.valueOf(st.nextToken()).shortValue();
  84.                     ar.raS = Double.valueOf(st.nextToken()).shortValue();
  85.                 }
  86.                 
  87.                 ar.decD = Short.valueOf(st.nextToken()).shortValue();
  88.                 if (noSeconds) 
  89.                 {
  90.                     double d = Double.valueOf(st.nextToken()).doubleValue();
  91.                     ar.decM = (short)d;
  92.                     d = Math.round(d*60.0 - (double)ar.decM*(double)60.0);
  93.                     ar.decS = (short)d;
  94.                 }
  95.                 else 
  96.                 {
  97.                     ar.decM = Short.valueOf(st.nextToken()).shortValue();
  98.                     ar.decS = Double.valueOf(st.nextToken()).shortValue();
  99.                 }
  100.                 
  101.                 ar.magnitude = st.nextToken();
  102.                 ar.size = st.nextToken();
  103.  
  104.                 System.out.println(ar.toString());
  105.                 records.addElement(ar);
  106.                 recordIndex.addOffset(new RecordOffset());
  107.                 
  108.             }
  109.             
  110.         }
  111.         catch (EOFException ee) 
  112.         {
  113.             // do nothing
  114.         }
  115.         catch (Exception e) 
  116.         {
  117.             System.err.println(e);
  118.             e.printStackTrace();
  119.             return;
  120.         }
  121.         
  122.         try 
  123.         {
  124.             /*
  125.              * Calculate the size of the header & index.
  126.              */
  127.             int offset = databaseHeader.getSize() +
  128.                          recordIndex.getSize();
  129.  
  130.             /*
  131.              * Set the location of the app info.
  132.              */
  133.             databaseHeader.setAppInfoID(offset);
  134.             offset += astroAppInfo.getSize();
  135.  
  136.             /*
  137.              * Up date the app info structure.
  138.              */
  139.             astroAppInfo.numRecords = recordIndex.getNumRecords();
  140.             astroAppInfo.currRecord = 0;
  141.  
  142.             /*
  143.              * Build the record index
  144.              */
  145.             for (int i=0; i < astroAppInfo.numRecords; i++) 
  146.             {
  147.                 RecordOffset ro = recordIndex.getOffset(i);
  148.                 AstroRecord ar = (AstroRecord)records.elementAt(i);
  149.                 ro.setOffset(offset);
  150.                 offset = offset + ar.getSize();
  151.             }
  152.  
  153.             br.close();
  154.  
  155.             /*
  156.              * Write the header, index and app info.
  157.              */
  158.             databaseHeader.write(raf);
  159.             recordIndex.write(raf);
  160.             astroAppInfo.write(raf);
  161.  
  162.             /*
  163.              * Write the records
  164.              */
  165.             for (int i=0; i < astroAppInfo.numRecords; i++) 
  166.             {
  167.                 AstroRecord ar = (AstroRecord)records.elementAt(i);
  168.                 ar.write(raf);
  169.             }
  170.             raf.close();
  171.         }
  172.         catch (Exception e) 
  173.         {
  174.             System.err.println(e);
  175.             e.printStackTrace();
  176.         }
  177.     }
  178.     
  179.     static public void main(String args[]) throws IOException
  180.     {
  181.         int argsOffset = 0;
  182.         boolean noSeconds = true;
  183.         while (argsOffset <= args.length && args[argsOffset].charAt(0)=='-')
  184.         try {
  185.             switch (args[argsOffset].charAt(1))
  186.             {
  187.                 case 's':
  188.                     noSeconds = false;
  189.                     break;
  190.                 default:
  191.                     System.err.println("Invalid option.");
  192.                         return;
  193.             }
  194.             argsOffset++;            
  195.         }
  196.         catch (Exception e) 
  197.         {
  198.             System.err.println("Failed parsing arguments.");
  199.             return;
  200.         }
  201.  
  202.         encode(args[argsOffset],args[argsOffset+1],noSeconds);
  203.     }    
  204. }
  205.