The R Project SVN R

Rev

Rev 42521 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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