Skip to content

Commit

Permalink
csharp: Control Structures should use curly braces {}
Browse files Browse the repository at this point in the history
According to Microsoft, and Sonar C#, all Control structures (do, for,
foreach, while, if) should use curly braces. While not technically incorrect,
the omission of curly braces can be misleading, and may lead to the
introduction of errors during maintenance.

See CERT, EXP19-C Use braces for the body of an if, for, or while statement
https://www.securecoding.cert.org/confluence/x/1QGMAg

See CERT, EXP52-J Use braces for the body of an if, for, or while statement
https://www.securecoding.cert.org/confluence/x/3wHEAw

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Apr 27, 2020
1 parent a7433eb commit 282b0dd
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bindings/csharp/Attr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ public long read_long()
public void write(bool val)
{
if (val)
{
write("1");
}
else
{
write("0");
}
}

/// <summary>Set this attribute to the value contained in the <c>long</c> argument.</summary>
Expand Down
26 changes: 26 additions & 0 deletions bindings/csharp/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ public override string read()
StringBuilder builder = new StringBuilder(1024);
int err = iio_channel_attr_read(chn, name, builder, (uint) builder.Capacity);
if (err < 0)
{
throw new Exception("Unable to read channel attribute " + err);
}
return builder.ToString();
}

public override void write(string str)
{
int err = iio_channel_attr_write(chn, name, str);
if (err < 0)
{
throw new Exception("Unable to write channel attribute " + err);
}
}
}

Expand Down Expand Up @@ -143,13 +147,19 @@ internal Channel(IntPtr chn)
uint nb_attrs = iio_channel_get_attrs_count(chn);

for (uint i = 0; i < nb_attrs; i++)
{
attrs.Add(new ChannelAttr(this.chn, Marshal.PtrToStringAnsi(iio_channel_get_attr(chn, i))));
}

IntPtr name_ptr = iio_channel_get_name(this.chn);
if (name_ptr == IntPtr.Zero)
{
name = "";
}
else
{
name = Marshal.PtrToStringAnsi(name_ptr);
}

id = Marshal.PtrToStringAnsi(iio_channel_get_id(this.chn));
output = iio_channel_is_output(this.chn);
Expand Down Expand Up @@ -184,9 +194,13 @@ public bool is_enabled()
public byte[] read(IOBuffer buffer, bool raw = false)
{
if (!is_enabled())
{
throw new Exception("Channel must be enabled before the IOBuffer is instantiated");
}
if (this.output)
{
throw new Exception("Unable to read from output channel");
}

byte[] array = new byte[(int) (buffer.samples_count * sample_size)];
MemoryStream stream = new MemoryStream(array, true);
Expand All @@ -195,9 +209,13 @@ public byte[] read(IOBuffer buffer, bool raw = false)
uint count;

if (raw)
{
count = iio_channel_read_raw(this.chn, buffer.buf, addr, buffer.samples_count * sample_size);
}
else
{
count = iio_channel_read(this.chn, buffer.buf, addr, buffer.samples_count * sample_size);
}
handle.Free();
stream.SetLength((long) count);
return stream.ToArray();
Expand All @@ -216,18 +234,26 @@ public byte[] read(IOBuffer buffer, bool raw = false)
public uint write(IOBuffer buffer, byte[] array, bool raw = false)
{
if (!is_enabled())
{
throw new Exception("Channel must be enabled before the IOBuffer is instantiated");
}
if (!this.output)
{
throw new Exception("Unable to write to an input channel");
}

GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
IntPtr addr = handle.AddrOfPinnedObject();
uint count;

if (raw)
{
count = iio_channel_write_raw(this.chn, buffer.buf, addr, (uint) array.Length);
}
else
{
count = iio_channel_write(this.chn, buffer.buf, addr, (uint) array.Length);
}
handle.Free();

return count;
Expand Down
18 changes: 18 additions & 0 deletions bindings/csharp/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ private static IntPtr getContextFromString(string str)
{
IntPtr ptr = iio_create_context_from_uri(str);
if (ptr == IntPtr.Zero)
{
ptr = iio_create_network_context(str);
}
return ptr;
}

Expand All @@ -138,7 +140,9 @@ private Context(IntPtr ctx)
this.ctx = ctx;

if (ctx == IntPtr.Zero)
{
throw new Exception("Unable to create IIO context");
}

uint nb_devices = iio_context_get_devices_count(ctx);

Expand All @@ -147,9 +151,13 @@ private Context(IntPtr ctx)
{
IntPtr ptr = iio_context_get_device(ctx, i);
if (iio_device_is_trigger(ptr))
{
devices.Add(new Trigger(this, ptr));
}
else
{
devices.Add(new Device(this, ptr));
}
}

xml = Marshal.PtrToStringAnsi(iio_context_get_xml(ctx));
Expand All @@ -167,14 +175,18 @@ private Context(IntPtr ctx)
builder.Clear();
int err = iio_context_get_version(ctx, ref major, ref minor, builder);
if (err < 0)
{
throw new Exception("Unable to read backend version");
}
backend_version = new Version(major, minor, builder.ToString());
}

~Context()
{
if (ctx != IntPtr.Zero)
{
Dispose(false);
}
}

/// <summary>Clone this instance.</summary>
Expand All @@ -192,7 +204,9 @@ public Device get_device(string name)
foreach (Device each in devices) {
if (each.name.CompareTo(name) == 0 ||
each.id.CompareTo(name) == 0)
{
return each;
}
}

throw new Exception("Device " + name + " not found");
Expand All @@ -205,7 +219,9 @@ public void set_timeout(uint timeout)
{
int ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0)
{
throw new Exception("Unable to set timeout");
}
}

/// <summary>Releases all resource used by the <see cref="iio.Context"/> object.</summary>
Expand All @@ -223,7 +239,9 @@ private void Dispose(bool clean)
if (ctx != IntPtr.Zero)
{
if (clean)
{
GC.SuppressFinalize(this);
}
iio_context_destroy(ctx);
ctx = IntPtr.Zero;
}
Expand Down
38 changes: 36 additions & 2 deletions bindings/csharp/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ public override string read()
StringBuilder builder = new StringBuilder(1024);
int err = iio_device_attr_read(dev, name, builder, 1024);
if (err < 0)
{
throw new Exception("Unable to read device attribute " + err);
}
return builder.ToString();
}

public override void write(string str)
{
int err = iio_device_attr_write(dev, name, str);
if (err < 0)
{
throw new Exception("Unable to write device attribute " + err);
}
}
}

Expand All @@ -81,15 +85,19 @@ public override string read()
StringBuilder builder = new StringBuilder(1024);
int err = iio_device_debug_attr_read(dev, name, builder, 1024);
if (err < 0)
{
throw new Exception("Unable to read debug attribute " + err);
}
return builder.ToString();
}

public override void write(string str)
{
int err = iio_device_debug_attr_write(dev, name, str);
if (err < 0)
{
throw new Exception("Unable to write debug attribute " + err);
}
}
}

Expand Down Expand Up @@ -165,20 +173,30 @@ internal Device(Context ctx, IntPtr dev)
nb_debug_attrs = iio_device_get_debug_attrs_count(dev);

for (uint i = 0; i < nb_channels; i++)
{
channels.Add(new Channel(iio_device_get_channel(dev, i)));
}

for (uint i = 0; i < nb_attrs; i++)
{
attrs.Add(new DeviceAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_attr(dev, i))));
}
for (uint i = 0; i < nb_debug_attrs; i++)
{
debug_attrs.Add(new DeviceDebugAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_debug_attr(dev, i))));
}

id = Marshal.PtrToStringAnsi(iio_device_get_id(dev));

IntPtr name_ptr = iio_device_get_name(dev);
if (name_ptr == IntPtr.Zero)
{
name = "";
}
else
{
name = Marshal.PtrToStringAnsi(name_ptr);
}
}

/// <summary>Get the <see cref="iio.Channel"/> object of the specified name.</summary>
Expand All @@ -187,10 +205,13 @@ internal Device(Context ctx, IntPtr dev)
/// name or ID could not be found in the current context.</exception>
public Channel get_channel(string name)
{
foreach (Channel each in channels) {
foreach (Channel each in channels)
{
if (each.name.CompareTo(name) == 0 ||
each.id.CompareTo(name) == 0)
{
return each;
}
}

throw new Exception("Channel " + name + " not found");
Expand All @@ -203,7 +224,9 @@ public void set_trigger(Trigger trig)
{
int err = iio_device_set_trigger(this.dev, trig == null ? IntPtr.Zero : trig.dev);
if (err < 0)
{
throw new Exception("Unable to set trigger: err=" + err);
}
}

/// <summary>Get the current trigger affected to this device.</summary>
Expand All @@ -214,13 +237,18 @@ public Trigger get_trigger()
IntPtr ptr = (IntPtr)0;
int err = iio_device_get_trigger(this.dev, ptr);
if (err < 0)
{
throw new Exception("Unable to get trigger: err=" + err);
}

ptr = Marshal.ReadIntPtr(ptr);

foreach (Trigger trig in ctx.devices) {
foreach (Trigger trig in ctx.devices)
{
if (trig.dev == ptr)
{
return trig;
}
}

return null;
Expand All @@ -233,7 +261,9 @@ public uint get_sample_size()
{
int ret = iio_device_get_sample_size(dev);
if (ret < 0)
{
throw new Exception("Internal error. Please report any bug.");
}
return (uint) ret;
}
/// <summary>Set a value to one register of this device.</summary>
Expand All @@ -244,7 +274,9 @@ public void reg_write(uint addr, uint value)
{
int err = iio_device_reg_write(dev, addr, value);
if (err < 0)
{
throw new Exception("Unable to write register");
}
}

/// <summary>Read the content of a register of this device.</summary>
Expand All @@ -255,7 +287,9 @@ public uint reg_read(uint addr)
uint value = 0;
int err = iio_device_reg_read(dev, addr, ref value);
if (err < 0)
{
throw new Exception("Unable to read register");
}
return value;
}
}
Expand Down
Loading

0 comments on commit 282b0dd

Please sign in to comment.