The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4394 ripley 1
/*
2
 * GraphApp - Cross-Platform Graphics Programming Library.
3
 *
4
 * File: strings.c -- local string functions.
5
 * Platform: Windows  Version: 2.44  Date: 1998/09/09
6
 *
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
8
 * Version: 2.00  Changes: Added del_string.
9
 * Version: 2.30  Changes: Now uses array(), not malloc().
10
 * Version: 2.44  Changes: Now uses array() instead of static char[].
11
 */
12
 
13
/* Copyright (C) 1993-1998 Lachlan Patrick
14
 
15
   This file is part of GraphApp, a cross-platform C graphics library.
16
 
17
   GraphApp is free software; you can redistribute it and/or modify it
18
   under the terms of the GNU Library General Public License.
19
   GraphApp is distributed in the hope that it will be useful, but
20
   WITHOUT ANY WARRANTY.
21
 
22
   See the file COPYLIB.TXT for details.
23
*/
24
 
25
#include "internal.h"
26
 
27
/*
28
 *  Create and return a newly allocated copy of a string.
29
 *  Null strings are converted into empty strings first.
30
 */
31
char *new_string(char *src)
32
{
33
	char *str;
34
	if (! src)
35
		src = "";
36
	str = array (string_length(src), char);
37
	if (str)
38
		copy_string(str, src);
39
	return str;
40
}
41
 
42
void del_string(char *str)
43
{
44
	discard(str);
45
}
46
 
47
/*
48
 *  String length function.
49
 *  If the string is null, the length is defined to be zero.
50
 */
51
long string_length(char *s)
52
{
53
	long len = 0;
54
	if (s)
55
		while (s[len])
56
			len++;
57
	return len;
58
}
59
 
60
/*
61
 *  Copy a string.
62
 *  Same as strcpy except it avoids doing anything to null strings.
63
 */
64
void copy_string(char *dest, char *src)
65
{
66
	int len;
67
	if ((dest) && (src)) {
68
		for (len=0; src[len]; len++)
69
			dest[len] = src[len];
70
		dest[len] = '\0';
71
	}
72
}
73
 
74
/*
75
 *  String comparison function.
76
 *  Null == null, null == "", "" == null, otherwise same as strcmp.
77
 */
78
int compare_strings(char *s1, char *s2)
79
{
80
	int len, diff;
81
 
82
	if (s1 == s2)
83
		return 0;
84
	else if (s1 == NULL) { /* s2 cannot be null since s1 != s2 */
85
		if (s2[0] == '\0')
86
			return 0;
87
		return -1;
88
	}
89
	else if (s2 == NULL) { /* s1 is not null */
90
		if (s1[0] == '\0')
91
			return 0;
92
		return +1;
93
	}
94
	else {
95
		for (len=0; s1[len] || s2[len]; len++) {
96
			diff = s1[len] - s2[len];
97
			if (diff)
98
				return diff;
99
		}
100
		return 0;
101
	}
102
}
103
 
104
/*
105
 *  Append one string to another, and return the result.
106
 *  This function correctly handles the situation where one or both
107
 *  of the parameters are temporary strings returned by an earlier
108
 *  call to this function.
109
 */
110
char *add_strings(char *s1, char *s2)
111
{
112
	static char *buffer = NULL;
113
	char *prev;
114
	int len1, len2;
115
 
116
	prev = buffer;
117
 
118
	if (! s1)
119
		return s2;
120
	else if (! s2)
121
		return s1;
122
 
123
	len1 = string_length(s1);
124
	len2 = string_length(s2);
125
 
126
	buffer = array (len1+len2, char);
127
 
128
	if (buffer != NULL) {
129
		copy_string(buffer, s1);
130
		copy_string(buffer+len1, s2);
131
	}
132
 
133
	if (prev)
134
		discard(prev); /* free previous string buffer */
135
 
136
	return buffer;
137
}
138
 
139
/*
140
 *  Convert a char to a string, return the result in a static buffer.
141
 */
142
char *char_to_string(char ch)
143
{
144
	static char str[2];
145
	str[0] = ch;
146
	str[1] = '\0';
147
	return str;
148
}
149
 
150
/*
151
 *  Convert an integer to a string, return the result in a static buffer.
152
 */
153
char *int_to_string(long i)
154
{
155
	static char *str = NULL;
156
	if (str == NULL)
157
		str = array(40,char);
158
	sprintf(str, "%ld", i);
159
	return str;
160
}
161
 
162
/*
163
 *  Convert a floating point number to a string,
164
 *  return the result in a static buffer.
165
 */
166
char *float_to_string(float f)
167
{
168
	static char *str = NULL;
169
	if (str == NULL)
170
		str = array(40,char);
171
	sprintf(str, "%g", f);
172
	return str;
173
}
174
 
175
/*
176
 *  Case-insensitive string comparison function.
177
 */
178
PROTECTED
179
int string_diff(char *s, char *t)
180
{
181
	int ch1, ch2;
182
	int diff = 0;
183
 
184
	while ((diff == 0) && ((*s != '\0') || (*t != '\0')))
185
	{
186
		ch1 = *s; ch2 = *t;
187
		if ((ch1 >= 'A') && (ch1 <= 'Z'))
188
			ch1 = ch1 - 'A' + 'a';
189
		if ((ch2 >= 'A') && (ch2 <= 'Z'))
190
			ch2 = ch2 - 'A' + 'a';
191
		diff = (ch1 - ch2);
192
		s++; t++;
193
	}
194
 
195
	return diff;
196
}
197
 
198
/*
199
 *  Produce a new string which has CR-LF instead of just '\n'
200
 *  to mark end of line. The returned string must later be freed.
201
 */
202
PROTECTED
203
char *to_dos_string(char *text)
204
{
205
	char *s;
206
	char *newstr;
207
	char prev;
208
	long length = 0;
209
 
210
	if (!text)
211
		return text;
212
	prev = '\0';
213
	for (s = text; *s != '\0'; s++) {
214
		length++;
215
		if ((*s == '\n') && (prev != '\r'))
216
			length++;
217
		prev = *s;
218
	}
219
 
220
	newstr = array (length, char);
221
 
222
	prev = '\0';
223
	for (s = newstr; *text != '\0'; s++, text++) {
224
		if ((*text == '\n') && (prev != '\r')) {
225
			*s = '\r';
226
			s++;
227
		}
228
		*s = *text;
229
		prev = *s;
230
	}
231
	*s = '\0';
232
 
233
	return newstr;
234
}
235
 
236
/*
237
 *  Strip carriage returns from a string. The resulting
238
 *  string must later be freed.
239
 */
240
PROTECTED
241
char *to_c_string(char *text)
242
{
243
	char *s;
244
	char *newstr;
245
	long length = 0;
246
 
247
	if (!text)
248
		return text;
249
	for (s = text; *s != '\0'; s++) {
250
		length++;
251
		if ((*s == '\r') && (*(s+1) == '\n'))
252
			length--;
253
	}
254
 
255
	newstr = array (length, char);
256
 
257
	for (s = newstr; *text != '\0'; s++, text++) {
258
		if ((*text == '\r') && (*(text+1) == '\n'))
259
			text++;
260
		*s = *text;
261
	}
262
	*s = '\0';
263
 
264
	return newstr;
265
}