public static void GenerateCSharp(String InputDirectory, String OutputDirectory) { var Files = new Dictionary <String, FileParserResult>(); foreach (var FilePath in Directory.EnumerateFiles(InputDirectory, "*.tree", SearchOption.AllDirectories)) { var AbsolutePath = FileNameHandling.GetAbsolutePath(FilePath, System.Environment.CurrentDirectory); var FileContent = Txt.ReadFile(FilePath); var Text = TokenParser.BuildText(FileContent, AbsolutePath); var Result = FileParser.ParseFile(Text); Files.Add(AbsolutePath, Result); } foreach (var p in Files) { var RelativePath = FileNameHandling.GetRelativePath(p.Key, FileNameHandling.GetAbsolutePath(InputDirectory, System.Environment.CurrentDirectory)); var FileName = FileNameHandling.GetFileName(p.Key); var ecsg = new EmbeddedCSharpGenerator(); var Content = String.Join("\r\n", ecsg.Generate(p.Value.File).Select(Line => Line.TrimEnd(' '))); var OutputPath = FileNameHandling.GetPath(FileNameHandling.GetPath(OutputDirectory, FileNameHandling.GetFileDirectory(RelativePath)), FileNameHandling.GetMainFileName(FileName) + ".cs"); var OutputDir = FileNameHandling.GetFileDirectory(OutputPath); if (!Directory.Exists(OutputDir)) { Directory.CreateDirectory(OutputDir); } WriteToFileIfChanged(OutputPath, Content); } }
public static void DumpSyntaxResult(String InputDirectory, String OutputDirectory) { var Files = new Dictionary <String, FileParserResult>(); foreach (var FilePath in Directory.EnumerateFiles(InputDirectory, "*.tree", SearchOption.AllDirectories)) { var AbsolutePath = FileNameHandling.GetAbsolutePath(FilePath, System.Environment.CurrentDirectory); var FileContent = Txt.ReadFile(FilePath); var Text = TokenParser.BuildText(FileContent, AbsolutePath); var Result = FileParser.ParseFile(Text); Files.Add(AbsolutePath, Result); } //var arr = AmbiguousRemover.Reduce(Files, new List<String> { }, new TypeProvider()); //if (arr.UnresolvableAmbiguousOrErrors.Count > 0) //{ // var l = new List<String> { }; // foreach (var p in arr.UnresolvableAmbiguousOrErrors) // { // var ErrorMessage = p.Key; // var ErrorRange = p.Value; // l.Add(p.Key + ": " + ErrorRange.Text.Path + (ErrorRange.Range.OnSome ? ": " + ErrorRange.Range.Value.ToString() : "")); // } // var OutputPath = FileNameHandling.GetPath(OutputDirectory, "Error.txt"); // var OutputDir = FileNameHandling.GetFileDirectory(OutputPath); // if (!Directory.Exists(OutputDir)) { Directory.CreateDirectory(OutputDir); } // Txt.WriteFile(OutputPath, String.Join("\r\n", l) + "\r\n"); // return; //} //Files = arr.Files; foreach (var p in Files) { var RelativePath = FileNameHandling.GetRelativePath(p.Key, FileNameHandling.GetAbsolutePath(InputDirectory, System.Environment.CurrentDirectory)); var FileName = FileNameHandling.GetFileName(p.Key); var fd = new FileDumper(); var Comment = "==========================================================================" + "\r\n" + "\r\n" + " SourceFile: " + FileName + "\r\n" + "\r\n" + "=========================================================================="; var f = fd.Dump(p.Value, Comment); var OutputPath = FileNameHandling.GetPath(FileNameHandling.GetPath(OutputDirectory, FileNameHandling.GetFileDirectory(RelativePath)), FileNameHandling.GetMainFileName(FileName) + ".syn.tree"); var OutputDir = FileNameHandling.GetFileDirectory(OutputPath); if (!Directory.Exists(OutputDir)) { Directory.CreateDirectory(OutputDir); } TreeFile.WriteRaw(OutputPath, f); } }
public ReliableFileWriteStream(String FilePath) { AbsoluteFilePath = FileNameHandling.GetAbsolutePath(FilePath, System.Environment.CurrentDirectory); if (File.Exists(AbsoluteFilePath + ".new")) { throw new InvalidOperationException("FileExists: {0}".Formats(AbsoluteFilePath + ".new")); } if (File.Exists(AbsoluteFilePath)) { if (File.Exists(AbsoluteFilePath + ".old")) { throw new InvalidOperationException("FileExists: {0}".Formats(AbsoluteFilePath + ".old")); } } FileStream = Streams.CreateResizable(AbsoluteFilePath + ".new"); }
public static void ExportCollection(String ConnectionString, String DataDir, List <String> ExportEntityNames) { var bs = Niveum.ObjectSchema.BinarySerializerWithString.Create(); var Pair = LoadData(ConnectionString, bs); var s = Pair.Key; var Value = Pair.Value; var Entities = s.Types.Where(t => t.OnEntity).Select(t => t.Entity).ToList(); HashSet <String> ExportEntityNameSet = null; if (ExportEntityNames.Count != 0) { ExportEntityNameSet = new HashSet <String>(ExportEntityNames.Distinct(StringComparer.OrdinalIgnoreCase), StringComparer.OrdinalIgnoreCase); } var Dir = FileNameHandling.GetAbsolutePath(DataDir, Environment.CurrentDirectory); if (Dir != "" && !Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } var rvts = new RelationValueTreeSerializer(s); foreach (var p in Entities.ZipStrict(Value.Tables, (a, b) => new KeyValuePair <EntityDef, TableVal>(a, b))) { var e = p.Key; var tv = p.Value; if ((ExportEntityNameSet == null) || (ExportEntityNameSet.Contains(e.Name))) { var l = rvts.WriteTable(e, tv); var t = RelationValueSyntaxTreeBuilder.BuildTable(e, l); var TreeFilePath = FileNameHandling.GetPath(DataDir, e.Name + ".tree"); TreeFile.WriteRaw(TreeFilePath, new Syntax.Forest { MultiNodesList = new List <Syntax.MultiNodes> { t } }); } } }
public InstanceInfo(String ConnectionString, IsolationLevel IsolationLevel, int NumPartition) { var Fragments = ConnectionString.Split(';'); foreach (var Fragment in Fragments) { var Pairs = Fragment.Split('='); if (Pairs.Length >= 2) { if (Pairs[0].Equals("File", StringComparison.OrdinalIgnoreCase)) { this.FilePath = FileNameHandling.GetAbsolutePath(Pairs[1], System.Environment.CurrentDirectory); } else if (Pairs[0].Equals("BackupDir", StringComparison.OrdinalIgnoreCase)) { this.BackupDir = FileNameHandling.GetAbsolutePath(Pairs[1], System.Environment.CurrentDirectory); } else if (Pairs[0].Equals("MaxBackupCount", StringComparison.OrdinalIgnoreCase)) { this.MaxBackupCount = int.Parse(Pairs[1]); } else if (Pairs[0].Equals("BackupInterval", StringComparison.OrdinalIgnoreCase)) { this.BackupInterval = int.Parse(Pairs[1]); } } } if (this.FilePath == "") { throw new InvalidOperationException("InvalidConnectionString: {0}".Formats(ConnectionString)); } Data = new KrustallosData(NumPartition); sbs = Niveum.ObjectSchema.BinarySerializerWithString.Create(); bs = KrustallosSerializer.Create(); using (var da = new KrustallosDataAccess(Instance, Data, IsolationLevel, (ITransactionLock)(null))) { Schema = da.Load(sbs, bs, FilePath); } }
public void Start() { var Root = FileNameHandling.GetDirectoryPathWithTailingSeparator(FileNameHandling.GetAbsolutePath(PhysicalPath, Environment.CurrentDirectory)); var Indices = this.Indices; var MimeTypes = new Dictionary <String, String>(StringComparer.OrdinalIgnoreCase); MimeTypes.Add("htm", "text/html"); MimeTypes.Add("html", "text/html"); MimeTypes.Add("shtml", "text/html"); MimeTypes.Add("xml", "text/xml"); MimeTypes.Add("css", "text/css"); MimeTypes.Add("js", "application/x-javascript"); MimeTypes.Add("txt", "text/plain"); MimeTypes.Add("gif", "image/gif"); MimeTypes.Add("jpg", "image/jpeg"); MimeTypes.Add("jpeg", "image/jpeg"); MimeTypes.Add("png", "image/png"); MimeTypes.Add("tif", "image/tiff"); MimeTypes.Add("tiff", "image/tiff"); MimeTypes.Add("ico", "image/x-icon"); MimeTypes.Add("mp3", "audio/mpeg"); MimeTypes.Add("wav", "audio/wav"); MimeTypes.Add("mid", "audio/midi"); MimeTypes.Add("midi", "audio/midi"); Func <String, String> GetMimeType = Extension => { if (MimeTypes.ContainsKey(Extension)) { return(MimeTypes[Extension]); } return("application/octet-stream"); }; Func <String, Int64, Optional <KeyValuePair <Int64, Int64> > > TryGetRange = (RangeStrs, FileLength) => { // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Int64 Start = 0; Int64 Length = FileLength; var RangeStrsTrimmed = RangeStrs.Trim(' '); if (RangeStrsTrimmed.StartsWith("bytes=")) { RangeStrsTrimmed = RangeStrsTrimmed.Substring("bytes=".Length); } else { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } var RangeStrsSplitted = RangeStrsTrimmed.Split(','); if (RangeStrsSplitted.Length != 1) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } var Parts = RangeStrsSplitted.Single().Trim(' ').Split('-').Select(p => p.Trim(' ')).ToArray(); if (Parts.Length != 2) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } if (Parts[0] == "") { // 不支持suffix-byte-range-spec = "-" suffix-length return(Optional <KeyValuePair <Int64, Int64> > .Empty); } if (!Int64.TryParse(Parts[0], out Start)) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } if ((Start < 0) || (Start > FileLength)) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } if (Parts[1] == "") { Length = FileLength - Start; } else { Int64 End; if (!Int64.TryParse(Parts[1], out End)) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } if (End < Start) { return(Optional <KeyValuePair <Int64, Int64> > .Empty); } Length = Math.Min(End + 1 - Start, FileLength - Start); } return(new KeyValuePair <Int64, Int64>(Start, Length)); }; Action <String, HttpListenerContext, IPEndPoint, Action, Action <Exception> > RequestHandler = (RelativePath, a, e, OnSuccess, OnFailure) => { if (a.Request.ContentLength64 > 0) { a.Response.StatusCode = 400; OnSuccess(); return; } if (RelativePathTranslator != null) { var oRelativePath = RelativePathTranslator(RelativePath); if (oRelativePath.OnNone) { a.Response.StatusCode = 404; OnSuccess(); return; } RelativePath = oRelativePath.Value; } var Path = FileNameHandling.GetAbsolutePath(RelativePath, Root); if ((RelativePath != "") && !Path.StartsWith(Root)) { a.Response.StatusCode = 400; OnSuccess(); return; } if (!File.Exists(Path)) { var Found = false; var UnescapedPath = Uri.UnescapeDataString(Path); if (File.Exists(UnescapedPath)) { Path = UnescapedPath; Found = true; } if (!Found) { foreach (var Index in Indices) { var IndexPath = FileNameHandling.GetPath(Path, Index); if (File.Exists(IndexPath)) { Path = IndexPath; Found = true; break; } } foreach (var Index in Indices) { var IndexPath = FileNameHandling.GetPath(UnescapedPath, Index); if (File.Exists(IndexPath)) { Path = IndexPath; Found = true; break; } } } if (!Found) { a.Response.StatusCode = 404; OnSuccess(); return; } } if (!Path.StartsWith(Root)) { a.Response.StatusCode = 400; OnSuccess(); return; } var Buffer = new Byte[512 * 1024]; using (var fs = Streams.OpenReadable(Path)) { var LastWriteTime = File.GetLastWriteTimeUtc(Path); var LastWriteTimeStr = LastWriteTime.ToString("R"); var ETag = "\"" + Times.DateTimeUtcToString(LastWriteTime) + "\""; var IfModifiedSinceStr = a.Request.Headers["If-Modified-Since"]; if ((IfModifiedSinceStr != null) && (LastWriteTimeStr == IfModifiedSinceStr)) { a.Response.StatusCode = 304; OnSuccess(); return; } var oRange = Optional <KeyValuePair <Int64, Int64> > .Empty; var IfRangeStr = a.Request.Headers["If-Range"]; if ((IfRangeStr == null) || (ETag == IfRangeStr) || (LastWriteTimeStr == IfRangeStr)) { var RangeStrs = a.Request.Headers["Range"]; if (RangeStrs != null) { oRange = TryGetRange(RangeStrs, fs.Length); } } Int64 Start; Int64 Length; if (oRange.OnSome) { var Range = oRange.Value; Start = Range.Key; Length = Range.Value; a.Response.StatusCode = 206; a.Response.Headers.Add("Content-Range", "bytes {0}-{1}/{2}".Formats(Range.Key, Range.Key + Range.Value - 1, fs.Length)); } else { Start = 0; Length = fs.Length; a.Response.StatusCode = 200; } a.Response.ContentLength64 = Length; a.Response.ContentType = GetMimeType(FileNameHandling.GetExtendedFileName(Path)); a.Response.Headers.Add("Accept-Ranges", "bytes"); a.Response.Headers.Add("Last-Modified", LastWriteTimeStr); a.Response.Headers.Add("ETag", ETag); try { using (var ros = a.Response.OutputStream) { var Count = (Length + Buffer.Length - 1) / Buffer.Length; fs.Position = Start; for (int k = 0; k < Count; k += 1) { var ChunkSize = (int)(Math.Min(Buffer.Length, Length - Buffer.Length * k)); fs.Read(Buffer, 0, ChunkSize); ros.Write(Buffer, 0, ChunkSize); } } } catch (System.Net.HttpListenerException) { } } OnSuccess(); }; Inner = new ExternalHttpServer(ServerContext, RequestHandler, QueueUserWorkItem, 8 * 1024); Inner.Bindings = Bindings; Inner.SessionIdleTimeout = SessionIdleTimeout; Inner.MaxConnections = MaxConnections; Inner.MaxConnectionsPerIP = MaxConnectionsPerIP; Inner.MaxBadCommands = MaxBadCommands; Inner.MaxUnauthenticatedPerIP = MaxUnauthenticatedPerIP; Inner.ServiceVirtualPath = ServiceVirtualPath; Inner.Start(); }