/*
 * Purpose: Print the contents of the character buffer.
 * Author: Jason Macfarlane
 * History/Versions: Created January 6, 2015, Last revised January 20, 2015 - Version 1.1
 * Called functions: printf(), b_getc(), b_eob()
 * Parameters: Buffer* pBD
 * Return value: int
 * Algorithm: If the buffer is empty, notify the user. Otherwise, get each character from the
 character buffer by calling b_getc(), and print it. Keep looping until the end of buffer
 is reached. Return the number of characters printed.
 */
int b_print(Buffer * const pBD) {
    int numChars = 0; /* The number of characters printed */
    char c; /* The character from the character buffer */
    
    /*
     * If the buffer is empty, print a message. Otherwise, print each character from
     * the character buffer.
     */
    if (!pBD->addc_offset) {
        printf("The buffer is empty.\n");
        return numChars;
    }
    
    pBD->getc_offset = 0;
    while(1) {  /* WARNING: constant in while-loop */
        c = b_getc(pBD);
        if (b_eob(pBD))
            break;
        printf("%c", c);
        ++numChars;
    }
    numChars = pBD->getc_offset;
    pBD->getc_offset = 0;
    printf("\n");
    
    return numChars;
}
Example #2
0
/*********************************************************************
*	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;
	}
}