⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optionizer.java

📁 ErGo是一个很早的Java通用围棋服务器(IGS/NNGS)客户端程序。有全部源码和文档
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	esp = brakchar - 1;
      }
      else esp = colchar - 1;
      int i,f;			// initial and final of keyword.
      for (i = 0; i < colchar; ++ i) {
        if (!Character.isWhitespace(line.charAt(i))) break;
      }
      for (f = esp; f >= 0; -- f) {
        if (!Character.isWhitespace(line.charAt(f))) break;
      }
      if (f < i) {
	Debug.println("No keyword found: " + line);
	return false;		// no string found.
      }
      int iv;			// initial of value.
      int length = line.length();
      for (iv = colchar + 1; iv < length; ++iv) {
	if (!Character.isWhitespace(line.charAt(iv))) break;
      }
      if (iv == length) {
	Debug.println("No value found: " + line);
	return false;
      }
      number = 0;
      if (numbered) {
	try {
	  number = Integer.parseInt(line.substring(brakchar + 1, cbrakchar));
	}
	catch (Exception e) {
	  Debug.println("Incorrect numeric format in ini file: " + line);
	  return false;
	}
      }

      keyword = line.substring(i, f + 1);
      originalkeyword = keyword;
      keyword = hyphenMap(keyword);
      value = line.substring(iv);
      return true;
    }
  } // end inner class iniParse

  iniParse iniParser = new iniParse();

  /** Reads in the contents of the init file from the given InputStream,
   *  updating the options specified therein.
   */
  public void inSuck (InputStream in) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while (true) {
      String line = br.readLine();
      if (line == null) break;
      if (!iniParser.parseLine(line)) continue;
      OptionStorage storage;
      //      Debug.println("Found option "+iniParser.keyword+
      //		    ", value "+iniParser.value+" number "+iniParser.number);

      // number is 0 (default position) if none is present.
      if ((storage = lookup(iniParser.keyword)) == null) {
	// if no-one has requested this so far, then we do not know its type...!
	storage = allocateQuietly(iniParser.keyword, iniParser.originalkeyword);
      }
      if (storage.type == TYPE_UNKNOWN) { // no one has ever heard of this option before.
	storage.applyData(iniParser.number, iniParser.value);
      }
      else {
	Object fruit = handlers[storage.type].handle(iniParser.keyword, iniParser.value); 
	updateOption(iniParser.keyword, iniParser.number, fruit);
	storage.internaldefault = true; // don't bother to rewrite.
      }
    }				// end loop lines
  }				// end inSuck

  private void unsetAllFlags () {
    osEnumeration ose = storages();
    while (ose.hasMoreElements()) {
      ose.nextElement().unsetAllFlags();
    }
  }

  private void calcallhighs () {
    osEnumeration ose = storages();
    while (ose.hasMoreElements()) {
      ose.nextElement().calchighestseen();
    }
  }

  // really cruddy function to guarantee we change savingString option.
  private Boolean opposite (boolean to) {
    if (to == true) return F;
    else return T;
  }

  /** Rewrite the init file we read in before (name is remembered).
   *  returns string suitable for indicating error to user (dialog?),
   *  or null if OK.
   *
   *  ---*** This beasty needs to be rewritten in a more modular style!
   */
  public String rewriteFile () {
    unsetAllFlags();

    // Read the INI file once. Work out what the last seen option
    // number is for each option. Do this now, rather than in
    // inSuck(), to minimise chances of the heinous users swiping the
    // INI file, and replacing it with another one, before we manage
    // to write it. 

    // +++ This needs to handle the case where there was no init file.
    //     Should prompt the user whether they'd like to save options
    //     in ini file, and if so, prompt for filename.

    FileInputStream fi = null;	// idiot compiler.
    BufferedReader br = null;
    try {
      fi = new FileInputStream(initFile);
      br = new BufferedReader(new InputStreamReader(fi));
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return ("Couldn't reopen INI file for reading");
    }
    try {
      while (true) {
        String line = br.readLine();
        if (line == null) break;
        if (iniParser.parseLine(line)) {  
	  OptionStorage os = lookup(iniParser.keyword);
          os.setFlagAt(iniParser.number); 
	} 
      }				// end each line.
      br.close();
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return("Couldn't read INI file " + initFile);
    }
    calcallhighs();
    unsetAllFlags();
   
    // Almost absolutely sure here we're going to rewrite the file.
    // gather in all dynamic options like window sizes that can't
    // be got easily at the time.

    updateOption(savingString, opposite(getBooleanOption(savingString)));

    boolean seenspecial = false;
    // now read it again, this time, rewriting it into temp file.
    try {
      fi = new FileInputStream(initFile);
      br = new BufferedReader(new InputStreamReader(fi));
      if (fi == null || br == null)
	throw new Exception("Baam.");
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return ("Couldn't reopen INI file for reading");
    }

    File initFileFile = new File(initFile);
    String path = initFileFile.getParent();
    File tempFileFile = new File(path, "OPSINI.TMP");
    FileOutputStream fo;
    PrintWriter pr = null;
    try {
      fo = new FileOutputStream(tempFileFile);
      pr = new PrintWriter(fo);
      if (fo == null || pr == null)
	throw new Exception ("Boom.");
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return("Couldn't open temporary file " + tempFileFile + " for writing");
    }
    try {
      while (true) {
	String line = br.readLine();
	if (line == null)
	  break;
	if (line.equals(specialLine))
	  seenspecial = true;
	if (!iniParser.parseLine(line)) {
	  pr.println(line);	// pass the comment unchanged through our bowels.
	}
	else {
	  OptionStorage os = lookup(iniParser.keyword);
	  // if this is an option we've got from outside the code, write it.
	  if (os.dataAt(iniParser.number) != null && !os.internaldefault)
	    writeOption(pr, os, iniParser.keyword, iniParser.number);
	  else {
	    pr.println(line);
	  }
	  // time to wheel out the rest of the options.
	  if (iniParser.number == os.highestseen) {
	    writeOptions(pr, os, iniParser.keyword);
	  }			// end if just seen last of known options in file.
	}			// end if we see an option line
      }				// end while each line in incoming INI file.
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return ("Failed to reread INI file");
    }
    Enumeration oe = optionmap.keys();
    // finally, write everything else.
    if (!seenspecial) {
      pr.println();
      pr.println(specialLine);
      pr.println();
    }
    while (oe.hasMoreElements()) {
      String keyword = (String) oe.nextElement();
      if (isSameKey(keyword, savingString))
	continue;                         // never write this dummy option.
      OptionStorage os = lookup(keyword);
      if (os == null) continue;
      writeOptions(pr, os, keyword);
    }  // end while all storages.

    // Finally, close all the buggers, and try to rename one over to
    // the other.
    try {
      if (pr.checkError())  // flush and check for errors
	return "Unknown error while writing init file";
      br.close();
      pr.close();
      //      if (fi!=null) return null;
      boolean deleted = initFileFile.delete();
      if (!deleted) 
        return("Failed to delete old INI file " + initFileFile.getName());
      boolean renamed = tempFileFile.renameTo(initFileFile);
      if (!renamed)
        return("Failed to rename temporary INI file " + tempFileFile.getName()); 
    }
    catch (Exception e) {
      Debug.backtrace(e);
      return ("Failed to finish rewriting INI file.");
    }
    return null;
  }				// end rewriteFile

  private void writeOptions (PrintWriter pr, OptionStorage os, String keyword) {
    for (int i = 0; i < os.size(); ++i) {
      if (os.dataAt(i) != null & !os.flagAt(i) & !os.internaldefault) { 
        writeOption(pr, os, keyword, i);
      }
    }				// end for options to wheel out.
  }

  private void writeOption (PrintWriter pr, OptionStorage os, String keyword, int number) {
    if (os.originalkeyword != null)
      pr.print(os.originalkeyword);
    else pr.print(keyword);
    if (os.size() > 1) {
      pr.print("[" + number + "]");
    }
    pr.print(": ");
    if (os.type == TYPE_UNKNOWN) // if no-one wanted it, just smash out the
      // string again....
      pr.println(os.dataAt(number)); 
    else
      pr.println(handlers[os.type].render(os.dataAt(number)));
    os.setFlagAt(number);
  }

  /** "External" function for updating an option.  This causes broadcast of
   *  the option change to all observers.
   */
  public void updateOption (String keyword, int number, Object value) {
    OptionStorage os = lookup(keyword);
    // unless it's null, check whether the value is unchanged.
    if (value != null) { 
      if (value.equals(os.dataAt(number))) {
	return;
      }
    }
    os.internaldefault = false;
    os.applyData(number, value);
    //    os.data=value;
    if (number == currentset) { // broadcast the change to all interested.
      for (int i = 0; i < os.targets.size(); ++i) {
        os.targets.optionizableAt(i).optionEvent(keyword, value);
      }
    }
  }
  
  // convenience updater, assumes "current" option set.
  public void updateOption (String keyword, Object value) {
    updateOption(keyword, currentset, value);
  }

  // convenience getter, assumes "current" option set.
  public Object getOption (String keyword) throws ErgoException {
    return getOption(keyword, currentset);
  }

  public Object getOption(String keyword, int number) {
    OptionStorage os = lookup(keyword);
    Debug.asser(os != null && os.type != TYPE_UNKNOWN,
		 "Option \"" + keyword + "\" requested before initialised.");
    if (number >= os.size() || os.dataAt(number) == null) {
      return os.dataAt(0);
    }
    return os.dataAt(number);
  }

  /*
  * Assisted option getters and unwrappers. In the event of failure,
  * these supply a type-specific "hyper-default" value.
  */

  public int getIntegerOption (String keyword) {
    try {
      Integer i = (Integer) getOption(keyword);
      if (i != null) return i.intValue();
    }
    catch(Exception e) {
      Debug.backtrace(e);
    }
    return 10;			// a reasonable number, in general..! :)
  }

  public boolean getBooleanOption (String keyword) {
    try {
      Boolean b = (Boolean) getOption(keyword);
      if (b != null) return b.booleanValue();
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    return false;
  }

  public Color getColorOption (String keyword) {
    try {
      ColorSpec c = (ColorSpec) getOption(keyword);
      if (c != null) return c.getColor();
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    return Color.white;
  }

  public Font getFontOption(String keyword) {
    try {
      Object o = getOption(keyword);
      //Debug.println("o = " + o);
      //Debug.println("keyword = " + keyword);
      FontSpec f = (FontSpec)o;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -