]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / ocaml / c_mathml_editor.cc
diff --git a/helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc b/helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
new file mode 100644 (file)
index 0000000..284f1ed
--- /dev/null
@@ -0,0 +1,157 @@
+
+#include <GdomeSmartDOMXSLT.hh>
+
+#include "ALogger.hh"
+#include "TDictionary.hh"
+#include "CMathMLFactoryXSLT.hh"
+#include "TPushLexer.hh"
+#include "TPushParser.hh"
+
+class CCallbackLogger : public ALogger
+{
+public:
+  CCallbackLogger(void (*)(int, const char*, void*), void*);
+  virtual ~CCallbackLogger() { };
+
+protected:
+  virtual void message(Level, const std::string&);
+
+private:
+  void (*callback)(int, const char*, void*);
+  void* user_data;
+};
+
+CCallbackLogger::CCallbackLogger(void (*cb)(int, const char*, void*), void* data) : callback(cb), user_data(data)
+{
+  assert(callback);
+}
+
+void
+CCallbackLogger::message(Level l, const std::string& s)
+{
+  assert(callback);
+  callback(l, s.c_str(), user_data);
+}
+
+struct Editor
+{
+  Editor(const DOM::Document&, const DOM::Document&, const DOM::Document&, void (*)(int, const char*, void*), void*);
+  ~Editor();
+
+  ALogger*        logger;
+  TDictionary*    dictionary;
+  DOMX::XSLTStylesheet* tml_mml;
+  DOMX::XSLTStylesheet* tml_tex;
+  AMathMLFactory* factory;
+  TPushParser*    parser;
+  APushLexer*     lexer;
+};
+
+Editor::Editor(const DOM::Document& dict, const DOM::Document& mml, const DOM::Document& tex,
+              void (*cb)(int, const char*, void*), void* data)
+{
+  assert(cb);
+  logger = new CCallbackLogger(cb, data);
+  dictionary = new TDictionary(*logger);
+  dictionary->load(DOM::Document(dict));
+  tml_mml = new DOMX::XSLTStylesheet(mml);
+  tml_tex = new DOMX::XSLTStylesheet(tex);
+  factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
+  parser = new TPushParser(*logger, *factory, *dictionary);
+  lexer = new TPushLexer(*logger, *parser);
+}
+
+Editor::~Editor()
+{
+  delete lexer;
+  delete parser;
+  delete factory;
+  delete tml_tex;
+  delete tml_mml;
+  delete dictionary;
+  delete logger;
+}
+
+extern "C" Editor*
+c_mathml_editor_new(GdomeDocument* dictionary,
+                   GdomeDocument* tml_mml,
+                   GdomeDocument* tml_tex,
+                   void (*log_message_cb)(int, const char*, void*),
+                   void* user_data)
+{
+  return new Editor(DOM::Document(dictionary),
+                   DOM::Document(tml_mml),
+                   DOM::Document(tml_tex), log_message_cb, user_data);
+}
+
+extern "C" void
+c_mathml_editor_destroy(Editor* editor)
+{
+  assert(editor);
+  delete editor;
+}
+
+extern "C" int
+c_mathml_editor_freeze(Editor* editor)
+{
+  assert(editor);
+  return editor->parser->freeze();
+}
+
+extern "C" int
+c_mathml_editor_thaw(Editor* editor)
+{
+  assert(editor);
+  return editor->parser->thaw();
+}
+
+extern "C" void
+c_mathml_editor_push(Editor* editor, char ch)
+{
+  assert(editor);
+  editor->lexer->push(ch);
+}
+
+extern "C" void
+c_mathml_editor_drop(Editor* editor, int alt)
+{
+  assert(editor);
+  editor->lexer->drop(alt != 0);
+}
+
+extern "C" char*
+c_mathml_editor_get_tex(const Editor* editor)
+{
+  assert(editor);
+  DOM::Document res = editor->tml_tex->apply(editor->parser->document());
+  DOM::Element root = res.get_documentElement();
+  return strdup(std::string(root.get_nodeValue()).c_str());
+}
+
+extern "C" void
+c_mathml_editor_reset(Editor* editor)
+{
+  assert(editor);
+  editor->parser->reset();
+}
+
+extern "C" GdomeDocument*
+c_mathml_editor_get_tml(const Editor* editor)
+{
+  assert(editor);
+  GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
+  GdomeDocument* doc = gdome_cast_doc(n);
+  assert(n && doc);
+  return doc;
+}
+
+extern "C" GdomeDocument*
+c_mathml_editor_get_mml(const Editor* editor)
+{
+  assert(editor);
+  GdomeNode* n = editor->factory->document().gdome_object();
+  GdomeDocument* doc = gdome_cast_doc(n);
+  assert(n && doc);
+  return doc;
+}
+