Package transientlibs.tex

Source Code of transientlibs.tex.StringAnalyst

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.tex;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.processors.misc.Detonator;
import transientlibs.objects.primitives.Comparison;
import transientlibs.objects.primitives.Int;
import transientlibs.objects.primitives.IntSlot;
import transientlibs.objects.primitives.MathOperator;
import transientlibs.objects.primitives.TBoolean;

/**
*
* @author kibertoad
*/
public class StringAnalyst {

    public String fullStr;
    public String nextStr;
    public String lastStr;
    StringBuilder buildStr = new StringBuilder();
    public boolean eof;
    public int counter;
    public int savedLength;
    public int rowCount;
    public String moduleDir;
    public String baseDir;
    public String fileName;
    public BufferedReader in = null;
    public int mapLineCount = -1; //how many lines in the file
    public int longestLength = -1;

    public StringAnalyst() {
    }

    public static boolean isFileExist(String withName) {
        File testFile = new File(Detonator.INSTANCE.gameDataDir + withName);
        return testFile.exists();
    }

    public static boolean isExactFileExist(String withName) {
        File testFile = new File(withName);
        return testFile.exists();
    }
   
   
    public void checkForAliases() {

        /*
         char c;
         int counter = 0;
         c = fullStr.charAt(counter);

         if ()

         counter++;

         */

        String nowStr;
        String newStr;

        while (counter < savedLength) {
            //Log.info("Start analyzing");
            //Log.info("Counter: "+counter);
            nowStr = getNextString();
            //Log.info("Counter after: "+counter+" ->"+nowStr+"<-");


            if (nowStr.startsWith("::")) {

                fullStr = nowStr.toLowerCase();

                Log.debug("ALIAS time");
                newStr = Binding.getAlias(nowStr);


                Log.debug("Will replace: " + nowStr + " with " + newStr);

                fullStr = fullStr.replace(nowStr, newStr);


                savedLength = fullStr.length();


                Log.debug("New string: " + fullStr);
                restartAnalysis();
            }
        }

        //Log.info("Done analyzing");

        restartAnalysis();
    }

    public void openGlobalFile(String theFile) {

        fileName = theFile;

        if ((Detonator.INSTANCE.pathToData) != null) {
            fileName = Detonator.INSTANCE.pathToData + fileName;
        }

        reset();

        Log.debug("Open: " + fileName);

        try {
            in = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }

        eof = false;

        try {
            nextStr = in.readLine();
        } catch (IOException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (nextStr == null) {
            eof = true;
        }

        //Log.info("OK");
    }

    public void openFile(String theFile) {

        fileName = Detonator.INSTANCE.gameDataDir + theFile;

        //if ((Detonator.INSTANCE.pathToData) != null) {
        //    fileName = Detonator.INSTANCE.pathToData + fileName;
        //}
        reset();

        Log.debug("Open: " + fileName);

        try {
            in = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }

        eof = false;

        try {
            nextStr = in.readLine();
        } catch (IOException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (nextStr == null) {
            eof = true;
        }
    }

    public void openFile(StringReader theFile) {
        /**
         * This is for reading from a randomly generated map.
         */
        Log.debug("Open StringReader: " + theFile);

        in = new BufferedReader(theFile);
        reset();
        eof = false;

        try {
            nextStr = in.readLine();
        } catch (IOException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (nextStr == null) {
            eof = true;
        }
    }

    public void closeFile() {
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }

        in = null;
    }

    public void countMapSize(StringReader fromStringReader, String stopAt) {
        /**
         * This is for reading from a randomly generated map. stopAt is for the
         * character to stop at, defualt "end"
         *
         */
        if (stopAt == null) {
            stopAt = "end";
        }
        //Log.info("count from: "+fileName);
        BufferedReader reader;
        try {
            reader = new BufferedReader(fromStringReader);
            String temp = reader.readLine();
            mapLineCount = 0;
            longestLength = 0;
            while ((temp != null) && (temp.length() != 0) && (!temp.contains(stopAt))) {
                if (temp.length() > longestLength) {
                    longestLength = temp.length(); //set map sizeX
                }
                mapLineCount++; //increment map sizeY
                temp = reader.readLine();
            }
            reader.close();
        } catch (Exception ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void countMapSize(String fromFile, String stopAt) {
        /**
         * This is for reading from a Tex map.map file. stopAt is for the
         * character to stop at, defualt "end"
         *
         */
        if (stopAt == null) {
            stopAt = "end";
        }
        //fileName = Detonator.INSTANCE.gameDataDir + fromFile;
        fileName = fromFile;
        //Log.info("count from: "+fileName);
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(fileName));
            String temp = reader.readLine();
            mapLineCount = 0;
            longestLength = 0;
            while ((temp != null) && (temp.length() != 0) && (!temp.contains(stopAt))) {
                if (temp.length() > longestLength) {
                    longestLength = temp.length(); //set map sizeX
                }
                mapLineCount++; //increment map sizeY
                temp = reader.readLine();
            }
            reader.close();
        } catch (Exception ex) {
            Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reset() {
        rowCount = 0;
        counter = 0;
    }

    public void loadNewString(String theString) {

        fullStr = theString;
        counter = 0;

    }

    public String restOfRow() {
        return fullStr.substring(counter, savedLength);
    }

    public IntSlot getNextBindedValue() {
        getNextString();

        if ((lastStr.charAt(0) == 'l') && (lastStr.charAt(1) == '_')) {
            //Log.info("Local SLOT: "+lastStr);
            return Detonator.INSTANCE.getBindedLocalValueSlot(lastStr);
        } else {
            //return Detonator.INSTANCE.getBindedGlobalValue(lastStr);
            //Log.info("Global SLOT: "+lastStr);
            return Detonator.INSTANCE.getBindedValueSlot(lastStr);
        }
    }

    public TBoolean getNextBindedTrigger() {
        getNextString();

        if ((lastStr.charAt(0) == 'l') && (lastStr.charAt(1) == '_')) {
            return Detonator.INSTANCE.getBindedLocalTrigger(lastStr);
        } else {
            return Detonator.INSTANCE.getBindedTrigger(lastStr);
        }

    }

    public boolean getNextBoolean() {

        getNextString();

        if ((lastStr.equals("+")) || (lastStr.equals("true")) || (lastStr.equals("yes"))) {
            return true;
        } else {
            return false;
        }
    }

    public char getNextChar() {

        //Log.info("champ: "+fullStr);
        //Log.info("count: "+counter);

        //counter++; counter++;
        char nowChar = fullStr.charAt(counter);
        counter++;

        return nowChar;
    }

    public void restartAnalysis() {
        counter = 0;
    }

    public String getNextString() {

        char nowChar;
        buildStr.setLength(0);



        if (counter < savedLength) //not end of row
        {
            nowChar = fullStr.charAt(counter);
            while ((nowChar == ' ') && (counter < savedLength - 1)) { //skip all empty spaces

                counter++;
                nowChar = fullStr.charAt(counter);
            }


        } else {
            nowChar = '-';
        }

        while ((nowChar != ' ') && (counter < savedLength)) {


            nowChar = fullStr.charAt(counter);
            if (nowChar != ' ') {
                buildStr.append(nowChar);
            }

            counter++;
        }



        lastStr = buildStr.toString().toLowerCase();

        if (lastStr.length() == 0) {
            counter = savedLength;
        }
//if (nowChar==' ') {counter++;}
        //System.out.println("Next string: " + lastStr);
        return lastStr;
    }

    public int getNextNumber() {
        return Integer.parseInt(getNextString());
    }

    public static boolean charIsNumber(char nowChar) {

        // Log.info("char check: "+nowChar);

        return ((nowChar == '0') || (nowChar == '1') || (nowChar == '2') || (nowChar == '3')
                || (nowChar == '4') || (nowChar == '5') || (nowChar == '6') || (nowChar == '7')
                || (nowChar == '8') || (nowChar == '9'));
    }

    public boolean checkIfNextIsNumber() {

        int tempCounter = counter;
        char nowChar = ' ';
        while ((nowChar == ' ') && (tempCounter < savedLength)) {
            nowChar = fullStr.charAt(tempCounter);
            tempCounter++;
        }

        boolean result = charIsNumber(nowChar);

        while ((nowChar != ' ') && (tempCounter < savedLength) && (result == true)) {


            nowChar = fullStr.charAt(tempCounter);
            tempCounter++;

            result = charIsNumber(nowChar);
        }


        return result;
    }

    public MathOperator getNextMathOperator() {
        getNextString();
        //Log.info("check for math: " + lastStr);

        /* Java 7 edition
         switch (lastStr) {
         case "+":
         return new MathOperator(MathOperator.MathOperatorValue.ADD);
         case "-":
         return new MathOperator(MathOperator.MathOperatorValue.SUBSTRACT);
         case "*":
         return new MathOperator(MathOperator.MathOperatorValue.MULTIPLY);
         case "/":
         case ":":
         return new MathOperator(MathOperator.MathOperatorValue.DIVIDE);
         case "=":
         case ":=":
         return new MathOperator(MathOperator.MathOperatorValue.ASSIGN);
         default:
         return null;
         }
         */

        if (lastStr.equals("+")) {
            return new MathOperator(MathOperator.MathOperatorValue.ADD);
        } else if (lastStr.equals("-")) {
            return new MathOperator(MathOperator.MathOperatorValue.SUBSTRACT);
        } else if (lastStr.equals("*")) {
            return new MathOperator(MathOperator.MathOperatorValue.MULTIPLY);
        } else if ((lastStr.equals("/")) || (lastStr.equals(":"))) {
            return new MathOperator(MathOperator.MathOperatorValue.DIVIDE);
        } else if ((lastStr.equals("=")) || (lastStr.equals(":="))) {
            return new MathOperator(MathOperator.MathOperatorValue.ASSIGN);
        } else {
            return null;
        }


    }

    public Comparison getNextComparison() {
        getNextString();

        if (lastStr.equals("<")) {
            return new Comparison(Comparison.ComparisonValue.BELOW);
        } else if (lastStr.equals("<=")) {
            return new Comparison(Comparison.ComparisonValue.EQUAL_OR_BELOW);
        } else if (lastStr.equals("<>")) {
            return new Comparison(Comparison.ComparisonValue.NON_EQUAL);
        } else if ((lastStr.equals("==")) || (lastStr.equals("="))) {
            return new Comparison(Comparison.ComparisonValue.EQUAL);
        } else if (lastStr.equals(">=")) {
            return new Comparison(Comparison.ComparisonValue.EQUAL_OR_ABOVE);
        } else if (lastStr.equals(">")) {
            return new Comparison(Comparison.ComparisonValue.ABOVE);
        } else {
            return null;
        }

        /* Java 7 Edition
         switch (lastStr) {
         case "<":
         return new Comparison(Comparison.ComparisonValue.BELOW);
         case "<=":
         return new Comparison(Comparison.ComparisonValue.EQUAL_OR_BELOW);
         case "<>":
         case "!=":
         return new Comparison(Comparison.ComparisonValue.NON_EQUAL);
         case "=":
         case "==":
         return new Comparison(Comparison.ComparisonValue.EQUAL);
         case ">=":
         return new Comparison(Comparison.ComparisonValue.EQUAL_OR_ABOVE);
         case ">":
         return new Comparison(Comparison.ComparisonValue.ABOVE);
         default:
         return null;
         }
         */


    }

    //public Int getNextBindedGlobalValue() {
    //    return Detonator.instance.getBindedGlobalValue(getNextString());
    //}
    public double getNextDouble() {
        return Double.parseDouble(getNextString());
    }

    public float getNextFloat() {
        return Float.parseFloat(getNextString());
    }

    //public int getNextBoolean() {
    //}
    public int getNextSkill() {
        return Binding.readBinding(Binding.skillBinding, getNextString());
    }

    public int getNextPerk() {
        return Binding.readBinding(Binding.perkBinding, getNextString());
    }

    public int getNextStat() {
        return Binding.readBinding(Binding.statBinding, getNextString());
    }

    public void readRow() {
        counter = 0;
        rowCount++;


        fullStr = nextStr;

        if (fullStr == null) {
            eof = true;
        } else {

            savedLength = fullStr.length();

            //Log.info("Read row: " + fullStr);

            try {
                nextStr = in.readLine();
            } catch (IOException ex) {
                Logger.getLogger(StringAnalyst.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (nextStr == null) {
                eof = true;
            }
        }





    }

    public void error(String errorMessage) {
        Log.error(fileName + ", row " + rowCount + ", symbol " + counter + ": " + errorMessage);
    }

    public void checkForBadChars(String theString) {



        for (int counter1 = 0; counter1 < theString.length(); counter1++) {
            char theChar;

            theChar = theString.charAt(counter1);

            if ((int) theChar == 65533) {
                Log.info(theString + " : check!");
                Log.info("Char code (" + theChar + "): " + (int) theChar);
                error("PLEASE DO NOT USE MICROSOFT_WORD FOR WRITING SCRIPTS. Thanks. Also, you've got bad symbol - ' (position " + counter1 + ") in your script. Please replace it with ASCII one. ");
            }




        }


    }

    public int getBinding(int bindType) {
        return Binding.getBinding(bindType, getNextString());
    }

    public int getListBinding(int listType, int bindType) {
        return Binding.getListBinding(listType, bindType, getNextString());
    }

    public int readBinding(int bindType) {
        return Binding.readBinding(bindType, getNextString());
    }

    public static ArrayList<String> getFileList(String byExtension, String inPath) {

        ArrayList<String> list = new ArrayList<String>();



        File dir = new File(inPath);
        String[] fileList = dir.list();

        if (fileList == null) {
            Log.error("Specified directory does not exist");
        }

        for (int x = 0; x < fileList.length; x++) {
            if ((fileList[x].endsWith(byExtension))) {
                Log.info(fileList[x]);
                list.add(fileList[x]);
            }

        }

        return list;
    }
    public final static StringAnalyst analyst = new StringAnalyst();
}
TOP

Related Classes of transientlibs.tex.StringAnalyst

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.