标题:请问各位大大们下面的一组C#代码的意思是什么 不太明白
取消只看楼主
ragnarok
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-1-24
结帖率:33.33%
已结贴  问题点数:18 回复次数:0 
请问各位大大们下面的一组C#代码的意思是什么 不太明白
using System;

namespace DQ7
{
    class SaveData
    {
        private static SaveData mThis;
        private String mFileName = null;
        private Byte[] mBuffer = null;
        private readonly System.Text.Encoding mEncode = System.Text.Encoding.UTF8;

        private SaveData()
        {}

        public static SaveData Instance()
        {
            if (mThis == null) mThis = new SaveData();
            return mThis;
        }

        public bool Open(String filename, bool force)
        {
            mFileName = filename;
            mBuffer = (mFileName);

            if (force || CheckSum() == ReadNumber(0, 4))
            {
                Backup();
                return true;
            }

            mFileName = null;
            mBuffer = null;
            return false;
        }

        public bool Save()
        {
            if (mFileName == null || mBuffer == null) return false;
            WriteNumber(0, 4, CheckSum());
            (mFileName, mBuffer);
            return true;
        }

        public bool SaveAs(String filenname)
        {
            if (mBuffer == null) return false;
            mFileName = filenname;
            return Save();
        }

        public uint ReadNumber(uint address, uint size)
        {
            if (mBuffer == null) return 0;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return 0;
            uint result = 0;
            for(int i = 0; i < size; i++)
            {
                result += (uint)(mBuffer[address + i]) << (i * 8);
            }
            return result;
        }

        // 0 to 7.
        public bool ReadBit(uint address, uint bit)
        {
            if (bit < 0) return false;
            if (bit > 7) return false;
            if (mBuffer == null) return false;
            address = CalcAddress(address);
            if (address > mBuffer.Length) return false;
            Byte mask = (Byte)(1 << (int)bit);
            Byte result = (Byte)(mBuffer[address] & mask);
            return result != 0;
        }

        public String ReadText(uint address, uint size)
        {
            if (mBuffer == null) return "";
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return "";

            Byte[] tmp = new Byte[size];
            for(uint i = 0; i < size; i++)
            {
                if (mBuffer[address + i] == 0) break;
                tmp[i] = mBuffer[address + i];
            }
            return mEncode.GetString(tmp).Trim('\0');
        }

        public void WriteNumber(uint address, uint size, uint value)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = (Byte)(value & 0xFF);
                value >>= 8;
            }
        }

        // 0 to 7.
        public void WriteBit(uint address, uint bit, bool value)
        {
            if (bit < 0) return;
            if (bit > 7) return;
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address > mBuffer.Length) return;
            Byte mask = (Byte)(1 << (int)bit);
            if (value) mBuffer[address] = (Byte)(mBuffer[address] | mask);
            else mBuffer[address] = (Byte)(mBuffer[address] & ~mask);
        }

        public void WriteText(uint address, uint size, String value)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            Byte[] tmp = mEncode.GetBytes(value);
            Array.Resize(ref tmp, (int)size);
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = tmp[i];
            }
        }

        public void Fill(uint address, uint size, Byte number)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = number;
            }
        }

        public void Copy(uint from, uint to, uint size)
        {
            if (mBuffer == null) return;
            from = CalcAddress(from);
            to = CalcAddress(to);
            if (from + size > mBuffer.Length) return;
            if (to + size > mBuffer.Length) return;
            for(uint i = 0; i < size; i++)
            {
                mBuffer[to + i] = mBuffer[from + i];
            }
        }

        public void Swap(uint from, uint to, uint size)
        {
            if (mBuffer == null) return;
            from = CalcAddress(from);
            to = CalcAddress(to);
            if (from + size > mBuffer.Length) return;
            if (to + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                Byte tmp = mBuffer[to + i];
                mBuffer[to + i] = mBuffer[from + i];
                mBuffer[from + i] = tmp;
            }
        }

        private uint CheckSum()
        {
            int result = 0;
            for (uint i = 0x10; i < mBuffer.Length; i++)
            {
                result += (SByte)ReadNumber(i, 1);
            }
            return (uint)result;
        }

        private uint CalcAddress(uint address)
        {
            return address;
        }

        private void Backup()
        {
            DateTime now = DateTime.Now;
            String path = (System.Reflection.Assembly.GetExecutingAssembly().Location);
            path = (path, "backup");
            if(!(path))
            {
                (path);
            }
            path = (path,
                String.Format("{0:0000}-{1:00}-{2:00} {3:00}-{4:00}", now.Year, now.Month, now.Day, now.Hour, now.Minute));
            (path, mBuffer);
        }
    }
}
搜索更多相关主题的帖子: null return size Byte address 
2023-01-27 21:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511173-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.126154 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved