tbftss/src/system/util.c

204 lines
3.5 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
Copyright (C) 2015-2019,2022 Parallel Realities
2015-10-20 13:51:49 +02:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <time.h>
2022-07-31 11:43:20 +02:00
#include "../common.h"
#include "../json/cJSON.h"
2022-07-31 11:43:20 +02:00
#include "util.h"
2015-10-20 13:51:49 +02:00
2015-12-10 11:05:00 +01:00
float mod(float n, float x)
2015-10-20 13:51:49 +02:00
{
2015-12-10 11:05:00 +01:00
return fmod(fmod(n, x) + x, x);
2015-10-20 13:51:49 +02:00
}
int getPercent(float current, float total)
{
2016-03-03 08:47:33 +01:00
return total != 0 ? (current / total) * 100 : 0;
2015-10-20 13:51:49 +02:00
}
int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
return (MAX(x1, x2) < MIN(x1 + w1, x2 + w2)) && (MAX(y1, y2) < MIN(y1 + h1, y2 + h2));
}
float getAngle(int x1, int y1, int x2, int y2)
{
float angle = -90 + atan2(y1 - y2, x1 - x2) * (180 / PI);
return angle >= 0 ? angle : 360 + angle;
}
int getDistance(int x1, int y1, int x2, int y2)
{
int x, y;
2015-10-20 13:51:49 +02:00
x = x2 - x1;
y = y2 - y1;
2022-07-31 11:43:20 +02:00
return sqrt(x * x + y * y);
2015-10-20 13:51:49 +02:00
}
2016-03-12 19:22:48 +01:00
char **toTypeArray(char *types, int *numTypes)
{
2022-07-31 11:43:20 +02:00
int i;
2016-03-12 19:22:48 +01:00
char **typeArray, *type;
*numTypes = 1;
2022-07-31 11:43:20 +02:00
for (i = 0; i < strlen(types); i++)
2016-03-12 19:22:48 +01:00
{
if (types[i] == ';')
{
*numTypes = *numTypes + 1;
}
}
2022-07-31 11:43:20 +02:00
typeArray = malloc(*numTypes * sizeof(char *));
2016-03-12 19:22:48 +01:00
i = 0;
type = strtok(types, ";");
while (type)
{
typeArray[i] = malloc(strlen(type) + 1);
strcpy(typeArray[i], type);
type = strtok(NULL, ";");
i++;
}
return typeArray;
}
2016-02-28 14:02:57 +01:00
char *timeToString(long millis, int showHours)
{
static char TIME[MAX_NAME_LENGTH];
2016-02-28 14:02:57 +01:00
int hours, minutes, seconds;
2016-02-28 14:02:57 +01:00
seconds = millis / FPS;
2016-06-26 16:40:49 +02:00
minutes = (seconds / FPS) % 60;
hours = seconds / (FPS * FPS);
2016-02-28 14:02:57 +01:00
seconds %= 60;
2016-02-28 14:02:57 +01:00
if (showHours)
{
2016-06-26 16:40:49 +02:00
sprintf(TIME, "%dh %02dm %02ds", hours, minutes, seconds);
2016-02-28 14:02:57 +01:00
}
else
{
sprintf(TIME, "%dm %02ds", minutes, seconds);
}
2016-02-28 14:02:57 +01:00
return TIME;
}
2016-05-15 09:19:26 +02:00
char *timeToDate(long millis)
{
static char DATE[MAX_NAME_LENGTH];
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
struct tm *timeinfo;
2022-07-31 11:43:20 +02:00
time_t time;
2019-11-07 09:13:57 +01:00
2018-12-13 08:34:46 +01:00
time = millis;
2019-11-07 09:13:57 +01:00
2018-12-13 08:34:46 +01:00
timeinfo = localtime(&time);
2019-11-07 09:13:57 +01:00
2021-05-01 14:32:57 +02:00
strftime(DATE, MAX_NAME_LENGTH, "%d %b %Y, %H:%M%p", timeinfo);
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
return DATE;
}
char *getJSONValueStr(cJSON *node, char *name, char *defValue)
{
cJSON *child;
if (node)
{
child = cJSON_GetObjectItem(node, name);
if (child)
{
return child->valuestring;
}
}
return defValue;
}
int getJSONValue(cJSON *node, char *name, int defValue)
{
2016-03-04 23:11:13 +01:00
cJSON *child;
2016-03-04 23:11:13 +01:00
if (node)
{
2016-03-04 23:11:13 +01:00
child = cJSON_GetObjectItem(node, name);
if (child)
{
return child->valueint;
}
}
2016-03-04 23:11:13 +01:00
return defValue;
}
void *resize(void *array, int oldSize, int newSize)
{
void **newArray;
2022-07-31 11:43:20 +02:00
int copySize;
2019-11-07 09:13:57 +01:00
2016-03-06 10:48:29 +01:00
copySize = newSize > oldSize ? oldSize : newSize;
2019-11-07 09:13:57 +01:00
newArray = malloc(newSize);
memset(newArray, 0, newSize);
2016-03-06 10:48:29 +01:00
memcpy(newArray, array, copySize);
free(array);
2019-11-07 09:13:57 +01:00
return newArray;
}
2018-12-06 09:37:19 +01:00
unsigned long hashcode(const char *str)
{
2022-07-31 11:43:20 +02:00
unsigned long hash = 5381;
int c;
2018-12-06 09:37:19 +01:00
c = *str;
while (c)
{
2022-07-31 11:43:20 +02:00
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2018-12-06 09:37:19 +01:00
2022-07-31 11:43:20 +02:00
c = *str++;
2018-12-06 09:37:19 +01:00
}
hash = ((hash << 5) + hash);
2019-11-07 09:13:57 +01:00
2018-12-06 09:37:19 +01:00
return hash;
}
int stringComparator(const void *a, const void *b)
{
2022-07-31 11:43:20 +02:00
char **s1 = (char **)a;
char **s2 = (char **)b;
return strcmp(*s1, *s2);
}