Exemplo n.º 1
0
  /**
   * Read and parse UNITSPOT.DAT
   *
   * @return double[][][]
   */
  public static double[][][] readUnitSpot() {

    String file_name = FN.S_UNITSPOT_DAT;
    double[][][] unit_spot = new double[C.UNIT_SPOT_HEX][C.UNIT_SPOT_PLANET][];
    int line_nr = 0;
    try (BufferedReader in = new BufferedReader(new FileReader(file_name))) {
      String s = in.readLine();
      line_nr++;
      // System.out.println("s = " + s);
      // true if between { and } false if between } and {
      boolean read = false;

      int terrain_type = -1; // initialized to -1 on purpose
      int planet_type = 0;

      Pattern mark_begin = Pattern.compile("^\\{");
      Pattern mark_end = Pattern.compile("^\\}");
      Pattern comment = Pattern.compile("^//");

      while (s != null) {

        Matcher matcher = comment.matcher(s);

        // if not comment
        if (!(matcher.find())) {
          // if between { and }
          if (read) {
            matcher = mark_end.matcher(s);
            // if found } at beginning of line
            if (matcher.find()) {
              read = false;
              // else read data
            } else {

              unit_spot[terrain_type][planet_type] = getCosts(s, file_name, line_nr);
              Util.debugPrint("Process record");

              planet_type++;
            }
            // else between } and {
          } else {
            matcher = mark_begin.matcher(s);
            // if found { at beginning of line
            if (matcher.find()) {
              read = true;
              planet_type = 0;
              terrain_type++; // initialized to -1
              // incorrect data file
            } else {
              throw new Exception();
            }
          }
        }
        s = in.readLine();
        line_nr++;
      }

    } catch (Exception e) {
      e.printStackTrace(System.out);
      System.out.println("Exception: " + e.getMessage());
      System.out.println("Failed to read " + file_name);
      Util.logEx(null, e);
      Util.logFFErrorAndExit(file_name, line_nr, e);
      // CrashReporter.showCrashReport(e);
    }

    //        printData(unit_spot);
    //        System.exit(0);
    return unit_spot;
  }