The program deals with tokens and seperators.
So I need to use recursion to get the next token from the input, choose if it is a token or separator, and then return the token buffer, and store the delimiters into another buffer which gets returned in another method.
My issue is the recursion. We are supposed to use fgets to put the input line into the line buffer, but once the char's are in the buffer, the only way i can think of going through it is using a while loop. Doing recursion with the method just scans in another line to the buffer, and everything after the first token and first separator is lost.
I have been struggling with this for a long time, please help!!
Code:
char *tokrdr_next_token(void){
if( strlen( _tokrdr_line_buffer) > _tokrdr_line_limit){
error_fatal(TOKRDR_ERROR_LINE_LENGTH, _tokrdr_line_number);
}
fgets ( _tokrdr_line_buffer, _tokrdr_line_limit, _tokrdr_input);
_tokrdr_token_index = 0;
_tokrdr_separator_index = 0;
_tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index];
if(_tokrdr_next_character == '\n'){
_tokrdr_line_number++;
}
printf("NEXT CHAR %s\n", _tokrdr_line_buffer);
while (_tokrdr_next_character != *_tokrdr_delimiters){
_tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index];
_tokrdr_token_buffer[_tokrdr_token_index] = _tokrdr_next_character;
_tokrdr_token_index++;
}
while (_tokrdr_next_character == *_tokrdr_delimiters){
_tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index + _tokrdr_separator_index];
_tokrdr_separator_buffer[_tokrdr_separator_index] = _tokrdr_next_character;
_tokrdr_separator_index++;
}
//tokrdr_next_token();
printf("NEXT CHAR %c\n", _tokrdr_line_buffer[_tokrdr_token_index]);
return _tokrdr_token_buffer;
}