2016-02-29 15:47:37 +03:00
|
|
|
#pragma once
|
2016-01-13 16:09:27 +03:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2016-03-09 20:25:26 +03:00
|
|
|
#include <stddef.h>
|
2016-02-25 14:14:23 +03:00
|
|
|
#include "types.h"
|
2016-02-22 18:31:07 +03:00
|
|
|
#include "platform.h"
|
2016-01-13 16:09:27 +03:00
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#define EXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
#define EXPORT
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 16:09:27 +03:00
|
|
|
typedef int unknown;
|
|
|
|
typedef void* typevar;
|
|
|
|
typedef void* any;
|
|
|
|
|
|
|
|
typedef char* string;
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int intsqrt(int x) { return (int)sqrt(x); }
|
|
|
|
EXPORT float itof(int x) { return (float)x; }
|
2016-03-17 21:42:00 +03:00
|
|
|
EXPORT float dtof(double x) { return (float)x; }
|
|
|
|
EXPORT double ftod(float x) { return (double)x; }
|
2016-01-13 16:09:27 +03:00
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
#ifdef max
|
|
|
|
#undef max
|
2016-02-25 19:05:28 +03:00
|
|
|
#endif
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int max(int x, int y) {
|
2016-01-21 21:59:01 +03:00
|
|
|
return x > y ? x : y;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string itos(int x) {
|
2016-01-13 16:09:27 +03:00
|
|
|
char *s = malloc(sizeof(char) * 32);
|
|
|
|
snprintf(s, 32, "%d", x);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT bool nullQMARK(void *p) {
|
2016-01-13 16:09:27 +03:00
|
|
|
return p == NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT bool not(bool x) {
|
2016-01-13 16:09:27 +03:00
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void panic(string msg) {
|
2016-01-13 16:09:27 +03:00
|
|
|
printf("Error: %s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void print(string msg) {
|
2016-01-13 16:09:27 +03:00
|
|
|
printf("%s", msg);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void println(string msg) {
|
2016-03-02 02:22:42 +03:00
|
|
|
assert(msg);
|
2016-01-13 16:09:27 +03:00
|
|
|
printf("%s\n", msg);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int* fake() {
|
2016-01-13 16:09:27 +03:00
|
|
|
return (int*)123;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void fake2(string *s) {
|
2016-01-13 16:09:27 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void eat_string(char *s) {
|
2016-01-16 21:07:10 +03:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void eat_void(void *nothing) {
|
2016-02-16 08:09:56 +03:00
|
|
|
// nothing!
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT char *string_copy(char *s) {
|
2016-02-22 16:39:17 +03:00
|
|
|
return strdup(s);
|
2016-01-20 03:23:23 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT char *string_append(char *s1, char *s2) {
|
2016-01-18 14:05:22 +03:00
|
|
|
char *new_str = malloc(strlen(s1) + strlen(s2) + 1);
|
|
|
|
new_str[0] = '\0';
|
|
|
|
strcat(new_str, s1);
|
|
|
|
strcat(new_str, s2);
|
|
|
|
return new_str;
|
|
|
|
}
|
2016-01-18 00:56:35 +03:00
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT bool file_existsQMARK(char *filename) {
|
2016-01-18 16:27:46 +03:00
|
|
|
FILE *f = fopen(filename, "r");
|
2016-02-25 19:23:51 +03:00
|
|
|
bool result = f != NULL;
|
|
|
|
if(result) {
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
return result;
|
2016-01-18 16:27:46 +03:00
|
|
|
}
|
|
|
|
|
2016-01-20 14:22:55 +03:00
|
|
|
typedef string* string_array;
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string_array string_array_new(int size) {
|
2016-01-20 14:22:55 +03:00
|
|
|
string_array a = calloc(size + 1, sizeof(string));
|
2016-01-20 12:58:57 +03:00
|
|
|
for(int i = 0; i < size; i++) {
|
2016-02-22 16:39:17 +03:00
|
|
|
a[i] = strdup("");
|
2016-01-20 12:58:57 +03:00
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int string_array_count(string_array array) {
|
2016-01-20 12:58:57 +03:00
|
|
|
int i = 0;
|
2016-01-20 14:22:55 +03:00
|
|
|
string_array p = array;
|
2016-01-20 12:58:57 +03:00
|
|
|
while(*p) {
|
|
|
|
i++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string string_array_get(string_array array, int pos) {
|
2016-02-22 16:39:17 +03:00
|
|
|
return strdup(array[pos]);
|
2016-01-20 12:58:57 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string_array string_array_set(string_array array, int pos, string new_value) {
|
2016-02-22 16:39:17 +03:00
|
|
|
array[pos] = strdup(new_value);
|
2016-01-20 12:58:57 +03:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2016-01-25 18:55:56 +03:00
|
|
|
typedef string (*string_to_string_fn)(string);
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string_array string_array_map(string_to_string_fn f, string_array array) {
|
2016-01-25 18:55:56 +03:00
|
|
|
string_array p = array;
|
|
|
|
while(*p) {
|
|
|
|
string old_string = *p;
|
|
|
|
string new_string = f(old_string);
|
|
|
|
*p = new_string;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int inc(x) { return x + 1; }
|
|
|
|
EXPORT int dec(x) { return x - 1; }
|
2016-01-21 22:47:42 +03:00
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void async(void *f) {
|
2016-02-10 13:37:33 +03:00
|
|
|
printf("Async starting.\n");
|
2016-02-20 12:39:54 +03:00
|
|
|
carp_thread_t th = carp_thread_create(f, "Async");
|
|
|
|
carp_thread_destroy(th);
|
2016-02-10 13:37:33 +03:00
|
|
|
printf("Async done.\n");
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT int last_index_of(string s, char c) {
|
2016-02-21 11:12:37 +03:00
|
|
|
int len = (int)strlen(s);
|
2016-02-11 13:57:25 +03:00
|
|
|
for(int i = len - 1; i >= 0; i--) {
|
|
|
|
if(s[i] == c) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string substring(string s, int index) {
|
2016-02-11 13:57:25 +03:00
|
|
|
if(index >= strlen(s)) {
|
|
|
|
panic("substring out of bounds");
|
|
|
|
}
|
|
|
|
const char *sub = s + index;
|
2016-02-22 16:39:17 +03:00
|
|
|
return strdup(sub);
|
2016-02-11 13:57:25 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string file_path_component(string s) {
|
2016-02-11 13:57:25 +03:00
|
|
|
int i = last_index_of(s, '/');
|
|
|
|
return substring(s, i + 1);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT string get_input() {
|
2016-02-11 14:10:00 +03:00
|
|
|
char in[1024];
|
|
|
|
fgets(in, 1024, stdin);
|
2016-02-22 16:39:17 +03:00
|
|
|
return strdup(in);
|
2016-02-11 14:10:00 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void call(void *f()) {
|
2016-02-12 01:39:05 +03:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void call1(void *f(int)) {
|
2016-02-12 01:39:05 +03:00
|
|
|
f(1);
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void calls(void *f(char*)) {
|
2016-02-12 01:39:05 +03:00
|
|
|
f("hejsan");
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
EXPORT void printret(int (*f)()) {
|
2016-02-12 01:54:24 +03:00
|
|
|
int x = f();
|
|
|
|
printf("ret = %d\n", x);
|
|
|
|
}
|
|
|
|
|
2016-03-04 13:50:03 +03:00
|
|
|
EXPORT int mod(int x, int y) {
|
|
|
|
return x % y;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:38:16 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
EXPORT void sleep(int millis) {
|
2016-03-23 12:08:47 +03:00
|
|
|
carp_sleep(millis);
|
2016-02-25 18:38:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-24 20:52:47 +03:00
|
|
|
typedef struct {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
} FauxVec2;
|
|
|
|
|
|
|
|
FauxVec2 *position() {
|
|
|
|
FauxVec2 *v2 = malloc(sizeof(FauxVec2));
|
|
|
|
v2->x = 100.0f;
|
|
|
|
v2->y = 200.0f;
|
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:14:01 +03:00
|
|
|
EXPORT CARP_PLATFORM platform() {
|
|
|
|
return carp_get_platform();
|
|
|
|
}
|
2016-02-25 16:38:28 +03:00
|
|
|
|
2016-03-23 12:08:47 +03:00
|
|
|
EXPORT string get_normal_console_color() {
|
|
|
|
#ifdef WIN32
|
|
|
|
return strdup("");
|
|
|
|
#else
|
|
|
|
return strdup("\e[0m");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT string get_console_color(int x) {
|
|
|
|
#ifdef WIN32
|
|
|
|
return strdup("");
|
|
|
|
#else
|
|
|
|
char buffer[16];
|
|
|
|
snprintf(buffer, 16, "\e[3%dm", x);
|
|
|
|
return strdup(buffer);
|
|
|
|
#endif
|
|
|
|
}
|