internal void Parse(string shpFile) { using (var sr = new StreamReader(shpFile)) { var remarks = string.Empty; var myType = string.Empty; while (sr.Peek() >= 0) { var line = sr.ReadLine(); if (line != null) { if (line.Trim().Length == 0) { continue; } if (line.StartsWith(";")) { remarks += line + "\n"; continue; } // *shapenumber,defbytes,shapename // *0,4,font-name // *UNIFONT,6,font-name // *BIGFONT nchars,nranges,b1,e1,b2,e2,... if (line.StartsWith("*")) { if (myType.Length == 0) { if (line.StartsWith("*0,4")) { myType = "FONT"; _shxFile.ShxType = myType; SetFontParam(sr, line); } else if (line.StartsWith("*UNIFONT")) { myType = "UNIFONT"; _shxFile.ShxType = myType; SetFontParam(sr, line); } else if (line.StartsWith("*BIGFONT")) { myType = "BIGFONT"; _shxFile.ShxType = myType; _shxFile.HeaderBigFont = line; line = sr.ReadLine(); SetFontParam(sr, line); } else { myType = "SYMBOL"; _shxFile.ShxType = myType; } _ctx.ChangeTracker.DetectChanges(); _ctx.SaveChanges(); } } } } } }
private static void ProcessShxFiles(string path) { var files = Directory.GetFiles(path, "*.shx", SearchOption.TopDirectoryOnly); if (files.Length > 0) { using (var ctx = new FontsDbContext(Properties.Settings.Default.ShxConnStrings)) { ctx.Configuration.AutoDetectChangesEnabled = true; var machineName = Environment.MachineName; var lds = ctx.LoadDirectories.Where(d => d.FullPath.Equals(path, StringComparison.InvariantCultureIgnoreCase) && d.ComputerName.Equals(machineName, StringComparison.CurrentCultureIgnoreCase)).ToList(); LoadDirectory loadDir; if (lds.Count == 0) { loadDir = new LoadDirectory { FullPath = path, ComputerName = machineName }; ctx.LoadDirectories.Add(loadDir); ctx.SaveChanges(); } else { loadDir = lds[0]; } var tmpDir = CreateUniqueTempDirectory(); foreach (var file in files) { var fi = new FileInfo(file); var shpFile = Path.ChangeExtension(Path.Combine(tmpDir, fi.Name), ".SHP"); try { if (File.Exists(shpFile)) { File.Delete(shpFile); } var shxFile = new ShxFontFile { ShxFileName = fi.Name, ShxFileSize = (int)fi.Length, ShxFileDate = fi.CreationTime, Crc32 = Crc32OfFile(file), Remarks = "Before Read" }; loadDir.ShxFontFiles.Add(shxFile); ctx.SaveChanges(); var exitCode = -1; using (var proc = new Process()) { proc.StartInfo.FileName = Properties.Settings.Default.DumpShxProgram; proc.StartInfo.Arguments = "-o \"" + shpFile + "\" \"" + fi + "\""; proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = true; proc.Start(); proc.WaitForExit(1000); exitCode = proc.ExitCode; } if (exitCode == 0 && File.Exists(shpFile)) { Console.WriteLine($"\t{shpFile}"); var shpFontFile = new ShpFontFile(ctx, shxFile); shpFontFile.Parse(shpFile); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } }