int ct() throws IOException { FileReader fr = new FileReader("mf.dat"); StreamTokenizer st = new StreamTokenizer(fr); st.eolIsSignificant(true); int tok = st.nextToken(); int ctr = 0; while (tok != -1) { switch (tok) { case -3: { break; } case -2: { break; } case 10: { ctr++; break; } } tok = st.nextToken(); } return ctr; }
public void loadTree() { System.out.println("Loading tree"); StreamTokenizer stream = null; try { FileInputStream f = new FileInputStream(tree); Reader input = new BufferedReader(new InputStreamReader(f)); stream = new StreamTokenizer(input); stream.resetSyntax(); stream.wordChars(32, 127); } catch (Exception e) { System.out.println("Error opening " + tree); System.exit(1); } list = new ArrayList(); try { // read the file to the end while (stream.nextToken() != StreamTokenizer.TT_EOF) { // is a word being read if (stream.ttype == StreamTokenizer.TT_WORD) { list.add(new String(stream.sval)); } // is a number being read if (stream.ttype == StreamTokenizer.TT_NUMBER) { list.add(new Double(stream.nval)); } } } catch (Exception e) { System.out.println("\nError reading " + tree + ". Exiting..."); System.exit(1); } }
/** * This method reads in passenger data and builds a new passenger object. * * @return new Passenger object built from file inputs */ private static Passenger newPassenger() { String passengerName = ""; byte floor = 1; Passenger newArrival = null; boolean okToContinue = true; try { in.nextToken(); passengerName = in.sval; in.nextToken(); floor = (byte) in.nval; } catch (IOException e) { System.err.println("Unexpected error in file! Can't continue."); okToContinue = false; newArrival = null; } if (okToContinue) { newArrival = new Passenger(passengerName, floor); } return newArrival; } // end newPassenger
public static Map<String, String> getStyles(String str) throws IOException { HashMap<String, String> styles = new HashMap<String, String>(); if (str == null) return styles; StreamTokenizer tt = new StreamTokenizer(new StringReader(str)); tt.resetSyntax(); tt.wordChars('!', '9'); tt.wordChars('<', '~'); tt.wordChars(128 + 32, 255); tt.whitespaceChars(0, ' '); while (tt.nextToken() != StreamTokenizer.TT_EOF) { if (tt.ttype != ';') { String key, value; if (tt.ttype != StreamTokenizer.TT_WORD) { throw new IOException( "Key token expected in " + str + " " + Integer.toHexString(tt.ttype)); } key = tt.sval; if (tt.nextToken() != ':') { throw new IOException("Colon expected after " + key + " in " + str); } if (tt.nextToken() != StreamTokenizer.TT_WORD) { throw new IOException( "Value token expected after " + key + " in " + str + " " + tt.ttype); } value = tt.sval; while (tt.nextToken() == StreamTokenizer.TT_WORD) { value += ' ' + tt.sval; } tt.pushBack(); styles.put(key, value); } } return styles; }
public static void main(String[] args) throws IOException { final StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"))); final PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1")); in.nextToken(); int n = (int) in.nval; short[] list = new short[n]; int i; // read the first list for (i = 0; i < n; i++) { in.nextToken(); list[i] = (short) in.nval; } in.nextToken(); n = (int) in.nval; // read second list searching for desired sum int j = 0, k; for (i = 0; i < n; i++) { in.nextToken(); k = (short) in.nval; while (list[j] + k < DESIRED_SUM && j < list.length - 1) j++; while (list[j] + k > DESIRED_SUM && j > 0) j--; if (list[j] + k == DESIRED_SUM) { out.print("YES"); out.flush(); return; } } out.print("NO"); out.flush(); }
/** * Returns the string array associated with a key, assuming it is defined. It is recommended to * check that it is defined first with {@link #hasValue(String)}. * * @throws RuntimeException if the key is not defined. * @see #hasValue(String) */ public String[] getList(/*@KeyFor("this.map")*/ String key) { try { if (!hasValue(key)) { throw new RuntimeException(String.format("Key '%s' is not defined", key)); } final String sValue = getValue(key); StreamTokenizer tok = new StreamTokenizer(new StringReader(sValue)); tok.quoteChar('"'); tok.whitespaceChars(' ', ' '); ArrayList<String> lValues = new ArrayList<String>(); int tokInfo = tok.nextToken(); while (tokInfo != StreamTokenizer.TT_EOF) { if (tok.ttype != '"') continue; assert tok.sval != null : "@AssumeAssertion(nullness)"; // tok.type == '"' guarantees not null lValues.add(tok.sval.trim()); tokInfo = tok.nextToken(); } return lValues.toArray(new String[] {}); } catch (IOException ex) { throw new RuntimeException(String.format("Parsing for key '%s' failed", key), ex); } }
static void scanTo(int tt) { // scans to a given token type: // TT_NUMBER or TT_WORD boolean found = false; try { while (!found) { int ttype = tokenizer.nextToken(); if (ttype == tt) { found = true; } else if (ttype == tokenizer.TT_EOF) { println("End of File reached while scanning for input."); found = true; } else if (ttype == tokenizer.TT_EOL) { // skip over end of line } else if ((tt == tokenizer.TT_WORD) && (ttype != tokenizer.TT_NUMBER)) { found = true; } } } catch (IOException e) { println("IOException while scanning for input."); } }
static String next() throws IOException { in.nextToken(); return in.sval; }
public static AffineTransform getTransform(String str) throws IOException { AffineTransform t = new AffineTransform(); if (str != null) { StreamTokenizer tt = new StreamTokenizer(new StringReader(str)); tt.resetSyntax(); tt.wordChars('a', 'z'); tt.wordChars('A', 'Z'); tt.wordChars(128 + 32, 255); tt.whitespaceChars(0, ' '); tt.whitespaceChars(',', ','); tt.parseNumbers(); while (tt.nextToken() != StreamTokenizer.TT_EOF) { if (tt.ttype != StreamTokenizer.TT_WORD) { throw new IOException("Illegal transform " + str); } String type = tt.sval; if (tt.nextToken() != '(') { throw new IOException("'(' not found in transform " + str); } if (type.equals("matrix")) { double[] m = new double[6]; for (int i = 0; i < 6; i++) { if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException( "Matrix value " + i + " not found in transform " + str + " token:" + tt.ttype + " " + tt.sval); } if (tt.nextToken() == StreamTokenizer.TT_WORD && tt.sval.startsWith("E")) { double mantissa = tt.nval; tt.nval = Double.valueOf(tt.nval + tt.sval); } else { tt.pushBack(); } m[i] = tt.nval; } t.concatenate(new AffineTransform(m)); } else if (type.equals("translate")) { double tx, ty; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("X-translation value not found in transform " + str); } tx = tt.nval; if (tt.nextToken() == StreamTokenizer.TT_NUMBER) { ty = tt.nval; } else { tt.pushBack(); ty = 0; } t.translate(tx, ty); } else if (type.equals("scale")) { double sx, sy; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("X-scale value not found in transform " + str); } sx = tt.nval; if (tt.nextToken() == StreamTokenizer.TT_NUMBER) { sy = tt.nval; } else { tt.pushBack(); sy = sx; } t.scale(sx, sy); } else if (type.equals("rotate")) { double angle, cx, cy; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("Angle value not found in transform " + str); } angle = tt.nval; if (tt.nextToken() == StreamTokenizer.TT_NUMBER) { cx = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("Y-center value not found in transform " + str); } cy = tt.nval; } else { tt.pushBack(); cx = cy = 0; } t.rotate(angle * Math.PI / 180d, cx * Math.PI / 180d, cy * Math.PI / 180d); } else if (type.equals("skewX")) { double angle; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("Skew angle not found in transform " + str); } angle = tt.nval; t.concatenate(new AffineTransform(1, 0, Math.tan(angle * Math.PI / 180), 1, 0, 0)); } else if (type.equals("skewY")) { double angle; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("Skew angle not found in transform " + str); } angle = tt.nval; t.concatenate(new AffineTransform(1, Math.tan(angle * Math.PI / 180), 0, 1, 0, 0)); } else { throw new IOException("Unknown transform " + type + " in " + str); } if (tt.nextToken() != ')') { throw new IOException("')' not found in transform " + str); } } } return t; }
public static List<BezierPath> fromPathData(String str) throws IOException { LinkedList<BezierPath> paths = new LinkedList<BezierPath>(); BezierPath path = null; Point2D.Double p = new Point2D.Double(); Point2D.Double c1 = new Point2D.Double(); Point2D.Double c2 = new Point2D.Double(); StreamTokenizer tt = new StreamTokenizer(new StringReader(str)); tt.resetSyntax(); tt.parseNumbers(); tt.whitespaceChars(0, ' '); tt.whitespaceChars(',', ','); char nextCommand = 'M'; char command = 'M'; while (tt.nextToken() != StreamTokenizer.TT_EOF) { if (tt.ttype > 0) { command = (char) tt.ttype; } else { command = nextCommand; tt.pushBack(); } BezierPath.Node node; switch (command) { // moveto case 'M': if (path != null) { paths.add(path); } path = new BezierPath(); if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.moveTo(p.x, p.y); nextCommand = 'L'; break; case 'm': if (path != null) { paths.add(path); } path = new BezierPath(); if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.moveTo(p.x, p.y); nextCommand = 'l'; // close path break; case 'Z': case 'z': p.x = path.get(0).x[0]; p.y = path.get(0).y[0]; path.setClosed(true); // lineto break; case 'L': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.lineTo(p.x, p.y); nextCommand = 'L'; break; case 'l': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.lineTo(p.x, p.y); nextCommand = 'l'; break; case 'H': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; path.lineTo(p.x, p.y); nextCommand = 'H'; break; case 'h': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; path.lineTo(p.x, p.y); nextCommand = 'h'; break; case 'V': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.lineTo(p.x, p.y); nextCommand = 'V'; break; case 'v': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.lineTo(p.x, p.y); nextCommand = 'v'; // curveto break; case 'C': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.y = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.y = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y); nextCommand = 'C'; break; case 'c': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.x = p.x + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.y = p.y + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.x = p.x + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.y = p.y + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y); nextCommand = 'c'; break; case 'S': node = path.get(path.size() - 1); c1.x = node.x[0] * 2d - node.x[1]; c1.y = node.y[0] * 2d - node.y[1]; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.y = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y); nextCommand = 'S'; break; case 's': node = path.get(path.size() - 1); c1.x = node.x[0] * 2d - node.x[1]; c1.y = node.y[0] * 2d - node.y[1]; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.x = p.x + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c2.y = p.y + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y); nextCommand = 's'; // quadto break; case 'Q': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.y = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.quadTo(c1.x, c1.y, p.x, p.y); nextCommand = 'Q'; break; case 'q': if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.x = p.x + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); c1.y = p.y + tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.quadTo(c1.x, c1.y, p.x, p.y); nextCommand = 'q'; break; case 'T': node = path.get(path.size() - 1); c1.x = node.x[0] * 2d - node.x[1]; c1.y = node.y[0] * 2d - node.y[1]; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x = tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y = tt.nval; path.quadTo(c1.x, c1.y, p.x, p.y); nextCommand = 'T'; break; case 't': node = path.get(path.size() - 1); c1.x = node.x[0] * 2d - node.x[1]; c1.y = node.y[0] * 2d - node.y[1]; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.x += tt.nval; if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); p.y += tt.nval; path.quadTo(c1.x, c1.y, p.x, p.y); nextCommand = 's'; break; default: throw new IOException("Illegal command: " + command); } } if (path != null) { paths.add(path); } return paths; }
static float nextFloat() throws IOException { in.nextToken(); return (float) in.nval; }
private static void readConfig(File confFile) { System.out.println("Reading configuration file: " + confFile); try { StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new FileReader(confFile))); tokenizer.eolIsSignificant(true); tokenizer.slashStarComments(true); boolean EOF = false; int tokType = 0; Vector words = new Vector(); while (!EOF) { if ((tokType = tokenizer.nextToken()) == StreamTokenizer.TT_EOF) { EOF = true; } else if (tokType != StreamTokenizer.TT_EOL) { if (tokenizer.sval != null) { words.addElement(tokenizer.sval); } } else { if (words.size() == 2) { String key = (String) words.elementAt(0); String value = (String) words.elementAt(1); if (key.equals("SRSServer")) { srsServer = new String(value); } else if (key.equals("Database")) { database = new String(value); } else if (key.equals("Layout")) { layout = new String(value); } else if (key.equals("AutosaveInterval")) { if (value.equals("none")) { setAutosaveInterval(-1); } else { try { setAutosaveInterval(Integer.parseInt(value)); } catch (NumberFormatException e) { System.err.println("Can't parse number: " + value); } } } else if (key.equals("ColourSchemeInstall")) { try { String installString = value; int breakIndex = installString.indexOf(":"); if (breakIndex < 0) { // adapterRegistry.installDataAdapter(installString); } else { String driverName = installString.substring(0, breakIndex); String driverDesc = installString.substring(breakIndex + 1); // adapterRegistry.installDataAdapter(driverName); } } catch (Throwable e) { System.err.println("Could not install driver " + value + " because of " + e); } } else if (key.equals("FormatAdapterInstall")) { try { String installString = value; int breakIndex = installString.indexOf(":"); if (breakIndex < 0) { // adapterRegistry.installDataAdapter(installString); } else { String driverName = installString.substring(0, breakIndex); String driverDesc = installString.substring(breakIndex + 1); // adapterRegistry.installDataAdapter(driverName); } } catch (Throwable e) { System.err.println("Could not install driver " + value + " because of " + e); } } else { System.out.println("Unknown config key " + key); } } else { if (words.size() != 0) { System.out.println( "Too many words on line beginning " + (String) words.elementAt(0) + " in config file"); } } words.removeAllElements(); } } return; } catch (Exception ex) { System.out.println(ex); return; } }
private int nextToken() throws IOException { int token = st.nextToken(); debug(st); return token; }
static long nextLong() throws IOException { in.nextToken(); return (long) in.nval; }
static char nextChar() throws IOException { in.nextToken(); return in.sval.charAt(0); }
private ArrayList<String> nextRecordLocal() throws IOException { if (atEOF) { return null; } ArrayList<String> record = new ArrayList<String>(maxFieldCount); StringBuilder fieldValue = null; while (true) { int token = parser.nextToken(); if (token == StreamTokenizer.TT_EOF) { addField(record, fieldValue); atEOF = true; break; } if (token == StreamTokenizer.TT_EOL) { addField(record, fieldValue); break; } if (token == separator) { addField(record, fieldValue); fieldValue = null; continue; } if (token == StreamTokenizer.TT_WORD) { if (fieldValue != null) { throw new CSVParseException("Unknown error", parser.lineno()); } fieldValue = new StringBuilder(parser.sval); continue; } if (token == '"') { if (fieldValue != null) { throw new CSVParseException( "Found unescaped quote. A value with quote should be within a quote", parser.lineno()); } while (true) { token = parser.nextToken(); if (token == StreamTokenizer.TT_EOF) { atEOF = true; throw new CSVParseException( "EOF reached before closing an opened quote", parser.lineno()); } if (token == separator) { fieldValue = appendFieldValue(fieldValue, token); continue; } if (token == StreamTokenizer.TT_EOL) { fieldValue = appendFieldValue(fieldValue, "\n"); continue; } if (token == StreamTokenizer.TT_WORD) { fieldValue = appendFieldValue(fieldValue, parser.sval); continue; } if (token == '"') { int nextToken = parser.nextToken(); if (nextToken == '"') { // escaped quote fieldValue = appendFieldValue(fieldValue, nextToken); continue; } if (nextToken == StreamTokenizer.TT_WORD) { throw new CSVParseException( "Not expecting more text after end quote at line " + parser.lineno(), parser.lineno()); } else { parser.pushBack(); break; } } } } } if (record.size() > maxFieldCount) { maxFieldCount = record.size(); } return record; }
/** * Read and process each item in the simulation file. It reads each input item in the file. If a * PASSENGER entry, a passenger object is created, and the elevators are checked to see if one is * available to service this passenger. If so, the passenger is logically added to the elevator. * If an elevator is not available, the passenger must take the stairs. * * <p>If the input item is a TICK, it indicates that one time unit has passed. At EOF, the method * is invoked to shut the elevators down and perform the end of day processing. * * <p>If an error is detected at any time, this method returns the proper boolean to the calling * method. * * @return True or False to indicate an error */ private static boolean processSimulationFile() { // local constants used by this method final String PASSENGER = "passenger"; final String TICK = "tick"; final String SIMULATION_END = "done"; boolean done = false; // set when done boolean error = false; // set if error detected boolean addedOK = false; // set if no elevator is available Passenger newPass = null; // new passenger object Elevator availableEl = null; // elevator object // Read next token from file and process try { while (!done && !error && in.nextToken() != in.TT_EOF) { if (in.sval.equals(PASSENGER)) { // if a new passenger, create a new object, look // for an elevator and add to elevator. If elevator // is not available, passenger takes the stairs. newPass = newPassenger(); availableEl = findElevator(newPass); if (availableEl == null) takingStairs(newPass); else { System.out.print( "Passenger: " + newPass.getName() + " arrives on floor " + newPass.getFloor() + "."); addedOK = availableEl.addPassenger(newPass); if (!addedOK) { error = true; } } } else if (in.sval.equals(TICK)) { // token read from file is a time indicator recordTick(); } else if (in.sval.equals(SIMULATION_END)) { // end of file == end of workday done = true; } else { error = true; } } // end while if (in.nextToken() == in.TT_EOF) { // perform end of day reporting endOfDay(); } // catch for file exception } catch (IOException e) { error = true; } // end try return error; } // end processSimulationFile
static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; }
/** * Return an interned VarInfoAux that represents a given string. Elements are separated by commas, * in the form: * * <p>x = a, "a key" = "a value" * * <p>Parse allow for quoted elements. White space to the left and right of keys and values do not * matter, but inbetween does. */ public static /*@Interned*/ VarInfoAux parse(String inString) throws IOException { Reader inStringReader = new StringReader(inString); StreamTokenizer tok = new StreamTokenizer(inStringReader); tok.resetSyntax(); tok.wordChars(0, Integer.MAX_VALUE); tok.quoteChar('\"'); tok.whitespaceChars(' ', ' '); tok.ordinaryChar('['); tok.ordinaryChar(']'); tok.ordinaryChars(',', ','); tok.ordinaryChars('=', '='); Map</*@Interned*/ String, /*@Interned*/ String> map = theDefault.map; String key = ""; String value = ""; boolean seenEqual = false; boolean insideVector = false; for (int tokInfo = tok.nextToken(); tokInfo != StreamTokenizer.TT_EOF; tokInfo = tok.nextToken()) { @SuppressWarnings("interning") // initialization-checking pattern boolean mapUnchanged = (map == theDefault.map); if (mapUnchanged) { // We use default values if none are specified. We initialize // here rather than above to save time when there are no tokens. map = new HashMap</*@Interned*/ String, /*@Interned*/ String>(theDefault.map); } /*@Interned*/ String token; if (tok.ttype == StreamTokenizer.TT_WORD || tok.ttype == '\"') { assert tok.sval != null : "@AssumeAssertion(nullness): representation invariant of StreamTokenizer"; token = tok.sval.trim().intern(); } else { token = ((char) tok.ttype + "").intern(); } debug.fine("Token info: " + tokInfo + " " + token); if (token == "[") { // interned if (!seenEqual) throw new IOException("Aux option did not contain an '='"); if (insideVector) throw new IOException("Vectors cannot be nested in an aux option"); if (value.length() > 0) throw new IOException("Cannot mix scalar and vector values"); insideVector = true; value = ""; } else if (token == "]") { // interned if (!insideVector) throw new IOException("']' without preceding '['"); insideVector = false; } else if (token == ",") { // interned if (!seenEqual) throw new IOException("Aux option did not contain an '='"); if (insideVector) throw new IOException("',' cannot be used inside a vector"); map.put(key.intern(), value.intern()); key = ""; value = ""; seenEqual = false; } else if (token == "=") { // interned if (seenEqual) throw new IOException("Aux option contained more than one '='"); if (insideVector) throw new IOException("'=' cannot be used inside a vector"); seenEqual = true; } else { if (!seenEqual) { key = (key + " " + token).trim(); } else if (insideVector) { value = value + " \"" + token.trim() + "\""; } else { value = (value + " " + token).trim(); } } } if (seenEqual) { map.put(key.intern(), value.intern()); } // Interning VarInfoAux result = new VarInfoAux(map).intern(); assert interningMap != null : "@AssumeAssertion(nullness): application invariant: postcondition of intern(), which was just called"; if (debug.isLoggable(Level.FINE)) { debug.fine("New parse " + result); debug.fine("Intern table size: " + new Integer(interningMap.size())); } return result; }
static double nextDouble() throws IOException { in.nextToken(); return in.nval; }
/** * Parses a text file and creates an Obj3d. The file format is as follows: * * <pre> * * # a simple kite made of two triangular polygons * # by foo bar, 27 Aug 2002 * 2 * t f CC99FF 12.1 2.4 3.5, 12.4 2.2 3.5, 3.2 5 7.4, * t t 336699 9.7 0 3.5, 2.2 2.2 3.5, 3.2 5 7.3, * * </pre> * * The above represents an Obj3d with two polygons, the second of which is double sided; each is a * differnt color; each is made up of 3 points. Both cast shadows. The first flag indicates if the * polygon casts a shadow. The second flag indicates if the polygon is double sided. */ public Obj3d(StreamTokenizer st, ModelViewer modelViewer, boolean register) throws IOException, FileFormatException { // call to constructor must be first, but we do not // know how many polygons yet, hence the following this(modelViewer, register); // gobble EOL's due to comments while (st.nextToken() == StreamTokenizer.TT_EOL) {; } // how many polygons int num = (int) st.nval; // set num polygons this.setNumPolygons(num); // gobble up new line st.nextToken(); // Tools3d.debugTokens(st); //tmp // System.exit(0); // read a line a data for each polygon for (int i = 0; i < num; i++) { // has shadow ? st.nextToken(); boolean shadow = "t".equals(st.sval); // double sided ? st.nextToken(); boolean doubleSided = "t".equals(st.sval); // color - a hex number Color color = null; st.nextToken(); try { color = Color.decode("0x" + st.sval); } catch (NumberFormatException e) { throw new FileFormatException( "Unable to parse color: " + st.sval + ", polygon index: " + i); } if (color == null) { color = COLOR_DEFAULT; } // read point co-ords until we reach end of line Vector points = new Vector(); while (st.nextToken() != StreamTokenizer.TT_EOL) { float[] p = new float[3]; p[0] = (float) st.nval; st.nextToken(); p[1] = (float) st.nval; st.nextToken(); p[2] = (float) st.nval; // gobble up comma which seperates points // note there must be a comma after the last point if (st.nextToken() != ',') { throw new FileFormatException("Unable to parse co-ordinates; comma expected: " + st); } points.addElement(p); } // convert vector to an array float[][] vs = new float[points.size()][]; for (int j = 0; j < points.size(); j++) { vs[j] = (float[]) points.elementAt(j); } // finally, add the polygon if (!shadow) { this.addPolygon(vs, color, doubleSided); } else { this.addPolygonWithShadow(vs, color, doubleSided); } } }