Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Text;
- namespace _2
- {
- public class TagList
- {
- public String list;
- public String filePath;
- public TagList(String path = null) { filePath = path; }
- public String Open(String tagName, int level, String value = null)
- {
- String shift = new String('\t', level);
- list += String.Format("\n{0}<{1}>", shift, tagName);
- if (!(value is null))
- Value(value, level);
- return list;
- }
- public String Close(String tagName, int level)
- {
- String shift = new String('\t', level);
- list += String.Format("\n{0}</{1}>", shift, tagName);
- return list;
- }
- public String Insert(String tagName, int level, String value, bool newLine = false)
- {
- String shift = new String('\t', level);
- String enter = newLine ? "\n" : "";
- list += String.Format("{0}{1}<{2}>{3}</{2}>{0}", enter, shift, tagName, value);
- return list;
- }
- public String Value(String value, int level = 0)
- {
- String shift = new String('\t', level);
- list += '\n' + shift + value;
- return list;
- }
- public void Write(String path = null)
- {
- if (path is null)
- path = this.filePath;
- using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write))
- {
- byte[] bytes = System.Text.Encoding.Default.GetBytes(list);
- fs.Write(bytes, 0, bytes.Length);
- }
- }
- public String Show() => list;
- public void Clear() { list = new String(""); }
- public String NewLine(int count = 0) => list += new String('\n', count);
- public static String GetBetween(String strSource, String strStart, String strEnd)
- {
- int Start, End;
- if (strSource.Contains(strStart) && strSource.Contains(strEnd))
- {
- Start = strSource.IndexOf(strStart, 0) + strStart.Length;
- End = strSource.IndexOf(strEnd, Start);
- return strSource.Substring(Start, End - Start);
- }
- else
- {
- return "";
- }
- }
- public static int GetShift(String tagName)
- {
- int start = tagName.IndexOf("_", 0) + 1;
- return int.Parse(tagName.Substring(start, tagName.Length-start)) - 1;
- }
- public static List<((String, int), String)> Parse(String path="")
- {
- //String[] lines = new String[6]{"<tag_1>", "\t1", "\t<tag_2>", "\t\t2", "\t</tag_2>", "</tag_1>" };
- String[] lines = File.ReadAllLines(path, Encoding.UTF8);
- var tuples = new List<((String, int), String)>();
- //drop empty lines
- int j = -1;
- String emptyName = "";
- do
- {
- j++;
- emptyName = GetBetween(lines[j], "<", ">");
- } while (emptyName == "" && lines.Length != j);
- //fill tags
- for (int i = j; i < lines.Length; i++)
- {
- String tagName = GetBetween(lines[i], "<", ">");
- int shift = GetShift(tagName);
- String tagValue = "";
- if (tagName[0] != '/')
- {
- String localTagName = "";
- do
- {
- i++;
- localTagName = GetBetween(lines[i], "<", ">");
- if (localTagName == "")
- {
- if (tagValue != "")
- tagValue += '\n';
- tagValue += lines[i];
- }
- } while (localTagName == "" && lines.Length != i);
- i--;
- }
- tuples.Add(((tagName,shift),tagValue));
- }
- return tuples;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //Exercise 1
- Console.WriteLine(CreateTagFile("file.txt"));
- //Exercise 2
- Console.WriteLine(WriteParsedTagsWithInsert("file.txt","file2.txt"));
- Console.WriteLine(WriteParsedValues("file2.txt","file3.txt"));
- }
- static public String CreateTagFile(String path)
- {
- Console.WriteLine("CreateTagFile");
- Console.WriteLine(new String('-', 20));
- TagList tagList = new TagList();
- for (int i = 0; i < 10; i++)
- {
- tagList.Open($"tag_{i + 1}", i);
- tagList.Value(Convert.ToString(i + 1), i + 1);
- }
- for (int i = 9; i >= 0; i--)
- {
- tagList.Close($"tag_{i + 1}", i);
- }
- tagList.Write(path);
- return tagList.Show();
- }
- static public String WriteParsedTagsWithInsert(String read_path, String save_path, int couple_tags=5, String value="insert value")
- {
- Console.WriteLine("WriteParsedTagsWithInsert");
- Console.WriteLine(new String('-', 20));
- TagList tagList = new TagList();
- var tuples = TagList.Parse(read_path);
- for (int i = 0; i < tuples.Count; i++)
- {
- String tagName = tuples[i].Item1.Item1;
- int level = tuples[i].Item1.Item2;
- String tagValeus = tuples[i].Item2;
- if (tagName[0] != '/')
- {
- tagList.Open(tagName, level);
- tagList.Value(tagValeus);
- if (i+1 == couple_tags)
- {
- tagList.Value(value, level+1);
- }
- }
- else
- {
- tagList.Close(tagName.Substring(1,tagName.Length-1), level);
- }
- }
- tagList.Write(save_path);
- return tagList.Show();
- }
- static public String WriteParsedValues(String read_path, String save_path)
- {
- Console.WriteLine("WriteParsedValues");
- Console.WriteLine(new String('-', 20));
- TagList tagList = new TagList();
- var tuples = TagList.Parse(read_path);
- for (int i = 0; i < tuples.Count; i++)
- {
- String tagName = tuples[i].Item1.Item1;
- int level = tuples[i].Item1.Item2;
- String tagValeus = tuples[i].Item2;
- if (tagName[0] != '/')
- {
- tagList.Value(tagValeus);
- }
- }
- tagList.Write(save_path);
- return tagList.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement