-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetword.h
51 lines (37 loc) · 972 Bytes
/
getword.h
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
#ifndef GETWORDH
#define GETWORDH 1
#include <stdlib.h>
#include <string.h>
char* getword(char delim, char** line) {
// Skip leading delimiters
while(**line == delim) {
#ifdef DEBUG
printf("Inside getword - removing leading delimiter.\n");
#endif
(*line)++;
}
char* word_start = *line;
char* word_end = *line - 1;
// Is this an empty line?
if((!line) || **line == EOF || **line == '\n') { return NULL; }
// Proceed to the end of the word
while (**line && **line != EOF && **line != '\n' && **line != delim) {
word_end = *line;
(*line)++;
}
int word_length = 1 + word_end - word_start;
#if DEBUG
printf("Inside getword - word length: [%d] characters.\n", word_length);
#endif
if(word_length < 1) {
#ifdef DEBUG
printf("Inside getword - Blank word.\n");
#endif
return NULL;
}
char* result = malloc(sizeof(char) * (word_length + 1));
memcpy(result, word_start, word_length);
result[word_length] = '\0';
return result;
}
#endif