aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxutil/string_stream.c
blob: c3cf423d8463f8b5ccba8a26cdbab25456a01814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2001, 2002 Hewlett-Packard Company.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 * IOStream subtype for input and output to strings.
 * Usable from user or kernel code (with __KERNEL__ defined).
 */

#include "sys_string.h"
#include "string_stream.h"
#include "allocate.h"

static int string_print(IOStream *io, const char *msg, va_list args);
static int string_getc(IOStream *io);
static int string_error(IOStream *io);
static int string_close(IOStream *io);
static void string_free(IOStream *io);

/** Methods for a string stream. */
static IOMethods string_methods = {
    //print: string_print,
    //getc:  string_getc,
    error: string_error,
    close: string_close,
    free:  string_free,
};

/** Get the string stream state.
 *
 * @param io string stream
 * @return state
 */
static inline StringData *get_string_data(IOStream *io){
    return (StringData*)io->data;
}

/** Read a character from a string stream.
 *
 * @param io string stream
 * @return character read, IOSTREAM_EOF if no more input
 */
static int string_getc(IOStream *io){
    StringData *data = get_string_data(io);
    int c = IOSTREAM_EOF;
    char *s = data->in;

    if(s && s < data->end){
        c = (unsigned)*s;
        data->in = s+1;
    }
    return c;
}

/** Print to a string stream.
 * Formats the data to an internal buffer and prints it.
 * The formatted data must fit into the internal buffer.
 *
 * @param io string stream
 * @param format print format
 * @param args print arguments
 * @return result of the print
 */
static int string_print(IOStream *io, const char *msg, va_list args){
    StringData *data = get_string_data(io);
    int k = data->end - data->out;
    int n = vsnprintf(data->out, k, (char*)msg, args);
    if(n < 0 || n > k ){
        n = k;
        IOStream_close(io);
    } else {
        data->out += n;
    }
    return n;
}

/** Test if a string stream has an error.
 *
 * @param io string stream
 * @return 0 if ok, error code otherwise
 */
static int string_error(IOStream *io){
    StringData *data = get_string_data(io);
    return data->out == NULL;
}

/** Close a string stream.
 *
 * @param io string stream
 * @return 0
 */
static int string_close(IOStream *io){
    StringData *data = get_string_data(io);
    data->in = NULL;
    data->out = NULL;
    return 0;
}

/** Free a string stream.
 * The stream must have been allocated, not statically created.
 * The stream state is freed, but the underlying string is not.
 *
 * @param io string stream
 */
static void string_free(IOStream *io){
    StringData *data = get_string_data(io);
    zero(data, sizeof(*data));
    deallocate(data);
}

/** Get the methods to use for a string stream.
 *
 * @return methods
 */
IOMethods *string_stream_get_methods(void){
    return &string_methods;
}

/** Initialise a string stream, usually from static data.
 *
 * @param io address of IOStream to fill in
 * @param data address of StringData to fill in
 * @param s string to use
 * @param n length of the string
 */
void string_stream_init(IOStream *io, StringData *data, char *s, int n){
    if(data && io){
        zero(data, sizeof(*data));
        data->string = (char*)s;
        data->in = data->string;
        data->out = data->string;
        data->size = n;
        data->end = data->string + n;
        zero(io, sizeof(*io));
        io->methods = &string_methods;
        io->data = data;
    }
}

/** Allocate and initialise a string stream.
 *
 * @param s string to use
 * @param n length of the string
 * @return new stream (free using IOStream_free)
 */
IOStream *string_stream_new(char *s, int n){
    int ok = 0;
    StringData *data = ALLOCATE(StringData);
    IOStream *io = ALLOCATE(IOStream);
    if(data && io){
        ok = 1;
        string_stream_init(io, data, s, n);
    }
    if(!ok){
        deallocate(data);
        deallocate(io);
        io = NULL;
    }
    return io;
}