cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

printsize.h
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 #ifndef CFENGINE_PRINTSIZE_H
25 #define CFENGINE_PRINTSIZE_H
26 
27 /** Size of buffer needed to sprintf() an integral value.
28  *
29  * The constant 53/22 is a (very slightly) high approximation to
30  * log(256)/log(10), the number of decimal digits needed per byte of
31  * the value. The offset of 3 accounts for: rounding details, the
32  * '\0' you need at the end of the buffer and the sign. The last is a
33  * wasted byte when using "%u", but it's simpler this way !
34  *
35  * This macro should be preferred over using a char [64] buffer (or
36  * 128 or 32 or whatever guess you've made at "big enough") and
37  * trusting that your integer shall fit - for now it saves wasting
38  * stack space; one day it may even (when we have bigger integral
39  * types) save us a buffer over-run. Limitation: if CHAR_BIT ever
40  * isn't 8, we should change the / 22 to * CHAR_BIT / 176.
41  *
42  * This deals with the simple case of a "%d", possibly with type
43  * modifiers (jhzl, etc.) or sign (%+d). If you're using fancier
44  * modifiers (e.g. field-width), you need to think about what effect
45  * they have on the print width (max of field-width and this macro);
46  * if there is other text in your format, you need to allow space for
47  * it, too.
48  *
49  * If you are casting the value to be formatted (e.g. because there's
50  * no modifier for its type), applying this macro to the value shall
51  * get the maximum size that a %d-based format can produce for it (its
52  * value can't be any bigger, even if it's cast to a huge type); but,
53  * if you're using a %u-based format and the value is signed, you
54  * should call this macro on the type to which you're casting it
55  * (because, if it's negative, it'll wrap to a huge value of the cast
56  * type).
57  *
58  * @param #what The integral expression or its type. Is not evaluated
59  * at run-time, only passed to sizeof() at compile-time.
60  *
61  * @return The size for a buffer big enough to hold sprintf()'s
62  * representation of an integer of this type. This is a compile-time
63  * constant, so can be used in static array declarations or struct
64  * member array declarations.
65  */
66 #define PRINTSIZE(what) (3 + 53 * sizeof(what) / 22)
67 
68 #endif /* CFENGINE_PRINTSIZE_H */