/
smwwiki
/
OneScript_fork
Обзор
Документация
Войти
/
smwwiki
/
OneScript_fork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/OneScript.Core/Sources/FileCodeSource.cs
68 строк
2 KB
EvilBeaver
Поправил мелкие замечания сонара
24 апр 2024, 13:50
24 апр 2024, 13:50
ebfc286
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System.IO; using System.Text; using OneScript.Commons; using System; namespace OneScript.Sources { public class FileCodeSource : ICodeSource { private readonly string _path; private readonly Encoding _noBomEncoding; public FileCodeSource(string path, Encoding defaultEncoding) { _path = Path.GetFullPath(path); _noBomEncoding = defaultEncoding; } public FileCodeSource(string path) { _path = path; _noBomEncoding = Encoding.UTF8; } public string GetSourceCode() { using (var fStream = new FileStream(_path, FileMode.Open, FileAccess.Read)) { var buf = new byte[2]; fStream.Read(buf, 0, 2); Encoding enc; var skipShebang = false; if (IsLinuxScript(buf)) { enc = Encoding.UTF8; // скрипты с shebang считать в формате UTF-8 skipShebang = true; } else { fStream.Position = 0; enc = FileOpener.AssumeEncoding(fStream, _noBomEncoding); } using (var reader = new StreamReader(fStream, enc)) { if (skipShebang) reader.ReadLine(); return reader.ReadToEnd(); } } } private static bool IsLinuxScript(byte[] buf) { return buf[0] == '#' && buf[1] == '!'; } public string Location => string.Concat(_path[0].ToString().ToUpperInvariant(), _path.AsSpan(1)); } }