-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.h
148 lines (118 loc) · 6.44 KB
/
format.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef __FORMAT_H__
#define __FORMAT_H__
#include <stdint.h>
#define WITH_AUTOCR
char HexDigit(uint8_t Val);
void Format_Bytes ( void (*Output)(char), const uint8_t *Bytes, uint8_t Len);
inline void Format_Bytes ( void (*Output)(char), const char *Bytes, uint8_t Len) { Format_Bytes(Output, (const uint8_t *)Bytes, Len); }
void Format_String( void (*Output)(char), const char *String);
void Format_String( void (*Output)(char), const char *String, uint8_t MinLen, uint8_t MaxLen);
void Format_Hex( void (*Output)(char), uint8_t Byte );
void Format_Hex( void (*Output)(char), uint16_t Word );
void Format_Hex( void (*Output)(char), uint32_t Word );
void Format_Hex( void (*Output)(char), uint64_t Word );
// void Format_Hex( void (*Output)(char), uint32_t Word, uint8_t Digits);
void Format_MAC( void (*Output)(char), const uint8_t *MAC, uint8_t Len=6);
void Format_HexBytes( void (*Output)(char), const uint8_t *Byte, uint8_t Bytes);
void Format_UnsDec ( void (*Output)(char), uint16_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0);
void Format_SignDec( void (*Output)(char), int16_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0, uint8_t NoPlus=0);
void Format_UnsDec ( void (*Output)(char), uint32_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0);
void Format_SignDec( void (*Output)(char), int32_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0, uint8_t NoPlus=0);
void Format_UnsDec ( void (*Output)(char), uint64_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0);
void Format_SignDec( void (*Output)(char), int64_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0, uint8_t NoPlus=0);
uint8_t Format_String(char *Out, const char *String);
uint8_t Format_String(char *Out, const char *String, uint8_t MinLen, uint8_t MaxLen);
uint8_t Format_UnsDec (char *Out, uint32_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0);
uint8_t Format_SignDec(char *Out, int32_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0, uint8_t NoPlus=0);
uint8_t Format_UnsDec (char *Out, uint64_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0);
uint8_t Format_SignDec(char *Out, int64_t Value, uint8_t MinDigits=1, uint8_t DecPoint=0, uint8_t NoPlus=0);
uint8_t Format_Hex(char *Output, uint8_t Byte );
uint8_t Format_Hex(char *Output, uint16_t Word );
uint8_t Format_Hex(char *Output, uint32_t Word );
uint8_t Format_Hex(char *Output, uint32_t Word, uint8_t Digits);
uint8_t Format_Hex(char *Output, uint64_t Word );
uint8_t Format_HexBytes(char *Output, const uint8_t *Byte, uint8_t Bytes);
// uint8_t Format_Hex(char *Output, const uint8_t *Bytes, uint8_t Len );
// uint8_t Format_Hex( char *Output, uint64_t Word, uint8_t Digits);
template <class Type>
uint8_t Format_Hex( char *Output, Type Word, uint8_t Digits)
{ for(uint8_t Idx=Digits; Idx>0; )
{ Output[--Idx]=HexDigit(Word&0x0F);
Word>>=4; }
return Digits; }
template <class Type>
void Format_Bin( void (*Output)(char), Type Word)
{ const uint8_t Digits = sizeof(Type)<<3;
for( Type Mask = (Type)1<<(Digits-1); Mask; Mask>>=1)
{ bool Bit = Word&Mask;
(*Output)('0'+Bit); }
}
uint8_t Format_HHcMMcSS(char *Out, uint32_t Time);
uint8_t Format_HHMMSS(char *Out, uint32_t Time);
void Format_HHMMSS(void (*Output)(char), uint32_t Time);
uint8_t Format_Period(char *Out, int32_t Time);
void Format_Period(void (*Output)(char), int32_t Time);
uint8_t Format_Latitude (char *Out, int32_t Lat); // [1/600000deg] => DDMM.MMMMs
uint8_t Format_Longitude(char *Out, int32_t Lon); // [1/600000deg] => DDDMM.MMMMs
int8_t Read_Hex1(char Digit);
int8_t Read_Dec1(char Digit); // convert single digit into an integer
inline int8_t Read_Dec1(const char *Inp) { return Read_Dec1(Inp[0]); }
int8_t Read_Dec2(const char *Inp); // convert two digit decimal number into an integer
int16_t Read_Dec3(const char *Inp); // convert three digit decimal number into an integer
int16_t Read_Dec4(const char *Inp); // convert four digit decimal number into an integer
int32_t Read_Dec5(const char *Inp); // convert five digit decimal number into an integer
template <class Type>
int8_t Read_Hex(Type &Int, const char *Inp, uint8_t MaxDig=0) // convert variable number of digits hexadecimal number into an integer
{ if(Inp==0) return 0;
if(MaxDig==0) MaxDig=2*sizeof(Type);
Int=0; int8_t Len=0;
for( ; MaxDig; MaxDig--)
{ int8_t Dig=Read_Hex1(Inp[Len]); if(Dig<0) break;
Int = (Int<<4) + Dig; Len++; }
return Len; } // return number of characters read
template <class Type>
int8_t Read_UnsDec(Type &Int, const char *Inp) // convert variable number of digits unsigned decimal number into an integer
{ Int=0; int8_t Len=0;
if(Inp==0) return 0;
for( ; ; )
{ int8_t Dig=Read_Dec1(Inp[Len]); if(Dig<0) break;
Int = 10*Int + Dig; Len++; }
return Len; } // return number of characters read
template <class Type>
int8_t Read_SignDec(Type &Int, const char *Inp) // convert signed decimal number into in16_t or int32_t
{ Int=0; int8_t Len=0;
if(Inp==0) return 0;
char Sign=Inp[0];
if((Sign=='+')||(Sign=='-')) Len++;
Len+=Read_UnsDec(Int, Inp+Len); if(Sign=='-') Int=(-Int);
return Len; } // return number of characters read
template <class Type>
int8_t Read_Int(Type &Value, const char *Inp)
{ Value=0; int8_t Len=0;
if(Inp==0) return 0;
char Sign=Inp[0]; int8_t Dig;
if((Sign=='+')||(Sign=='-')) Len++;
if((Inp[Len]=='0')&&(Inp[Len+1]=='x'))
{ Len+=2; Dig=Read_Hex(Value, Inp+Len); }
else
{ Dig=Read_UnsDec(Value, Inp+Len); }
if(Dig<=0) return Dig;
Len+=Dig;
if(Sign=='-') Value=(-Value);
return Len; }
template <class Type>
int8_t Read_Float1(Type &Value, const char *Inp) // read floating point, take just one digit after decimal point
{ Value=0; int8_t Len=0;
if(Inp==0) return 0;
char Sign=Inp[0]; int8_t Dig;
if((Sign=='+')||(Sign=='-')) Len++;
Len+=Read_UnsDec(Value, Inp+Len); Value*=10;
if(Inp[Len]!='.') goto Ret;
Len++;
Dig=Read_Dec1(Inp[Len]); if(Dig<0) goto Ret;
Value+=Dig; Len++;
Dig=Read_Dec1(Inp[Len]); if(Dig>=5) Value++;
Ret: if(Sign=='-') Value=(-Value); return Len; }
int8_t Read_LatDDMMSS(int32_t &Lat, const char *Inp);
int8_t Read_LonDDMMSS(int32_t &Lon, const char *Inp);
#endif // __FORMAT_H__