getfileword.c 1.46 KB
Newer Older
priyank's avatar
priyank committed
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
/*
 *
 * FUNCTION getfileword(fp,word)
 * last modified on 25 May 1991.
 *
 * This function fetches the next word from the current line
 * of the specified file. It returns the length of the word if aword is read;
 * otherwise returns 0 for '\n'(previous word read was the last word in the 
 * line), or returns -1 for '\0'(previous word read was the last word in input).
 *
 * Open the file before calling this function
 * 
 * All valid C identifiers are returned as words.
 * Initial non-letters are ignored. That is, 12abc45efg77 is read as abc45efg77.
 * All non-letters and non-digits(except for "_")  are treated as 
 * field seperators, e.g. west@point23 will be read as west and point23.
 * 
 */

#include <stdio.h>
#include <ctype.h>

int getfileword(fp,word)
FILE *fp;
char *word;
{
	int i = 0;
        int c;

	word[0] = '\0';
	if ((c = getc(fp)) == EOF)
		return (-1); /* first char EOF or ERROR in file read */
	if (c == '\n' )
	{
		word[0] = '\n';
		word[1] = '\0';
		return (0);  /* first char read is newline */ 
	}
/*
	while ((c != EOF) && (!(isalpha(c))) && (c != '\n') )
*/
	while ((c==' ') ||(c=='\t'))
		c = getc(fp);    /* skipping all  spaces, */
	if (c == EOF)
		return (-1);
	word[i++] = c;
	if (c == '\n' )
	{
		word[1] = '\0';
		return (0);  /* only newline found */
	}
	while (isalpha(c =getc(fp)) || isdigit(c) || (c == '_') || (c == '-'))
		word[i++] = c;
	if (c != EOF)
		ungetc(c, fp); /* EOF cannot be ungetted */
	word[i] = '\0';
	return (i); /* word found */
}