embkernel
 All Classes Functions Variables Typedefs Groups Pages
LibStringFormat.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef LIB_STRING_FORMAT_HPP_
7 #define LIB_STRING_FORMAT_HPP_
8 
9 #include <stdarg.h>
10 
11 
12 class LibStringFormat {
13 private:
14  typedef union {
15  struct {
16  int leftJustify :1;
17  int forceSign :1;
18  int forceSpace :1;
19  int sharp :1;
20  int leftPadWith0 :1;
21  //Internals
22  int isNegative :1;
23  int isLowerCase :1;
24  } bits;
25  int value;
26  } FLAGS;
27 
28  enum {
29  LENGTH_HALF,
30  LENGTH_LONG,
31  LENGTH_DOUBLE
32  };
33 
34  typedef struct {
35  FLAGS flags;
36  int width;
37  int precision;
38  int length;
39  unsigned int base;
40  } FORMAT_PARAMETERS;
41 
42  static const char HEX_CHARS_LOWER_CASE[];
43  static const char HEX_CHARS_UPPER_CASE[];
44 
45 public:
46  static int printf(class LibStreamOut& stream, const char* format, ...) __attribute__ ((format (printf, 2, 3)));
47  static int vprintf(class LibStreamOut& stream, const char* format, va_list args);
48 
49 private:
50  static const char* parseFlags(const char* format, FLAGS& flags);
51  static const char* parseWidth(const char* format, int& width);
52  static const char* parsePrecision(const char* format, int& precision);
53  static const char* parseLength(const char* format, int& length);
54 
55  static int writeChar(class LibStreamOut& stream, const char c, const FORMAT_PARAMETERS& parameters);
56  static int writeNaturalNumber(class LibStreamOut& stream, unsigned int value, const FORMAT_PARAMETERS& parameters);
57  static int writeFloat(class LibStreamOut& stream, double value, FORMAT_PARAMETERS& parameters);
58  static int writeString(class LibStreamOut& stream, const char* string, const FORMAT_PARAMETERS& parameters);
59  static int writePad(class LibStreamOut& stream, const char c, int& size);
60 };
61 
62 #endif /* LIB_STRING_FORMAT_HPP_ */