pipetools/bin2csv.c

111 lines
1.9 KiB
C
Raw Permalink Normal View History

2020-04-18 17:30:45 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <err.h>
#define CHUNK_DEFAULT (0x1000)
int
usage(const char* progname)
{
fprintf(stderr, "usage: %s [-c chunk [-n]] <-s size | -u size | -f size | -d size>\n", progname);
return 1;
}
int
main(int argc, char** argv)
{
int optch;
int cflag = 0;
const char* carg = NULL;
int nflag = 0;
enum { INV = -1, INT, UINT, FLOAT, DOUBLE } tflag = INV;
const char* sizearg = NULL;
while ((optch = getopt(argc, argv, "c:ns:u:f:d:h")) != -1) {
switch (optch) {
case 'c':
cflag = 1;
carg = optarg;
break;
case 'n':
nflag = cflag;
break;
case 's':
tflag = INT;
sizearg = optarg;
break;
case 'u':
tflag = UINT;
sizearg = optarg;
break;
case 'f':
tflag = FLOAT;
sizearg = optarg;
break;
case 'd':
tflag = DOUBLE;
sizearg = optarg;
break;
case 'h':
default:
return usage(*argv);
}
}
if (tflag == INV)
return usage(*argv);
size_t membsize = strtoull(sizearg, NULL, 10);
size_t chunk = cflag? strtoull(carg, NULL, 10) : CHUNK_DEFAULT;
uint8_t* buffer = aligned_alloc(0x40, chunk * membsize);
size_t rd;
if (buffer == NULL)
err(1, "malloc()");
while ((rd = fread(buffer, membsize, chunk, stdin))) {
for (size_t i = 0; i < rd; ++i) {
switch (tflag) {
case INT: {
int64_t v;
memcpy(&v, &buffer[i * membsize], membsize);
fprintf(stdout, "%ld,", v);
break;
}
case UINT: {
uint64_t v;
memcpy(&v, &buffer[i * membsize], membsize);
fprintf(stdout, "%lu,", v);
break;
}
case FLOAT: {
float v;
memcpy(&v, &buffer[i * membsize], membsize);
fprintf(stdout, "%f,", v);
break;
}
case DOUBLE: {
double v;
memcpy(&v, &buffer[i * membsize], membsize);
fprintf(stdout, "%lf,", v);
break;
}
case INV:
return 1;
}
}
if (nflag)
fputs("\n", stdout);
fflush(stdout);
}
return 0;
}