Перенос.
This commit is contained in:
25
src/Common/Utils/Files/ProjectsChooser.java
Normal file
25
src/Common/Utils/Files/ProjectsChooser.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package Common.Utils.Files;
|
||||
import Common.Utils.Utils;
|
||||
import ProjectData.Project.db_project_info;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
public class ProjectsChooser extends VFileChooser_ {
|
||||
public ProjectsChooser(String title) {
|
||||
super(title, new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
return
|
||||
!Utils.ContainsCyrillic(f.getAbsolutePath()) &&
|
||||
!f.getName().equalsIgnoreCase(db_project_info.data)
|
||||
;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Все папки";
|
||||
}
|
||||
});
|
||||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
}
|
||||
}
|
||||
21
src/Common/Utils/Files/VDirectoryChooser.java
Normal file
21
src/Common/Utils/Files/VDirectoryChooser.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package Common.Utils.Files;
|
||||
import Common.Utils.Utils;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
public class VDirectoryChooser extends VFileChooser_ {
|
||||
public VDirectoryChooser(String title) {
|
||||
super(title, new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
return !Utils.ContainsCyrillic(f.getAbsolutePath());
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Все папки";
|
||||
}
|
||||
});
|
||||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
}
|
||||
}
|
||||
44
src/Common/Utils/Files/VFileChooser.java
Normal file
44
src/Common/Utils/Files/VFileChooser.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package Common.Utils.Files;
|
||||
import Common.Utils.Utils;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Vector;
|
||||
public class VFileChooser extends VFileChooser_ {
|
||||
Vector<String> Extensions = new Vector<>();
|
||||
String extensionsLine = "";
|
||||
public VFileChooser(String title, String... extensions_in) {
|
||||
super(title, null);
|
||||
Extensions.addAll(Arrays.asList(extensions_in));
|
||||
if (Extensions.isEmpty())
|
||||
extensionsLine = "*.*";
|
||||
else
|
||||
for (String ext : Extensions)
|
||||
extensionsLine += "*" + (ext.isEmpty() ? "" : ".") + ext + ";";
|
||||
fileChooser.setFileFilter(new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
return !Utils.ContainsCyrillic(f.getName())
|
||||
&& (f.isDirectory() || acceptExtensions(f));
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return extensionsLine;
|
||||
}
|
||||
});
|
||||
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
}
|
||||
public VFileChooser(String title, FileFilter filter_in) {
|
||||
super(title, filter_in);
|
||||
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
}
|
||||
public boolean acceptExtensions(File file) {
|
||||
if (Extensions.isEmpty()) return true;
|
||||
String file_ext = Utils.getExtension(file);
|
||||
for (String ext : Extensions)
|
||||
if (ext.equalsIgnoreCase(file_ext)) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
50
src/Common/Utils/Files/VFileChooser_.java
Normal file
50
src/Common/Utils/Files/VFileChooser_.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package Common.Utils.Files;
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Vector;
|
||||
public class VFileChooser_ {
|
||||
protected JFileChooser fileChooser = new JFileChooser() {
|
||||
@Override
|
||||
protected JDialog createDialog(Component parent) throws HeadlessException {
|
||||
JDialog res = super.createDialog(parent);
|
||||
res.setAlwaysOnTop(true);
|
||||
return res;
|
||||
}
|
||||
};
|
||||
public File getCurrentDirectory(){
|
||||
return fileChooser.getCurrentDirectory();
|
||||
}
|
||||
public VFileChooser_(String title, FileFilter filter) {
|
||||
fileChooser.setDialogTitle(title);
|
||||
fileChooser.setAcceptAllFileFilterUsed(false);
|
||||
fileChooser.setFileFilter(filter);
|
||||
}
|
||||
public void setTitle(String title_in) {
|
||||
fileChooser.setDialogTitle(title_in);
|
||||
}
|
||||
public File ShowDialog() {
|
||||
fileChooser.setMultiSelectionEnabled(false);
|
||||
File result = null;
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
result = fileChooser.getSelectedFile();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public Vector<File> ShowMultiDialog() {
|
||||
fileChooser.setMultiSelectionEnabled(true);
|
||||
Vector<File> result = new Vector<>();
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
result = new Vector<>(Arrays.asList(fileChooser.getSelectedFiles()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public void SetCurrentDirectory(String dir) {
|
||||
fileChooser.setCurrentDirectory(new File(dir));
|
||||
}
|
||||
public void SetCurrentDirectory(File dir) {
|
||||
fileChooser.setCurrentDirectory(dir);
|
||||
}
|
||||
}
|
||||
24
src/Common/Utils/Index.java
Normal file
24
src/Common/Utils/Index.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package Common.Utils;
|
||||
import java.io.Serializable;
|
||||
public class Index implements Serializable {
|
||||
int value = 0;
|
||||
public int Inc() {
|
||||
return value++;
|
||||
}
|
||||
public int Dec() {
|
||||
return value--;
|
||||
}
|
||||
public void Set(int value_in) {
|
||||
value = value_in;
|
||||
}
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
public void Reset() {
|
||||
value = 0;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
}
|
||||
27
src/Common/Utils/InterruptThread.java
Normal file
27
src/Common/Utils/InterruptThread.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package Common.Utils;
|
||||
import ProjectData.Project.db_project_info;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.Callable;
|
||||
public class InterruptThread extends Thread{
|
||||
//------------
|
||||
public static final String Eliminated = "Eliminated";
|
||||
public InterruptThread(int sleep_ms, Callable action){
|
||||
super(() -> {
|
||||
File interruptFile = new File(db_project_info.interrupt);
|
||||
try {
|
||||
while (true) {
|
||||
Thread.sleep(sleep_ms);
|
||||
if (interruptFile.exists()) {
|
||||
FileUtils.writeStringToFile(new File(Eliminated + " by INTERRUPT file"), "");
|
||||
FileUtils.forceDelete(interruptFile);
|
||||
action.call();
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
16
src/Common/Utils/Stopwatch.java
Normal file
16
src/Common/Utils/Stopwatch.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package Common.Utils;
|
||||
public class Stopwatch {
|
||||
long st, en;
|
||||
double res;
|
||||
public void Start() {
|
||||
st = System.nanoTime();
|
||||
}
|
||||
public void Stop() {
|
||||
en = System.nanoTime();
|
||||
res = en - st;
|
||||
}
|
||||
public String Print() {
|
||||
//https://hr-vector.com/java/formatirovanie-chisel-strok
|
||||
return String.format("%,3.2f", res * 0.000001) + " ms";
|
||||
}
|
||||
}
|
||||
63
src/Common/Utils/StringTemplate.java
Normal file
63
src/Common/Utils/StringTemplate.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package Common.Utils;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
public class StringTemplate {
|
||||
//https://javarush.ru/groups/posts/regulyarnye-vyrazheniya-v-java
|
||||
public static String separator = "(\\s)+"; //хотя бы один пробел
|
||||
public static String parameter = "(.)+"; //хотя бы один символ
|
||||
//------------------------------------------------------------------
|
||||
public String prefix = ""; //часть команды до параметра, запакованная через пробел
|
||||
public String suffix = ""; //часть команды после параметра, запакованная через пробел
|
||||
public String pattern = "";
|
||||
//------------------------------------------------------------------
|
||||
public StringTemplate(String p, String s) {
|
||||
prefix = Utils.pack(p);
|
||||
suffix = Utils.pack(s);
|
||||
// System.out.println(Utils.Brackets(prefix));
|
||||
// System.out.println(Utils.Brackets(suffix));
|
||||
String[] prefix_words = prefix.split(" ");
|
||||
String[] suffix_words = suffix.split(" ");
|
||||
//настраиваем регулярное выражение----------
|
||||
prefix = "^(\\s)*";
|
||||
for (String word : prefix_words) {
|
||||
if (!word.isEmpty())
|
||||
prefix += (word + separator);
|
||||
}
|
||||
suffix = "";
|
||||
for (String word : suffix_words) {
|
||||
if (!word.isEmpty())
|
||||
suffix += (separator + word);
|
||||
}
|
||||
suffix += "(\\s)*$";
|
||||
pattern = prefix + parameter + suffix;
|
||||
}
|
||||
public static String getFirstMatch(String pattern_in, String text_in) {
|
||||
Matcher m = Pattern.compile(pattern_in, Pattern.CASE_INSENSITIVE).matcher(text_in);
|
||||
m.find();
|
||||
return text_in.substring(m.start(), m.end());
|
||||
}
|
||||
public boolean check(String text_in) {
|
||||
// System.out.println("sentense = " + Utils.Brackets(text_in));
|
||||
// System.out.println("pattern = " + Utils.Brackets(pattern));
|
||||
return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(text_in).find();
|
||||
}
|
||||
public String check_and_get_param(String text_in) {
|
||||
Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
|
||||
Matcher matcher = regex.matcher(text_in);
|
||||
// System.out.println("sentense = " + Utils.Brackets(text_in));
|
||||
// System.out.println("pattern = " + Utils.Brackets(pattern));
|
||||
if (matcher.find()) {
|
||||
// System.out.println("match found " + matcher.start() + " " + matcher.end());
|
||||
String sentence = text_in.substring(matcher.start(), matcher.end());
|
||||
// System.out.println("sentence=" + Utils.Brackets(sentence));
|
||||
String prefix_ = getFirstMatch(prefix, sentence);
|
||||
// System.out.println("prefix_ = " + Utils.Brackets(prefix_));
|
||||
String suffix_ = getFirstMatch(suffix, sentence);
|
||||
// System.out.println("suffix_ = " + Utils.Brackets(suffix_));
|
||||
String param = sentence.substring(prefix_.length(), sentence.length() - suffix_.length());
|
||||
// System.out.println("param = " + Utils.Brackets(param));
|
||||
return param;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
22
src/Common/Utils/TextLog.java
Normal file
22
src/Common/Utils/TextLog.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package Common.Utils;
|
||||
import Common.Global;
|
||||
public class TextLog {
|
||||
String text = "";
|
||||
public void Writeln(String line) {
|
||||
text += line + "\n";
|
||||
}
|
||||
public void Writeln_(String line) {
|
||||
text += line + "\n";
|
||||
Global.Log.Print(line);
|
||||
}
|
||||
public void Clear() {
|
||||
text = "";
|
||||
}
|
||||
public boolean isEmpty() {
|
||||
return text.isEmpty();
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
1432
src/Common/Utils/Utils.java
Normal file
1432
src/Common/Utils/Utils.java
Normal file
File diff suppressed because it is too large
Load Diff
241
src/Common/Utils/Validators/DVMHelpParser.java
Normal file
241
src/Common/Utils/Validators/DVMHelpParser.java
Normal file
@@ -0,0 +1,241 @@
|
||||
package Common.Utils.Validators;
|
||||
import Common.Utils.Utils;
|
||||
import GlobalData.Compiler.Compiler;
|
||||
import GlobalData.CompilerEnvironment.CompilerEnvironment;
|
||||
import GlobalData.CompilerOption.CompilerOption;
|
||||
|
||||
import java.util.Arrays;
|
||||
public class DVMHelpParser {
|
||||
public static HelpParserState state;
|
||||
public static String t_line;
|
||||
public static String line;
|
||||
//-
|
||||
public static OptionState optionState;
|
||||
public static CompilerOption option;
|
||||
//-
|
||||
public static int spacesCounter;
|
||||
public static CompilerEnvironment environment;
|
||||
public static EnvironmentState environmentState;
|
||||
//-
|
||||
public static String descriptionLine = "";
|
||||
//-
|
||||
public static Compiler compiler = null;
|
||||
public static String[] banned_options = new String[]{
|
||||
"-o",
|
||||
"-c",
|
||||
"-f90",
|
||||
"-FI"
|
||||
};
|
||||
public static void ResetOption() {
|
||||
optionState = OptionState.SearchName;
|
||||
option = null;
|
||||
descriptionLine = "";
|
||||
}
|
||||
public static void TryConfirmOptionDescriptionLine() {
|
||||
if (option != null && !descriptionLine.isEmpty()) {
|
||||
option.description.add(descriptionLine.trim());
|
||||
descriptionLine = "";
|
||||
}
|
||||
}
|
||||
public static void TryConfirmOption() {
|
||||
if ((option != null) && (!compiler.options.containsKey(option.name))) {
|
||||
if (!descriptionLine.isEmpty())
|
||||
option.description.add(descriptionLine.trim());
|
||||
option.CheckParameterVariants();
|
||||
if (!Arrays.asList(banned_options).contains(option.name)) {
|
||||
compiler.options.put(option.name, option);
|
||||
}
|
||||
ResetOption();
|
||||
}
|
||||
}
|
||||
public static void ResetEnvironment() {
|
||||
environmentState = EnvironmentState.SearchName;
|
||||
spacesCounter = 0;
|
||||
environment = null;
|
||||
descriptionLine = "";
|
||||
}
|
||||
public static void TryConfirmEnvironmentDescriptionLine() {
|
||||
if (environment != null && !descriptionLine.isEmpty()) {
|
||||
environment.description.add(descriptionLine.trim());
|
||||
descriptionLine = "";
|
||||
}
|
||||
}
|
||||
public static void TryConfirmEnvironment() {
|
||||
if ((environment != null) && (!compiler.environments.containsKey(environment.name))) {
|
||||
if (!descriptionLine.isEmpty())
|
||||
environment.description.add(descriptionLine.trim());
|
||||
environment.CheckDefaults();
|
||||
compiler.environments.put(environment.name, environment);
|
||||
ResetEnvironment();
|
||||
}
|
||||
}
|
||||
public static void ReadOptions(Compiler compiler_in) {
|
||||
compiler = compiler_in;
|
||||
String[] lines = compiler.helpText.split("\n");
|
||||
state = HelpParserState.Search;
|
||||
for (String line_ : lines) {
|
||||
line = line_; //нужна для окружения. там пробелы нужно считать сразу.
|
||||
t_line = Utils.remove(line_.trim(), "\r");
|
||||
switch (state) {
|
||||
case Search:
|
||||
switch (t_line) {
|
||||
case "Output and debugging options:":
|
||||
case "Convertation options:":
|
||||
case "Optimization options:":
|
||||
System.out.println(t_line + " Options chapter started!");
|
||||
state = HelpParserState.OptionsChapter;
|
||||
ResetOption();
|
||||
break;
|
||||
case "Environment variables":
|
||||
System.out.println(t_line + " Environments chapter started!");
|
||||
state = HelpParserState.EnvironmentsChapterHeader;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case EnvironmentsChapterHeader:
|
||||
state = HelpParserState.EnvironmentsChapter;
|
||||
ResetEnvironment();
|
||||
break;
|
||||
case OptionsChapter:
|
||||
if (t_line.isEmpty()) {
|
||||
TryConfirmOption();
|
||||
System.out.println("Chapter ended");
|
||||
state = HelpParserState.Search;
|
||||
} else {
|
||||
char[] symbols = t_line.toCharArray();
|
||||
//- Новая строка.
|
||||
optionState = OptionState.SearchName;
|
||||
//-
|
||||
for (char c : symbols) {
|
||||
//-
|
||||
// System.out.print(c);
|
||||
//-
|
||||
switch (optionState) {
|
||||
case SearchName:
|
||||
switch (c) {
|
||||
case '-':
|
||||
TryConfirmOption();
|
||||
//-
|
||||
option = new CompilerOption();
|
||||
option.name += c;
|
||||
optionState = OptionState.Name;
|
||||
break;
|
||||
default:
|
||||
descriptionLine += c;
|
||||
optionState = OptionState.Description;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Name:
|
||||
switch (c) {
|
||||
case '<':
|
||||
optionState = OptionState.Parameter;
|
||||
break;
|
||||
case ' ':
|
||||
case '=':
|
||||
case '\t':
|
||||
option.parameterSeparator += c;
|
||||
optionState = OptionState.SearchParameter;
|
||||
break;
|
||||
default:
|
||||
option.name += c;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SearchParameter:
|
||||
if (c == '<') {
|
||||
optionState = OptionState.Parameter;
|
||||
} else {
|
||||
option.parameterSeparator = "";
|
||||
optionState = OptionState.Description;
|
||||
descriptionLine += c;
|
||||
}
|
||||
break;
|
||||
case Parameter:
|
||||
if (c == '>') {
|
||||
optionState = OptionState.SearchDescription;
|
||||
} else {
|
||||
option.parameterName += c;
|
||||
}
|
||||
break;
|
||||
case SearchDescription:
|
||||
if (c != ' ') {
|
||||
descriptionLine += c;
|
||||
optionState = OptionState.Description;
|
||||
}
|
||||
break;
|
||||
case Description:
|
||||
descriptionLine += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//-
|
||||
TryConfirmOptionDescriptionLine();
|
||||
}
|
||||
break;
|
||||
case EnvironmentsChapter:
|
||||
if (t_line.isEmpty()) {
|
||||
TryConfirmEnvironment();
|
||||
System.out.println("Chapter ended");
|
||||
state = HelpParserState.Search;
|
||||
} else {
|
||||
char[] symbols = line.toCharArray();
|
||||
//- Новая строка.
|
||||
environmentState = EnvironmentState.SearchName;
|
||||
spacesCounter = 0;
|
||||
//-
|
||||
for (char c : symbols) {
|
||||
switch (environmentState) {
|
||||
case SearchName:
|
||||
if (c == ' ') {
|
||||
if (spacesCounter++ > 4) {
|
||||
//имя нам уже не встретится. это строка описания.
|
||||
environmentState = EnvironmentState.Description;
|
||||
}
|
||||
} else if (Character.isLetter(c)) {
|
||||
if (spacesCounter == 4) {
|
||||
TryConfirmEnvironment();
|
||||
environment = new CompilerEnvironment();
|
||||
environment.name += c;
|
||||
environmentState = EnvironmentState.Name;
|
||||
}
|
||||
} else {
|
||||
descriptionLine += c;
|
||||
environmentState = EnvironmentState.Description;
|
||||
}
|
||||
break;
|
||||
case Name:
|
||||
//в имени окружения пробелов быть не может. ждем описания.
|
||||
if (c == ' ') {
|
||||
environmentState = EnvironmentState.SearchDescription;
|
||||
} else if (Character.isLetterOrDigit(c) || c == '_') {
|
||||
//буквы цифры и подчеркивания - имя продолжается.
|
||||
environment.name += c;
|
||||
} else {
|
||||
descriptionLine += c;
|
||||
environmentState = EnvironmentState.Description;
|
||||
}
|
||||
break;
|
||||
case SearchDescription:
|
||||
if (c == ' ') {
|
||||
//игнорируем.
|
||||
} else {
|
||||
descriptionLine += c;
|
||||
environmentState = EnvironmentState.Description;
|
||||
}
|
||||
break;
|
||||
case Description:
|
||||
descriptionLine += c;
|
||||
break;
|
||||
}
|
||||
TryConfirmOptionDescriptionLine();
|
||||
}
|
||||
//-
|
||||
TryConfirmEnvironmentDescriptionLine();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------------>>
|
||||
}
|
||||
8
src/Common/Utils/Validators/EnvironmentState.java
Normal file
8
src/Common/Utils/Validators/EnvironmentState.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Common.Utils.Validators;
|
||||
public enum EnvironmentState {
|
||||
SearchName,
|
||||
Name,
|
||||
//-
|
||||
SearchDescription,
|
||||
Description
|
||||
}
|
||||
10
src/Common/Utils/Validators/HelpParserState.java
Normal file
10
src/Common/Utils/Validators/HelpParserState.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package Common.Utils.Validators;
|
||||
public enum HelpParserState {
|
||||
Search,
|
||||
OptionsChapter,
|
||||
//------------------
|
||||
//------------------
|
||||
EnvironmentsChapterHeader,
|
||||
EnvironmentsChapter,
|
||||
End
|
||||
}
|
||||
11
src/Common/Utils/Validators/OptionState.java
Normal file
11
src/Common/Utils/Validators/OptionState.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package Common.Utils.Validators;
|
||||
public enum OptionState {
|
||||
SearchName,
|
||||
Name,
|
||||
//-
|
||||
SearchParameter,
|
||||
Parameter,
|
||||
//-
|
||||
SearchDescription,
|
||||
Description
|
||||
}
|
||||
68
src/Common/Utils/Validators/PathValidator.java
Normal file
68
src/Common/Utils/Validators/PathValidator.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package Common.Utils.Validators;
|
||||
import Common.Utils.TextLog;
|
||||
import Common.Utils.Utils;
|
||||
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
|
||||
public class PathValidator extends Validator {
|
||||
PathValidatorState state;
|
||||
StringBuilder name;
|
||||
int spaces_count;
|
||||
public PathValidator(String string, String string_name_in, TextLog log_in) {
|
||||
super(string, string_name_in, log_in);
|
||||
}
|
||||
@Override
|
||||
protected void reset() {
|
||||
state = PathValidatorState.Start;
|
||||
name = new StringBuilder();
|
||||
spaces_count = 0;
|
||||
}
|
||||
@Override
|
||||
protected boolean continueCondition() {
|
||||
return (state == PathValidatorState.Name) || state == PathValidatorState.Start;
|
||||
}
|
||||
@Override
|
||||
protected void Body() {
|
||||
switch (state) {
|
||||
case Start:
|
||||
if (RSyntaxUtilities.isLetter(c) || RSyntaxUtilities.isDigit(c) || c == '_') {
|
||||
name.append(c);
|
||||
state = PathValidatorState.Name;
|
||||
} else state = PathValidatorState.WrongNameFormat;
|
||||
break;
|
||||
case Name:
|
||||
switch (c) {
|
||||
case '/':
|
||||
reset();
|
||||
break;
|
||||
case ' ':
|
||||
spaces_count++;
|
||||
name.append(c);
|
||||
break;
|
||||
default:
|
||||
if (Utils.isForbidden(c))
|
||||
state = PathValidatorState.Forbidden;
|
||||
else name.append(c);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void PerformFinish() {
|
||||
switch (state) {
|
||||
case WrongNameFormat:
|
||||
Log.Writeln(string_name + ": имя файла или каталога в пути имеет неверный формат");
|
||||
break;
|
||||
case Forbidden:
|
||||
Log.Writeln(string_name + ": Составляющие путь имена содержат запрещённые символы \n" + Utils.all_forbidden_characters_string);
|
||||
break;
|
||||
case Name:
|
||||
if (spaces_count > 0)
|
||||
Log.Writeln(string_name + ": Пробелы в окончании пути к файлу запрещены.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected int getStartIndex() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
8
src/Common/Utils/Validators/PathValidatorState.java
Normal file
8
src/Common/Utils/Validators/PathValidatorState.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Common.Utils.Validators;
|
||||
public enum PathValidatorState {
|
||||
Start,
|
||||
Name,
|
||||
WrongNameFormat,
|
||||
Forbidden,
|
||||
SpacesInLastName
|
||||
}
|
||||
229
src/Common/Utils/Validators/ShellParser.java
Normal file
229
src/Common/Utils/Validators/ShellParser.java
Normal file
@@ -0,0 +1,229 @@
|
||||
package Common.Utils.Validators;
|
||||
import Common.Global;
|
||||
import Common.Utils.Utils;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Vector;
|
||||
public class ShellParser {
|
||||
public static ShellParserState state;
|
||||
public static StringBuilder lineBuilder;
|
||||
public static String userName;
|
||||
public static StringBuilder invitationBuilder;
|
||||
public static char c;
|
||||
public static char[] buffer = new char[1];
|
||||
public static Vector<String> lines = new Vector<>();
|
||||
public static boolean bracketOpened = false;
|
||||
public static boolean return_active = false;
|
||||
public static boolean isCommandSymbol() {
|
||||
int code = c;
|
||||
return code <= 31 || c == 127;
|
||||
}
|
||||
public static void ResetLine() {
|
||||
invitationBuilder = new StringBuilder();
|
||||
lineBuilder = new StringBuilder();
|
||||
bracketOpened = false;
|
||||
state = ShellParserState.NewLine;
|
||||
return_active = false;
|
||||
}
|
||||
public static boolean isNameCharacter() {
|
||||
//латиница, цифры,подчеркивания. и -
|
||||
return String.valueOf(c).matches("[\\w\\-]*") || c == '?';
|
||||
}
|
||||
//false наоборот означать что конец строки ЕСТЬ.
|
||||
public static boolean checkEndLine() {
|
||||
if (return_active) {
|
||||
switch (c) {
|
||||
case '\n':
|
||||
//ложная тревога. возврат каретки ни на что не влияет.
|
||||
lines.add(lineBuilder.toString());
|
||||
ResetLine();
|
||||
return false;
|
||||
case '\r':
|
||||
return false;
|
||||
default:
|
||||
//тут был возврат. надо игнорить символ, ибо он уже прочитан.
|
||||
return_active = false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
switch (c) {
|
||||
case '\r':
|
||||
return_active = true;
|
||||
return false;
|
||||
case '\n':
|
||||
lines.add(lineBuilder.toString());
|
||||
ResetLine();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static void NewLine() {
|
||||
if (c == '[') {//приглашение со скобками.
|
||||
bracketOpened = true;
|
||||
invitationBuilder.append(c);
|
||||
state = ShellParserState.UserName;
|
||||
} else {
|
||||
if (isNameCharacter()) {
|
||||
invitationBuilder.append(c);
|
||||
state = ShellParserState.UserName;
|
||||
} else
|
||||
//не буква и не скобка. значит в этой строке приглашения нет.
|
||||
state = ShellParserState.Skip;
|
||||
}
|
||||
}
|
||||
public static void UserName() {
|
||||
if (c == '@') { //проверить. а тот ли юзернейм.
|
||||
String test = invitationBuilder.toString();
|
||||
if (bracketOpened) test = test.substring(1);
|
||||
test = test.toLowerCase();
|
||||
state = test.endsWith(userName.toLowerCase()) ? ShellParserState.MachineName : ShellParserState.Skip;
|
||||
invitationBuilder.append(c);
|
||||
} else if (isNameCharacter()||(c=='['))
|
||||
invitationBuilder.append(c);
|
||||
else
|
||||
state = ShellParserState.Skip;
|
||||
}
|
||||
public static void MachineName() {
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case ':':
|
||||
state = ShellParserState.Path;
|
||||
invitationBuilder.append(c);
|
||||
break;
|
||||
default:
|
||||
if (isNameCharacter())
|
||||
invitationBuilder.append(c);
|
||||
else state = ShellParserState.Skip;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static void Path() {
|
||||
switch (c) {
|
||||
case '$':
|
||||
case '#':
|
||||
case '>':
|
||||
invitationBuilder.append(c);
|
||||
state = ShellParserState.Space; //приглашение завершено. осталось прочитать пробел после него
|
||||
break;
|
||||
case ']':
|
||||
if (bracketOpened) {
|
||||
invitationBuilder.append(c);
|
||||
bracketOpened = false;
|
||||
} else {
|
||||
// UI.Info("KEK");
|
||||
state = ShellParserState.Skip; //непарная скобка, все, привет
|
||||
}
|
||||
break;
|
||||
default:
|
||||
invitationBuilder.append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static void Space() {
|
||||
if (c == ' ') {
|
||||
state = ShellParserState.End;
|
||||
invitationBuilder.append(c);
|
||||
} else {
|
||||
state = ShellParserState.Skip;
|
||||
}
|
||||
}
|
||||
public static void setUserName(String userName_in) {
|
||||
userName = userName_in;
|
||||
}
|
||||
public static void printChar() {
|
||||
int code = c;
|
||||
if ((!return_active) || (c == '\n')) {
|
||||
System.out.print(c == '\r' ? ("\\r") :
|
||||
|
||||
(c=='\n'? "\\n\n":c));
|
||||
if (isCommandSymbol())
|
||||
System.out.print(Utils.RBrackets(code));
|
||||
}
|
||||
}
|
||||
public static void ReadInvitation(InputStreamReader fromServer) {
|
||||
lines.clear();
|
||||
ResetLine();
|
||||
do {
|
||||
try {
|
||||
if (fromServer.read(buffer) >= 0) {
|
||||
c = buffer[0];
|
||||
printChar();
|
||||
// if (!isCommandSymbol()) {
|
||||
if (checkEndLine() && (!isCommandSymbol())) {
|
||||
lineBuilder.append(c);
|
||||
switch (state) {
|
||||
case NewLine:
|
||||
NewLine();
|
||||
break;
|
||||
case UserName:
|
||||
UserName();
|
||||
break;
|
||||
case MachineName:
|
||||
MachineName();
|
||||
break;
|
||||
case Path:
|
||||
Path();
|
||||
break;
|
||||
case Space:
|
||||
Space();
|
||||
break;
|
||||
case Skip:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// System.out.println(Utils.Brackets(state));
|
||||
// }
|
||||
} else
|
||||
state = ShellParserState.End;
|
||||
} catch (Exception ex) {
|
||||
Global.Log.PrintException(ex);
|
||||
state = ShellParserState.End;
|
||||
}
|
||||
} while (!state.equals(ShellParserState.End));
|
||||
}
|
||||
public static void ReadLine(InputStreamReader fromServer) {
|
||||
state = ShellParserState.NewLine;
|
||||
do {
|
||||
try {
|
||||
if (fromServer.read(buffer) >= 0) {
|
||||
c = buffer[0];
|
||||
printChar();
|
||||
switch (c) {
|
||||
case '\r':
|
||||
break;
|
||||
case '\n':
|
||||
return;
|
||||
}
|
||||
} else
|
||||
state = ShellParserState.End;
|
||||
} catch (Exception ex) {
|
||||
Global.Log.PrintException(ex);
|
||||
state = ShellParserState.End;
|
||||
}
|
||||
} while (!state.equals(ShellParserState.End));
|
||||
}
|
||||
public static String getCommandResult(InputStreamReader fromServer) {
|
||||
//если последняя строка ответа - кончается на приглашение, то ничего не делаем.
|
||||
//если нет. значит ответ кончается на перевод строки. или пуст.
|
||||
// нужно прочитать еще одно приглашение.
|
||||
String last_line = "";
|
||||
String res = "";
|
||||
boolean no_extra_read = false;
|
||||
if (lines.size() > 0) {
|
||||
last_line = lines.lastElement();
|
||||
if (no_extra_read = last_line.endsWith(invitationBuilder.toString())) {
|
||||
System.out.println("needs trim");
|
||||
lines.remove(lines.size() - 1);
|
||||
//больше ничего не читаем. но. обрезаем ее конец.
|
||||
last_line = last_line.substring(0, last_line.length() - invitationBuilder.length());
|
||||
lines.add(last_line);
|
||||
}
|
||||
res = String.join("\n", lines);
|
||||
}
|
||||
if (!no_extra_read) {
|
||||
ReadInvitation(fromServer);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
12
src/Common/Utils/Validators/ShellParserState.java
Normal file
12
src/Common/Utils/Validators/ShellParserState.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package Common.Utils.Validators;
|
||||
public enum ShellParserState {
|
||||
//--------------------------------
|
||||
NewLine, //начало строки.
|
||||
UserName, //имя пользователя.
|
||||
MachineName, //имя машины
|
||||
Path, //путь к текущей папке - часть приглашения
|
||||
Space, //завершающий приглашение пробел.
|
||||
Skip, //гарантированно не приглашение.
|
||||
//--------------------------------
|
||||
End//выход
|
||||
}
|
||||
14
src/Common/Utils/Validators/StatementsChecker.java
Normal file
14
src/Common/Utils/Validators/StatementsChecker.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package Common.Utils.Validators;
|
||||
import Common.Utils.TextLog;
|
||||
public class StatementsChecker {
|
||||
public static boolean Check(TextLog Log, boolean drop_on_first, Object... args) {
|
||||
if (args.length % 2 != 0) return false;
|
||||
for (int i = 0; i < args.length; i += 2) {
|
||||
if ((boolean) args[i]) {
|
||||
Log.Writeln((String) args[i + 1]);
|
||||
if (drop_on_first) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
36
src/Common/Utils/Validators/Validator.java
Normal file
36
src/Common/Utils/Validators/Validator.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package Common.Utils.Validators;
|
||||
import Common.Utils.TextLog;
|
||||
public class Validator {
|
||||
protected char c;
|
||||
protected int i;
|
||||
protected String string_name;
|
||||
protected char[] chars;
|
||||
protected TextLog Log;
|
||||
public Validator(String string, String string_name_in, TextLog log_in) {
|
||||
string_name = string_name_in;
|
||||
chars = string.toCharArray();
|
||||
Log = log_in;
|
||||
reset();
|
||||
}
|
||||
protected void reset() {
|
||||
}
|
||||
protected int getStartIndex() {
|
||||
return 0;
|
||||
}
|
||||
protected boolean continueCondition() {
|
||||
return true;
|
||||
}
|
||||
protected void Body() {
|
||||
}
|
||||
protected void PerformFinish() {
|
||||
}
|
||||
public void Validate() {
|
||||
for (i = getStartIndex(); i < chars.length && continueCondition(); ++i) {
|
||||
c = chars[i];
|
||||
System.out.print(c);
|
||||
Body();
|
||||
}
|
||||
System.out.println();
|
||||
PerformFinish();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user