Blame src/common.c

8535a3
1015c5
#include <glib.h></glib.h>
1015c5
8535a3
#include "private.h"
8535a3
8535a3
8535a3
cairo_t *heliCairo;
8eb855
int heliInitialized;
8eb855
HeliArray heliObjectsSet;
8535a3
8535a3
1015c5
struct _Directory {
1015c5
	HeliArray files;
1015c5
};
1015c5
1015c5
09c823
static char *colors[] = {
09c823
	"transparent", "0 0 0 0",
09c823
	
09c823
	"black",       "0 0 0 1",
09c823
	"white",       "1 1 1 1",
09c823
	"gray",        "0.5 0.5 0.5 1",
09c823
	
09c823
	"red",         "1 0 0 1",
09c823
	"green"        "0 1 0 1",
09c823
	"blue"         "0 0 1 1",
09c823
	
09c823
	"yellow",      "1 1 0 1",
09c823
	"magenta",     "1 0 1 1",
09c823
	"cyan",        "0 1 1 1",
09c823
09c823
	"yellow",      "1 1 0 1",
09c823
	"magenta",     "1 0 1 1",
09c823
	"cyan",        "0 1 1 1",
09c823
	
09c823
	"brown",       "0.5 0.5 0 1",
09c823
};
09c823
09c823
8535a3
int randomNumber(int min, int max)
8535a3
	{ return max <= min ? min : rand()%(max - min + 1) + min; }
8535a3
8535a3
double randomFloat()
8535a3
	{ return (double)rand()/(double)RAND_MAX; }
8535a3
8535a3
1015c5
Directory openDirectory(const char *path) {
8eb855
	if (!heliInitialized) return NULL;
8eb855
1015c5
	GDir *dir = g_dir_open(path, 0, NULL);
1015c5
	if (!dir) return NULL;
1015c5
	
1015c5
	Directory d = calloc(1, sizeof(Directory));
1015c5
	while(TRUE) {
1015c5
		const char* name = g_dir_read_name(dir);
1015c5
		if (!name) break;
1015c5
		if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
1015c5
		heliStringmapAdd(&d->files, name, NULL, NULL);
1015c5
	}
1015c5
	g_dir_close(dir);
1015c5
	
8eb855
	heliObjectRegister(d, (HeliFreeCallback)&closeDirectory);
1015c5
	return d;
1015c5
}
1015c5
1015c5
void closeDirectory(Directory directory) {
1015c5
	if (!directory) return;
8eb855
	heliObjectUnregister(directory);
1015c5
	heliArrayDestroy(&directory->files);
1015c5
	free(directory);
1015c5
}
1015c5
1015c5
int directoryGetCount(Directory directory)
1015c5
	{ return directory->files.count; }
1015c5
1015c5
const char* directoryGet(Directory directory, int i)
1015c5
	{ return (const char*)heliArrayGetKey(&directory->files, i); }
1015c5
1015c5
1015c5
int fileExists(const char *path)
1015c5
	{ return g_file_test(path, G_FILE_TEST_IS_REGULAR) ? TRUE : FALSE; }
1015c5
int directoryExists(const char *path)
1015c5
	{ return g_file_test(path, G_FILE_TEST_IS_DIR) ? TRUE : FALSE; }
1015c5
1015c5
8535a3
char* heliStringCopy(const char *x) {
8535a3
	int len = strlen(x) + 1;
8535a3
	char *cp = malloc(len + 1);
8535a3
	memcpy(cp, x, len);
8535a3
	return cp;
8535a3
}
8535a3
8535a3
char* heliStringConcat3(const char *a, const char *b, const char *c) {
8535a3
	int la = strlen(a);
8535a3
	int lb = strlen(b);
8535a3
	int lc = strlen(c);
8535a3
	char *s = malloc(la + lb + lc + 1);
8535a3
	memcpy(s, a, la);
8535a3
	memcpy(s + la, b, lb);
8535a3
	memcpy(s + la + lb, c, lc);
8535a3
	s[la + lb + lc] = 0;
8535a3
	return s;
8535a3
}
8535a3
8535a3
int heliStringEndsWithLowcase(const char *s, const char *tail) {
8535a3
	int ls = strlen(s);
8535a3
	int lt = strlen(tail);
8535a3
	if (lt > ls) return FALSE;
8535a3
	for(int i = 0; i < lt; ++i)
8535a3
		if (tolower(s[ls - i]) != tolower(tail[lt - i]))
8535a3
			return FALSE;
8535a3
	return TRUE;
8535a3
}
8535a3
8535a3
void heliLowercase(char *x)
981405
	{ while(*x) { *x = tolower(*x); ++x; } }
8535a3
09c823
8535a3
void heliParseColor(const char *x, double *color) {
09c823
	color[0] = color[1] = color[2] = 0;
09c823
	color[3] = 1;
09c823
	
09c823
	if (*x == '#') {
09c823
		++x;
09c823
		int hex[8] = { 0, 0, 0, 0, 0, 0, 15, 15 };
09c823
		for(int i = 0; *x && i < 8; ++i) {
09c823
			char c = tolower(*x);
09c823
			if (c >= '0' && c <= '9') hex[i] = c - '0';
09c823
			if (c >= 'a' && c <= 'f') hex[i] = c - 'a' + 10;
09c823
		}
09c823
		for(int i = 0; i < 4; ++i)
09c823
			color[i] = (hex[i*2]*16 + hex[i*2 + 1])/255.0;
09c823
	} else
09c823
	if (isalpha(*x)) {
09c823
		int count = (int)(sizeof(colors)/sizeof(*colors)/2);
09c823
		for(int i = 0; i < count; ++i)
09c823
			if (strcmp(x, colors[i*2]) == 0)
09c823
				{ heliParseColor(colors[i*2 + 1], color); return; }
09c823
	} else {
09c823
		sscanf(x, "%lf %lf %lf %lf", &color[0], &color[1], &color[2], &color[3]);
09c823
	}
8535a3
}
8535a3
09c823
8eb855
static int objectsCompare(const void *a, const void *b)
8eb855
	{ return a < b ? -1 : b < a ? 1 : 0; }
8eb855
8eb855
static void* objectsKeyClone(void *k)
8eb855
	{ return k; }
8eb855
8eb855
void heliObjectRegister(void *o, HeliFreeCallback fo)
8eb855
	{ heliMapAdd(&heliObjectsSet, o, &objectsCompare, (HeliCloneCallback)objectsKeyClone, fo, NULL, NULL); }
8eb855
8eb855
void heliObjectUnregister(void *o) {
8eb855
	int i;
8eb855
	HeliPair *item = heliMapFind(&heliObjectsSet, o, &objectsCompare, &i);
8eb855
	if (item) {
8eb855
		item->freeKey = NULL;
8eb855
		heliArrayRemove(&heliObjectsSet, i);
8eb855
	}
8eb855
}