ia64/xen-unstable

view tools/libxutil/string_stream.c @ 2422:2274a0386cc9

bitkeeper revision 1.1159.69.5 (4138e882jA1YaR_OfTfNHe_uT4PDIg)

trivial
author iap10@labyrinth.cl.cam.ac.uk
date Fri Sep 03 21:56:18 2004 +0000 (2004-09-03)
parents 0e23f01219c6
children beb02da3f69f 42bdac6c8985 0a4b76b6b5a0
line source
1 /*
2 * Copyright (C) 2001, 2002 Hewlett-Packard Company.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library 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 Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * IOStream subtype for input and output to strings.
21 * Usable from user or kernel code (with __KERNEL__ defined).
22 */
24 #include "sys_string.h"
25 #include "string_stream.h"
26 #include "allocate.h"
28 static int string_error(IOStream *io);
29 static int string_close(IOStream *io);
30 static void string_free(IOStream *io);
32 /** Methods for a string stream. */
33 static IOMethods string_methods = {
34 //print: string_print,
35 //getc: string_getc,
36 error: string_error,
37 close: string_close,
38 free: string_free,
39 };
41 /** Get the string stream state.
42 *
43 * @param io string stream
44 * @return state
45 */
46 static inline StringData *get_string_data(IOStream *io){
47 return (StringData*)io->data;
48 }
50 /** Test if a string stream has an error.
51 *
52 * @param io string stream
53 * @return 0 if ok, error code otherwise
54 */
55 static int string_error(IOStream *io){
56 StringData *data = get_string_data(io);
57 return data->out == NULL;
58 }
60 /** Close a string stream.
61 *
62 * @param io string stream
63 * @return 0
64 */
65 static int string_close(IOStream *io){
66 StringData *data = get_string_data(io);
67 data->in = NULL;
68 data->out = NULL;
69 return 0;
70 }
72 /** Free a string stream.
73 * The stream must have been allocated, not statically created.
74 * The stream state is freed, but the underlying string is not.
75 *
76 * @param io string stream
77 */
78 static void string_free(IOStream *io){
79 StringData *data = get_string_data(io);
80 memzero(data, sizeof(*data));
81 deallocate(data);
82 }
84 /** Get the methods to use for a string stream.
85 *
86 * @return methods
87 */
88 IOMethods *string_stream_get_methods(void){
89 return &string_methods;
90 }
92 /** Initialise a string stream, usually from static data.
93 *
94 * @param io address of IOStream to fill in
95 * @param data address of StringData to fill in
96 * @param s string to use
97 * @param n length of the string
98 */
99 void string_stream_init(IOStream *io, StringData *data, char *s, int n){
100 if(data && io){
101 memzero(data, sizeof(*data));
102 data->string = (char*)s;
103 data->in = data->string;
104 data->out = data->string;
105 data->size = n;
106 data->end = data->string + n;
107 memzero(io, sizeof(*io));
108 io->methods = &string_methods;
109 io->data = data;
110 }
111 }
113 /** Allocate and initialise a string stream.
114 *
115 * @param s string to use
116 * @param n length of the string
117 * @return new stream (free using IOStream_free)
118 */
119 IOStream *string_stream_new(char *s, int n){
120 int ok = 0;
121 StringData *data = ALLOCATE(StringData);
122 IOStream *io = ALLOCATE(IOStream);
123 if(data && io){
124 ok = 1;
125 string_stream_init(io, data, s, n);
126 }
127 if(!ok){
128 deallocate(data);
129 deallocate(io);
130 io = NULL;
131 }
132 return io;
133 }