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 Writer(RelationSchemaLoaderResult rslr, String Title, String CopyrightText) { this.Schema = rslr.Schema; this.Title = Title; this.CopyrightText = CopyrightText; TypeInfoDict = new Dictionary <String, TypeInfo>(StringComparer.OrdinalIgnoreCase); String Root = ""; if (rslr.Positions.Count > 0) { Func <String, String, String> GetCommonHead = (a, b) => { var lc = new List <Char>(); var k = 0; while (true) { if (k >= a.Length || k >= b.Length) { break; } if (a[k] != b[k]) { break; } lc.Add(a[k]); k += 1; } return(new String(lc.ToArray())); }; Root = rslr.Positions.Select(p => FileNameHandling.GetDirectoryPathWithTailingSeparator(FileNameHandling.GetFileDirectory(p.Value.Text.Path))).Aggregate((a, b) => GetCommonHead(a, b)); if (Root != FileNameHandling.GetDirectoryPathWithTailingSeparator(Root)) { Root = FileNameHandling.GetFileDirectory(Root); } } var Map = Schema.GetMap().ToDictionary(p => p.Key, p => p.Value); foreach (var t in Schema.Types) { if (t.OnQueryList) { continue; } var Name = t.Name(); var Path = "Default.tree"; if (rslr.Positions.ContainsKey(t)) { Path = FileNameHandling.GetRelativePath(rslr.Positions[t].Text.Path, Root); } var PathWithoutExt = FileNameHandling.GetPath(FileNameHandling.GetFileDirectory(Path), FileNameHandling.GetMainFileName(Path)); var DocFilePath = PathWithoutExt.Replace(@"\", @"_").Replace(@"/", @"_").Replace(@".", "_").Replace(@":", @"_").Replace(@"#", @"_") + @".html"; var tli = new TypeInfo { Def = Map[Name], FriendlyPath = PathWithoutExt.Replace(@"\", @"/"), DocFilePath = DocFilePath, DocPath = String.Format("{0}#{1}", DocFilePath, Name) }; TypeInfoDict.Add(Name, tli); } }
public static void WriteLog(String LogDir, SessionLogEntry Entry) { var LocalTime = Entry.Time.ToLocalTime(); var Dir = FileNameHandling.GetPath(LogDir, LocalTime.Date.ToString("yyyyMMdd")); if (Dir != "" && !Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } var Path = FileNameHandling.GetPath(Dir, String.Format("{0}-{1}.log", Entry.RemoteEndPoint.ToString().Replace(".", "_").Replace(":", "_"), Entry.Token)); var e = Encoding.UTF8; var Lines = GetLines(Entry); if (File.Exists(Path)) { e = Txt.GetEncoding(Path, e); } using (var r = new StreamWriter(Path, true, e)) { foreach (var Line in Lines) { r.WriteLine(Line); } } }
public static void ObjectSchemaToJavaBinaryCode(String JavaCodeDirPath, String PackageName) { var ObjectSchema = GetObjectSchema(); var CompiledFiles = ObjectSchema.CompileToJavaBinary(PackageName); foreach (var f in CompiledFiles) { var FilePath = FileNameHandling.GetPath(JavaCodeDirPath, f.Key.Replace('/', Path.DirectorySeparatorChar)); var Compiled = f.Value; if (File.Exists(FilePath)) { var Original = Txt.ReadFile(FilePath); if (String.Equals(Compiled, Original, StringComparison.Ordinal)) { continue; } } var Dir = FileNameHandling.GetFileDirectory(FilePath); if (Dir != "" && !Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } Txt.WriteFile(FilePath, Compiled); } }
public static void ObjectSchemaToXhtml(String XhtmlDir, String Title, String CopyrightText) { var oslr = GetObjectSchemaLoaderResult(); var CompiledFiles = oslr.CompileToXhtml(Title, CopyrightText); foreach (var f in CompiledFiles) { var Compiled = f.Content; var Path = FileNameHandling.GetPath(XhtmlDir, f.Path); if (File.Exists(Path)) { var Original = Txt.ReadFile(Path); if (String.Equals(Compiled, Original, StringComparison.Ordinal)) { continue; } } var Dir = FileNameHandling.GetFileDirectory(Path); if (Dir != "" && !Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } Txt.WriteFile(Path, TextEncoding.UTF8, Compiled); } }
private static Func <String, IDbConnection> GetConnectionFactoryMySQL() { var Path = FileNameHandling.GetPath(FileNameHandling.GetFileDirectory(Assembly.GetEntryAssembly().Location), "MySql.Data.dll"); var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path)); var t = asm.GetType("MySql.Data.MySqlClient.MySqlConnection"); return(ConnectionString => (IDbConnection)Activator.CreateInstance(t, ConnectionString)); }
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 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 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(); }
public void SaveAll() { lock (SaveLockee) { var InstancesSnapshot = new List <Lazy <InstanceInfo> >(); lock (Instances) { InstancesSnapshot = Instances.Values.ToList(); } foreach (var p in InstancesSnapshot) { var i = p.Value; DateTime Time; Version Version; using (var da = new KrustallosDataAccess(i.Instance, i.Data, IsolationLevel.Snapshot, (ITransactionLock)(null))) { Version = da.GetReaderVersion(); Time = DateTime.UtcNow; var Dir = FileNameHandling.GetFileDirectory(i.FilePath); if (!Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } using (var s = new ReliableFileWriteStream(i.FilePath)) { da.Save(i.sbs, i.bs, s, i.Schema); } } if (i.BackupDir != "") { if (!Directory.Exists(i.BackupDir)) { Directory.CreateDirectory(i.BackupDir); } var FileName = FileNameHandling.GetFileName(i.FilePath); var FilePath = FileNameHandling.GetPath(i.BackupDir, FileNameHandling.GetMainFileName(FileName) + Time.ToString("yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture) + "-" + Version.ToString() + "." + FileNameHandling.GetExtendedFileName(FileName)); if (!File.Exists(FilePath)) { File.Copy(i.FilePath, FilePath + ".new", true); File.Move(FilePath + ".new", FilePath); } if (i.MaxBackupCount.OnSome) { var MaxBackupCount = i.MaxBackupCount.Value; var FilesToDelete = new List <String>(); var FilePaths = new SortedSet <String>(StringComparer.OrdinalIgnoreCase); foreach (var f in Directory.EnumerateFiles(i.BackupDir, "*", SearchOption.TopDirectoryOnly)) { FilePaths.Add(f); while (FilePaths.Count > MaxBackupCount) { var First = FilePaths.First(); FilePaths.Remove(First); FilesToDelete.Add(First); } } foreach (var f in FilesToDelete) { if (File.Exists(f)) { try { File.Delete(f); } catch { } } } } } } } }