int scanner_init(Buffer * sc_buf) { if(b_isempty(sc_buf)) return EXIT_FAILURE;/*1*/ b_set_getc_offset(sc_buf,0);/* in case the buffer has been read previously */ b_reset(str_LTBL); line = 1; return EXIT_SUCCESS;/*0*/ /* scerrnum = 0; *//*no need - global ANSI C */ }
/********************************************************************* * Purpose: Print out the characters that are stored in the Buffer. * Author: Kwok Hong Kelvin Chan * History/Versions: 1.0, September 24th, 2013 * Called functions: b_isempty b_getc() b_eob() printf() * Parameters: Type: Parameter: Values/Range: Buffer * pBD * Return value: type: value: int -1 / printed_char * Algorithm: Check if the input Buffer's pointer is NULL, if not, check whether the Buffer is empty. if not, print out the Buffer content, and increment the printed character variable. *********************************************************************/ int b_print (Buffer * const pBD){ int is_empty; /* store the return value from function b_isempty to see if Buffer is empty */ int printed_char = 0; /* variable to store the total number of characters printed */ char symbol; /* variable to store the character read from the Buffer */ /* * Check whether if the Buffer pointer is NULL, if so, return NULL. */ if (pBD != NULL){ is_empty = b_isempty(pBD); /* check if Buffer is empty */ /* * Switch statement to only print the Buffer's content when the Buffer is not empty. */ switch (is_empty){ case BUFFER_EMPTY: printf("The buffer is empty.\n"); return NUM_0; break; case BUFFER_AVAILABLE: b_set_getc_offset(pBD, NUM_0); /* reset the getc_offset to 0, and get ready to print */ /* * while true, using b_getc to get one character at a time from the Buffer, check it reaches the end of Buffer. * If not, print the character, and increment printed_char. */ while (NUM_1){ symbol = b_getc(pBD); /* using b_getc to get a character from the Buffer */ /* * check if it reaches the end of Buffer */ if (b_eob(pBD) != NUM_1){ printf("%c", symbol); ++printed_char; }else{ break; } } printf("\n"); return printed_char; break; default: return R_FAIL_1; break; } }else{ return R_FAIL_1; } }