The R Project SVN R

Rev

Rev 42521 | Go to most recent revision | Details | Compare with Previous | 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
 */
41793 ripley 31
char *new_string(const char *src)
4394 ripley 32
{
45017 ripley 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;
4394 ripley 40
}
41
 
41793 ripley 42
void del_string(const char *str)
4394 ripley 43
{
45017 ripley 44
    discard(str);
4394 ripley 45
}
46
 
47
/*
48
 *  String length function.
49
 *  If the string is null, the length is defined to be zero.
50
 */
41793 ripley 51
long string_length(const char *s)
4394 ripley 52
{
45017 ripley 53
    long len = 0;
54
    if (s)
55
	while (s[len])
56
	    len++;
57
    return len;
4394 ripley 58
}
59
 
60
/*
61
 *  Copy a string.
62
 *  Same as strcpy except it avoids doing anything to null strings.
63
 */
41793 ripley 64
void copy_string(char *dest, const char *src)
4394 ripley 65
{
45017 ripley 66
    int len;
67
    if ((dest) && (src)) {
68
	for (len=0; src[len]; len++)
69
	    dest[len] = src[len];
70
	dest[len] = '\0';
71
    }
4394 ripley 72
}
73
 
74
/*
75
 *  String comparison function.
76
 *  Null == null, null == "", "" == null, otherwise same as strcmp.
77
 */
41793 ripley 78
int compare_strings(const char *s1, const char *s2)
4394 ripley 79
{
45017 ripley 80
    int len, diff;
4394 ripley 81
 
45017 ripley 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;
4394 ripley 99
	}
45017 ripley 100
	return 0;
101
    }
4394 ripley 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
 */
41793 ripley 110
const char *add_strings(const char *s1, const char *s2)
4394 ripley 111
{
45017 ripley 112
    static char *buffer = NULL;
113
    char *prev;
114
    int len1, len2;
4394 ripley 115
 
45017 ripley 116
    prev = buffer;
4394 ripley 117
 
45017 ripley 118
    if (! s1)
119
	return s2;
120
    else if (! s2)
121
	return s1;
4394 ripley 122
 
45017 ripley 123
    len1 = string_length(s1);
124
    len2 = string_length(s2);
4394 ripley 125
 
45017 ripley 126
    buffer = array (len1+len2, char);
4394 ripley 127
 
45017 ripley 128
    if (buffer != NULL) {
129
	copy_string(buffer, s1);
130
	copy_string(buffer+len1, s2);
131
    }
4394 ripley 132
 
45017 ripley 133
    if (prev)
134
	discard(prev); /* free previous string buffer */
4394 ripley 135
 
45017 ripley 136
    return buffer;
4394 ripley 137
}
138
 
139
/*
140
 *  Convert a char to a string, return the result in a static buffer.
141
 */
41793 ripley 142
char *char_to_string(const char ch)
4394 ripley 143
{
45017 ripley 144
    static char str[2];
145
    str[0] = ch;
146
    str[1] = '\0';
147
    return str;
4394 ripley 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
{
45017 ripley 155
    static char *str = NULL;
156
    if (str == NULL)
157
	str = array(40,char);
158
    sprintf(str, "%ld", i);
159
    return str;
4394 ripley 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
{
45017 ripley 168
    static char *str = NULL;
169
    if (str == NULL)
170
	str = array(40,char);
171
    sprintf(str, "%g", f);
172
    return str;
4394 ripley 173
}
174
 
175
/*
176
 *  Case-insensitive string comparison function.
177
 */
178
PROTECTED
41793 ripley 179
int string_diff(const char *s, const char *t)
4394 ripley 180
{
45017 ripley 181
    int ch1, ch2;
182
    int diff = 0;
4394 ripley 183
 
45017 ripley 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
    }
4394 ripley 194
 
45017 ripley 195
    return diff;
4394 ripley 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
41793 ripley 203
char *to_dos_string(const char *text)
4394 ripley 204
{
45017 ripley 205
    const char *s;
206
    char *newstr, *ss;
207
    char prev;
208
    long length = 0;
4394 ripley 209
 
45017 ripley 210
    if (!text)
211
	return (char *) NULL;
212
    prev = '\0';
213
    for (s = text; *s != '\0'; s++) {
214
	length++;
215
	if ((*s == '\n') && (prev != '\r'))
216
	    length++;
217
	prev = *s;
218
    }
4394 ripley 219
 
45017 ripley 220
    newstr = array (length, char);
4394 ripley 221
 
45017 ripley 222
    prev = '\0';
223
    for (ss = newstr; *text != '\0'; ss++, text++) {
224
	if ((*text == '\n') && (prev != '\r')) {
225
	    *ss = '\r';
226
	    ss++;
4394 ripley 227
	}
45017 ripley 228
	*ss = *text;
229
	prev = *ss;
230
    }
231
    *ss = '\0';
4394 ripley 232
 
45017 ripley 233
    return newstr;
4394 ripley 234
}
235
 
236
/*
237
 *  Strip carriage returns from a string. The resulting
238
 *  string must later be freed.
239
 */
240
PROTECTED
41793 ripley 241
char *to_c_string(const char *text)
4394 ripley 242
{
45017 ripley 243
    const char *s;
244
    char *newstr, *ss;
245
    long length = 0;
4394 ripley 246
 
45017 ripley 247
    if (!text)
248
	return (char *) NULL;
249
    for (s = text; *s != '\0'; s++) {
250
	length++;
251
	if ((*s == '\r') && (*(s+1) == '\n'))
252
	    length--;
253
    }
4394 ripley 254
 
45017 ripley 255
    newstr = array (length, char);
4394 ripley 256
 
45017 ripley 257
    for (ss = newstr; *text != '\0'; ss++, text++) {
258
	if ((*text == '\r') && (*(text+1) == '\n'))
259
	    text++;
260
	*ss = *text;
261
    }
262
    *ss = '\0';
4394 ripley 263
 
45017 ripley 264
    return newstr;
4394 ripley 265
}