forked from stgl/poly3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetwords.c
128 lines (115 loc) · 3.65 KB
/
getwords.c
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
/*==================================================
SET TABSTOPS AT EVERY FOUR SPACES FOR PROPER DISPLAY
====================================================*/
/*****************************************************************************
* FILE: getwords.c
* DATE: June, 1993
* BY: Andrew L. Thomas
*
* A function for reading input lines and setting an array of pointers to
* each word on the line.
*****************************************************************************/
/***************************** Includes/Defines *****************************/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include "getwords.h"
#define TRUE 1
#define FALSE 0
/**************************** function: getwords ***********************
*
* Reads a line of input into the character array line from the file ifp
* using fgets. Copies line to the static array wline. Then sets the
* character pointers word[0]..word[n-1] to point to the first letters
* of the 1st..nth words in line. Changes the space following each word
* in line to '\0', so that each word[i] will be a null terminated,
* one-word string.
*
* Returns the number of words on the line, or one of the following error
* codes:
* GW_EOF_ERR - End of File
* GW_MALLOC_ERR - Malloc() error
* GW_MAXWORDS_ERR - Max number of words exceeded error
*
* In: ifp - input file pointer
* maxline - maximum number of characters on a line
* maxword - maximum number of words on a line
* continue_char - line continuation character
* Out: line - input line
* word - array of words on input line
************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
int getwords(FILE *ifp, char *line, int maxline, char *word[],int maxwords, char continue_char)
#else
int getwords(ifp, line, maxline, word, maxwords, continue_char)
FILE *ifp;
char *line;
int maxline;
char *word[];
int maxwords;
char continue_char;
#endif
{
int i;
int j = 0; /* index for word[] */
int inword = FALSE;
int inquotes = FALSE;
char *start_at;
static char *wline = NULL;
static int oldmaxline;
/* Free/allocate memory for wline if necessary
----------------------------------------------*/
if ((wline == NULL) || (maxline > oldmaxline)) {
if (wline != NULL)
free(wline);
if ((wline = (char *) malloc((size_t) maxline)) == NULL)
return(GW_MALLOC_ERR);
oldmaxline = maxline;
}
/* Read lines from ifp until a line not ending in continue_char is
found. Concatonate these lines together into a single getwords() line
-------------------------------------------------------------------------*/
start_at = line;
while (start_at != NULL) {
if (start_at != line) {
*start_at = ' ';
start_at++;
}
if (fgets(start_at,maxline-(start_at-line),ifp) == NULL)
return(GW_EOF_ERR);
start_at = strchr(line,continue_char);
}
strcpy(wline,line);
/* Step through line and set word pointers
------------------------------------------*/
for (i = 0; wline[i] != '\0'; i++) {
if (wline[i]=='"') {
if (!inquotes) {
if (inword) {
if (j >= maxwords-1) return (GW_MAXWORDS_ERR);
word[j++] = &wline[i+1];
} /*if*/
else
if (j >= maxwords-1) return (GW_MAXWORDS_ERR);
word[j++] = &wline[i+1];
inquotes = TRUE;
} /*if*/
else {
wline[i] = '\0';
inquotes = FALSE;
} /*else*/
} /*if*/
else if (!inquotes && !inword && !isspace(wline[i])) {
if (j >= maxwords-1) return (GW_MAXWORDS_ERR);
word[j++] = &wline[i];
inword = TRUE;
} /*if*/
else if (inword && isspace(wline[i])) {
wline[i] = '\0';
inword = FALSE;
} /*else if*/
} /*for*/
return j;
}