-
Notifications
You must be signed in to change notification settings - Fork 2
/
prext.c
114 lines (92 loc) · 2.9 KB
/
prext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
prnf() extensions
This source file is an example only.
It is expected to be extended with various prext_mything(mything_t mything) functions specific to your application.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <complex.h>
#include "prnf.h"
// Include the configuration file PRNF_CFG_FILE (see prnf.h)
#define prnfcfg_xstr(s) prnfcfg_str(s)
#define prnfcfg_str(s) #s
#include prnfcfg_xstr(PRNF_CFG_FILE)
#ifndef PRNF_SUPPORT_EXTENSIONS
#error You are compiling prext.c (prnf extensions), but have not enabled extensions in your config file.
#endif
#ifndef prnf_malloc
#error You are compiling prext.c (prnf extensions), but have not configured a memory allocator in your config file.
#endif
//********************************************************************************************************
// Public functions
//********************************************************************************************************
int* prext_cplex_rec(complex float c)
{
#define TXT_SIZE sizeof("(1234567890.1234567890, 1234567890.1234567890)")
char* txt = prnf_malloc(TXT_SIZE);
snprnf(txt, TXT_SIZE, "(%f, %f)", creal(c), cimag(c));
#undef TXT_SIZE
return (int*)txt;
}
int* prext_cplex_pol(complex float c)
{
#define TXT_SIZE sizeof("(1234567890.1234567890 @1234567890.1234567890)")
char* txt = prnf_malloc(TXT_SIZE);
snprnf(txt, TXT_SIZE, "(%f @%f)", cabsf(c), cargf(c));
#undef TXT_SIZE
return (int*)txt;
}
// fmt example "%y.%m.%d-%H:%M:%S"
int* prext_tstamp(const char* fmt)
{
#define TXT_SIZE 100
time_t tmr = time(NULL);
char* txt = prnf_malloc(TXT_SIZE);
PRNF_ASSERT(txt);
txt[0] = 0;
strftime(txt, TXT_SIZE, fmt, localtime(&tmr));
#undef TXT_SIZE
return (int*)txt;
}
int* prext_period(uint32_t seconds)
{
#define TXT_SIZE (sizeof("XXy XXXd XXh XXm XXs "))
char* txt;
txt = prnf_malloc(TXT_SIZE);
PRNF_ASSERT(txt);
txt[0] = 0;
if(seconds >= 31536000)
{
snappf_SL(txt, TXT_SIZE, "%"PRIu32"y ", seconds/31536000);
seconds %= 31536000;
};
if(seconds >= 86400)
{
snappf_SL(txt, TXT_SIZE, "%"PRIu32"d ", seconds/86400);
seconds %= 86400;
};
if(seconds >= 3600)
{
snappf_SL(txt, TXT_SIZE, "%"PRIu32"h ", seconds/3600);
seconds %= 3600;
};
if(seconds >= 60)
{
snappf_SL(txt, TXT_SIZE, "%"PRIu32"m ", seconds/60);
seconds %= 60;
};
if(seconds)
snappf_SL(txt, TXT_SIZE, "%"PRIu32"s ", seconds);
else if(!txt[0])
snappf_SL(txt, TXT_SIZE, "0s ");
#undef TXT_SIZE
txt[strlen(txt)-1] = 0; //remove trailing ' '
return (int*)txt;
}
//********************************************************************************************************
// Private functions
//********************************************************************************************************