Пример #1
0
    static int PrependLineNumber(ConsoleService c, string cmdName, string str)
    {
        ConsoleParam[] args =
        {
            new ConsoleParam("[srcFile]", ConsoleService.Prompt, "Src File Path: ",  ConsoleService.EvalNotEmpty, null),
            new ConsoleParam("DEST",      ConsoleService.Prompt, "Dest File Path: ", ConsoleService.EvalNotEmpty, null),
        };

        ConsoleParamValueList vl = c.ParseCommandList(cmdName, str, args);

        string src  = vl.DefaultParam.StrValue;
        string dest = vl["DEST"].StrValue;

        if (src._IsSamei(dest))
        {
            Con.WriteError("Source is Dest.");
            return(1);
        }

        var body = Lfs.ReadStringFromFile(src);

        string[] lines = body._GetLines(singleLineAtLeast: true);

        int keta = lines.Length.ToString().Length;

        StringWriter w = new StringWriter();

        for (int i = 0; i < lines.Length; i++)
        {
            string lineStr = (i + 1).ToString("D" + keta);
            string tmp     = lineStr + ": " + lines[i];

            w.WriteLine(tmp);
        }

        Lfs.WriteStringToFile(dest, w.ToString(), writeBom: true, flags: FileFlags.AutoCreateDirectory);

        return(0);
    }
Пример #2
0
    protected override void InitImpl(object?param = null)
    {
        // sensors コマンドが利用可能かどうか確認
        if (EasyExec.ExecAsync(Consts.LinuxCommands.Sensors, "-u")._TryGetResult() != default)
        {
            IsSensorsCommandOk = true;
        }

        // /sys/class/thermal/ から取得可能な値一覧を列挙
        FileSystemEntity[]? dirList = null;
        try
        {
            dirList = Lfs.EnumDirectory(Consts.LinuxPaths.SysThermal);
        }
        catch { }

        if (dirList != null)
        {
            foreach (var dir in dirList)
            {
                string fileName = Lfs.PathParser.Combine(dir.FullPath, "temp");

                if (Lfs.IsFileExists(fileName))
                {
                    try
                    {
                        Lfs.ReadStringFromFile(fileName);

                        ThermalFiles.Add(dir.Name, fileName);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
Пример #3
0
        static int MergeResourceHeader(ConsoleService c, string cmdName, string str)
        {
            string baseFile   = @"C:\git\IPA-DNP-DeskVPN\src\PenCore\resource.h";
            string targetFile = @"C:\sec\Desk\current\Desk\DeskVPN\PenCore\resource.h";
            string destFile   = @"c:\tmp\200404\resource.h";
            int    minId      = 2500;

            var baseDict   = DevTools.ParseHeaderConstants(Lfs.ReadStringFromFile(baseFile));
            var targetDict = DevTools.ParseHeaderConstants(Lfs.ReadStringFromFile(targetFile));

            KeyValueList <string, int> adding = new KeyValueList <string, int>();

            // 利用可能な ID の最小値
            int newId = Math.Max(baseDict.Values.Where(x => x < 40000).Max(), minId);

            foreach (var kv in targetDict.OrderBy(x => x.Value))
            {
                if (baseDict.ContainsKey(kv.Key) == false)
                {
                    adding.Add(kv.Key, ++newId);
                }
            }

            // 結果を出力
            StringWriter w = new StringWriter();

            foreach (var kv in adding)
            {
                int paddingCount = Math.Max(31 - kv.Key.Length, 0);

                w.WriteLine($"#define {kv.Key}{Str.MakeCharArray(' ', paddingCount)} {kv.Value}");
            }

            Lfs.WriteStringToFile(destFile, w.ToString(), FileFlags.AutoCreateDirectory);

            return(0);
        }
Пример #4
0
        static int ConvertTextFiles(ConsoleService c, string cmdName, string str)
        {
            ConsoleParam[] args =
            {
                new ConsoleParam("[srcdir]", ConsoleService.Prompt, "Input source directory: ",      ConsoleService.EvalNotEmpty, null),
                new ConsoleParam("dst",      ConsoleService.Prompt, "Input destination directory: ", ConsoleService.EvalNotEmpty, null),
                new ConsoleParam("encode"),
                new ConsoleParam("bom"),
                new ConsoleParam("newline"),
            };

            ConsoleParamValueList vl = c.ParseCommandList(cmdName, str, args);

            string srcdir = vl.DefaultParam.StrValue;

            string dstdir = vl["dst"].StrValue;

            string encode = vl["encode"].StrValue._FilledOrDefault("utf8");

            bool bom = vl["bom"].BoolValue;

            string newline = vl["newline"].StrValue._FilledOrDefault("crlf");

            Encoding?encoding = null;

            switch (encode.ToLower())
            {
            case "sjis":
                encoding = Str.ShiftJisEncoding;
                break;

            case "euc":
                encoding = Str.EucJpEncoding;
                break;

            case "utf8":
                encoding = Str.Utf8Encoding;
                break;

            default:
                throw new CoresException("encode param is invalid.");
            }

            CrlfStyle crlfStyle = CrlfStyle.CrLf;

            switch (newline.ToLower())
            {
            case "crlf":
                crlfStyle = CrlfStyle.CrLf;
                break;

            case "lf":
                crlfStyle = CrlfStyle.Lf;
                break;

            case "platform":
                crlfStyle = CrlfStyle.LocalPlatform;
                break;

            default:
                throw new CoresException("newline param is invalid.");
            }

            var srcFileList = Lfs.EnumDirectory(srcdir, true);

            foreach (var srcFile in srcFileList)
            {
                if (srcFile.IsFile)
                {
                    string relativeFileName = Lfs.PathParser.GetRelativeFileName(srcFile.FullPath, srcdir);

                    string destFileName = Lfs.PathParser.Combine(dstdir, relativeFileName);

                    string body = Lfs.ReadStringFromFile(srcFile.FullPath);

                    body = Str.NormalizeCrlf(body, crlfStyle);

                    Con.WriteLine(relativeFileName);

                    Lfs.WriteStringToFile(destFileName, body, FileFlags.AutoCreateDirectory, encoding: encoding, writeBom: bom);
                }
            }

            return(0);
        }
Пример #5
0
        static int GoogleMapsDirectionCross(ConsoleService c, string cmdName, string str)
        {
            ConsoleParam[] args =
            {
                new ConsoleParam("[dir]", ConsoleService.Prompt, "Directory path: ", ConsoleService.EvalNotEmpty, null),
            };

            ConsoleParamValueList vl = c.ParseCommandList(cmdName, str, args);

            string dir = vl.DefaultParam.StrValue;

            string apiKey = Lfs.ReadStringFromFile(dir._CombinePath("ApiKey.txt"), oneLine: true);

            string srcListText  = Lfs.ReadStringFromFile(dir._CombinePath("Source.txt"));
            string destListText = Lfs.ReadStringFromFile(dir._CombinePath("Destination.txt"));

            string[] srcList  = srcListText._GetLines(removeEmpty: true);
            string[] destList = destListText._GetLines(removeEmpty: true);

            using var googleMapsApi = new GoogleMapsApi(new GoogleMapsApiSettings(apiKey: apiKey));

            DateTimeOffset departure = Util.GetStartOfDay(DateTime.Now.AddDays(2))._AsDateTimeOffset(isLocalTime: true);

            List <DirectionCrossResults> csv = new List <DirectionCrossResults>();

            foreach (string src in srcList)
            {
                foreach (string dest in destList)
                {
                    Console.WriteLine($"「{src}」 → 「{dest}」 ...");

                    DirectionCrossResults r = new DirectionCrossResults();

                    r.Start = src;
                    r.End   = dest;

                    try
                    {
                        var result = googleMapsApi.CalcDurationAsync(src, dest, departure)._GetResult();

                        if (result.IsError == false)
                        {
                            r.StartAddress = result.StartAddress;
                            r.EndAddress   = result.EndAddress;
                            r.Error        = "";
                            r.Duration     = result.Duration;
                            r.DistanceKm   = result.DistanceKm;
                            r.RouteSummary = result.RouteSummary;

                            $"  {r.Duration} - {r.DistanceKm} km ({r.RouteSummary})"._Print();
                        }
                        else
                        {
                            r.Error = result.ErrorString;
                            r.Error._Print();
                        }
                    }
                    catch (Exception ex)
                    {
                        ex._Debug();
                        r.Error = ex.Message;
                    }

                    csv.Add(r);
                }
            }

            string csvText = csv._ObjectArrayToCsv(withHeader: true);

            Lfs.WriteStringToFile(dir._CombinePath("Result.csv"), csvText, writeBom: true);

            return(0);
        }