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>
|
|
|
|
|
|
|
|
typedef int unknown;
|
|
|
|
typedef void* typevar;
|
|
|
|
typedef void* any;
|
|
|
|
|
|
|
|
typedef char* string;
|
|
|
|
|
|
|
|
int intsqrt(int x) { return sqrt(x); }
|
|
|
|
float itof(int x) { return (float)x; }
|
|
|
|
|
2016-01-21 21:59:01 +03:00
|
|
|
int max(int x, int y) {
|
|
|
|
return x > y ? x : y;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-20 03:23:23 +03:00
|
|
|
char *string_copy(char *s) {
|
|
|
|
return strdup(s);
|
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
a[i] = strdup("");
|
|
|
|
}
|
|
|
|
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-01-22 17:59:55 +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-01-20 16:56:31 +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;
|
|
|
|
printf("old: %s\n", old_string);
|
|
|
|
string new_string = f(old_string);
|
|
|
|
printf("new: %s\n", new_string);
|
|
|
|
/* //free(p); */
|
|
|
|
*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-01-13 16:09:27 +03:00
|
|
|
#endif
|