Carp/shared/shared.h

242 lines
4.1 KiB
C
Raw Normal View History

#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"
#include "platform.h"
2016-01-13 16:09:27 +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;
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
#ifdef max
#undef max
#endif
EXPORT int max(int x, int y) {
return x > y ? x : y;
}
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;
}
EXPORT bool nullQMARK(void *p) {
2016-01-13 16:09:27 +03:00
return p == NULL;
}
EXPORT bool not(bool x) {
2016-01-13 16:09:27 +03:00
return !x;
}
EXPORT void panic(string msg) {
2016-01-13 16:09:27 +03:00
printf("Error: %s\n", msg);
exit(1);
}
EXPORT void print(string msg) {
2016-01-13 16:09:27 +03:00
printf("%s", msg);
}
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);
}
EXPORT int* fake() {
2016-01-13 16:09:27 +03:00
return (int*)123;
}
EXPORT void fake2(string *s) {
2016-01-13 16:09:27 +03:00
}
EXPORT void eat_string(char *s) {
2016-01-16 21:07:10 +03:00
free(s);
}
EXPORT void eat_void(void *nothing) {
2016-02-16 08:09:56 +03:00
// nothing!
}
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
}
EXPORT char *string_append(char *s1, char *s2) {
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
EXPORT bool file_existsQMARK(char *filename) {
FILE *f = fopen(filename, "r");
2016-02-25 19:23:51 +03:00
bool result = f != NULL;
if(result) {
fclose(f);
}
return result;
}
typedef string* string_array;
EXPORT string_array string_array_new(int size) {
string_array a = calloc(size + 1, sizeof(string));
for(int i = 0; i < size; i++) {
2016-02-22 16:39:17 +03:00
a[i] = strdup("");
}
return a;
}
EXPORT int string_array_count(string_array array) {
int i = 0;
string_array p = array;
while(*p) {
i++;
p++;
}
return i;
}
EXPORT string string_array_get(string_array array, int pos) {
2016-02-22 16:39:17 +03:00
return strdup(array[pos]);
}
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);
return array;
}
typedef string (*string_to_string_fn)(string);
EXPORT string_array string_array_map(string_to_string_fn f, string_array array) {
string_array p = array;
while(*p) {
string old_string = *p;
string new_string = f(old_string);
*p = new_string;
p++;
}
return array;
}
EXPORT int inc(x) { return x + 1; }
EXPORT int dec(x) { return x - 1; }
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");
}
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;
}
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
}
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);
}
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
}
EXPORT void call(void *f()) {
f();
}
EXPORT void call1(void *f(int)) {
f(1);
}
EXPORT void calls(void *f(char*)) {
f("hejsan");
}
EXPORT void printret(int (*f)()) {
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;
}
#ifdef WIN32
EXPORT void sleep(int millis) {
2016-03-23 12:08:47 +03:00
carp_sleep(millis);
}
#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-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
}