Debian Codegraph @

contrib/C#/NGraphite/FeatureRef.cs

raw
1 // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2 // Copyright (C) 2012 SIL International
3 using System;
4 using System.Runtime.InteropServices;
5
6 namespace NGraphite
7 {
8 public class FeatureRef
9 {
10 IntPtr _featureRef;
11
12 internal FeatureRef(IntPtr featureRef)
13 {
14 _featureRef = featureRef;
15 }
16
17 public UInt32 Id
18 {
19 get
20 {
21 return Graphite2Api.FrefId(_featureRef);
22 }
23 }
24
25 public string Label(string langid, Encform utf)
26 {
27 UInt32 length;
28 ushort langId = (ushort)Graphite2Api.StrToTag(langid);
29 IntPtr labelPtr = Graphite2Api.FrefLabel(_featureRef, ref langId, utf, out length);
30 if (labelPtr == IntPtr.Zero)
31 return String.Empty;
32 string retLabel = ConvertGraphiteLabelToString(labelPtr, utf, length);
33 Graphite2Api.LabelDestroy(labelPtr);
34 return retLabel;
35 }
36
37 /// <summary>
38 /// As Id as a string.
39 /// </summary>
40 public string IdLabel
41 {
42 get
43 {
44 IntPtr ptr = Marshal.StringToCoTaskMemAnsi(" ");
45 Graphite2Api.TagToStr(Id, ptr);
46 var str = Marshal.PtrToStringAnsi(ptr);
47 Marshal.FreeCoTaskMem(ptr);
48 return str;
49 }
50 }
51
52 public string ValueLabel(UInt16 settingsno, string langid, Encform utf)
53 {
54 UInt32 length;
55 ushort langId = (ushort)Graphite2Api.StrToTag(langid);
56 IntPtr labelPtr = Graphite2Api.FrefValueLabel(_featureRef, settingsno, ref langId, utf, out length);
57 if (labelPtr == IntPtr.Zero)
58 return String.Empty;
59 string retLabel = ConvertGraphiteLabelToString(labelPtr, utf, length);
60 Graphite2Api.LabelDestroy(labelPtr);
61 return retLabel;
62 }
63
64 public UInt16 NValues()
65 {
66 return Graphite2Api.FrefNValues(_featureRef);
67 }
68
69 public UInt16 Value(UInt16 settingno)
70 {
71 return Graphite2Api.FrefValue(_featureRef, settingno);
72 }
73
74 public UInt16 FeatureValue(Featureval feats)
75 {
76 return Graphite2Api.FrefFeatureValue(_featureRef, feats.FeatureValPtr);
77 }
78
79 public int SetFeatureValue(UInt16 val, Featureval dest)
80 {
81 return Graphite2Api.FrefSetFeatureValue(_featureRef, val, dest.FeatureValPtr);
82 }
83
84 #region helper methods
85
86 protected string ConvertGraphiteLabelToString(IntPtr labelPtr, Encform utf, UInt32 length)
87 {
88 switch(utf)
89 {
90 case Encform.Utf8:
91 return Marshal.PtrToStringAnsi(labelPtr, (int)length);
92 case Encform.Utf16:
93 return Marshal.PtrToStringUni(labelPtr, (int)length);
94 case Encform.utf32:
95 // I am assuming that PtrToStringUni only works for utf-16
96 throw new NotImplementedException("Getting labels as utf32 not supportted");
97 default:
98 throw new NotImplementedException("Unknown EncForm");
99 }
100 }
101
102 #endregion
103 }
104 }