File: string.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (340 lines) | stat: -rw-r--r-- 7,923 bytes parent folder | download | duplicates (4)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* ----------------------------------------------------------------------------
@COPYRIGHT  :
              Copyright 1993,1994,1995 David MacDonald,
              McConnell Brain Imaging Centre,
              Montreal Neurological Institute, McGill University.
              Permission to use, copy, modify, and distribute this
              software and its documentation for any purpose and without
              fee is hereby granted, provided that the above copyright
              notice appear in all copies.  The author and McGill University
              make no representations about the suitability of this
              software for any purpose.  It is provided "as is" without
              express or implied warranty.
---------------------------------------------------------------------------- */

#include  <internal_volume_io.h>

static const  STRING  empty_string = "";

VIOAPI  STRING  alloc_string(
    int   length )
{
    STRING   str;

    ALLOC( str, length+1 );

    return( str );
}

VIOAPI  STRING  create_string(
    STRING    initial )
{
    STRING   str;

    if( initial == NULL )
        initial = empty_string;

    str = alloc_string( string_length(initial) );

    (void) strcpy( str, initial );

    return( str );
}

VIOAPI  void  delete_string(
    STRING   string )
{
    if( string != NULL )
        FREE( string );
}

VIOAPI  STRING  concat_strings(
    STRING   str1,
    STRING   str2 )
{
    STRING  str;

    if( str1 == NULL )
        str1 = empty_string;

    if( str2 == NULL )
        str2 = empty_string;

    ALLOC( str, string_length(str1) + string_length(str2) + 1 );

    (void) strcpy( str, str1 );
    (void) strcat( str, str2 );

    return( str );
}

VIOAPI  void  replace_string(
    STRING   *string,
    STRING   new_string )
{
    delete_string( *string );
    *string = new_string;
}

VIOAPI  void  concat_char_to_string(
    STRING   *string,
    char     ch )
{
    int  len;

    len = string_length( *string );

    if( *string == NULL )
        *string = alloc_string( 1 );
    else
        SET_ARRAY_SIZE( *string, len+1, len+2, 1 );

    (*string)[len] = ch;
    (*string)[len+1] = END_OF_STRING;
}

VIOAPI  void  concat_to_string(
    STRING   *string,
    STRING   str2 )
{
    STRING  new_string;

    new_string = concat_strings( *string, str2 );
    replace_string( string, new_string );
}

VIOAPI  int  string_length(
    STRING   string )
{
    if( string == NULL )
        return( 0 );
    else
        return( (int) strlen( string ) );
}

VIOAPI  BOOLEAN  equal_strings(
    STRING   str1,
    STRING   str2 )
{
    if( str1 == NULL )
        str1 = empty_string;
    if( str2 == NULL )
        str2 = empty_string;

    return( strcmp( str1, str2 ) == 0 );
}

VIOAPI  BOOLEAN  is_lower_case(
    char  ch )
{
    return( ch >= 'a' && ch <= 'z' );
}

VIOAPI  BOOLEAN  is_upper_case(
    char  ch )
{
    return( ch >= 'A' && ch <= 'Z' );
}

VIOAPI  char  get_lower_case(
    char   ch )
{
    if( is_upper_case( ch ) )
        return( (char) ((ch)+'a'-'A') );
    else
        return( ch );
}

VIOAPI  char  get_upper_case(
    char   ch )
{
    if( is_lower_case( ch ) )
        return( (char) ((ch)+'A'-'a') );
    else
        return( ch );
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : string_ends_in
@INPUT      : string
            : ending
@OUTPUT     : 
@RETURNS    : TRUE if string ends in "ending"
@DESCRIPTION: Checks if the string ends in ending, e.g., 
            : string_ends_in( "main.c", ".c" ) returns true.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  BOOLEAN  string_ends_in(
    STRING   string,
    STRING   ending )
{
    int      len_string, len_ending;
    BOOLEAN  ending_present;

    len_string = string_length( string );
    len_ending = string_length( ending );

    if( len_ending > len_string )
        ending_present = FALSE;
    else
        ending_present = equal_strings( &string[len_string-len_ending], ending);

    return( ending_present );
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : strip_outer_blanks
@INPUT      : str
@OUTPUT     : stripped
@RETURNS    : 
@DESCRIPTION: Creates a new string which is the original str without any
            : leading or trailing blanks.  Output argument may be the same
              pointer as input.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI    STRING   strip_outer_blanks(
    STRING  str )
{
    STRING  stripped;
    int  i, first_non_blank, last_non_blank, len;

    len = string_length( str );

    /* --- skip leading blanks */

    first_non_blank = 0;
    while( first_non_blank < len && str[first_non_blank] == ' ' )
    {
        ++first_non_blank;
    }

    /* --- skip trailing blanks */

    last_non_blank = len-1;
    while( last_non_blank >= 0 && str[last_non_blank] == ' ' )
    {
        --last_non_blank;
    }

    /* --- now copy string, without leading or trailing blanks */

    if( first_non_blank > last_non_blank )
        last_non_blank = first_non_blank - 1;

    stripped = alloc_string( last_non_blank - first_non_blank + 1 );

    for_inclusive( i, first_non_blank, last_non_blank )
        stripped[i-first_non_blank] = str[i];

    stripped[last_non_blank - first_non_blank + 1] = END_OF_STRING;

    return( stripped );
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : find_character
@INPUT      : string
            : ch
@OUTPUT     : 
@RETURNS    : index of ch within string or -1
@DESCRIPTION: Finds the index of the given character within the string.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  int  find_character(
    STRING    string,
    char      ch )
{
    int   i;

    if( string == NULL )
        return( -1 );

    i = 0;
    while( string[i] != END_OF_STRING )
    {
        if( string[i] == ch )
            return( i );
        ++i;
    }

    return( -1 );
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : make_string_upper_case
@INPUT      : string
@OUTPUT     : string
@RETURNS    : 
@DESCRIPTION: Converts every lower case character in string to upper case.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  void  make_string_upper_case(
    STRING    string )
{
    int   i, len;

    len = string_length( string );

    for_less( i, 0, len )
    {
        string[i] = get_upper_case( string[i] );
    }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : blank_string
@INPUT      : string
@OUTPUT     : 
@RETURNS    : TRUE if string is blank
@DESCRIPTION: Checks to see if the string is blank; only contains space,
              tabs, and newlines.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : 1993            David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  BOOLEAN  blank_string(
    STRING   string )
{
    int      i;
    BOOLEAN  blank;

    if( string == NULL )
        string = empty_string;

    blank = TRUE;
    i = 0;
    while( string[i] != END_OF_STRING )
    {
        if( string[i] != ' ' && string[i] != '\t' && string[i] != '\n' )
        {
            blank = FALSE;
            break;
        }
        ++i;
    }

    return( blank );
}