1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/utils.c

146 lines
2.6 KiB
C
Raw Normal View History

2015-06-05 12:15:31 +02:00
/** General purpose helper functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <signal.h>
2015-05-06 11:28:08 +02:00
#include <pthread.h>
2015-03-17 22:44:09 +01:00
#include "config.h"
#include "cfg.h"
#include "utils.h"
2015-09-17 00:55:20 +02:00
int version_parse(const char *s, struct version *v)
{
return sscanf(s, "%u.%u", &v->major, &v->minor) != 2;
}
int version_compare(struct version *a, struct version *b) {
int major = a->major - b->major;
int minor = a->minor - b->minor;
return major ? major : minor;
}
int strftimespec(char *s, uint max, const char *format, struct timespec *ts)
{
struct tm t;
if (localtime_r(&(ts->tv_sec), &t) == NULL)
return -1;
return strftime(s, max, format, &t);
}
double box_muller(float m, float s)
{
double x1, x2, y1;
static double y2;
static int use_last = 0;
if (use_last) { /* use value from previous call */
y1 = y2;
use_last = 0;
}
else {
double w;
do {
x1 = 2.0 * randf() - 1.0;
x2 = 2.0 * randf() - 1.0;
w = x1*x1 + x2*x2;
} while (w >= 1.0);
w = sqrt(-2.0 * log(w) / w);
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return m + y1 * s;
}
double randf()
{
return (double) random() / RAND_MAX;
}
2015-06-05 12:15:31 +02:00
/** This global variable holds the main thread ID.
* Its used by die().
*/
pthread_t _mtid;
void die()
2014-12-05 12:17:27 +01:00
{
2015-05-06 11:28:08 +02:00
if (pthread_equal(_mtid, pthread_self()))
exit(EXIT_FAILURE);
else
pthread_kill(_mtid, SIGINT);
2014-12-05 12:17:27 +01:00
}
char * strcatf(char **dest, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vstrcatf(dest, fmt, ap);
va_end(ap);
2015-08-07 01:11:43 +02:00
return *dest;
}
char * vstrcatf(char **dest, const char *fmt, va_list ap)
{
char *tmp;
int n = *dest ? strlen(*dest) : 0;
int i = vasprintf(&tmp, fmt, ap);
*dest = (char *)(realloc(*dest, n + i + 1));
if (*dest != NULL)
strncpy(*dest+n, tmp, i + 1);
free(tmp);
return *dest;
}
cpu_set_t to_cpu_set(int set)
{
cpu_set_t cset;
CPU_ZERO(&cset);
for (int i = 0; i < sizeof(set) * 8; i++) {
if (set & (1L << i))
CPU_SET(i, &cset);
}
return cset;
}
void * alloc(size_t bytes)
{
void *p = malloc(bytes);
if (!p)
error("Failed to allocate memory");
memset(p, 0, bytes);
2015-08-07 01:11:43 +02:00
return p;
}
void * memdup(const void *src, size_t bytes)
{
void *dst = alloc(bytes);
memcpy(dst, src, bytes);
return dst;
}