no message

This commit is contained in:
2024-10-07 14:22:52 +03:00
parent 6b1576461d
commit 61fc37b574
173 changed files with 960 additions and 1526 deletions

View File

@@ -1,4 +1,6 @@
package Common_old.Utils;
import Common.CommonConstants;
import Common.Utils.CommonUtils;
import Common.Utils.Index;
import Common.Utils.StringTemplate;
import Common.Utils.TextLog;
@@ -38,13 +40,6 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Utils {
//--->>
public static String hideRegularMetasymbols(String word) {
String res = word.replace("\\", "\\\\");
for (char c : Constants.regular_metasymbols)
res = res.replace(String.valueOf(c), "\\" + c);
return res;
}
public static boolean isLinuxSystemCommand(String text) {
for (String command : Constants.linux_system_commands) {
if (text.equalsIgnoreCase(command)) return true;
@@ -72,48 +67,9 @@ public class Utils {
for (char f : Constants.forbidden_file_name_characters)
Constants.all_forbidden_characters_string += f + " ";
}
public static String DQuotes(Object o) {
return "\"" + o.toString() + "\"";
}
public static String Quotes(Object o) {
return "'" + o.toString() + "'";
}
public static String Brackets(Object o) {
return "[" + o.toString() + "]";
}
public static String Bold(Object o) {
return "<b>" + o.toString() + "</b>";
}
public static String RBrackets(Object o) {
return "(" + o.toString() + ")";
}
public static String MFVar(Object o) {
return "$(" + o.toString() + ")";
}
public static String TBrackets(Object o) {
return "<" + o.toString() + ">";
}
public static String getExtension(File file) {
String fn = file.getName();
int di = fn.lastIndexOf(".");
return (di >= 0) ? fn.substring(di + 1).toLowerCase() : "";
}
public static String getExtensionByName(String fn) {
;
int di = fn.lastIndexOf(".");
return (di >= 0) ? fn.substring(di + 1).toLowerCase() : "";
}
public static String getFileNameWithoutExtension(File file) {
return getNameWithoutExtension(file.getName());
}
public static String getNameWithoutExtension(String fn) {
return (fn.contains(".")) ? fn.substring(0, fn.lastIndexOf(".")).toLowerCase() : fn.toLowerCase();
}
public static boolean ContainsCyrillic(String string) {
return string.chars()
.mapToObj(Character.UnicodeBlock::of)
.anyMatch(b -> b.equals(Character.UnicodeBlock.CYRILLIC));
}
public static void CheckDirectory(File dir) {
if (!dir.exists()) {
try {
@@ -145,8 +101,6 @@ public class Utils {
public static Object requireNonNullElse(Object value, Object default_value) {
return (value != null) ? value : default_value;
}
//https://javadevblog.com/kak-schitat-fajl-v-string-primer-chteniya-fajla-na-java.html
//https://habr.com/ru/post/269667/
public static String ReadAllText(File file) {
try {
return new String(Files.readAllBytes(file.toPath()));
@@ -155,16 +109,6 @@ public class Utils {
}
return "";
}
public static String toU(String path) {
return path.replace('\\', '/');
}
public static String toW(String path) {
return path.replace('/', '\\');
}
public static double getFileSizeMegaBytes(File file) {
double res = file.length() / (1024 * 1024);
return res;
}
public static void CleanDirectory(File dir) {
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
@@ -179,7 +123,7 @@ public class Utils {
}
}
}
public static long last_ticks = Constants.Nan;
public static long last_ticks = CommonConstants.Nan;
public static void sleep(long millis) {
try {
Thread.sleep(millis);
@@ -213,187 +157,6 @@ public class Utils {
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
}
public static boolean isDigit(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean isEnglishLetter(char c) {
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}
public static boolean isRussianLetter(char c) {
return ((c >= 'а') && (c <= 'я'))
|| ((c >= 'А') && (c <= 'Я'))
|| (c == 'Ё')
|| (c == 'ё');
}
public static boolean isSign(char c) {
switch (c) {
//арифметика.
case '+':
case '-':
case '*':
case '/':
case '<':
case '>':
case '&':
case '=':
case '%':
case '^':
//- обр слеш
case '\\':
//препинание
case ' ':
case '_':
case '.':
case ',':
case '!':
case '?':
case ';':
case ':':
//escape последовательности
case '\t':
case '\n':
case '\r':
//кавычки
case '\'':
case '"':
//- скобки
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
//прочее
case '~':
case '`':
case '|':
case '@':
case '$':
case '#':
case '№':
return true;
}
return false;
}
public static char Translit(char c) {
switch (c) {
case 'А':
case 'а':
case 'Я':
case 'я':
return 'A';
//
case 'Б':
case 'б':
return 'B';
//-
case 'В':
case 'в':
return 'V';
//
case 'Г':
case 'г':
return 'G';
//
case 'Д':
case 'д':
return 'D';
//
case 'Е':
case 'е':
case 'Ё':
case 'ё':
case 'Э':
case 'э':
return 'E';
//
case 'Ж':
case 'ж':
return 'J';
//
case 'З':
case 'з':
return 'Z';
//
case 'И':
case 'и':
case 'Й':
case 'й':
return 'I';
//
case 'К':
case 'к':
return 'K';
//
case 'Л':
case 'л':
return 'L';
//
case 'М':
case 'м':
return 'M';
//
case 'Н':
case 'н':
return 'N';
//
case 'О':
case 'о':
return 'O';
//
case 'П':
case 'п':
return 'P';
//
case 'Р':
case 'р':
return 'R';
//
case 'С':
case 'с':
return 'S';
case 'Т':
case 'т':
return 'T';
//
case 'У':
case 'у':
case 'Ю':
case 'ю':
return 'U';
case 'Х':
case 'х':
case 'Щ':
case 'щ':
case 'Ш':
case 'ш':
return 'H';
//
case 'Ф':
case 'ф':
return 'F';
//
case 'Ч':
case 'ч':
case 'Ц':
case 'ц':
return 'C';
//
case 'Ы':
case 'ы':
return 'Y';
//
}
return ' ';
}
public static String ending(boolean flag) {
return flag ? ")" : ",";
}
// http://java-online.ru/blog-archive.xhtml
public static void getFilesCountR(File dir, Index res) {
for (File f : dir.listFiles()) {
@@ -412,18 +175,6 @@ public class Utils {
DateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
public static boolean isBracketsBalanced(String fragment) {
int cc = 0;
for (char c : fragment.toCharArray()) {
if (c == '(')
cc++;
if (c == ')')
cc--;
if (cc < 0)
return false;
}
return (cc == 0);
}
public static File CreateTempResourceFile(String res_name) throws Exception {
URL u = (Utils.class.getResource("/files/" + res_name));
InputStream i = u.openStream();
@@ -477,7 +228,7 @@ public class Utils {
Log.Writeln_("Имя файла не может быть пустым");
res = false;
}
if (ContainsCyrillic(name)) {
if (CommonUtils.ContainsCyrillic(name)) {
Log.Writeln_("Имя файла не может содержать кириллицу");
res = false;
}
@@ -491,7 +242,7 @@ public class Utils {
//идет по всем уровням файлов
public static boolean validateProjectFile(File file, TextLog Log) {
String name = file.getName();
if (ContainsCyrillic(name) || ContainsForbiddenName(name)) {
if (CommonUtils.ContainsCyrillic(name) || ContainsForbiddenName(name)) {
Log.Writeln_(file.getAbsolutePath());
return false;
}
@@ -567,11 +318,11 @@ public class Utils {
} else return;
if (file.exists()) {
attempts++;
System.out.println("файл " + Brackets(file.getAbsolutePath()) + " занят");
System.out.println("файл " + CommonUtils.Brackets(file.getAbsolutePath()) + " занят");
Thread.sleep(2000);
} else return;
}
throw new PassException("Не удалось удалить файл " + Brackets(file.getAbsolutePath()) + " за " + attempts + " попыток");
throw new PassException("Не удалось удалить файл " + CommonUtils.Brackets(file.getAbsolutePath()) + " за " + attempts + " попыток");
}
public static void GetVertices(float R, float r, float x0, float y0, int n, float phi) {
boolean inner = false;
@@ -625,11 +376,11 @@ public class Utils {
} else return;
if (file.exists()) {
attempts++;
Global.Log.Print("неудачная попытка удаления: файл " + Brackets(file.getAbsolutePath()) + " занят");
Global.Log.Print("неудачная попытка удаления: файл " + CommonUtils.Brackets(file.getAbsolutePath()) + " занят");
Thread.sleep(2000);
} else return;
}
throw new PassException("Не удалось удалить файл " + Brackets(file.getAbsolutePath()) + " за " + attempts + " попыток");
throw new PassException("Не удалось удалить файл " + CommonUtils.Brackets(file.getAbsolutePath()) + " за " + attempts + " попыток");
}
public static byte[] packFile(File src) throws Exception {
byte[] dst = Files.readAllBytes(src.toPath());
@@ -669,7 +420,7 @@ public class Utils {
if (files != null) {
for (File file : files) {
if (file.isFile()) {
String file_extension = getExtension(file);
String file_extension = CommonUtils.getExtension(file);
for (String ext : extensions) {
if (file_extension.equalsIgnoreCase(ext)) {
res.add(file);
@@ -710,7 +461,7 @@ public class Utils {
public static File createScript(File scriptDirectory, File targetDirectory, String name, String scriptText) throws Exception {
//->
File scriptFile = Paths.get(scriptDirectory.getAbsolutePath(), name + (Global.isWindows ? ".bat" : "")).toFile();
FileUtils.write(scriptFile, "cd " + Utils.DQuotes(targetDirectory.getAbsolutePath()) + "\n" + scriptText);
FileUtils.write(scriptFile, "cd " + CommonUtils.DQuotes(targetDirectory.getAbsolutePath()) + "\n" + scriptText);
if (!scriptFile.setExecutable(true)) throw new PassException("Не удалось создать исполняемый файл для скрипта");
return scriptFile;
}
@@ -847,25 +598,6 @@ public class Utils {
}
return res;
}
public static Vector<String> unpackStrings(String string, boolean brackets) {
Vector<String> res = new Vector<>();
if (string.isEmpty())
res.add(brackets ? "[]" : "");
else {
StringBuilder line = new StringBuilder();
for (char c : string.toCharArray()) {
if (c == '\n') {
res.add(brackets ? Utils.Brackets(line.toString()) : line.toString());
line = new StringBuilder();
} else
line.append(c);
}
}
return res;
}
public static Vector<String> unpackStrings(String string) {
return unpackStrings(string, false);
}
public static boolean isTimeout(long startDate, long maxtime_sec) {
Date now = new Date();
long delta = (now.getTime() - startDate) / 1000;
@@ -914,7 +646,7 @@ public class Utils {
}
protected static boolean isSource(File file) {
if (file.isFile()) {
String extension = getExtension(file).toLowerCase();
String extension = CommonUtils.getExtension(file).toLowerCase();
switch (extension) {
case "f":
case "fdv":
@@ -946,7 +678,7 @@ public class Utils {
}
}
if (sources.isEmpty()) {
if (question) return UI.Question("Папка " + Brackets(folder.getName()) + "\n" +
if (question) return UI.Question("Папка " + CommonUtils.Brackets(folder.getName()) + "\n" +
"не содержит ни одного файла, распознанного как поддерживаемый код\n" +
"Всё равно открыть её как проект");
else return false;
@@ -955,8 +687,8 @@ public class Utils {
public static void Kill(String PID, boolean force) {
if (!PID.isEmpty()) {
String killCommand =
force ? Global.isWindows ? "taskkill /PID " + Utils.DQuotes(PID) + " /F /T" : "kill -9 " + Utils.DQuotes(PID) :
Global.isWindows ? "taskkill /PID " + Utils.DQuotes(PID) : "kill -2 " + Utils.DQuotes(PID);
force ? Global.isWindows ? "taskkill /PID " + CommonUtils.DQuotes(PID) + " /F /T" : "kill -9 " + CommonUtils.DQuotes(PID) :
Global.isWindows ? "taskkill /PID " + CommonUtils.DQuotes(PID) : "kill -2 " + CommonUtils.DQuotes(PID);
System.out.println(killCommand);
try {
Process killer = Utils.startScript(Global.TempDirectory, Global.TempDirectory, "killer", killCommand);
@@ -971,12 +703,12 @@ public class Utils {
if (name.isEmpty())
return false;
char[] letters = name.toCharArray();
if (!(isEnglishLetter(letters[0]) || letters[0] == '_')) {
if (!(CommonUtils.isEnglishLetter(letters[0]) || letters[0] == '_')) {
return false;
}
//---
for (int i = 1; i < letters.length; ++i) {
if (!(isEnglishLetter(letters[i]) || letters[i] == '_' || Utils.isDigit(String.valueOf(letters[i])))) {
if (!(CommonUtils.isEnglishLetter(letters[i]) || letters[i] == '_' || CommonUtils.isDigit(String.valueOf(letters[i])))) {
return false;
}
}