/// <summary> /// Save a PrinterObject to disk /// </summary> /// <param name="obj">object to save</param> /// <param name="fileName">full path of fileName to save</param> public static void Save(PrinterObject obj, string fileName) { FileInfo fi = new FileInfo(fileName); obj.CurrentDirectory = fi.DirectoryName; PersistentDataObject.Save(fi, obj); }
/// <summary> /// Create /// </summary> /// <param name="pv">printer version</param> /// <returns>printer object</returns> public static PrinterObject Create(PrinterVersion pv) { PrinterObject po = new PrinterObject(Path.GetDirectoryName(pv.FullName), pv.LatestVersion); PrinterObject.Save(po, pv.FullName); return(po); }
/// <summary> /// Constructor with currentDirectory setted /// </summary> /// <param name="cd">current directory</param> /// <param name="version">version</param> protected PrinterObject(string cd, string version) { PrinterObject.InitializePersonalDirectory(); this.Set(versionName, version); this.Set(revisionName, 0); this.Set(currentDirectoryName, cd); this.Set(variablesName, new Dictionary <string, PrinterVariable>()); this.Set(dataName, new List <string>()); this.Set(uniqueName, new UniqueStrings()); this.Set(configurationName, new Configuration()); }
/// <summary> /// Select a specific version /// </summary> /// <param name="version"></param> /// <returns>printer object selected</returns> public PrinterObject Select(string version) { if (!String.IsNullOrEmpty(version)) { return(PrinterObject.Load(String.Format("{0}-{1}.prt", Path.Combine(this.path, this.fileName), version))); } else { return(PrinterObject.Load(String.Format("{0}.prt", Path.Combine(this.path, this.fileName)))); } }
/// <summary> /// Save a PrinterObject to memory /// You must close the stream after this method /// </summary> /// <param name="obj">object to save</param> /// <param name="stream">stream buffer</param> public static void Save(PrinterObject obj, Stream stream) { BinaryFormatter bf = new BinaryFormatter(); try { bf.Serialize(stream, obj); } catch (Exception) { throw; } }
/// <summary> /// Clone this object /// </summary> /// <returns>new object</returns> public object Clone() { PrinterObject newPo = new PrinterObject(); foreach (string s in this.Datas) { newPo.Strings.Add(s.Clone() as string); } newPo.Unique = new UniqueStrings(this.Unique.Counter); foreach (PrinterVariable pv in this.Values) { newPo.Variables.Add(pv.Name, pv.Clone() as PrinterVariable); } return(newPo); }
/// <summary> /// Load a file from memory /// You must close the stream after this method /// </summary> /// <param name="stream">stream buffer</param> /// <returns>object</returns> public static PrinterObject Load(Stream stream) { PrinterObject po = null; BinaryFormatter bf = new BinaryFormatter(); try { po = bf.Deserialize(stream) as PrinterObject; } catch (Exception) { throw; } return(po); }
/// <summary> /// Write output as interpretation result /// </summary> /// <param name="w">writer</param> /// <param name="indentValue">space size</param> /// <param name="currentLine">in-progress line add</param> /// <param name="config">configuration</param> public void Execute(TextWriter w, ref int indentValue, ref string currentLine, Configuration config) { foreach (string e in this.Strings) { if (e.StartsWith("[") && e.EndsWith("]")) { string r = e.Substring(1, e.Length - 2); r = config.Execute(r); if (this.ExistTestVariable(r)) { this.Variables[r].Execute(w, ref indentValue, ref currentLine, config, this.CurrentDirectory); } } else { string val = config.Execute(e); PrinterObject.IndentSource(w, indentValue, ref currentLine, val); } } }
/// <summary> /// Add a new version /// </summary> /// <param name="po">printer object</param> public void AddVersion(PrinterObject po) { if (this.currentMinorVersion == 0 && this.currentMajorVersion == 1) { PrinterObject.Save(po, Path.Combine(this.path, String.Format("{0}.prt", this.fileName))); } else { PrinterObject.Save(po, Path.Combine(this.path, String.Format("{0}-{1}.prt", this.fileName, this.LatestVersion))); } if (this.currentMinorVersion == 9) { this.currentMinorVersion = 0; ++this.currentMajorVersion; } else { ++this.currentMinorVersion; } }
/// <summary> /// Execute the variable /// </summary> /// <param name="w">writer</param> /// <param name="indentValue">space size</param> /// <param name="currentLine">in-progress line add</param> /// <param name="config">configuration</param> /// <param name="dir">directory</param> public void Execute(TextWriter w, ref int indentValue, ref string currentLine, Configuration config, string dir) { if (this.Indent) { ++indentValue; } if (this.Include) { string fileName = config.Execute(this.Value); FileInfo fi = new FileInfo(Path.Combine(dir, fileName)); if (fi.Exists) { using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { PrinterObject po = PrinterObject.Load(fs); po.CurrentDirectory = dir; foreach (PrinterVariable pv in this.Values) { po.AddVariable(pv.Name, pv); } po.ImportConfiguration(config); po.Execute(w, ref indentValue, ref currentLine, po.Configuration); fs.Close(); } } } else { if (!String.IsNullOrEmpty(this.Value)) { string val = config.Execute(this.Value); PrinterObject.IndentSource(w, indentValue, ref currentLine, val); } } if (this.Indent) { --indentValue; } }
/// <summary> /// Create a new printer object /// </summary> /// <returns>printer object</returns> public PrinterObject Create() { return(PrinterObject.Create(this)); }
static void Main(string[] args) { try { if (args.Length == 0) { throw new ArgumentException("USAGE : printer file.prt"); } PrinterObject po = null; FileInfo fi = new FileInfo(args[0]); if (fi.Exists) { po = PrinterObject.Load(args[0]); } else { po = PrinterObject.Create(new PrinterVersion(Path.GetDirectoryName(args[0]), Path.GetFileName(args[0]))); po.AddData(@"using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace "); po.AddData("[Printer]"); po.AddData(@" { class "); po.AddData("[Program]"); po.AddData(@" { static void Main(string[] args) { //"); PrinterVariable pv = new PrinterVariable(); pv.Name = "condition"; pv.Include = true; pv.Indent = true; pv.AddVariable("expression", "true"); pv.AddVariable("then", "code"); pv.AddVariable("else", "code"); pv.Value = "C/if.prt"; po.AddVariable("condition", pv); po.UseVariable("condition"); po.AddData(@" } } } "); } Console.WriteLine(po.Execute()); Console.WriteLine("code:"); Console.WriteLine(po.ToString()); PrinterObject.Save(po, args[0]); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Touch your keyboard"); Console.ReadKey(); } }