aboutsummaryrefslogtreecommitdiff
path: root/src/inc/stdout.c
blob: 0160e0b013f31c4ffc79853ae760defb23aceea7 (plain)
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
149
150
151
152
153
154
#include "stdout.h"

#ifdef STDOUT_put_string
  void stdout_put_string(void (*putc)(char c), const char* s)
  {
    register unsigned char c;
    while ((c=*s++))
      putc(c);
  }
#endif

#ifdef STDOUT_put_uint
  void stdout_put_uint(void (*putc)(const char c), int n)
  {
    unsigned char i;
    for(i=0; _devisor_ten[i]>n; i++);
    int c=0;
    for(; i<sizeof(_devisor_ten) / sizeof(int); i++)
    {
      for(c=0; (n - _devisor_ten[i]) < n; c++)
        n -= _devisor_ten[i];
      putc('0' + c);
    }
    /// // if processor has can devide this might be better:
    /// register unsigned int d=1;
    /// for(; d<n; d*=10);
    /// for(d /= 10; d>0; n %= d, d /= 10)
      /// putc('0' + (n / d));
  }
#endif

#ifdef STDOUT_reverse_bits
  char stdout_reverse_bits(unsigned char v)
  {
    unsigned char s = sizeof(v) * CHAR_BIT; // bit size; must be power of 2
    unsigned char mask = ~0;
    while ((s >>= 1) > 0)
    {
      mask ^= (mask << s);
      v = ((v >> s) & mask) | ((v << s) & ~mask);
    }
    return v;
  }
#endif

#ifdef STDOUT_put_binary
  void stdout_put_binary(void (*putc)(const char c), const unsigned char data)
  {
    char i;
    for(i=CHAR_BIT-1; i>=0; i--)
    {
      if(data & (1<<i))
        putc('1');
      else
        putc('0');
    }
  }
#endif

#ifdef STDOUT_putf
void stdout_putf(void (*putc)(const char c), const char* format, ...)
{
  va_list vl;
  va_start(vl, format);

  typedef union
  {
    char as_char         : (sizeof(char) * CHAR_BIT);
    int as_int           : (sizeof(int) * CHAR_BIT);
    uint32_t as_uint     : (sizeof(int) * CHAR_BIT);
  } va_data;
  va_data data;

  do
  {
    if(format[0] != '%')
      putc(format[0]);
    else
    {
      char f = '\0'; // fill-char for fixed width numbers (\0 means OFF)
      switch((++format)[0])
      {
        case '%':
          putc('%');
        break;
        case 'c':
          putc(va_arg(vl, int));
        break;
        case '0':
          if(f == '\0')
            f = '0';
        case '-':
          if(f == '\0')
            f = ' ';
        case ' ':
        case 'd':
        {
          int width = 0;
          data.as_int = va_arg(vl, unsigned int);

          if(data.as_int < 0)
          {
            putc('-');
            width        = -1;
            data.as_int *= -1;
          }
          else if(format[0] == ' ')
              putc(' ');

          if(f != '\0')
          {
            // translate string with user-requested fixed length...
            static const int _potences[] = {1, 10, 100, 1000};

            int s     = 0;
            while((++format)[1] !='d' && format[0] >='0' && format[0] <='9')
              s++;
            for(; s>=0; --s)
              width += _potences[sizeof(_potences) - s] * (format[-s] - '0');

            // count numbers width in decimal-system...
            for(s=10; s<data.as_int; s*=10, --width);

            // remove trailing numbers and append spacechar if wanted...
            for(; width>1; width--, putc(f)); // prepend whitespace-character[space]
          }

          stdout_put_uint(putc, data.as_uint);
          if(format[0] != 'd')
            ++format;
        }
        break;
        case 's':
          #ifdef STDOUT_put_string
            stdout_put_string(putc, va_arg(vl, char*));
          #else
            va_arg(vl, int); // remove next argument
          #endif
        break;
        case 'b':
          #ifdef STDOUT_put_binary
            data.as_int = va_arg(vl, int);
            stdout_put_binary(putc, data.as_uint);
          #else
            va_arg(vl, int); // remove next argument
          #endif
        break;
      }
    }
  }
  while((++format)[0] != '\0');
  return;
}
#endif
..