Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-string-private.h
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-string-private.h String utility class (internal to D-Bus implementation)
3  * 
4  * Copyright (C) 2002, 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifndef DBUS_STRING_PRIVATE_H
25 #define DBUS_STRING_PRIVATE_H
26
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-memory.h>
29 #include <dbus/dbus-types.h>
30
31 #ifndef DBUS_CAN_USE_DBUS_STRING_PRIVATE
32 #error "Don't go including dbus-string-private.h for no good reason"
33 #endif
34
35 DBUS_BEGIN_DECLS
36
37 /**
38  * @brief Internals of DBusString.
39  * 
40  * DBusString internals. DBusString is an opaque objects, it must be
41  * used via accessor functions.
42  */
43 typedef struct
44 {
45   unsigned char *str;            /**< String data, plus nul termination */
46   int            len;            /**< Length without nul */
47   int            allocated;      /**< Allocated size of data */
48   unsigned int   constant : 1;   /**< String data is not owned by DBusString */
49   unsigned int   locked : 1;     /**< DBusString has been locked and can't be changed */
50   unsigned int   invalid : 1;    /**< DBusString is invalid (e.g. already freed) */
51   unsigned int   align_offset : 3; /**< str - align_offset is the actual malloc block */
52 } DBusRealString;
53
54 _DBUS_STATIC_ASSERT (sizeof (DBusRealString) == sizeof (DBusString));
55
56 /**
57  * @defgroup DBusStringInternals DBusString implementation details
58  * @ingroup  DBusInternals
59  * @brief DBusString implementation details
60  *
61  * The guts of DBusString.
62  *
63  * @{
64  */
65
66 /**
67  * The maximum length of a DBusString
68  */
69 #define _DBUS_STRING_MAX_LENGTH (_DBUS_INT32_MAX - _DBUS_STRING_ALLOCATION_PADDING)
70
71 /**
72  * Checks a bunch of assertions about a string object
73  *
74  * @param real the DBusRealString
75  */
76 #define DBUS_GENERIC_STRING_PREAMBLE(real) \
77   do { \
78       _dbus_assert ((real) != NULL); \
79       _dbus_assert (!(real)->invalid); \
80       _dbus_assert ((real)->len >= 0); \
81       _dbus_assert ((real)->allocated >= 0); \
82       _dbus_assert ((real)->len <= ((real)->allocated - _DBUS_STRING_ALLOCATION_PADDING)); \
83       _dbus_assert ((real)->len <= _DBUS_STRING_MAX_LENGTH); \
84   } while (0)
85
86 /**
87  * Checks assertions about a string object that needs to be
88  * modifiable - may not be locked or const. Also declares
89  * the "real" variable pointing to DBusRealString. 
90  * @param str the string
91  */
92 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
93   DBUS_GENERIC_STRING_PREAMBLE (real);                                          \
94   _dbus_assert (!(real)->constant);                                             \
95   _dbus_assert (!(real)->locked)
96
97 /**
98  * Checks assertions about a string object that may be locked but
99  * can't be const. i.e. a string object that we can free.  Also
100  * declares the "real" variable pointing to DBusRealString.
101  *
102  * @param str the string
103  */
104 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
105   DBUS_GENERIC_STRING_PREAMBLE (real);                                                 \
106   _dbus_assert (!(real)->constant)
107
108 /**
109  * Checks assertions about a string that may be const or locked.  Also
110  * declares the "real" variable pointing to DBusRealString.
111  * @param str the string.
112  */
113 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
114   DBUS_GENERIC_STRING_PREAMBLE (real)
115
116 /**
117  * Checks for ASCII blank byte
118  * @param c the byte
119  */
120 #define DBUS_IS_ASCII_BLANK(c) ((c) == ' ' || (c) == '\t')
121
122 /**
123  * Checks for ASCII whitespace byte
124  * @param c the byte
125  */
126 #define DBUS_IS_ASCII_WHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
127
128 /** @} */
129
130 DBUS_END_DECLS
131
132 #endif /* DBUS_STRING_PRIVATE_H */