<commit_before>/*
 *  Copyright 2011 RoboViz
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package rv;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;

/**
 * Configuration parameters for RoboVis startup
 * 
 * @author <NAME>
 */
public class Configuration {

    private static final String CONFIG_FILE_PATH = "config.txt";

    public String getNextLine(BufferedReader in) throws IOException {
        String result = in.readLine();
        while (result != null && result.startsWith("#")) {
            result = in.readLine();
        }
        return result;
    }

    public class Graphics {

        public boolean useBloom         = false;
        public boolean usePhong         = false;
        public boolean useShadows       = false;
        public boolean useSoftShadows   = false;
        public boolean useStereo        = false;
        public boolean useVsync         = true;
        public boolean useFsaa          = false;
        public int     fsaaSamples      = 4;
        public int     targetFPS        = 60;
        public int     frameWidth       = 800;
        public int     frameHeight      = 600;
        public int     shadowResolution = 1024;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            useBloom = Boolean.parseBoolean(getVal(getNextLine(in)));
            usePhong = Boolean.parseBoolean(getVal(getNextLine(in)));
            useShadows = Boolean.parseBoolean(getVal(getNextLine(in)));
            useSoftShadows = Boolean.parseBoolean(getVal(getNextLine(in)));
            shadowResolution = Integer.parseInt(getVal(getNextLine(in)));
            useStereo = Boolean.parseBoolean(getVal(getNextLine(in)));
            useVsync = Boolean.parseBoolean(getVal(getNextLine(in)));
            useFsaa = Boolean.parseBoolean(getVal(getNextLine(in)));
            fsaaSamples = Integer.parseInt(getVal(getNextLine(in)));
            targetFPS = Integer.parseInt(getVal(getNextLine(in)));
            frameWidth = Integer.parseInt(getVal(getNextLine(in)));
            frameHeight = Integer.parseInt(getVal(getNextLine(in)));
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Graphics Settings:\n");
            out.write(String.format("%-20s : %b\n", "Bloom", useBloom));
            out.write(String.format("%-20s : %b\n", "Phong", usePhong));
            out.write(String.format("%-20s : %b\n", "Shadows", useShadows));
            out.write(String.format("%-20s : %b\n", "Soft Shadows", useSoftShadows));
            out.write(String.format("%-20s : %d\n", "Shadow Resolution", shadowResolution));
            out.write(String.format("%-20s : %b\n", "Stereo 3D", useStereo));
            out.write(String.format("%-20s : %b\n", "V-Sync", useVsync));
            out.write(String.format("%-20s : %b\n", "FSAA", useFsaa));
            out.write(String.format("%-20s : %d\n", "FSAA Samples", fsaaSamples));
            out.write(String.format("%-20s : %d\n", "Target FPS", targetFPS));
            out.write(String.format("%-20s : %d\n", "Frame Width", frameWidth));
            out.write(String.format("%-20s : %d\n", "Frame Height", frameHeight));
            out.write("\n");
        }
    }

    public class Networking {
        public boolean autoConnect      = true;
        public String  serverHost       = "localhost";
        public int     serverPort       = 3200;
        public int     listenPort       = 32769;
        public int     autoConnectDelay = 1000;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            autoConnect = Boolean.parseBoolean(getVal(getNextLine(in)));
            autoConnectDelay = Integer.parseInt(getVal(getNextLine(in)));
            serverHost = getVal(getNextLine(in));
            serverPort = Integer.parseInt(getVal(getNextLine(in)));
            listenPort = Integer.parseInt(getVal(getNextLine(in)));
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Networking Settings:\n");
            out.write(String.format("%-20s : %b\n", "Auto-Connect", autoConnect));
            out.write(String.format("%-20s : %d\n", "Auto-Connect Delay", autoConnectDelay));
            out.write(String.format("%-20s : %s\n", "Server Host", serverHost));
            out.write(String.format("%-20s : %d\n", "Server Port", serverPort));
            out.write(String.format("%-20s : %d\n", "Drawing Port", listenPort));
            out.write("\n");
        }
    }

    public class General {
        public boolean recordLogs = false;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            recordLogs = Boolean.parseBoolean(getVal(getNextLine(in)));
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("General Settings:\n");
            out.write(String.format("%-20s : %b\n", "Record Logfiles", recordLogs));
            out.write("\n");
        }
    }

    public class TeamColors {
        private HashMap<String, float[]> colorByTeamName = new HashMap<String, float[]>();

        public float[] find(String teamName) {
            return colorByTeamName.get(teamName);
        }

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            String line;
            while (true) {
                line = getNextLine(in);
                if (line == null || line.trim().length() == 0)
                    break;
                String key = getKey(line);
                String val = getVal(line);
                String[] bits = val.split("\\s+");
                if (bits.length == 3) {
                    float[] color = new float[3];
                    color[0] = Float.parseFloat(bits[0]);
                    color[1] = Float.parseFloat(bits[1]);
                    color[2] = Float.parseFloat(bits[2]);
                    colorByTeamName.put(key, color);
                }
            }
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Team Colors:\n");
            for (String teamName : colorByTeamName.keySet()) {
                float[] color = colorByTeamName.get(teamName);
                out.write(String.format(Locale.US, "%-20s : %f %f %f\n", teamName, color[0],
                        color[1], color[2]));
            }
            out.write("\n");
        }
    }

    public final Graphics   graphics   = new Graphics();
    public final Networking networking = new Networking();
    public final General    general    = new General();
    public final TeamColors teamColors = new TeamColors();

    private static String getKey(String line) {
        return line.substring(0, line.indexOf(":") - 1).trim();
    }

    private static String getVal(String line) {
        return line.substring(line.indexOf(":") + 1).trim();
    }

    public void write() {
        File configFile = new File(CONFIG_FILE_PATH);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(configFile));
            graphics.write(out);
            networking.write(out);
            general.write(out);
            teamColors.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {<commit_msg>Configuration: get rid of some redundant logic<commit_after>/*
 *  Copyright 2011 RoboViz
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package rv;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;

/**
 * Configuration parameters for RoboVis startup
 * 
 * @author <NAME>
 */
public class Configuration {

    private static final String CONFIG_FILE_PATH = "config.txt";

    public static String getNextLine(BufferedReader in) throws IOException {
        String result = in.readLine();
        while (result != null && result.startsWith("#")) {
            result = in.readLine();
        }
        return result;
    }

    public class Graphics {

        public boolean useBloom         = false;
        public boolean usePhong         = false;
        public boolean useShadows       = false;
        public boolean useSoftShadows   = false;
        public boolean useStereo        = false;
        public boolean useVsync         = true;
        public boolean useFsaa          = false;
        public int     fsaaSamples      = 4;
        public int     targetFPS        = 60;
        public int     frameWidth       = 800;
        public int     frameHeight      = 600;
        public int     shadowResolution = 1024;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            useBloom = getNextBool(in);
            usePhong = getNextBool(in);
            useShadows = getNextBool(in);
            useSoftShadows = getNextBool(in);
            shadowResolution = getNextInt(in);
            useStereo = getNextBool(in);
            useVsync = getNextBool(in);
            useFsaa = getNextBool(in);
            fsaaSamples = getNextInt(in);
            targetFPS = getNextInt(in);
            frameWidth = getNextInt(in);
            frameHeight = getNextInt(in);
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Graphics Settings:\n");
            writeVal(out, "Bloom", useBloom);
            writeVal(out, "Phong", usePhong);
            writeVal(out, "Shadows", useShadows);
            writeVal(out, "Soft Shadows", useSoftShadows);
            writeVal(out, "Shadow Resolution", shadowResolution);
            writeVal(out, "Stereo 3D", useStereo);
            writeVal(out, "V-Sync", useVsync);
            writeVal(out, "FSAA", useFsaa);
            writeVal(out, "FSAA Samples", fsaaSamples);
            writeVal(out, "Target FPS", targetFPS);
            writeVal(out, "Frame Width", frameWidth);
            writeVal(out, "Frame Height", frameHeight);
            out.write("\n");
        }
    }

    public class Networking {
        public boolean autoConnect      = true;
        public String  serverHost       = "localhost";
        public int     serverPort       = 3200;
        public int     listenPort       = 32769;
        public int     autoConnectDelay = 1000;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            autoConnect = getNextBool(in);
            autoConnectDelay = getNextInt(in);
            serverHost = getNextString(in);
            serverPort = getNextInt(in);
            listenPort = getNextInt(in);
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Networking Settings:\n");
            writeVal(out, "Auto-Connect", autoConnect);
            writeVal(out, "Auto-Connect Delay", autoConnectDelay);
            writeVal(out, "Server Host", serverHost);
            writeVal(out, "Server Port", serverPort);
            writeVal(out, "Drawing Port", listenPort);
            out.write("\n");
        }
    }

    public class General {
        public boolean recordLogs = false;

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            recordLogs = Boolean.parseBoolean(getVal(getNextLine(in)));
            getNextLine(in);
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("General Settings:\n");
            writeVal(out, "Record Logfiles", recordLogs);
            out.write("\n");
        }
    }

    public class TeamColors {
        private HashMap<String, float[]> colorByTeamName = new HashMap<String, float[]>();

        public float[] find(String teamName) {
            return colorByTeamName.get(teamName);
        }

        private void read(BufferedReader in) throws IOException {
            getNextLine(in);
            String line;
            while (true) {
                line = getNextLine(in);
                if (line == null || line.trim().length() == 0)
                    break;
                String key = getKey(line);
                String val = getVal(line);
                String[] bits = val.split("\\s+");
                if (bits.length == 3) {
                    float[] color = new float[3];
                    color[0] = Float.parseFloat(bits[0]);
                    color[1] = Float.parseFloat(bits[1]);
                    color[2] = Float.parseFloat(bits[2]);
                    colorByTeamName.put(key, color);
                }
            }
        }

        private void write(BufferedWriter out) throws IOException {
            out.write("Team Colors:\n");
            for (String teamName : colorByTeamName.keySet()) {
                float[] color = colorByTeamName.get(teamName);
                out.write(String.format(Locale.US, "%-20s : %f %f %f\n", teamName, color[0],
                        color[1], color[2]));
            }
            out.write("\n");
        }
    }

    public final Graphics   graphics   = new Graphics();
    public final Networking networking = new Networking();
    public final General    general    = new General();
    public final TeamColors teamColors = new TeamColors();

    private static String getKey(String line) {
        return line.substring(0, line.indexOf(":") - 1).trim();
    }

    private static String getVal(String line) {
        return line.substring(line.indexOf(":") + 1).trim();
    }

    private static int getNextInt(BufferedReader in) throws IOException {
        return Integer.parseInt(getVal(getNextLine(in)));
    }

    private static boolean getNextBool(BufferedReader in) throws IOException {
        return Boolean.parseBoolean(getVal(getNextLine(in)));
    }

    private static String getNextString(BufferedReader in) throws IOException {
        return getVal(getNextLine(in));
    }

    private static void writeVal(Writer out, String name, int value) throws IOException {
        out.write(String.format("%-20s : %d\n", name, value));
    }

    private static void writeVal(Writer out, String name, boolean value) throws IOException {
        out.write(String.format("%-20s : %b\n", name, value));
    }

    private static void writeVal(Writer out, String name, String value) throws IOException {
        out.write(String.format("%-20s : %s\n", name, value));
    }

    public void write() {
        File configFile = new File(CONFIG_FILE_PATH);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(configFile));
            graphics.write(out);
            networking.write(out);
            general.write(out);
            teamColors.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
{'fraction_non_alphanumeric': 0.11293471629042098, 'fraction_numerical': 0.008907870652837095, 'mean_word_length': 2.1286505058217218, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1328723', 'n_tokens_mistral': 4562, 'n_tokens_neox': 4379, 'n_words': 1135}