KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
convert.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : convert.c
11  * Created : 8 feb. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 #include "util/convert.h"
16 
17 #include <ctype.h>
18 #include <string.h>
19 #include <stdio.h>
20 
21 int cnvParse(const char * input, uint32_t * output, CnvParams p)
22 {
23  int c = 0;
24  int v;
25 
26  uint32_t tmp = 0;
27 
28  char ch = *input;
29  while (p.len > 0 && ch != '\0')
30  {
31 
32  if (ch >= '0' && ch <= '9') {
33  v = ch - '0';
34  }
35  else if (ch >= 'A' && ch <= 'Z') {
36  v = ( ch - 'A' ) + 10;
37  }
38  else if (ch >= 'a' && ch <= 'z') {
39  v = ( ch - 'a' ) + 10;
40  }
41  else {
42  break;
43  }
44  if (v >= p.base) {
45  return 0;
46  }
47 
48  tmp *= p.base;
49  tmp += v;
50 
51  c++;
52  input++;
53  ch = *input;
54  }
55 
56  *output = tmp;
57 
58  return c;
59 }
60 
61 int cnvParseU(const char * input, uint32_t * output, CnvParams p)
62 {
63  if (p.len == 0) p.len = 12;
64  int c = 0;
65  int t;
66 
67  while (isblank(*input))
68  {
69  input++;
70  c++;
71  p.len--;
72  if (p.len == 0) return 0;
73  }
74 
75 
76  if (*input == '+') {
77  input++;
78  p.len--;
79  c++;
80  if (p.len == 0) return 0;
81  }
82 
83  t = cnvParse(input, output, p);
84  if (t == 0) return 0;
85  c += t;
86 
87  return c;
88 }
89 
90 int cnvParseI(const char * input, int32_t * output, CnvParams p)
91 {
92  bool minus = false;
93  uint32_t v;
94  int c = 0;
95  int t = 0;
96 
97  if (p.len == 0) p.len = 13;
98 
99  while (isspace(*input))
100  {
101  input++;
102  c++;
103  p.len--;
104  if (p.len == 0) return 0;
105  }
106 
107 
108  if (*input == '-') {
109  minus = true;
110  input++;
111  p.len--;
112  c++;
113  if (p.len == 0) return 0;
114  }
115 
116  t = cnvParse(input, &v, p);
117  if (t == 0) return 0;
118  c += t;
119 
120  *output = minus ? -v : v;
121 
122  return c;
123 }
124 
125 static uint32_t cnvFindDiv(uint32_t input, CnvParams p)
126 {
127  uint32_t div = 1;
128 
129  while (input / div >= p.base)
130  {
131  div *= p.base;
132  }
133 
134  return div;
135 }
136 
137 int cnvFormatU(int32_t input, char * output, CnvParams p)
138 {
139  uint32_t div = cnvFindDiv(input, p);
140  int digit;
141  int c = 0;
142  while (div > 0)
143  {
144  digit = (input / div) % p.base;
145 
146  if (digit < 10) {
147  *output = '0' + digit;
148  } else {
149  *output = (p.uppercase ? 'A' : 'a') + (digit - 10);
150  }
151  c++;
152  output++;
153  div /= p.base;
154  }
155  *output = '\0';
156  return c;
157 }
158 
159 
160 int cnvFormatI(int32_t input, char * output, CnvParams p)
161 {
162  int c = 0;
163  uint32_t v;
164  if (input < 0)
165  {
166  v = -input;
167  *output = '-';
168  output++;
169  c++;
170  } else {
171  v = input;
172  }
173  c += cnvFormatU(v, output, p);
174 
175  return c;
176 }
177 
178 
179 void cnvFill(const char * input, char * output, CnvParams p)
180 {
181  int l = strlen(input);
182  int c = p.len - l;
183  if (c <= 0)
184  {
185  memcpy(output, input, p.len);
186  output[p.len] = '\0';
187  return;
188  }
189  else if (p.alignleft)
190  {
191  memcpy(output, input, l);
192  output += l;
193  while (c > 0) {
194  *output = p.fill;
195  output++;
196  c--;
197  }
198  }
199  else
200  {
201  while (c > 0) {
202  *output = p.fill;
203  output++;
204  c--;
205  }
206  memcpy(output, input, l);
207  output += l;
208  }
209  *output = '\0';
210 }
int cnvFormatU(int32_t input, char *output, CnvParams params)
Formats an unsigned integer into a character buffer.
Definition: convert.c:137
bool alignleft
Aligning left instead of right (default)
Definition: convert.h:43
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
bool uppercase
Format &gt;10 base values with uppercase.
Definition: convert.h:42
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
This module implements parsing and formating of strings and integers.