source: icGREP/icgrep-devel/cudd-2.5.1/util/cstringstream.c @ 5820

Last change on this file since 5820 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 5.5 KB
Line 
1/**CFile***********************************************************************
2
3  FileName    [cstringstream.c]
4
5  PackageName [cstringstream]
6
7  Synopsis    [Simple string streams in C.]
8
9  Description [Simple string streams in C.]
10
11  Author      [Fabio Somenzi]
12
13  Copyright   [Copyright (c) 2014, Regents of the University of Colorado
14
15  All rights reserved.
16
17  Redistribution and use in source and binary forms, with or without
18  modification, are permitted provided that the following conditions
19  are met:
20
21  Redistributions of source code must retain the above copyright
22  notice, this list of conditions and the following disclaimer.
23
24  Redistributions in binary form must reproduce the above copyright
25  notice, this list of conditions and the following disclaimer in the
26  documentation and/or other materials provided with the distribution.
27
28  Neither the name of the University of Colorado nor the names of its
29  contributors may be used to endorse or promote products derived from
30  this software without specific prior written permission.
31
32  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
38  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
42  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.]
44
45******************************************************************************/
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include "cstringstream.h"
51
52struct _cstringstream {
53  size_t capacity; /* elements allocated */
54  size_t inUse;    /* elements currently in use */
55  char * data;     /* actual data */
56};
57
58cstringstream newStringStream(void) {
59  cstringstream ss;
60  ss = (cstringstream) malloc(sizeof(struct _cstringstream));
61  if (!ss) return NULL;
62  ss->capacity = 1; /* parsimonious */
63  ss->inUse = 0;
64  ss->data = (char *) malloc(sizeof(char) * ss->capacity);
65  if (!ss->data) {
66    free(ss);
67    return NULL;
68  }
69  return ss;
70}
71
72void deleteStringStream(cstringstream ss) {
73  if (ss) {
74    free(ss->data);
75    free(ss);
76  }
77}
78
79int clearStringStream(cstringstream ss) {
80  if (!ss) return -1;
81  ss->inUse = 0;
82  return 0;
83}
84
85cstringstream copyStringStream(const_cstringstream src) {
86  cstringstream dest;
87  if (!src) return 0;
88  dest = newStringStream();
89  if (!dest) return 0;
90  if (resizeStringStream(dest, src->inUse)) {
91    deleteStringStream(dest);
92    return 0;
93  }
94  strncpy(dest->data, src->data, src->inUse);
95  return dest;
96}
97
98int resizeStringStream(cstringstream ss, size_t newSize) {
99  if (newSize > ss->capacity) {
100    /* To avoid too many calls to realloc, we choose the larger of
101     * twice the current size and the new requested size. */
102    size_t newCapacity = 2 * ss->capacity;
103    if (newCapacity < newSize)
104      newCapacity = newSize;
105    char * tmp = (char *) realloc(ss->data, newCapacity * sizeof(char));
106    /* If the allocation fails, leave the array alone. */
107    if (!tmp) return -1;
108    ss->data = tmp;
109    ss->capacity = newCapacity;
110  }
111  /* Here we are guaranteed that newSize <= ss->capacity. */
112  ss->inUse = newSize;
113  return 0;
114}
115
116int sizeStringStream(const_cstringstream ss, size_t * num) {
117  if (!ss || !num) return -1;
118  *num = ss->inUse;
119  return 0;
120}
121
122int getStringStream(const_cstringstream ss, size_t index, char * c) {
123  if (!ss || !c || index >= ss->inUse) return -1;
124  *c = ss->data[index];
125  return 0;
126}
127
128int appendCharStringStream(cstringstream ss, char c) {
129  if (!ss) return -1;
130  if (resizeStringStream(ss, ss->inUse + 1)) return -1;
131  /* Now we have space. */
132  ss->data[ss->inUse-1] = c;
133  return 0;
134}
135
136int appendStringStringStream(cstringstream ss, char const * s) {
137  if (!ss) return -1;
138  size_t len = strlen(s);
139  if (resizeStringStream(ss, ss->inUse + len)) return -1;
140  /* Now we have space. */
141  strncpy(ss->data + ss->inUse - len, s, len); 
142  return 0;
143}
144
145int appendIntStringStream(cstringstream ss, int d) {
146  char str[256];
147  if (!ss) return -1;
148  sprintf(str, "%d", d);
149  return appendStringStringStream(ss, str);
150}
151
152int appendUnsignedStringStream(cstringstream ss, unsigned u) {
153  char str[256];
154  if (!ss) return -1;
155  sprintf(str, "%u", u);
156  return appendStringStringStream(ss, str);
157}
158
159int appendLongStringStream(cstringstream ss, long ld) {
160  char str[256];
161  if (!ss) return -1;
162  sprintf(str, "%ld", ld);
163  return appendStringStringStream(ss, str);
164}
165
166int appendUnsignedLongStringStream(cstringstream ss, unsigned long lu) {
167  char str[256];
168  if (!ss) return -1;
169  sprintf(str, "%lu", lu);
170  return appendStringStringStream(ss, str);
171}
172
173int appendDoubleStringStream(cstringstream ss, double g) {
174  char str[256];
175  if (!ss) return -1;
176  sprintf(str, "%g", g);
177  return appendStringStringStream(ss, str);
178}
179
180int putStringStream(cstringstream ss, size_t index, char c) {
181  if (!ss || index >= ss->inUse) return -1;
182  ss->data[index] = c;
183  return 0;
184}
185
186char * stringFromStringStream(const_cstringstream ss) {
187  if (!ss) return 0;
188  char * str = (char *) malloc(sizeof(char) * (ss->inUse + 1));
189  if (!str) return 0;
190  strncpy(str, ss->data, ss->inUse);
191  str[ss->inUse] = '\0';
192  return str;
193}
Note: See TracBrowser for help on using the repository browser.