index.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!doctype html>
  2. <title>CodeMirror: C-like mode</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../../doc/docs.css">
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="../../addon/edit/matchbrackets.js"></script>
  8. <script src="clike.js"></script>
  9. <style>.CodeMirror {border: 2px inset #dee;}</style>
  10. <div id=nav>
  11. <a href="http://codemirror.net"><img id=logo src="../../doc/logo.png"></a>
  12. <ul>
  13. <li><a href="../../index.html">Home</a>
  14. <li><a href="../../doc/manual.html">Manual</a>
  15. <li><a href="https://github.com/marijnh/codemirror">Code</a>
  16. </ul>
  17. <ul>
  18. <li><a href="../index.html">Language modes</a>
  19. <li><a class=active href="#">C-like</a>
  20. </ul>
  21. </div>
  22. <article>
  23. <h2>C-like mode</h2>
  24. <div><textarea id="c-code">
  25. /* C demo code */
  26. #include <zmq.h>
  27. #include <pthread.h>
  28. #include <semaphore.h>
  29. #include <time.h>
  30. #include <stdio.h>
  31. #include <fcntl.h>
  32. #include <malloc.h>
  33. typedef struct {
  34. void* arg_socket;
  35. zmq_msg_t* arg_msg;
  36. char* arg_string;
  37. unsigned long arg_len;
  38. int arg_int, arg_command;
  39. int signal_fd;
  40. int pad;
  41. void* context;
  42. sem_t sem;
  43. } acl_zmq_context;
  44. #define p(X) (context->arg_##X)
  45. void* zmq_thread(void* context_pointer) {
  46. acl_zmq_context* context = (acl_zmq_context*)context_pointer;
  47. char ok = 'K', err = 'X';
  48. int res;
  49. while (1) {
  50. while ((res = sem_wait(&amp;context->sem)) == EINTR);
  51. if (res) {write(context->signal_fd, &amp;err, 1); goto cleanup;}
  52. switch(p(command)) {
  53. case 0: goto cleanup;
  54. case 1: p(socket) = zmq_socket(context->context, p(int)); break;
  55. case 2: p(int) = zmq_close(p(socket)); break;
  56. case 3: p(int) = zmq_bind(p(socket), p(string)); break;
  57. case 4: p(int) = zmq_connect(p(socket), p(string)); break;
  58. case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &amp;p(len)); break;
  59. case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
  60. case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
  61. case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
  62. case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
  63. }
  64. p(command) = errno;
  65. write(context->signal_fd, &amp;ok, 1);
  66. }
  67. cleanup:
  68. close(context->signal_fd);
  69. free(context_pointer);
  70. return 0;
  71. }
  72. void* zmq_thread_init(void* zmq_context, int signal_fd) {
  73. acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
  74. pthread_t thread;
  75. context->context = zmq_context;
  76. context->signal_fd = signal_fd;
  77. sem_init(&amp;context->sem, 1, 0);
  78. pthread_create(&amp;thread, 0, &amp;zmq_thread, context);
  79. pthread_detach(thread);
  80. return context;
  81. }
  82. </textarea></div>
  83. <h2>C++ example</h2>
  84. <div><textarea id="cpp-code">
  85. #include <iostream>
  86. #include "mystuff/util.h"
  87. namespace {
  88. enum Enum {
  89. VAL1, VAL2, VAL3
  90. };
  91. int Helper(const MyType& param) {
  92. return 0;
  93. }
  94. } // namespace
  95. class ForwardDec;
  96. template <class T, class V>
  97. class Class : public BaseClass {
  98. const MyType<T, V> member_;
  99. public:
  100. const MyType<T, V>& Method() const {
  101. return member_;
  102. }
  103. void Method2(MyType<T, V>* value);
  104. }
  105. template <class T, class V>
  106. void Class::Method2(MyType<T, V>* value) {
  107. std::out << 1 >> method();
  108. value->Method3(member_);
  109. member_ = value;
  110. }
  111. </textarea></div>
  112. <h2>Java example</h2>
  113. <div><textarea id="java-code">
  114. import com.demo.util.MyType;
  115. import com.demo.util.MyInterface;
  116. public enum Enum {
  117. VAL1, VAL2, VAL3
  118. }
  119. public class Class<T, V> implements MyInterface {
  120. public static final MyType<T, V> member;
  121. private class InnerClass {
  122. public int zero() {
  123. return 0;
  124. }
  125. }
  126. @Override
  127. public MyType method() {
  128. return member;
  129. }
  130. public void method2(MyType<T, V> value) {
  131. method();
  132. value.method3();
  133. member = value;
  134. }
  135. }
  136. </textarea></div>
  137. <script>
  138. var editor = CodeMirror.fromTextArea(document.getElementById("c-code"), {
  139. lineNumbers: true,
  140. matchBrackets: true,
  141. mode: "text/x-csrc"
  142. });
  143. var editor = CodeMirror.fromTextArea(document.getElementById("cpp-code"), {
  144. lineNumbers: true,
  145. matchBrackets: true,
  146. mode: "text/x-c++src"
  147. });
  148. var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
  149. lineNumbers: true,
  150. matchBrackets: true,
  151. mode: "text/x-java"
  152. });
  153. </script>
  154. <p>Simple mode that tries to handle C-like languages as well as it
  155. can. Takes two configuration parameters: <code>keywords</code>, an
  156. object whose property names are the keywords in the language,
  157. and <code>useCPP</code>, which determines whether C preprocessor
  158. directives are recognized.</p>
  159. <p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
  160. (C code), <code>text/x-c++src</code> (C++
  161. code), <code>text/x-java</code> (Java
  162. code), <code>text/x-csharp</code> (C#).</p>
  163. </article>