Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. module test;
  2.  
  3. import Configurator;
  4. import tango.io.Stdout;
  5.  
  6. int main() {
  7. auto man = new ConfigurationManager("extensions.xml");
  8. Stdout("no problems yet?").newline.flush;
  9. return 0;
  10. }
  11.  
  12. second file
  13.  
  14. /*******************************************************************************
  15. Author: Lester L. Martin II
  16. Copyright: Lester L. Martin II and CollabEdit Project
  17. Liscense: $(GPLv1)
  18. Release: Initial, June 2009
  19. *******************************************************************************/
  20.  
  21. module src.configuration.Configurator;
  22.  
  23. private {
  24. import tango.text.xml.Document;
  25. import tango.io.model.IConduit;
  26. import tango.io.vfs.model.Vfs;
  27. import tango.io.vfs.FileFolder;
  28. import TUtil = tango.text.Util;
  29. debug(Configurator) {
  30. import tango.io.Stdout;
  31. }
  32. }
  33.  
  34. /*******************************************************************************
  35. define an extension as char[]
  36. *******************************************************************************/
  37. alias char[] Extension;
  38.  
  39. /*******************************************************************************
  40. define the operations on a file
  41. *******************************************************************************/
  42. enum Operations {
  43. Open,
  44. Close
  45. }
  46.  
  47. /*******************************************************************************
  48. Describes an entire style
  49.  
  50. Members:
  51. color; the color of this style
  52.  
  53. style; I don't understand this part as of yet? Might have
  54. something to do with fonts?
  55. *******************************************************************************/
  56. public struct Style {
  57. char[] color;
  58. char[] style;
  59. }
  60.  
  61. /*******************************************************************************
  62. Describes the Configuration for a certain extension
  63.  
  64. Members:
  65. name; name of the language
  66. ex: D, C++, not cpp,cc,d,di and stuff
  67.  
  68. keywords; a set of keywords split into groups by name
  69. ex: keywords["delimiters"] should return a char[] containing
  70. "\" \''" for d.xml
  71.  
  72. styles: a set of styles split into groups by name
  73. ex: styles["default"] should return a style
  74.  
  75. used: how many times is this configuration used, if not at all
  76. wtf is it still doing around? Change to the bool part
  77. of the union Configuration
  78. *******************************************************************************/
  79. public class ConfigurationT {
  80. public:
  81. char[] name;
  82. char[][char[]] keywords;
  83. Style[char[]] styles;
  84. ulong used;
  85. }
  86.  
  87. public union Configuration {
  88. bool isNull = true;
  89. ConfigurationT conf;
  90. }
  91.  
  92. /*******************************************************************************
  93. describes handlers as a type
  94. *******************************************************************************/
  95. typedef ConfigurationT delegate(Extension ext) OpenHandler;
  96. typedef void delegate(Extension ext) CloseHandler;
  97.  
  98. /*******************************************************************************
  99. Describes the configuration of the entire editor.
  100. Every time a file is opened, this is asked for it's corresponding syntax
  101. highlight configuration.
  102.  
  103. Has events:
  104. onClose(Extension ext) to be called when closing a file with the files
  105. extension
  106. onOpen(Extension ext) to be called when opening a file with the files
  107. extension, returns the configuration
  108.  
  109. Usage:
  110. auto conf = new ConfigurationManager("extensionDescriptors.xml");
  111. // your class then does something like this if
  112. this.register(&(conf.onOpen), Operations.Open);
  113. this.register(&(conf.onClose), Operations.Close);
  114.  
  115. // that sets your class to call it whenever it opens and whenever
  116. // it closes a file
  117.  
  118. Extra Information:
  119. The configuration manager only has the neccessary syntax/style
  120. descriptor files open only when a file using the extension is
  121. being used. If not, the tables for the Configuration and the file
  122. are closed to reduce program memory
  123. *******************************************************************************/
  124. public class ConfigurationManager {
  125. private:
  126. Configuration[Extension] configurations = null;
  127.  
  128. /*
  129. describes language by it's name... has a list of extensions as a string
  130. use something to figure out if extension (char[]) is in the string of
  131. language
  132. */
  133. char[][char[]] languages;
  134.  
  135. /*
  136. should be able to get a *.xml for ext and parse it into a Configuration
  137. should store in configuration Table
  138. if there's no configuration give it the NullConf value
  139. */
  140. void getConf(Extension ext) {
  141. foreach(Extension loc, char[] list; languages) {
  142. if(TUtil.containsPattern(ext, list)) parseExt(loc ~ ".xml");
  143. }
  144. }
  145.  
  146. /*
  147. opens a language descriptor file
  148. and parses it into the configurations
  149. */
  150. void parseExt(char[] loc) {
  151. /* open and read file, set up document (xml) */
  152. auto text = pull(new FileHost(loc));
  153. Document!(char) doc = new Document!(char);
  154.  
  155. /* parse the document before playing with it :-) */
  156. doc.parse(text);
  157.  
  158. /* parse it into the languages array */
  159. auto root = doc.tree;
  160.  
  161. auto conf = new ConfigurationT;
  162.  
  163. /* do some actual parsing :-) */
  164. foreach(elem; root.query["lang"]) {
  165. conf.name = elem.attributes.value("name").value;
  166.  
  167. foreach(elem2; elem.query["keywordLists"]) {
  168. foreach(elem3; elem2.query["keywords"]) {
  169. conf.keywords[elem3.attributes.value("name").value]
  170. = elem3.value;
  171. }
  172. }
  173.  
  174. foreach(elem2; elem.query["styles"]) {
  175. foreach(elem3; elem2.query["wordsStyle"]) {
  176. conf.styles[elem3.attributes.value("name").value]
  177. = Style(
  178. elem3.attributes.value("color").value,
  179. elem3.attributes.value("style").value
  180. );
  181. }
  182. }
  183. configurations[elem.attributes.value("name").value].conf = conf;
  184. }
  185. }
  186.  
  187. /*
  188. pulls all of the text out this input stream
  189. returns the text
  190. */
  191. char[] pull(FileHost loc) {
  192. char[] ret, temp;
  193. size_t loca;
  194.  
  195. do {
  196. loca = loc.input.read(temp);
  197. ret ~= temp;
  198. } while(loca != IOStream.Eof);
  199.  
  200. debug(Configurator) {
  201. Stdout(ret).newline.flush;
  202. }
  203.  
  204. delete loc;
  205.  
  206. return ret;
  207. }
  208.  
  209. public:
  210. /* gets master descriptor of extensions file and parses it */
  211. this(char[] loc) {
  212.  
  213. /* open and read file, set up document (xml) */
  214. auto text = pull(new FileHost(loc));
  215. Document!(char) doc = new Document!(char);
  216.  
  217. /* parse the document before playing with it :-) */
  218. doc.parse(text);
  219.  
  220. /* parse it into the languages array */
  221. auto root = doc.tree;
  222.  
  223. /* ok time for some actual parsing, hooray! */
  224. foreach(elem; root.query["extensions"]) {
  225. foreach(elem2; elem.query["ext"]) {
  226. languages[elem2.attributes.value("conf").value]
  227. = elem2.attributes.value("ext").value;
  228. }
  229. }
  230. }
  231.  
  232. /*
  233. the on open event
  234. synchronized so no 2 accesses at same file
  235. and no 2 accesses on the configurations
  236. */
  237. synchronized ConfigurationT onOpen(Extension ext) {
  238. Extension use;
  239.  
  240. foreach(Extension loc, char[] list; languages) {
  241. if(TUtil.containsPattern(ext, list)) use = loc;
  242. }
  243.  
  244. if(configurations[use].isNull)
  245. getConf(ext);
  246. configurations[use].conf.used++;
  247. return configurations[use].conf;
  248. }
  249.  
  250. /*
  251. the on close event
  252. synchronized so the proper reading of the used
  253. var is done so it can actually release mem to GC
  254. */
  255. synchronized void onClose(Extension ext) {
  256. Extension use;
  257.  
  258. foreach(Extension loc, char[] list; languages) {
  259. if(TUtil.containsPattern(ext, list)) use = loc;
  260. }
  261.  
  262. if(!configurations[use].isNull) {
  263. configurations[use].conf.used--;
  264.  
  265. if(configurations[use].conf.used <= 0) {
  266. // this should make the gc able to delete the ConfigurationT
  267. configurations[use].conf = null;
  268. configurations[use].isNull = true;
  269. }
  270. }
  271. }
  272. }
Add Comment
Please, Sign In to add comment