C-like mode

 
1
/* C demo code */
2
3
#include <zmq.h>
4
#include <pthread.h>
5
#include <semaphore.h>
6
#include <time.h>
7
#include <stdio.h>
8
#include <fcntl.h>
9
#include <malloc.h>
10
11
typedef struct {
12
  void* arg_socket;
13
  zmq_msg_t* arg_msg;
14
  char* arg_string;
15
  unsigned long arg_len;
16
  int arg_int, arg_command;
17
18
  int signal_fd;
19
  int pad;
20
  void* context;
21
  sem_t sem;
22
} acl_zmq_context;
23
24
#define p(X) (context->arg_##X)
25
26
void* zmq_thread(void* context_pointer) {
27
  acl_zmq_context* context = (acl_zmq_context*)context_pointer;
28
  char ok = 'K', err = 'X';
29
  int res;
30
31
  while (1) {
32
    while ((res = sem_wait(&context->sem)) == EINTR);
33
    if (res) {write(context->signal_fd, &err, 1); goto cleanup;}
34
    switch(p(command)) {
35
    case 0: goto cleanup;
36
    case 1: p(socket) = zmq_socket(context->context, p(int)); break;
37
    case 2: p(int) = zmq_close(p(socket)); break;
38
    case 3: p(int) = zmq_bind(p(socket), p(string)); break;
39
    case 4: p(int) = zmq_connect(p(socket), p(string)); break;
40
    case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &p(len)); break;
41
    case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
42
    case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
43
    case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
44
    case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
45
    }
46
    p(command) = errno;
47
    write(context->signal_fd, &ok, 1);
48
  }
49
 cleanup:
50
  close(context->signal_fd);
51
  free(context_pointer);
52
  return 0;
53
}
54
55
void* zmq_thread_init(void* zmq_context, int signal_fd) {
56
  acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
57
  pthread_t thread;
58
59
  context->context = zmq_context;
60
  context->signal_fd = signal_fd;
61
  sem_init(&context->sem, 1, 0);
62
  pthread_create(&thread, 0, &zmq_thread, context);
63
  pthread_detach(thread);
64
  return context;
65
}
66

C++ example

39
 
1
#include <iostream>
2
#include "mystuff/util.h"
3
4
namespace {
5
enum Enum {
6
  VAL1, VAL2, VAL3
7
};
8
9
char32_t unicode_string = U"\U0010FFFF";
10
string raw_string = R"delim(anything
11
you
12
want)delim";
13
14
int Helper(const MyType& param) {
15
  return 0;
16
}
17
} // namespace
18
19
class ForwardDec;
20
21
template <class T, class V>
22
class Class : public BaseClass {
23
  const MyType<T, V> member_;
24
25
 public:
26
  const MyType<T, V>& Method() const {
27
    return member_;
28
  }
29
30
  void Method2(MyType<T, V>* value);
31
}
32
33
template <class T, class V>
34
void Class::Method2(MyType<T, V>* value) {
35
  std::out << 1 >> method();
36
  value->Method3(member_);
37
  member_ = value;
38
}
39

Java example

28
 
1
import com.demo.util.MyType;
2
import com.demo.util.MyInterface;
3
4
public enum Enum {
5
  VAL1, VAL2, VAL3
6
}
7
8
public class Class<T, V> implements MyInterface {
9
  public static final MyType<T, V> member;
10
  
11
  private class InnerClass {
12
    public int zero() {
13
      return 0;
14
    }
15
  }
16
17
  @Override
18
  public MyType method() {
19
    return member;
20
  }
21
22
  public void method2(MyType<T, V> value) {
23
    method();
24
    value.method3();
25
    member = value;
26
  }
27
}
28

Simple mode that tries to handle C-like languages as well as it can. Takes two configuration parameters: keywords, an object whose property names are the keywords in the language, and useCPP, which determines whether C preprocessor directives are recognized.

MIME types defined: text/x-csrc (C code), text/x-c++src (C++ code), text/x-java (Java code), text/x-csharp (C#).