123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- using TDMS.Externals;
- namespace TDMS.Common
- {
- internal class ChannelPropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
- {
- public override TDMSDataType GetDataType(string propertyName)
- {
- var success = DDC.GetChannelPropertyType(_SelfPtr, propertyName, out var type);
- return type;
- }
- public override bool Exists(string propertyName)
- {
- var success = DDC.ChannelPropertyExists(_SelfPtr, propertyName, out var exists);
- return success == Error.NoError ? exists : false;
- }
- public override uint GetStringLength(string propertyName)
- {
- var success = DDC.GetChannelStringPropertyLength(_SelfPtr, propertyName, out var length);
- return success == Error.NoError ? length : 0;
- }
- public override string[] GetPropertyNames()
- {
- var success = DDC.CountChannelProperties(_SelfPtr, out var count);
- if(success != Error.NoError || count ==0)return new string[0];
- var names = new IntPtr[count];
- success = DDC.GetChannelPropertyNames(_SelfPtr, names, count);
- if (success != Error.NoError) return new string[count];
- var result = new string[count];
- for (var i = 0; i < count; i++)
- {
- result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
- }
- return result;
- }
- protected override Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length)
- {
- var success = DDC.GetChannelPropertyString(_SelfPtr, propertyName, chars, length);
- return success;
- }
- public override byte GetByteValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyUInt8(_SelfPtr, propertyName, out var value);
- return success == Error.NoError ? value : (byte)0;
- }
- public override short GetShortValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyInt16(_SelfPtr, propertyName, out var value);
- return success == Error.NoError ? value : (short)0;
- }
- public override int GetIntValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyInt32(_SelfPtr, propertyName, out var value);
- return success == Error.NoError ? value : 0;
- }
- public override float GetFloatValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyFloat(_SelfPtr, propertyName, out var value);
- return success == Error.NoError ? value : 0;
- }
- public override double GetDoubleValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyDouble(_SelfPtr, propertyName, out var value);
- return success == Error.NoError ? value : 0;
- }
- public override DateTime GetDateTimeValue(string propertyName)
- {
- var success = DDC.GetChannelPropertyTimestampComponents(_SelfPtr,
- propertyName,
- out var year,
- out var month,
- out var day,
- out var hour,
- out var minute,
- out var second,
- out var milliSecond,
- out var weekDay);
-
- return success == Error.NoError? new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond):DateTime.MinValue;
- }
- public override void SetStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.SetChannelPropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.SetChannelPropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.SetChannelPropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.SetChannelPropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.SetChannelPropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.SetChannelPropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.SetChannelPropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- public override void CreateStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.CreateChannelPropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.CreateChannelPropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.CreateChannelPropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.CreateChannelPropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.CreateChannelPropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.CreateChannelPropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.CreateChannelPropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- }
- internal class ChannelGroupPropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
- {
- public override TDMSDataType GetDataType(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyType(_SelfPtr, propertyName, out var type);
- return type;
- }
- public override bool Exists(string propertyName)
- {
- var success = DDC.ChannelGroupPropertyExists(_SelfPtr, propertyName, out var exists);
- return exists;
- }
- public override uint GetStringLength(string propertyName)
- {
- var success = DDC.GetChannelGroupStringPropertyLength(_SelfPtr, propertyName, out var length);
- if (success != 0) return 0;
- return length;
- }
- public override string[] GetPropertyNames()
- {
- var success = DDC.CountChannelGroupProperties(_SelfPtr, out var count);
- if(success!= Error.NoError)return new string[0];
- var names = new IntPtr[count];
- success = DDC.GetChannelGroupPropertyNames(_SelfPtr, names, count);
- if (success != Error.NoError) return new string[count];
- var result = new string[count];
- for (var i = 0; i < count; i++)
- {
- result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
- }
- return result;
- }
- protected override Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length)
- {
- var success = DDC.GetChannelGroupPropertyString(_SelfPtr, propertyName, chars, length);
- return success;
- }
- public override byte GetByteValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyUInt8(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override short GetShortValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyInt16(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override int GetIntValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyInt32(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override float GetFloatValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyFloat(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override double GetDoubleValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyDouble(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override DateTime GetDateTimeValue(string propertyName)
- {
- var success = DDC.GetChannelGroupPropertyTimestampComponents(_SelfPtr,
- propertyName,
- out var year,
- out var month,
- out var day,
- out var hour,
- out var minute,
- out var second,
- out var milliSecond,
- out var weekDay);
- return success == Error.NoError? new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond): DateTime.MinValue;
- }
- public override void SetStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.SetChannelGroupPropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- public override void CreateStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.CreateChannelGroupPropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- }
- internal class FilePropertyOperator(IntPtr selfPtr) : PropertyOperator(selfPtr)
- {
- public override TDMSDataType GetDataType(string propertyName)
- {
- var success = DDC.GetFilePropertyType(_SelfPtr, propertyName, out var type);
- return type;
- }
- public override bool Exists(string propertyName)
- {
- var success = DDC.FilePropertyExists(_SelfPtr, propertyName, out var exists);
- return exists;
- }
- public override uint GetStringLength(string propertyName)
- {
- var success = DDC.GetFileStringPropertyLength(_SelfPtr, propertyName, out var length);
- if (success != 0) return 0;
- return length;
- }
- public override string[] GetPropertyNames()
- {
- var success = DDC.CountFileProperties(_SelfPtr, out var count);
- if (success != Error.NoError) return new string[0];
- var names = new IntPtr[count];
- success = DDC.GetFilePropertyNames(_SelfPtr, names, count);
- if (success != Error.NoError) return new string[count];
- var result = new string[count];
- for (var i = 0; i < count; i++)
- {
- result[i] = Marshal.PtrToStringAnsi(names[i]) ?? string.Empty;
- }
- return result;
- }
- protected override Error DDCGetStringValue(string propertyName,StringBuilder chars, uint length)
- {
- var success = DDC.GetFilePropertyString(_SelfPtr, propertyName, chars, length);
- return success;
- }
- public override byte GetByteValue(string propertyName)
- {
- var success = DDC.GetFilePropertyUInt8(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override short GetShortValue(string propertyName)
- {
- var success = DDC.GetFilePropertyInt16(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override int GetIntValue(string propertyName)
- {
- var success = DDC.GetFilePropertyInt32(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override float GetFloatValue(string propertyName)
- {
- var success = DDC.GetFilePropertyFloat(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override double GetDoubleValue(string propertyName)
- {
- var success = DDC.GetFilePropertyDouble(_SelfPtr, propertyName, out var value);
- return value;
- }
- public override DateTime GetDateTimeValue(string propertyName)
- {
- var success = DDC.GetFilePropertyTimestampComponents(_SelfPtr,
- propertyName,
- out var year,
- out var month,
- out var day,
- out var hour,
- out var minute,
- out var second,
- out var milliSecond,
- out var weekDay);
- if (success != 0) return DateTime.MinValue;
- return new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second, (int)milliSecond);
- }
- public override void SetStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.SetFilePropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.SetFilePropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.SetFilePropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.SetFilePropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.SetFilePropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.SetFilePropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void SetDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.SetFilePropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- public override void CreateStringValue(string propertyName, string propertyValue)
- {
- var success = DDC.CreateFilePropertyString(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateByteValue(string propertyName, byte propertyValue)
- {
- var success = DDC.CreateFilePropertyUInt8(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateShortValue(string propertyName, short propertyValue)
- {
- var success = DDC.CreateFilePropertyInt16(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateIntValue(string propertyName, int propertyValue)
- {
- var success = DDC.CreateFilePropertyInt32(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateFloatValue(string propertyName, float propertyValue)
- {
- var success = DDC.CreateFilePropertyFloat(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDoubleValue(string propertyName, double propertyValue)
- {
- var success = DDC.CreateFilePropertyDouble(_SelfPtr, propertyName, propertyValue);
- }
- public override void CreateDateTimeValue(string propertyName, DateTime propertyValue)
- {
- var success = DDC.CreateFilePropertyTimestampComponents(_SelfPtr,
- propertyName,
- (uint)propertyValue.Year,
- (uint)propertyValue.Month,
- (uint)propertyValue.Day,
- (uint)propertyValue.Hour,
- (uint)propertyValue.Minute,
- (uint)propertyValue.Second,
- propertyValue.Millisecond);
- }
- }
- internal abstract class PropertyOperator(IntPtr selfPtr) : IDisposable
- {
- protected IntPtr _SelfPtr = selfPtr;
- public abstract TDMSDataType GetDataType(string propertyName);
- public abstract bool Exists(string propertyName);
- public abstract uint GetStringLength(string propertyName);
- public abstract string[] GetPropertyNames();
- public string GetStringValue(string propertyName)
- {
- if (!Exists(propertyName))
- return string.Empty;
- var length = GetStringLength(propertyName);
- if (length <= 0)
- return string.Empty;
- length++;
- var chars = new StringBuilder((int)length);
- var success = DDCGetStringValue(propertyName, chars, length);
- return success == Error.NoError ? chars.ToString().TrimEnd('\0') : string.Empty;
- }
- protected abstract Error DDCGetStringValue(string propertyName, StringBuilder chars, uint length);
- public abstract byte GetByteValue(string propertyName);
- public abstract short GetShortValue(string propertyName);
- public abstract int GetIntValue(string propertyName);
- public abstract float GetFloatValue(string propertyName);
- public abstract double GetDoubleValue(string propertyName);
- public abstract DateTime GetDateTimeValue(string propertyName);
- public abstract void SetStringValue(string propertyName, string propertyValue);
- public abstract void SetByteValue(string propertyName, byte propertyValue);
- public abstract void SetShortValue(string propertyName, short propertyValue);
- public abstract void SetIntValue(string propertyName, int propertyValue);
- public abstract void SetFloatValue(string propertyName, float propertyValue);
- public abstract void SetDoubleValue(string propertyName, double propertyValue);
- public abstract void SetDateTimeValue(string propertyName, DateTime propertyValue);
- public abstract void CreateStringValue(string propertyName, string propertyValue);
- public abstract void CreateByteValue(string propertyName, byte propertyValue);
- public abstract void CreateShortValue(string propertyName, short propertyValue);
- public abstract void CreateIntValue(string propertyName, int propertyValue);
- public abstract void CreateFloatValue(string propertyName, float propertyValue);
- public abstract void CreateDoubleValue(string propertyName, double propertyValue);
- public abstract void CreateDateTimeValue(string propertyName, DateTime propertyValue);
- #region Implementation of IDisposable
- ~PropertyOperator()
- {
- Dispose(false);
- }
- protected virtual void Dispose(bool disposing)
- {
- _SelfPtr = IntPtr.Zero;
- if(disposing)
- {
- // 释放托管资源
- }
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- }
|