merge with 0.30.213
[util-vserver.git] / ensc_fmt / fmt-internal.h
1 // $Id: fmt-internal.h 1647 2004-08-19 13:53:54Z ensc $    --*- 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 #ifndef H_UTIL_VSERVER_LIB_FMT_COMMON_H
20 #define H_UTIL_VSERVER_LIB_FMT_COMMON_H
21
22 #define DIGITS                  "0123456789abcdefghijklmnopqrstuvwxyz"
23
24 #define FMT_P__(X,Y)            X ## Y
25 #define FMT_P_(X,Y)             FMT_P__(X,Y)
26 #define FMT_P(X)                FMT_P_(FMT_PREFIX, X)
27
28 #define CONCAT__(x,y,z)         x ## y ## z
29 #define CONCAT_(x,y,z)          CONCAT__(x,y,z)
30 #define CONCAT(x,z)             CONCAT_(x, FMT_BITSIZE, z)
31
32 #define FMT_FN(BASE,SZ)                                 \
33   do {                                                  \
34     register __typeof__(val)    v = val;                \
35     register size_t             l = 0;                  \
36                                                         \
37     if (ptr==0) {                                       \
38       do {                                              \
39         ++l;                                            \
40         v /= BASE;                                      \
41       } while (v!=0);                                   \
42     }                                                   \
43     else {                                              \
44       char                      buf[sizeof(val)*SZ];    \
45                                                         \
46       do {                                              \
47         register unsigned int   d = v%BASE;             \
48         v /= BASE;                                      \
49         ++l;                                            \
50         buf[sizeof(buf)-l] = DIGITS[d];                 \
51       } while (v!=0);                                   \
52                                                         \
53       memcpy(ptr, buf+sizeof(buf)-l, l);                \
54     }                                                   \
55                                                         \
56     return l;                                           \
57   } while (0)
58
59
60 #endif  //  H_UTIL_VSERVER_LIB_FMT_COMMON_H