2016-01-13 16:09:27 +03:00
|
|
|
#ifndef SHARED_H
|
|
|
|
#define SHARED_H
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2016-02-25 14:14:23 +03:00
|
|
|
#include "types.h"
|
|
|
|
#ifndef NO_PLATFORM
|
2016-02-22 18:31:07 +03:00
|
|
|
#include "platform.h"
|
2016-02-25 14:14:23 +03:00
|
|
|
#endif
|
2016-01-13 16:09:27 +03:00
|
|
|
|
|
|
|
typedef int unknown;
|
|
|
|
typedef void* typevar;
|
|
|
|
typedef void* any;
|
|
|
|
|
|
|
|
typedef char* string;
|
|
|
|
|
2016-02-21 11:12:37 +03:00
|
|
|
int intsqrt(int x) { return (int)sqrt(x); }
|
2016-01-13 16:09:27 +03:00
|
|
|
float itof(int x) { return (float)x; }
|
|
|
|
|
2016-02-20 12:39:54 +03:00
|
|
|
#ifndef max
|
2016-01-21 21:59:01 +03:00
|
|
|
int max(int x, int y) {
|
|
|
|
return x > y ? x : y;
|
|
|
|
}
|
2016-02-20 12:39:54 +03:00
|
|
|
#endif
|
2016-01-21 21:59:01 +03:00
|
|
|
|
2016-01-13 16:09:27 +03:00
|
|
|
string itos(int x) {
|
|
|
|
char *s = malloc(sizeof(char) * 32);
|
|
|
|
snprintf(s, 32, "%d", x);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nullQMARK(void *p) {
|
|
|
|
return p == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool not(bool x) {
|
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void panic(string msg) {
|
|
|
|
printf("Error: %s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-02-11 13:06:06 +03:00
|
|
|
|
|
|
|
|
2016-01-13 16:09:27 +03:00
|
|
|
void print(string msg) {
|
|
|
|
printf("%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void println(string msg) {
|
|
|
|
printf("%s\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int* fake() {
|
|
|
|
return (int*)123;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fake2(string *s) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-16 21:07:10 +03:00
|
|
|
void eat_string(char *s) {
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2016-02-16 08:09:56 +03:00
|
|
|
void eat_void(void *nothing) {
|
|
|
|
// nothing!
|
|
|
|
}
|
|
|
|
|
2016-01-20 03:23:23 +03:00
|
|
|
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-01-29 23:25:05 +03:00
|
|
|
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-01-18 16:27:46 +03:00
|
|
|
bool file_existsQMARK(char *filename) {
|
|
|
|
FILE *f = fopen(filename, "r");
|
|
|
|
return f != NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:22:55 +03:00
|
|
|
typedef string* string_array;
|
|
|
|
|
|
|
|
string_array string_array_new(int size) {
|
|
|
|
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-01-20 14:22:55 +03:00
|
|
|
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-01-20 14:22:55 +03:00
|
|
|
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-01-20 14:22:55 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-21 22:47:42 +03:00
|
|
|
int inc(x) { return x + 1; }
|
|
|
|
int dec(x) { return x - 1; }
|
|
|
|
|
2016-02-10 13:37:33 +03:00
|
|
|
void async(void *f) {
|
|
|
|
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-11 13:57:25 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
string substring(string s, int index) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
string file_path_component(string s) {
|
|
|
|
int i = last_index_of(s, '/');
|
|
|
|
return substring(s, i + 1);
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:10:00 +03:00
|
|
|
string get_input() {
|
|
|
|
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-12 01:39:05 +03:00
|
|
|
void call(void *f()) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
void call1(void *f(int)) {
|
|
|
|
f(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void calls(void *f(char*)) {
|
|
|
|
f("hejsan");
|
|
|
|
}
|
|
|
|
|
2016-02-12 01:54:24 +03:00
|
|
|
void printret(int (*f)()) {
|
|
|
|
int x = f();
|
|
|
|
printf("ret = %d\n", x);
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:09:27 +03:00
|
|
|
#endif
|