25273f46c4509537731c570e35a07518b82dbb99
[util-vserver.git] / util-vserver / lib / fmt.hc
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "fmt.h"
24 #include <string.h>
25
26 static char const DIGITS[] = "0123456789abcdefghijklmnopqrstuvwxyz";
27
28 #define FMT_P__(X,Y)            X ## Y
29 #define FMT_P_(X,Y)             FMT_P__(X,Y)
30 #define FMT_P(X)                FMT_P_(FMT_PREFIX, X)
31
32 #define CONCAT__(x,y,z)         x ## y ## z
33 #define CONCAT_(x,y,z)          CONCAT__(x,y,z)
34 #define CONCAT(x,z)             CONCAT_(x, FMT_BITSIZE, z)
35
36 #define FMT_FN(BASE,SZ)                                 \
37   do {                                                  \
38     register __typeof__(val)    v = val;                \
39     register size_t             l = 0;                  \
40                                                         \
41     if (ptr==0) {                                       \
42       do {                                              \
43         ++l;                                            \
44         v /= BASE;                                      \
45       } while (v!=0);                                   \
46     }                                                   \
47     else {                                              \
48       char                      buf[sizeof(val)*SZ];    \
49                                                         \
50       do {                                              \
51         register unsigned int   d = v%BASE;             \
52         v /= BASE;                                      \
53         ++l;                                            \
54         buf[sizeof(buf)-l] = DIGITS[d];                 \
55       } while (v!=0);                                   \
56                                                         \
57       memcpy(ptr, buf+sizeof(buf)-l, l);                \
58     }                                                   \
59                                                         \
60     return l;                                           \
61   } while (0)
62
63 size_t
64 CONCAT(FMT_P(uint),_base)(char *ptr, CONCAT(uint_least,_t) val, char base)
65 {
66   //if (base==10) FMT_FN(10,3);
67   if (base==16) FMT_FN(16,2);
68
69   FMT_FN(base,8);
70 }
71
72 size_t
73 CONCAT(FMT_P(int),_base)(char *ptr,
74                          CONCAT(int_least,_t) val, char base)
75 {
76   size_t        offset=0;
77   if (val<0) {
78     val      = -val;
79     offset   = 1;
80
81     if (ptr!=0)
82       *ptr++ = '-';
83   }
84
85   return CONCAT(FMT_P(uint),_base)(ptr, val, base) + offset;
86 }