Beispiel #1
0
        /// <summary>
        /// 从指定的流的当前位置加载数据。
        /// </summary>
        /// <param name="stream"></param>
        private void Load(Stream stream)
        {
            using BinaryReader br = new BinaryReader(stream, Encoding.UTF8, true);
            FullPath = br.ReadString();
            IsFile   = !string.IsNullOrWhiteSpace(FullPath);
            fileSize = (FileSize)br.ReadInt64();
            int length = br.ReadInt32();

            if (length == 0)
            {
                fileSize = 0;
            }
            else
            {
                byte[] date = new byte[length];
                stream.Read(date, 0, date.Length);
                using MemoryStream xmlStream = new MemoryStream(date);
                xml.Load(xmlStream);
                fileSize = (FileSize)stream.Length;
            }
            bool isnull = br.ReadBoolean();

            if (isnull)
            {
                lines = null;
            }
            else
            {
                length = br.ReadInt32();
                lines  = new List <ITranslatingLine>(length);
                IReadWriter <XmlFileOfVSLine> readWriter = XmlFileOfVSLine.GetLineReadWriter(this);
                for (int i = 0; i < length; i++)
                {
                    lines.Add(readWriter.Read(stream));
                }
            }
        }
Beispiel #2
0
 public int Read(byte[] buffer, int offset, int count) => _rw.Read(buffer, offset, count);
Beispiel #3
0
 public void Read(string fileName)
 {
     readWriter.Read(this, fileName);
 }
Beispiel #4
0
        public static void RunCommand()
        {
            var r       = new Regex("(?<command>list |import file |export file )(?<args>.*)", RegexOptions.IgnoreCase);
            var rSearch = new Regex("(?<name>[a-z]+( [a-z])?)(?<date> .*)?", RegexOptions.IgnoreCase);

            Console.WriteLine("\"List All\" or \"List (Account)\" or \"Import File (FileName)\" " +
                              "or \"Export File (FileName)\"");
            var input = Console.ReadLine();
            var m     = r.Match(input);

            if (!m.Success)
            {
                Console.WriteLine("Command not recognised");
                return;
            }

            switch (m.Groups["command"].ToString().ToLower())
            {
            case "list ": {
                var m2 = rSearch.Match(m.Groups["args"].ToString());
                if (!m2.Success)
                {
                    Console.WriteLine("Name/date not recognised");
                    return;
                }
                var date = DateTime.Now;
                if (!m2.Groups["date"].ToString().Equals(""))
                {
                    try {
                        date = DateTime.Parse(m2.Groups["date"].ToString());
                    }
                    catch {
                        Console.WriteLine("Date not recognised");
                    }
                }
                var name = m2.Groups["name"].ToString();
                if (name.ToLower().Equals("all"))
                {
                    ListAll(date);
                }
                else
                {
                    ListAccount(name, date);
                }
                break;
            }

            case "import file ": {
                var filename = m.Groups["args"].ToString();
                try {
                    if (filename.EndsWith(".json"))
                    {
                        transactions.UnionWith(jsonReadWriter.Read(filename));
                    }
                    else if (filename.EndsWith(".xml"))
                    {
                        transactions.UnionWith(xmlReadWriter.Read(filename));
                    }
                    else
                    {
                        transactions.UnionWith(csvReadWriter.Read(filename));
                    }
                } catch {
                    Console.WriteLine($"Could not read file: {filename}");
                }
                break;
            }

            case "export file ": {
                var filename = m.Groups["args"].ToString();
                try {
                    if (filename.EndsWith(".json"))
                    {
                        jsonReadWriter.Write(filename, transactions);
                    }
                    else if (filename.EndsWith(".xml"))
                    {
                        xmlReadWriter.Write(filename, transactions);
                    }
                    else
                    {
                        csvReadWriter.Write(filename, transactions);
                    }
                } catch {
                    Console.WriteLine($"Could not write file: {filename}");
                }
                break;
            }
            }
        }