Beispiel #1
0
/**
 * Determine the model number, cost, and description of the product 
 *  that is associated with a barcode. If the product is on sale, this 
 *  check will return the sale price. 
 *
 * @return SUCCESS on success, else -1
 */
static int do_check(void) {
	Product *p = NULL;
	char bc[BARCODE_SZ] = {0};
	float sale_price = 0.0;
	unsigned int d_len = 0;

	// recv barcode
	RECV(STDIN, bc, BARCODE_SZ);

	// find product in inventory with matching bar code
	p = get_product_by_barcode(bc);

	// if not found, return -1
	if (NULL == p) return -1;

	// if found, send model number, cost, and description
	SEND(STDOUT, (char *)&p->model_num, sizeof(unsigned int));

	sale_price = p->sfn(p->model_num, p->cost);
	SEND(STDOUT, (char *)&sale_price, sizeof(float));

	d_len = strlen(p->desc, '\0');
	if (0 < d_len)
		SEND(STDOUT, p->desc, d_len);
	// terminate the description string
	SEND(STDOUT, DESC_TERM, 1);

	return SUCCESS;
}
Beispiel #2
0
/**
 * Process the listing of all products. Provide more detail depending on selected options.
 *
 * @return SUCCESS on success, else -1
 */
static int do_list(void) {
	Product *p = NULL;
	char options[4] = {0};
	unsigned int count = list_length(&inv);
	unsigned int d_len = 0;
	float sale_price = 0.0;
	struct node *cur = list_head_node(&inv);
	struct node *end = list_end_marker(&inv);
	// recv options
	RECV(STDIN, options, sizeof(options));

	if (0 == count) return -1;

	// send product info
	while ((NULL != cur) && (cur != end)) {
		p = (Product *)cur->data;

		// send barcode
		SEND(STDOUT, (char *)p->barcode, BARCODE_SZ);

		if (0 == options[0] % 2) {
			// send model_num
			SEND(STDOUT, (char *)&p->model_num, sizeof(unsigned int));
		}
		if (0 != options[1] % 2) {
			// send cost
			SEND(STDOUT, (char *)&p->cost, sizeof(float));
		}
		if (0 == options[2] % 2) {
			// send sale cost
			sale_price = p->sfn(p->model_num, p->cost);
			SEND(STDOUT, (char *)&sale_price, sizeof(float));
		}
		if (0 != options[3] % 2) {
			// send description
			d_len = strlen(p->desc, '\0');
			if (0 < d_len)
				SEND(STDOUT, p->desc, d_len);
			// terminate the description string
			SEND(STDOUT, DESC_TERM, 1);
		}

		cur = list_next_node(cur);
	}

	return SUCCESS;
}