File: strutil.c

package info (click to toggle)
sift 6.2.1-2
  • links: PTS, VCS
  • area: non-free
  • in suites: sid
  • size: 4,784 kB
  • sloc: ansic: 18,272; perl: 219; csh: 164; makefile: 152
file content (226 lines) | stat: -rwxr-xr-x 5,137 bytes parent folder | download | duplicates (5)
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
/* (C) Copyright 1993-9, Fred Hutchinson Cancer Research Center */
/* Use, modification or distribution of these programs is subject to */
/* the terms of the non-commercial licensing agreement in license.h. */

/* strutil.c: general string utilities */
/* Written by: Bill Alford */
/* Change log information is at the end of the file. */


/*	system headers not in global.h */
/*	blimps library headers not in global.h */
#include <global.h>

/*
 * Exported variables and data structures definitions
 */

char Buffer[LARGE_BUFF_LENGTH];

/*
 * Local variables and data structures
 */

/*
 * Function definitions
 */

/* 
 *  Boolean blank_line(s)
 *  char *eat_whitespace(s)
 *  void remove_trailing_whitespace(s)
 *  char *get_token(s)
 */

/* 
 * blank_line
 *  returns TRUE if the passed string is only whitespace
 *  Parameters:
 *    char* s: the string to check
 *  Return codes:
 *    TRUE if the line only has whitespace characters, FALSE otherwise.
 *  Error Codes:
 */

Boolean blank_line(s)
     char *s;
{
  char *tmp;
  int length;

  tmp = eat_whitespace(s);
  length = strlen(tmp);

  if (length == 0) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}


/*
 * eat_whitespace
 *   Eats the whitespace at the beginning of the string s1 and returns a 
 *   pointer to the first non whitespace character.  The whitespace characters
 *   are: space, tab, and newline.
 *   Parameters:
 *     char *s : input string
 *   Return Value: the return value is the pointer to the first non-whitespace
 *                 character
 *   Error codes:
 */

char *eat_whitespace(s)
     char *s;
{
/*  
  int white_length; 
*/

  while ((*s == ' ') ||
	 (*s == '\t') ||
	 (*s == '\n') ||
	 (*s == '\r')) {
    s++;
  }
  
  return (s);

/*
  white_length = strspn(s, WHITE_SPACE_CHARS);
  
  return (s+white_length);
*/
}


/*
 * remove_trailing_whitespace
 *   Removes trailing whitespace by scanning back from the end of the string
 *   and replacing the leftmost trailing whitespace character with a NULL.
 *   Whitespace characters are: space, tab, newline and carriage return.
 *   NOTE: this is destructive
 *   Parameters:
 *     char *s : input string
 *   Return Value: returns its first argument
 *   Error codes:
 */

char *remove_trailing_whitespace(s)
     char *s;
{
  int length;

  length = strlen(s);

  /* if the string is empty, exit */
  if (length == 0) {
    return(NULL);
  }

  length--;				/* indexing starts at zero */

  while ((s[length] == ' ') ||
	 (s[length] == '\t') ||
	 (s[length] == '\n') ||
	 (s[length] == '\r')) {
    length--;
  }
  
  s[length+1] = '\0';

  return(s);
}

/*
 * get_token
 *   get_token returns a pointer to a token delimited by whitespace.  The
 *   first call with a string returns the first token.  Subsequent calls (with
 *   the string as NULL) returns pointers to the following tokens.  A NULL
 *   is placed following the token.
 *   Note: get_token must be called with a non-null string pointer the first
 *         time or the behavior is ambiguous.
 *   Whitespace is: space, tab, newline, carriage return
 *   Parameters: 
 *     char *s: the string of tokens
 *   Return Value: the pointer to the token, NULL when there are no more tokens
 *   Error codes:
 *   Notes: get_token() wraps strtok() because the separators will always be 
 *          whitespace and strtok() is cryptic and easy to forget. (for me).
 */

char *get_token(s)
     char *s;
{
#ifdef NO_STRTOK
  char c;
  char *return_string;
  static char *saved_string_pointer; /* pointer right after NULL char */



  if (s != NULL) {
    /* save the new string pointer */
    saved_string_pointer = s;
    remove_trailing_whitespace(saved_string_pointer);
  }

  /* if finished with this string last time, return NULL right away */
  if (saved_string_pointer == NULL) {
    return NULL;
  }

  /* eat up the beginning whitespace, if any */
  saved_string_pointer = eat_whitespace(saved_string_pointer);

  /* read ahead from saved pointer */
  return_string = saved_string_pointer;
  
  c = *saved_string_pointer;
  while ((c != ' ')  &&
    	 (c != '\t') &&
	 (c != '\n') &&
	 (c != '\r') &&
	 (c != '\0')) {
    saved_string_pointer++;
    c = *saved_string_pointer;
  }

  /* if saw '\0', finished the string */
  if (*saved_string_pointer == '\0') {
    saved_string_pointer = NULL;
    return return_string;
  }
  else {
    /* write in the null delimiting character */
    *saved_string_pointer = '\0';
    saved_string_pointer++;
    return return_string;
  }

#else
  return (char *)strtok(s, " \t\n\r\0");  
                                             /* typecast shouldn't be needed */
					     /* but compiler says it is a */
					     /* type mismatch of pointer and */
					     /* integer */
#endif /* NO_STRTOK */

}



/* Change log information follows. */
/* 
 * Revision 2.2002  1994/07/07  00:45:58  billa
 * Made remove_trailing_whitespace() return its argument.
 *
 * Revision 2.2001  1994/05/18  19:16:26  billa
 * Removed global.c and moved Buffer variable to strutil.[ch].
 *
 * Revision 2.1001  1994/04/21  01:03:18  billa
 * Creation.  Moved these from files.[ch].
 *
 */