KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
convert.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : format.h
11  * Created : 8 feb. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #ifndef COVNERT_H_
17 #define CONVERT_H_
18 
19 /**
20  * @file
21  *
22  * @ingroup util
23  *
24  * This module implements parsing and formating of strings and integers.
25  */
26 
27 #include <stdint.h>
28 #include <stdbool.h>
29 
30 #define CNV_BASE_DEC 10 ///< Base for decimal numbers
31 #define CNV_BASE_BIN 2 ///< Base for binary numbers
32 #define CNV_BASE_HEX 16 ///< Base of hex numbers.
33 
34 /**
35  * This structure provides information about formatting and parsing.
36  */
37 typedef struct
38 {
39  uint8_t base; ///< Base of the number to format or parse.
40  uint8_t len; ///< Lenght of the buffer to format into or parse from
41  char fill; ///< Fill character
42  bool uppercase : 1; ///< Format >10 base values with uppercase
43  bool alignleft : 1; ///< Aligning left instead of right (default)
44 } __attribute__((packed)) CnvParams ;
45 
46 /// Default conversion/formatting parameters (base 10).
47 #define CNV_DEFAULT \
48 { \
49  .base = 10, \
50  .len = 0, \
51  .fill = ' ', \
52  .uppercase = false, \
53  .alignleft = false \
54 }
55 
56 #define CNV_DEFAULT_HEX \
57 { \
58  .base = 16, \
59  .len = 0, \
60  .fill = '0', \
61  .uppercase = false, \
62  .alignleft = false \
63 }
64 
65 
66 /**
67  * Parse a signed integer.
68  *
69  * @param input The input buffer
70  * @param output A pointer to the variable to write the parsed value into.
71  * @param params The conversion parameters
72  * @return The number of characters processed.
73  */
74 int cnvParseI(const char * input, int32_t * output, CnvParams params);
75 
76 /**
77  * Parse an unsigned integer.
78  *
79  * @param input The input buffer
80  * @param output A pointer to the variable to write the parsed value into.
81  * @param params The conversion parameters
82  * @return The number of characters processed.
83  */
84 int cnvParseU(const char * input, uint32_t * output, CnvParams params);
85 
86 /**
87  * Formats a signed integer into a character buffer.
88  *
89  * @param input The value to format
90  * @param output The output buffer to write into
91  * @param params The conversino parameters
92  * @return The number of characters formatted
93  */
94 int cnvFormatI(int32_t input, char * output, CnvParams params);
95 
96 /**
97  * Formats an unsigned integer into a character buffer.
98  *
99  * @param input The value to format
100  * @param output The output buffer to write into
101  * @param params The conversion parameters
102  * @return The number of characters formatted
103  */
104 int cnvFormatU(int32_t input, char * output, CnvParams params);
105 
106 /**
107  * Pads a string into a bigger buffer either prepending or postpending a padding character.
108  *
109  * @param input The input string to pad
110  * @param output The output buffer in which the padding is done
111  * @param params The conversion parameters.
112  */
113 void cnvFill(const char * input, char * output, CnvParams params);
114 
115 #endif /* FORMAT_H_ */
int cnvFormatU(int32_t input, char *output, CnvParams params)
Formats an unsigned integer into a character buffer.
Definition: convert.c:137
int cnvParseI(const char *input, int32_t *output, CnvParams params)
Parse a signed integer.
Definition: convert.c:90
int cnvParseU(const char *input, uint32_t *output, CnvParams params)
Parse an unsigned integer.
Definition: convert.c:61
int cnvFormatI(int32_t input, char *output, CnvParams params)
Formats a signed integer into a character buffer.
Definition: convert.c:160
This structure provides information about formatting and parsing.
Definition: convert.h:37
uint8_t base
Base of the number to format or parse.
Definition: convert.h:39
void cnvFill(const char *input, char *output, CnvParams params)
Pads a string into a bigger buffer either prepending or postpending a padding character.
Definition: convert.c:179
uint8_t len
Lenght of the buffer to format into or parse from.
Definition: convert.h:40
char fill
Fill character.
Definition: convert.h:41