Thomas Kolb
2e91fd7c42
All code is now licensed under GPLv3+. The documentation is licensed under CC BY-SA 4.0. This is now officially free software! \o/
149 lines
2.4 KiB
C
149 lines
2.4 KiB
C
/*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* Copyright (C) 2024 Thomas Kolb
|
|
* Copyright (C) 2024 Simon Ruderich
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
|
|
#include "utils.h"
|
|
|
|
bool dump_array_cf(const float complex *data, size_t n, float T, const char *filename)
|
|
{
|
|
FILE *f = fopen(filename, "wb");
|
|
if(!f) {
|
|
return false;
|
|
}
|
|
|
|
size_t written = fwrite("CPX_", 1, 4, f);
|
|
if(written != 4) {
|
|
goto err_close;
|
|
}
|
|
|
|
written = fwrite(&T, sizeof(T), 1, f);
|
|
if(written != 1) {
|
|
goto err_close;
|
|
}
|
|
|
|
written = fwrite(data, sizeof(data[0]), n, f);
|
|
if(written != n) {
|
|
goto err_close;
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
err_close:
|
|
fclose(f);
|
|
return false;
|
|
}
|
|
|
|
|
|
bool dump_array_f(const float *data, size_t n, float T, const char *filename)
|
|
{
|
|
FILE *f = fopen(filename, "w");
|
|
if(!f) {
|
|
return false;
|
|
}
|
|
|
|
size_t written = fwrite("FLT_", 1, 4, f);
|
|
if(written != 4) {
|
|
goto err_close;
|
|
}
|
|
|
|
written = fwrite(&T, sizeof(T), 1, f);
|
|
if(written != 1) {
|
|
goto err_close;
|
|
}
|
|
|
|
written = fwrite(data, sizeof(data[0]), n, f);
|
|
if(written != n) {
|
|
goto err_close;
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
err_close:
|
|
fclose(f);
|
|
return false;
|
|
}
|
|
|
|
double get_hires_time(void)
|
|
{
|
|
struct timespec clk;
|
|
clock_gettime(CLOCK_MONOTONIC, &clk);
|
|
return clk.tv_sec + 1e-9 * clk.tv_nsec;
|
|
}
|
|
|
|
void fsleep(double d)
|
|
{
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = (time_t)d;
|
|
ts.tv_nsec = (long)(1e9 * (d - (long)d));
|
|
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
void sleep_until(double hires_time)
|
|
{
|
|
struct timespec tv;
|
|
int ret;
|
|
|
|
tv.tv_sec = hires_time;
|
|
tv.tv_nsec = (uint64_t)(1e9 * hires_time) % 1000000000;
|
|
do {
|
|
ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tv, NULL);
|
|
} while(ret == EINTR);
|
|
}
|
|
|
|
void hexdump(const uint8_t *data, size_t len)
|
|
{
|
|
static const char lut[16] = "0123456789ABCDEF";
|
|
static const size_t BYTES_PER_LINE = 16;
|
|
|
|
size_t pos = 0;
|
|
|
|
while(pos < len) {
|
|
size_t bytes_in_line = len - pos;
|
|
if(bytes_in_line > BYTES_PER_LINE) {
|
|
bytes_in_line = BYTES_PER_LINE;
|
|
}
|
|
|
|
for(size_t i = 0; i < BYTES_PER_LINE; i++) {
|
|
if(i >= bytes_in_line) {
|
|
fputs(" ", stderr);
|
|
} else {
|
|
uint8_t byte = data[pos+i];
|
|
|
|
fprintf(stderr, "%c%c ", lut[byte >> 4], lut[byte & 0x0F]);
|
|
}
|
|
}
|
|
|
|
fputs(" ", stderr);
|
|
|
|
for(size_t i = 0; i < bytes_in_line; i++) {
|
|
uint8_t byte = data[pos+i];
|
|
|
|
if(isprint(byte)) {
|
|
putc(byte, stderr);
|
|
} else {
|
|
putc('.', stderr);
|
|
}
|
|
}
|
|
|
|
putc('\n', stderr);
|
|
pos += bytes_in_line;
|
|
}
|
|
}
|
|
|
|
|