All pastes #487059 Raw Edit

Something

public text v1 · immutable
·published 2007-05-14 07:22 UTC
rendered paste body
import static java.lang.System.*;
import java.util.Scanner;
import java.io.*;


class junits{

    static class options{
        public String format = "%s";
        public String headers;
        public String units = "junits.table";
        public boolean echo = false;
        public boolean silent = false;
        public boolean tableconvert = false;
        public int dump;
    };

   private static void parseopts(String[] args, options myoptions){
       getopts opts = new getopts(args, "f:h:u:estd");
        for(;;){
            int opt = opts.getopt();
            if(opt == getopts.END) break;
            switch(opt){
            case 'f':
                myoptions.format = opts.getoptarg();
                break;
            case 'h':
                myoptions.headers = opts.getoptarg();
                break;
            case 'u':
                myoptions.units = opts.getoptarg();
                break;
            case 's':
                myoptions.silent = true;
                break;
            case 'e':
                myoptions.echo = true;
                break;
            case 't':
                myoptions.tableconvert = true;
                break;
            case 'd':
                myoptions.dump++;
                break;

            default :
                out.printf ("-%c: invalid option%n", opts.geterrchar());
                break;
            }
        };
   }

    public static void parseunits(unitmap map, options opts){
        Scanner scan = new Scanner(in);
        if(opts.silent == false){
            out.print("junits> ");
        }

        while(scan.hasNextLine()){
            String line = scan.nextLine();
            if(opts.echo && opts.silent == false){
               out.println(" * " + line);
            }
            String[] words = line.split("\\s+");
 if(words.length != 2){
                syslib.warn("bad line");
            }else{
                String key1 = words[0];
                String key2 = words[1];
                int index1 = 0;
                int index2 = 0;
                int totalfactor1= 1;
                int totalfactor2 = 1;
                String baseunit1 = key1;
                String baseunit1 = key2;
                boolean  indexcheck = false;
                unitmap.unit myunit;
                while((myunit = map.get(baseunit1)) != null){
                    if(opts.tableconvert){
                        out.printf("%s =" + opts.format + "%s%n", baseunit1, myunit.factor,
                                   myunit.baseunit);
                    }
                    totalfactor1 *= myunit.factor;
                    baseunit1 = myunit.baseunit;
                    index1 ++;
                    if(index1 == 32){
                       indexcheck = true;
                       break;
                    }
                }
                if(indexcheck == true){
                    syslib.warn("circular definition");
                    continue;
                }
                while ((myunit = map.get(baseunit2))!= null){
                    if(opts.tableconvert){
 out.printf("%s =" + opts.format + "%s%n", baseunit1, myunit.factor,
                                   myunit.baseunit);
                    }
                    totalfactor1 *= myunit.factor;
                    baseunit1 = myunit.baseunit;
                    index1 ++;
                    if(index1 == 32){
                       indexcheck = true;
                       break;
                    }
                }
                if(indexcheck == true){
                    syslib.warn("circular definition");
                    continue;
                }
                while ((myunit = map.get(baseunit2))!= null){
                    if(opts.tableconvert){
                        out.printf("%s =" + opts.format + "%s%n", baseunit2, myunit.factor, myunit.baseunit);
                    totalfactor2 *= myunit.factor;
                    baseunit2 = myunit.baseunit;
                    index2++;
                    if(index2 == 32){
                        indexcheck = true;
                        break;
                    }
                }
                if(indexcheck == true){
                   syslib.warn("circular definition");
                   continue;
                }
                unitmap.unit unit1 = map.get(key1);
                unitmap.unit unit2 = map.get(key2);
                if(unit1 == null){
                    syslib.warn(key1 +" does not exist");
                }else if(unit2 == null){
                    syslib.warn(key2 +" does not exist");
                }else{
                    out.printf("%s = " + opts.format + " %s%n", key1,
                    unit1.factor, unit1.baseunit);
                    out.printf("%s = " + opts.format + " %s%n", key2,
                    unit2.factor, unit2.baseunit);
                }

            }
            if(opts.silent == false){
                out.print("junits> ");
            }

        }
        out.println();
 }

    public static void main(String[] args){
         unitmap mymap = new unitmap();
         options opts1 = new options();
         String env = getenv("JUNITS_TABLE");
         if(env != null){
             opts1.units = env;
         }
         parseopts(args, opts1);
         String defunit;
         double factor = 0;
         String baseunit = null;
         try{
            Scanner scan = new Scanner (new File(opts1.units));
            while (scan.hasNextLine ()) {
               String line = scan.nextLine ();
               line = line.trim();
               if(line.equals("") || line.startsWith("#")){
                   continue;
               }
               String[] words = line.split ("\\s+");
               if (words.length != 3) {
                   err.printf ("unit table error: %s%n", line);
                   System.exit(1);
               }else {
                 try {
                   defunit = words[0];
                   factor = Double.valueOf (words[1]);
                   baseunit = words[2];
                   if(mymap.get(defunit) != null){
                       syslib.warn(defunit + " is already defined.");
                       continue;
                   }
                   unitmap.unit myunit = new unitmap.unit(factor , baseunit);
                   mymap.put(defunit, myunit);
                   }catch (NumberFormatException error) {
                     syslib.warn("unit table error: %s%n%s%n", line, error);
                }
              }
            }
            scan.close();
         }catch(IOException error){
             syslib.die(error.getMessage());
         }
         parseunits(mymap, opts1);
         syslib.exit();
    }
}