improve a_version_parse and implement a_version_tostr
[liba.git] / java / src / version.c
blob65cc0a9e567aa6873e1ace23e92c7068dc02a858
1 #include "a.h"
2 #include "a/version.h"
3 #undef a_version_check
4 #include "liba_version.h"
6 #undef L
7 #define L Java_liba_version
8 static struct
10 jmethodID alloc;
11 jfieldID ctx;
12 } L = {NULL, NULL};
14 JNIEXPORT void JNICALL Java_liba_version_clinit(JNIEnv *_env, jclass _cls)
16 jclass _bb = (*_env)->FindClass(_env, "Ljava/nio/ByteBuffer;");
17 L.ctx = (*_env)->GetFieldID(_env, _cls, "ctx", "Ljava/nio/ByteBuffer;");
18 L.alloc = (*_env)->GetStaticMethodID(_env, _bb, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
19 (*_env)->SetStaticIntField(_env, _cls, (*_env)->GetStaticFieldID(_env, _cls, "MAJOR", "I"), A_VERSION_MAJOR);
20 (*_env)->SetStaticIntField(_env, _cls, (*_env)->GetStaticFieldID(_env, _cls, "MINOR", "I"), A_VERSION_MINOR);
21 (*_env)->SetStaticIntField(_env, _cls, (*_env)->GetStaticFieldID(_env, _cls, "PATCH", "I"), A_VERSION_PATCH);
22 (*_env)->SetStaticIntField(_env, _cls, (*_env)->GetStaticFieldID(_env, _cls, "TWEAK", "I"), A_VERSION_TWEAK);
25 JNIEXPORT void JNICALL Java_liba_version_init(JNIEnv *_env, jobject _obj, jint major, jint minor, jint third, jint extra)
27 jobject _ctx = (*_env)->CallObjectMethod(_env, _obj, L.alloc, (jint)sizeof(a_version));
28 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
29 (*_env)->SetObjectField(_env, _obj, L.ctx, _ctx);
30 ctx->major = (unsigned int)major;
31 ctx->minor = (unsigned int)minor;
32 ctx->third = (unsigned int)third;
33 ctx->extra = (unsigned int)extra;
34 ctx->alpha[0] = '.';
37 JNIEXPORT jint JNICALL Java_liba_version_check(JNIEnv *_env, jclass _cls, jint major, jint minor, jint patch)
39 return (void)(_env), (void)(_cls), a_version_check((unsigned int)major, (unsigned int)minor, (unsigned int)patch);
42 JNIEXPORT jstring JNICALL Java_liba_version_toString(JNIEnv *_env, jobject _obj)
44 char str[48];
45 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
46 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
47 a_version_tostr(ctx, str, sizeof(str));
48 return (*_env)->NewStringUTF(_env, str);
51 JNIEXPORT jint JNICALL Java_liba_version_major__(JNIEnv *_env, jobject _obj)
53 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
54 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
55 return (jint)ctx->major;
58 JNIEXPORT jobject JNICALL Java_liba_version_major__I(JNIEnv *_env, jobject _obj, jint major)
60 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
61 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
62 ctx->major = (unsigned int)major;
63 return _obj;
66 JNIEXPORT jint JNICALL Java_liba_version_minor__(JNIEnv *_env, jobject _obj)
68 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
69 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
70 return (jint)ctx->minor;
73 JNIEXPORT jobject JNICALL Java_liba_version_minor__I(JNIEnv *_env, jobject _obj, jint minor)
75 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
76 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
77 ctx->minor = (unsigned int)minor;
78 return _obj;
81 JNIEXPORT jint JNICALL Java_liba_version_third__(JNIEnv *_env, jobject _obj)
83 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
84 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
85 return (jint)ctx->third;
88 JNIEXPORT jobject JNICALL Java_liba_version_third__I(JNIEnv *_env, jobject _obj, jint third)
90 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
91 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
92 ctx->third = (unsigned int)third;
93 return _obj;
96 JNIEXPORT jint JNICALL Java_liba_version_extra__(JNIEnv *_env, jobject _obj)
98 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
99 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
100 return (jint)ctx->extra;
103 JNIEXPORT jobject JNICALL Java_liba_version_extra__I(JNIEnv *_env, jobject _obj, jint extra)
105 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
106 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
107 ctx->extra = (unsigned int)extra;
108 return _obj;
111 JNIEXPORT jstring JNICALL Java_liba_version_alpha__(JNIEnv *_env, jobject _obj)
113 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
114 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
115 char alpha[sizeof(ctx->alpha) + 1];
116 a_version_alpha(ctx, alpha);
117 return (*_env)->NewStringUTF(_env, alpha);
120 JNIEXPORT jobject JNICALL Java_liba_version_alpha__Ljava_lang_String_2(JNIEnv *_env, jobject _obj, jstring _alpha)
122 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
123 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
124 char const *alpha = (*_env)->GetStringUTFChars(_env, _alpha, 0);
125 a_version_set_alpha(ctx, alpha);
126 (*_env)->ReleaseStringUTFChars(_env, _alpha, alpha);
127 return _obj;
130 JNIEXPORT jobject JNICALL Java_liba_version_parse(JNIEnv *_env, jobject _obj, jstring _ver)
132 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
133 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
134 char const *ver = (*_env)->GetStringUTFChars(_env, _ver, NULL);
135 a_version_parse(ctx, ver);
136 (*_env)->ReleaseStringUTFChars(_env, _ver, ver);
137 return _obj;
140 JNIEXPORT jboolean JNICALL Java_liba_version_lt(JNIEnv *_env, jobject _obj, jobject _ver)
142 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
143 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
144 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
145 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
146 return a_version_lt(ctx, rhs);
149 JNIEXPORT jboolean JNICALL Java_liba_version_gt(JNIEnv *_env, jobject _obj, jobject _ver)
151 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
152 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
153 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
154 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
155 return a_version_gt(ctx, rhs);
158 JNIEXPORT jboolean JNICALL Java_liba_version_le(JNIEnv *_env, jobject _obj, jobject _ver)
160 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
161 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
162 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
163 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
164 return a_version_le(ctx, rhs);
167 JNIEXPORT jboolean JNICALL Java_liba_version_ge(JNIEnv *_env, jobject _obj, jobject _ver)
169 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
170 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
171 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
172 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
173 return a_version_ge(ctx, rhs);
176 JNIEXPORT jboolean JNICALL Java_liba_version_eq(JNIEnv *_env, jobject _obj, jobject _ver)
178 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
179 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
180 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
181 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
182 return a_version_eq(ctx, rhs);
185 JNIEXPORT jboolean JNICALL Java_liba_version_ne(JNIEnv *_env, jobject _obj, jobject _ver)
187 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
188 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
189 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
190 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
191 return a_version_ne(ctx, rhs);
194 JNIEXPORT jint JNICALL Java_liba_version_cmp(JNIEnv *_env, jobject _obj, jobject _ver)
196 jobject _ctx = (*_env)->GetObjectField(_env, _obj, L.ctx);
197 jobject _rhs = (*_env)->GetObjectField(_env, _ver, L.ctx);
198 a_version *ctx = (a_version *)(*_env)->GetDirectBufferAddress(_env, _ctx);
199 a_version *rhs = (a_version *)(*_env)->GetDirectBufferAddress(_env, _rhs);
200 return a_version_cmp(ctx, rhs);