import ctypes
import codecs
import functools


def add_string_properties_from_utf_16_le(c):
    """
    c_ubyte arrays that start with '_' are automatically converted to a string attribute
    with the name of the field without the leading '_'. The content of the original
    Bytarray is decoded using UTF-16-LE.
    :param c: Class to add the properties to
    :return: Nothing
    """
    for field_name, field_type in c._fields_:
        if field_type._type_ == ctypes.c_ubyte and field_type._length_ > 1 and field_name.startswith('_'):
            field = field_name[1:]
            def getter(self, name=None):
                value = getattr(self, "_%s" % name)
                return codecs.decode(value, 'UTF-16-LE', 'replace').split('%')[0]
            setattr(c, field, property(functools.partial(getter, name=field)))