@@ -197,7 +197,7 @@ private void Prepare(EDSsharp eds)
197
197
private int Prepare_var ( ODentry od , string indexH , string varName , string group )
198
198
{
199
199
DataProperties data = Get_dataProperties ( od . datatype , od . defaultvalue , od . prop . CO_stringLengthMin , indexH ) ;
200
- string attr = Get_attributes ( od , data . cTypeMultibyte ) ;
200
+ string attr = Get_attributes ( od , data . cTypeMultibyte , data . cTypeString ) ;
201
201
202
202
// data storage
203
203
string dataPtr = "NULL" ;
@@ -253,7 +253,7 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group)
253
253
DataType dataType = ( sub . datatype != DataType . UNKNOWN ) ? sub . datatype : od . datatype ;
254
254
255
255
DataProperties data = Get_dataProperties ( dataType , sub . defaultvalue , sub . prop . CO_stringLengthMin , indexH ) ;
256
- string attr = Get_attributes ( sub , data . cTypeMultibyte ) ;
256
+ string attr = Get_attributes ( sub , data . cTypeMultibyte , data . cTypeString ) ;
257
257
258
258
if ( sub . Subindex != i )
259
259
Warnings . AddWarning ( $ "Error in 0x{ indexH } : SubIndexes in ARRAY must be in sequence!", Warnings . warning_class . WARNING_BUILD ) ;
@@ -350,7 +350,7 @@ private int Prepare_rec(ODentry od, string indexH, string varName, string group)
350
350
foreach ( ODentry sub in od . subobjects . Values )
351
351
{
352
352
DataProperties data = Get_dataProperties ( sub . datatype , sub . defaultvalue , sub . prop . CO_stringLengthMin , indexH ) ;
353
- string attr = Get_attributes ( sub , data . cTypeMultibyte ) ;
353
+ string attr = Get_attributes ( sub , data . cTypeMultibyte , data . cTypeString ) ;
354
354
355
355
if ( sub . Subindex == 0 && ( data . cType != "uint8_t" || data . length != 1 ) )
356
356
Warnings . AddWarning ( $ "Error in 0x{ indexH } : Data type in RECORD, subIndex 0 must be UNSIGNED8, not { sub . datatype } !", Warnings . warning_class . WARNING_BUILD ) ;
@@ -661,6 +661,7 @@ private class DataProperties
661
661
public string cTypeArray = "" ;
662
662
public string cTypeArray0 = "" ;
663
663
public bool cTypeMultibyte = false ;
664
+ public bool cTypeString = false ;
664
665
public UInt32 length = 0 ;
665
666
public string cValue = null ;
666
667
}
@@ -824,29 +825,39 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
824
825
break ;
825
826
826
827
case DataType . VISIBLE_STRING :
828
+ data . cTypeString = true ;
827
829
if ( valueDefined || stringLength > 0 )
828
830
{
829
831
List < string > chars = new List < string > ( ) ;
830
832
UInt32 len = 0 ;
831
833
832
834
if ( valueDefined )
833
835
{
834
- Encoding ascii = Encoding . ASCII ;
835
- Byte [ ] encodedBytes = ascii . GetBytes ( defaultvalue ) ;
836
+ UTF8Encoding utf8 = new UTF8Encoding ( ) ;
837
+ Byte [ ] encodedBytes = utf8 . GetBytes ( defaultvalue ) ;
836
838
foreach ( Byte b in encodedBytes )
837
839
{
838
- chars . Add ( $ "'{ StringUnescape . Escape ( ( char ) b ) } '") ;
840
+ if ( ( char ) b == '\' ' )
841
+ chars . Add ( "'\\ ''" ) ;
842
+ else if ( b >= 0x20 && b < 0x7F )
843
+ chars . Add ( $ "'{ ( char ) b } '") ;
844
+ else
845
+ chars . Add ( $ "0x{ b : X2} ") ;
839
846
len ++ ;
840
847
}
841
848
}
849
+ /* fill unused bytes with nulls */
842
850
for ( ; len < stringLength ; len ++ )
843
851
{
844
- chars . Add ( "' \\ 0' " ) ;
852
+ chars . Add ( "0 " ) ;
845
853
}
846
854
855
+ // extra string terminator
856
+ chars . Add ( "0" ) ;
857
+
847
858
data . length = len ;
848
859
data . cType = "char" ;
849
- data . cTypeArray = $ "[{ len } ]";
860
+ data . cTypeArray = $ "[{ len + 1 } ]";
850
861
data . cTypeArray0 = "[0]" ;
851
862
data . cValue = $ "{{{string.Join(", ", chars)}}}" ;
852
863
}
@@ -866,7 +877,7 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
866
877
string [ ] strBytes = defaultvalue . Split ( new char [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
867
878
foreach ( string s in strBytes )
868
879
{
869
- bytes . Add ( String . Format ( "0x{0:X2}" , Convert . ToByte ( s , nobase ) ) ) ;
880
+ bytes . Add ( String . Format ( "0x{0:X2}" , Convert . ToByte ( s , 16 ) ) ) ;
870
881
len ++ ;
871
882
}
872
883
}
@@ -883,6 +894,8 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
883
894
}
884
895
break ;
885
896
case DataType . UNICODE_STRING :
897
+ data . cTypeString = true ;
898
+ data . cTypeMultibyte = true ;
886
899
if ( valueDefined || stringLength > 0 )
887
900
{
888
901
List < string > words = new List < string > ( ) ;
@@ -904,9 +917,12 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
904
917
words . Add ( "0x0000" ) ;
905
918
}
906
919
920
+ // extra string terminator
921
+ words . Add ( "0x0000" ) ;
922
+
907
923
data . length = len * 2 ;
908
924
data . cType = "uint16_t" ;
909
- data . cTypeArray = $ "[{ len } ]";
925
+ data . cTypeArray = $ "[{ len + 1 } ]";
910
926
data . cTypeArray0 = "[0]" ;
911
927
data . cValue = $ "{{{string.Join(", ", words)}}}" ;
912
928
}
@@ -986,9 +1002,10 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
986
1002
/// Get attributes from OD entry or sub-entry
987
1003
/// </summary>
988
1004
/// <param name="od"></param>
989
- /// <param name="multibyte"></param>
1005
+ /// <param name="cTypeMultibyte"></param>
1006
+ /// <param name="cTypeString"></param>
990
1007
/// <returns></returns>
991
- private string Get_attributes ( ODentry od , bool multibyte )
1008
+ private string Get_attributes ( ODentry od , bool cTypeMultibyte , bool cTypeString )
992
1009
{
993
1010
List < string > attributes = new List < string > ( ) ;
994
1011
@@ -1031,9 +1048,12 @@ private string Get_attributes(ODentry od, bool multibyte)
1031
1048
break ;
1032
1049
}
1033
1050
1034
- if ( multibyte )
1051
+ if ( cTypeMultibyte )
1035
1052
attributes . Add ( "ODA_MB" ) ;
1036
1053
1054
+ if ( cTypeString )
1055
+ attributes . Add ( "ODA_STR" ) ;
1056
+
1037
1057
return string . Join ( " | " , attributes ) ;
1038
1058
}
1039
1059
#endregion
0 commit comments