~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/libs3/inc/string_buffer.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-07-16 09:56:24 UTC
  • mfrom: (0.3.11)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20120716095624-azr2w4hbhei1rxmx
Tags: upstream-0.48
ImportĀ upstreamĀ versionĀ 0.48

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** **************************************************************************
2
 
 * string_buffer.h
3
 
 * 
4
 
 * Copyright 2008 Bryan Ischo <bryan@ischo.com>
5
 
 * 
6
 
 * This file is part of libs3.
7
 
 * 
8
 
 * libs3 is free software: you can redistribute it and/or modify it under the
9
 
 * terms of the GNU General Public License as published by the Free Software
10
 
 * Foundation, version 3 of the License.
11
 
 *
12
 
 * In addition, as a special exception, the copyright holders give
13
 
 * permission to link the code of this library and its programs with the
14
 
 * OpenSSL library, and distribute linked combinations including the two.
15
 
 *
16
 
 * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
17
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19
 
 * details.
20
 
 *
21
 
 * You should have received a copy of the GNU General Public License version 3
22
 
 * along with libs3, in a file named COPYING.  If not, see
23
 
 * <http://www.gnu.org/licenses/>.
24
 
 *
25
 
 ************************************************************************** **/
26
 
 
27
 
#ifndef STRING_BUFFER_H
28
 
#define STRING_BUFFER_H
29
 
 
30
 
#include <stdio.h>
31
 
 
32
 
 
33
 
// Declare a string_buffer with the given name of the given maximum length
34
 
#define string_buffer(name, len)                                        \
35
 
    char name[len + 1];                                                 \
36
 
    int name##Len
37
 
 
38
 
 
39
 
// Initialize a string_buffer
40
 
#define string_buffer_initialize(sb)                                    \
41
 
    do {                                                                \
42
 
        sb[0] = 0;                                                      \
43
 
        sb##Len = 0;                                                    \
44
 
    } while (0)
45
 
 
46
 
 
47
 
// Append [len] bytes of [str] to [sb], setting [all_fit] to 1 if it fit, and
48
 
// 0 if it did not
49
 
#define string_buffer_append(sb, str, len, all_fit)                     \
50
 
    do {                                                                \
51
 
        sb##Len += snprintf(&(sb[sb##Len]), sizeof(sb) - sb##Len - 1,   \
52
 
                            "%.*s", (int) (len), str);                  \
53
 
        if (sb##Len > (int) (sizeof(sb) - 1)) {                         \
54
 
            sb##Len = sizeof(sb) - 1;                                   \
55
 
            all_fit = 0;                                                \
56
 
        }                                                               \
57
 
        else {                                                          \
58
 
            all_fit = 1;                                                \
59
 
        }                                                               \
60
 
    } while (0)
61
 
 
62
 
 
63
 
// Declare a string multibuffer with the given name of the given maximum size
64
 
#define string_multibuffer(name, size)                                  \
65
 
    char name[size];                                                    \
66
 
    int name##Size
67
 
 
68
 
 
69
 
// Initialize a string_multibuffer
70
 
#define string_multibuffer_initialize(smb)                              \
71
 
    do {                                                                \
72
 
        smb##Size = 0;                                                  \
73
 
    } while (0)
74
 
 
75
 
 
76
 
// Evaluates to the current string within the string_multibuffer
77
 
#define string_multibuffer_current(smb)                                  \
78
 
    &(smb[smb##Size])
79
 
 
80
 
 
81
 
// Adds a new string to the string_multibuffer
82
 
#define string_multibuffer_add(smb, str, len, all_fit)                  \
83
 
    do {                                                                \
84
 
        smb##Size += (snprintf(&(smb[smb##Size]),                       \
85
 
                               sizeof(smb) - smb##Size,                 \
86
 
                               "%.*s", (int) (len), str) + 1);          \
87
 
        if (smb##Size > (int) sizeof(smb)) {                            \
88
 
            smb##Size = sizeof(smb);                                    \
89
 
            all_fit = 0;                                                \
90
 
        }                                                               \
91
 
        else {                                                          \
92
 
            all_fit = 1;                                                \
93
 
        }                                                               \
94
 
    } while (0)
95
 
 
96
 
 
97
 
// Appends to the current string in the string_multibuffer.  There must be a
98
 
// current string, meaning that string_multibuffer_add must have been called
99
 
// at least once for this string_multibuffer.
100
 
#define string_multibuffer_append(smb, str, len, all_fit)               \
101
 
    do {                                                                \
102
 
        smb##Size--;                                                    \
103
 
        string_multibuffer_add(smb, str, len, all_fit);                 \
104
 
    } while (0)
105
 
 
106
 
 
107
 
#endif /* STRING_BUFFER_H */