File: STRstring.c

package info (click to toggle)
gopher 2.3-2
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,364 kB
  • ctags: 2,030
  • sloc: ansic: 22,451; perl: 1,950; sh: 1,510; makefile: 397; asm: 1
file content (219 lines) | stat: -rw-r--r-- 4,475 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/********************************************************************
 * $Author: lindner $
 * $Revision: 3.11 $
 * $Date: 1995/09/25 22:07:19 $
 * $Source: /home/arcwelder/GopherSrc/CVS/gopher+/object/STRstring.c,v $
 * $Status: $
 *
 * Paul Lindner, University of Minnesota CIS.
 *
 * Copyright 1991, 1992 by the Regents of the University of Minnesota
 * see the file "Copyright" in the distribution for conditions of use.
 *********************************************************************
 * MODULE: STRstring.c
 * Implement dynamic string library functions
 *********************************************************************
 * Revision History:
 * $Log: STRstring.c,v $
 * Revision 3.11  1995/09/25  22:07:19  lindner
 * Ansification
 *
 * Revision 3.10  1995/06/30  20:32:04  lindner
 * start protoing
 *
 * Revision 3.9  1994/03/17  05:52:49  lindner
 * Fix for multiple STRinits
 *
 * Revision 3.8  1994/03/04  17:31:39  lindner
 * Don't assume a char is one byte long
 *
 * Revision 3.7  1994/02/20  16:23:13  lindner
 * Optimize STRinit so that memory isn't freed then reallocated
 *
 * Revision 3.6  1993/10/27  18:53:23  lindner
 * don't forget \0
 *
 * Revision 3.5  1993/10/22  20:15:52  lindner
 * Remove superfulous declaration of len (Fote)
 *
 * Revision 3.4  1993/10/19  20:46:00  lindner
 * Better, tighter STRstring stuff (Fote)
 *
 * Revision 3.3  1993/08/16  19:35:09  lindner
 * Return a correct value for STRcpy
 *
 * Revision 3.2  1993/03/24  17:07:52  lindner
 * STRset with a NULL value will STRinit() the string
 *
 * Revision 3.1.1.1  1993/02/11  18:03:03  lindner
 * Gopher+1.2beta release
 *
 * Revision 1.1  1992/12/10  23:27:52  lindner
 * gopher 1.1 release
 *
 *
 *********************************************************************/


#include "STRstring.h"
#include "String.h"
#include "Malloc.h"

/*
 * Make a new string, don't set anything for default yet.
 */

String *
STRnew(void)
{
     String *temp;

     temp = (String *) malloc(sizeof(String));
     temp->data = NULL;
     temp->len = 0;

     return(temp);
}

/*
 * Destroy a string
 */

void
STRdestroy(String *st)
{
     if (st != NULL) {
	  if (st->data != NULL)
	       free(st->data);
	  free(st);
     } else
	  perror("STRdestroy: non existant string!\n");

}


/*
 * Don't free the memory.  Instead, negate the length value and keep the
 * memory for later use.  More efficient that way.
 */

void 
STRinit(String *st) 
{
     if (st != NULL) {
	  if (st->len > 0)
	       st->len  = - st->len;
/*	  if (st->data != NULL) {
	       free(st->data);
	       st->data = NULL;
	  }*/
     } else
	  perror("STRinit, non existant string!");
}

/*
 * Set a string value
 */

void
STRset(String *st, char *str)
{
     register int len;

     /* To set a null value, STRinit the item */

     if (str == NULL) {
	  STRinit(st);
	  return;
     }

     /* Negative value for len means memory is initialized, reset to 
	positive value so the rest of the code works well.
      */
 
     if (st->len < 0) 
	  st->len = -st->len;

     if (*str == '\0')
	  len = 1;
     else
	  len = strlen(str)+1; /** Don't forget the '\0' **/

     /* Uninitialized data... */

     if (st->data == NULL) {
	  st->data = (char *) malloc(sizeof(char*) * len);
	  st->len = len;
     }

     /** Something's already there... **/

     else if (STRsize(st) < len) {
	  char *temp;

	  temp = (char *) realloc(st->data, sizeof(char) * len);
	  /*** Should check for NULL ... ***/
	  if (temp == NULL)
	       perror("realloc failed...");

	  st->data = temp;
	  st->len  = len;
     }
     /* space is ok and st->len is set, so copy in the new string */
     strcpy(st->data, str);
}

/*
 * Add a string to the end of the string that's there already
 */

String*
STRcat(String *st, char *cp)
{
     int len;
     char *temp;

     if (cp == NULL)
	  return(NULL);
     
     if (STRlen(st) == 0) {
	  STRset(st, cp);
	  return(st);
     }

     len = strlen(cp) + STRlen(st);

     temp = (char *) malloc(sizeof(char) * len);
     strcpy(temp, STRget(st));
     strcat(temp, cp);

     STRset(st, temp);
     
     free(temp);

     return(st);
}


int
STRcmp(String *st1, String *st2)
{
     register char *cp1, *cp2;

     cp1 = STRget(st1);
     cp2 = STRget(st2);

     if (cp1 == NULL) 
	  return(- !0);
     else if (cp2 == NULL)
	  return( !0);
     else
	  return(strcmp(cp1, cp2));
}
     
String*
STRcpy(String *s1, String *s2)
{
     STRset(s1, STRget(s2));
     return(s1);
}