Created
September 9, 2021 04:35
-
-
Save jhgorse/194cee1a1fa4c2b1696abf2682f5e207 to your computer and use it in GitHub Desktop.
output power details to csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2csv in c | |
// (C) 2021 Joe Gorse | |
// BSD 3 Clause | |
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
// #include <printf.h> | |
// #include <stdint.h> | |
// static int | |
// printf_arginfo_b(const struct printf_info *info, | |
// size_t n, | |
// int *argtypes) | |
// { | |
// /* "%b" always takes one argument, a pointer to uint8_t[6]. */ | |
// if (n > 0) { | |
// argtypes[0] = PA_POINTER; | |
// } | |
// return 1; | |
// } /* printf_arginfo_M */ | |
// static int | |
// printf_output_b(FILE *stream, | |
// const struct printf_info *info, | |
// const void *const *args) | |
// { | |
// uint8_t val = *(int*)(args[0]); | |
// for (int i=7; i>=0; i--) | |
// fprintf(stream, "%d", (val>>i)&1); | |
// return 8; | |
// } /* printf_output_M */ | |
int parseline(int lcount) { | |
char c; | |
char buf[32]; | |
int ccount = 0; // char count on a word buffer | |
int wcount = 0; // word count on a row | |
unsigned short status; | |
while ((c = fgetc(stdin)) > 0) { | |
if (c == ',') { | |
buf[ccount++] = '\0'; | |
printf("%s,", buf); ++wcount; | |
if (wcount == 4 && lcount == 0) { // expand the bits of field 4, 1st row | |
sscanf(buf, "%4hx", &status); | |
for (int i=15; i>=0; i--){ | |
putchar(0x30 + ((status>>i)&1)); | |
putchar(','); | |
// printf("%01d,", (status>>i)&1); | |
} | |
// putchar('\n'); // debug | |
} | |
ccount = 0; | |
} | |
else if (c == '\n') { | |
buf[ccount++] = '\0'; | |
printf("%s", buf); ++wcount; ++lcount; | |
// printf("\nnl wcount %d ccount %d lcount %d\n", wcount, ccount, lcount); // debug | |
break; | |
} | |
else { // up to 32 bytes, add the character | |
assert(ccount < 32); | |
buf[ccount++] = c; | |
} | |
} | |
return wcount; | |
} | |
void parse_block() { | |
assert(parseline(0) == 6); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar(','); | |
assert(parseline(1) == 10); | |
putchar('\n'); | |
} | |
int main(int argc, char *argv[]) { | |
char *line = NULL; | |
size_t len = 0; | |
ssize_t read; | |
int lcount = 0; | |
printf("hello world! argc %d\n", argc); | |
for (int i=0;i<argc;i++) | |
printf("%d %s\n", i, argv[i]); | |
assert(argc==1); // no parameters for now | |
// TODO: ARGV perl-like file input | |
while (1) { | |
printf("%05d,", ++lcount); | |
parse_block(); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment