Properties.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // $Id$
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace Lisp
  6. {
  7. public class Properties {
  8. private Hashtable Props = new Hashtable();
  9. private ArrayList LinearList;
  10. public Properties(List List) {
  11. LinearList = new ArrayList();
  12. for(int i = 0; i < List.Length; ++i) {
  13. object o = List[i];
  14. if(i == 0 && o is Symbol)
  15. continue;
  16. List ChildList = o as List;
  17. LinearList.Add(ChildList);
  18. if(o == null)
  19. throw new LispException("Child of properties lisp is not a list");
  20. if(ChildList.Length > 0) {
  21. Symbol name = ChildList[0] as Symbol;
  22. if(name == null)
  23. throw new LispException("property has no symbol as name");
  24. object old = Props[name.Name];
  25. if(old == null) {
  26. ArrayList AList = new ArrayList();
  27. AList.Add(ChildList);
  28. Props[name.Name] = AList;
  29. } else {
  30. ((ArrayList) old).Add(ChildList);
  31. }
  32. }
  33. }
  34. }
  35. private List Find(string Name) {
  36. ArrayList AList = (ArrayList) Props[Name];
  37. if(AList == null)
  38. return null;
  39. return (List) AList[0];
  40. }
  41. /// <summary>Checks if element exists</summary>
  42. /// <param name="Name">Name of element to find.</param>
  43. /// <returns>False if element doesn't exist, otherwise true.</returns>
  44. public bool Exists(string Name) {
  45. List list = Find(Name);
  46. if (list == null)
  47. return false;
  48. return true;
  49. }
  50. public bool Get(string Name, ref int Val) {
  51. List list = Find(Name);
  52. if(list == null)
  53. return false;
  54. if(! (list[1] is int))
  55. return false;
  56. Val = (int) list[1];
  57. return true;
  58. }
  59. public bool Get(string Name, ref uint Val) {
  60. List list = Find(Name);
  61. if(list == null)
  62. return false;
  63. if(! (list[1] is int))
  64. return false;
  65. int v = (int) list[1];
  66. if(v < 0)
  67. return false;
  68. Val = (uint) v;
  69. return true;
  70. }
  71. public bool Get(string Name, ref float Val) {
  72. List list = Find(Name);
  73. if(list == null)
  74. return false;
  75. if(list[1] is float) {
  76. Val = (float) list[1];
  77. return true;
  78. }
  79. if(list[1] is int) {
  80. Val = (float) ((int) list[1]);
  81. return true;
  82. }
  83. return false;
  84. }
  85. public bool Get(string Name, ref string Val) {
  86. List list = Find(Name);
  87. if(list == null)
  88. return false;
  89. if(!(list.Length >= 2 && list[1] is string))
  90. return false;
  91. Val = (string) list[1];
  92. return true;
  93. }
  94. public bool Get(string Name, ref bool Val) {
  95. List list = Find(Name);
  96. if(list == null)
  97. return false;
  98. if(! (list[1] is bool))
  99. return false;
  100. Val = (bool) list[1];
  101. return true;
  102. }
  103. /// <summary>
  104. /// Gets a string from somewhere unknown
  105. /// (due to bad documentation in the rest of this class) into an Enum.
  106. /// </summary>
  107. /// <param name="Name">Name of attribute to find.</param>
  108. /// <param name="Val">The <see cref="Enum"/> is returned in this.</param>
  109. /// <param name="proptype">A <see cref="Type"/> of the Enum we want.</param>
  110. /// <returns>False if we failed to get the value Name, otherwise true.</returns>
  111. public bool Get(string Name, ref Enum Val, Type proptype) {
  112. List list = Find(Name);
  113. if (list == null)
  114. return false;
  115. if (!(list[1] is string))
  116. return false;
  117. try {
  118. Val = (Enum)Enum.Parse(proptype, (string)list[1]);
  119. } catch(System.ArgumentException) {
  120. Console.WriteLine($"Properties.Get(): failed to convert: '{list[1]}' to enum at '{Name}'");
  121. throw;
  122. }
  123. return true;
  124. }
  125. public bool Get(string Name, ref List Val) {
  126. List list = Find(Name);
  127. if(list == null)
  128. return false;
  129. Val = list;
  130. return true;
  131. }
  132. public bool GetStringList(string Name, List<string> AList) {
  133. List list = Find(Name);
  134. if(list == null)
  135. return false;
  136. for(int i = 1; i < list.Length; ++i) {
  137. try {
  138. AList.Add((string) list[i]);
  139. }
  140. catch(InvalidCastException) {
  141. AList.Add(((Lisp.Symbol)list[i]).Name);
  142. }
  143. }
  144. return true;
  145. }
  146. public bool GetLispList(string Name, out Lisp.List result) {
  147. List list = Find(Name);
  148. if(list == null) {
  149. result = null;
  150. return false;
  151. } else {
  152. object[] entries = new object[list.Length - 1];
  153. for(int i = 1; i < list.Length; ++i) {
  154. entries[i - 1] = list[i];
  155. }
  156. result = new List(entries);
  157. return true;
  158. }
  159. }
  160. public bool GetUIntList(string Name, List<uint> AList) {
  161. List list = Find(Name);
  162. if(list == null)
  163. return false;
  164. for(int i = 1; i < list.Length; ++i) {
  165. int v = (int) list[i];
  166. AList.Add((uint) v);
  167. }
  168. return true;
  169. }
  170. public bool GetIntList(string Name, List<int> AList) {
  171. List list = Find(Name);
  172. if(list == null)
  173. return false;
  174. for(int i = 1; i < list.Length; ++i) {
  175. AList.Add((int) list[i]);
  176. }
  177. return true;
  178. }
  179. public bool GetFloatList(string Name, List<float> AList) {
  180. List list = Find(Name);
  181. if(list == null)
  182. return false;
  183. for(int i = 1; i < list.Length; ++i) {
  184. if( list[i] is float) {
  185. AList.Add((float) list[i]);
  186. } else if(list[1] is int) {
  187. AList.Add((float) ((int) list[i]));
  188. } else {
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. public IList GetList(string ChildType) {
  195. ArrayList AList = (ArrayList) Props[ChildType];
  196. if(AList == null)
  197. return new ArrayList();
  198. return AList;
  199. }
  200. public IList GetList() {
  201. return LinearList;
  202. }
  203. public void PrintUnusedWarnings() {
  204. }
  205. }
  206. }