public bool WriteFile() { try { var utc8nobom = new UTF8Encoding(false); System.Diagnostics.Trace.WriteLine("File " + FilePath + " written in " + FileEncoding.BodyName + " is utf8 no bom " + Equals(utc8nobom, FileEncoding)); using (StreamWriter sr = new StreamWriter(FilePath, false, FileEncoding)) { string rootpath = Path.GetDirectoryName(FilePath) + "\\"; sr.WriteLine("ACTIONFILE V4"); sr.WriteLine(); sr.WriteLine("ENABLED " + Enabled); sr.WriteLine(); if (InstallationVariables.Count > 0) { sr.WriteLine(InstallationVariables.ToString(prefix: "INSTALL ", separ: Environment.NewLine)); sr.WriteLine(); } if (FileEventList.Count > 0) { string currenteventgroup = null; for (int i = 0; i < FileEventList.Count; i++) { string evgroup = FileEventList[i].GroupName; if (evgroup != currenteventgroup) { if (currenteventgroup != null) { if (FileEventList[i].Tag != null) { sr.WriteLine(Environment.NewLine + (string)FileEventList[i].Tag); } else { sr.WriteLine(""); } } else { if (FileEventList[i].Tag != null) { sr.WriteLine((string)FileEventList[i].Tag); } else { sr.WriteLine(""); } } currenteventgroup = evgroup; sr.WriteLine("GROUP " + currenteventgroup + Environment.NewLine); } else { if (FileEventList[i].Tag != null) { sr.Write((string)FileEventList[i].Tag); } } sr.WriteLine("EVENT " + FileEventList[i].ToString(includeaction: true)); } sr.WriteLine(); } if (ProgramList.Count > 0) { for (int i = 0; i < ProgramList.Count; i++) { ActionProgram f = ProgramList.Get(i); List <string> elv = new List <string>() { "// Events: " }; int totonline = 0; for (int ic = 0; ic < FileEventList.Count; ic++) { Condition c = FileEventList[ic]; if (c.Action.Equals(f.Name)) { string e = c.EventName; if (!c.IsAlwaysTrue()) { e += "?(" + c.ToString() + ")"; } if (c.ActionVars.Count > 0) { e += "(" + c.ActionVars.ToString() + ")"; } if (elv.Last().Length > 120) { elv.Add("// Events: "); totonline = 0; } elv[elv.Count - 1] += (totonline > 0 ? ", " : "") + e; totonline++; } } if (elv[0] == "// Events: ") { elv[0] += "None"; } if (f.HeaderText.HasChars()) { string[] lines = f.HeaderText.Split(Environment.NewLine); bool doneevents = false; foreach (var l in lines) { if (l.StartsWith("// Events:")) { if (!doneevents) { sr.WriteLine(string.Join(Environment.NewLine, elv)); doneevents = true; } } else { sr.WriteLine(l); } } } else { sr.WriteLine(automarker); sr.WriteLine("// " + f.Name); sr.WriteLine(string.Join(Environment.NewLine, elv)); sr.WriteLine(automarker); } if (f.StoredInSubFile != null) { string full = f.StoredInSubFile; // try and simplify the path here.. if (full.StartsWith(rootpath)) { full = full.Substring(rootpath.Length); } sr.WriteLine("INCLUDE " + full); f.WriteFile(f.StoredInSubFile); } else { f.Write(sr); } sr.WriteLine(); } } sr.Close(); } return(true); } catch { } return(false); }
public string ReadFile(string filename, out bool readenable) // string, empty if no errors { readenable = false; Clear(filename, Path.GetFileNameWithoutExtension(filename)); try { var utc8nobom = new UTF8Encoding(false); // give it the default UTF8 no BOM encoding, it will detect BOM or UCS-2 automatically string currenteventgroup = null; using (StreamReader sr = new StreamReader(filename, utc8nobom)) // read directly from file.. presume UTF8 no bom { WriteTimeUTC = File.GetLastWriteTimeUtc(filename); string firstline = sr.ReadLine(); string precomments = null; bool inautocomments = false; FileEncoding = sr.CurrentEncoding; //System.Diagnostics.Trace.WriteLine("File " + filename + " is in " + fileencoding.BodyName + " is utc8nobom? " + Equals(utc8nobom, fileencoding)); if (firstline == "{") { return("JSON Not supported" + Environment.NewLine); } else if (firstline == "ACTIONFILE V4") { string line; int lineno = 1; // on actionFILE V4 while ((line = sr.ReadLine()) != null) { lineno++; // on line of read.. line = line.Trim(); if (line.StartsWith("ENABLED", StringComparison.InvariantCultureIgnoreCase)) { line = line.Substring(7).Trim().ToLowerInvariant(); if (line == "true") { Enabled = true; } else if (line == "false") { Enabled = false; } else { return(Name + " " + lineno + " ENABLED is neither true or false" + Environment.NewLine); } readenable = true; } else if (line.StartsWith("PROGRAM", StringComparison.InvariantCultureIgnoreCase)) { ActionProgram ap = new ActionProgram(null, null, precomments); string err = ap.Read(sr, ref lineno, line.Substring(7).Trim()); // Read it, prename it.. if (err.Length > 0) { return(Name + " " + err); } ProgramList.Add(ap); precomments = null; inautocomments = false; } else if (line.StartsWith("INCLUDE", StringComparison.InvariantCultureIgnoreCase)) { string incfilename = line.Substring(7).Trim(); if (!incfilename.Contains("/") && !incfilename.Contains("\\")) { incfilename = Path.Combine(Path.GetDirectoryName(filename), incfilename); } ActionProgram ap = new ActionProgram("", incfilename); // NAME will be filled in by PROGRAM statement in file string err = ap.ReadFile(incfilename); if (err.Length > 0) { return(Name + " " + err); } ProgramList.Add(ap); } else if (line.StartsWith("EVENT", StringComparison.InvariantCultureIgnoreCase)) { Condition c = new Condition(); string err = c.Read(line.Substring(5).Trim(), true); if (err.Length > 0) { return(Name + " " + lineno + " " + err + Environment.NewLine); } else if (c.Action.Length == 0 || c.EventName.Length == 0) { return(Name + " " + lineno + " EVENT Missing event name or action" + Environment.NewLine); } c.GroupName = currenteventgroup; c.Tag = precomments; // use the tag for precomments FileEventList.Add(c); InUseEventList.Add(new Condition(c)); // full clone precomments = null; inautocomments = false; } else if (line.StartsWith("GROUP", StringComparison.InvariantCultureIgnoreCase)) { currenteventgroup = line.Substring(5).Trim(); } else if (line.StartsWith("INSTALL", StringComparison.InvariantCultureIgnoreCase)) { Variables c = new Variables(); if (c.FromString(line.Substring(7).Trim(), Variables.FromMode.OnePerLine) && c.Count == 1) { InstallationVariables.Add(c); } else { return(Name + " " + lineno + " Incorrectly formatted INSTALL variable" + Environment.NewLine); } } else if (line.StartsWith("//") || line.StartsWith("REM", StringComparison.InvariantCultureIgnoreCase)) { if (line == automarker) // look out for auto generated areas, marked by this starter { inautocomments = !inautocomments; } else if (!inautocomments) // if not, its a comment on the below item { precomments = (precomments == null ? line : precomments + line) + Environment.NewLine; } } else if (line.Length == 0) { } else { return(Name + " " + lineno + " Invalid command" + Environment.NewLine); } } string missing = ""; foreach (Condition c in FileEventList.Enumerable) // lets see if any programs are missing { string progname = c.Action; if (ProgramList.Get(progname) == null) { missing += "Missing program " + progname + Environment.NewLine; } } return(missing); } else { return(Name + " Header file type not recognised" + Environment.NewLine); } } } catch (Exception e) { return(filename + " Not readable" + Environment.NewLine + " " + e.Message); } }