示例#1
0
static Product *
get_product_from_line (const char *line)
{
	Product *ret;

	const char *start;
	const char *next;
	/* We'll only strdup() name at the end, so errors don't leak it */
	const char *name_temp;
	size_t name_size;

	unsigned int id;
	char *name;
	int in_stock;
	double price;
	double discount;

	start = line;
	_GET_PRODUCT_MOVE_TO_NEXT_COMMA;

	id = (unsigned int) atoi (start);

	start = next + 1;
	_GET_PRODUCT_MOVE_TO_NEXT_COMMA;

	name_temp = start;
	name_size = (size_t) (next - start + 1);

	start = next + 1;
	_GET_PRODUCT_MOVE_TO_NEXT_COMMA;

	in_stock = atoi (start);

	start = next + 1;
	_GET_PRODUCT_MOVE_TO_NEXT_COMMA;

	price = (double) atof (start);

	start = next + 1;

	discount = (double) atof (start);

	name = strndup (name_temp, name_size);

	ret = product_new (id, name, in_stock, price, discount);

	free (name);

	return ret;
}
示例#2
0
文件: client.c 项目: kpjjpk/so-tp1
int __write_product(product_name name, int quantity){
	char toSend[sizeof(product_msg)];
	msg_type resp_type;
	char resp_body[sizeof(error_resp)];
	int code;

	__connect();	
	msg_serialize_product_msg(__get_id(), WRITE_PRODUCT, product_new(name, quantity), toSend);
	__send(toSend, sizeof(product_msg));
	__recv(&resp_type, sizeof(msg_type));
	if (resp_type == OK_RESP) {
		__recv(resp_body, sizeof(int));
		msg_deserialize_code(resp_body, &code);
		__disconnect();
		return OK;
	}
	return __handle_not_ok_resp(resp_type);
}
示例#3
0
文件: cmd.c 项目: odrevet/lca-tool
bool cmd_new_product(APP_LCA *p_app){
  //ask for the product name
  char product_name[BUFSIZ] = {0};
  printf("Product name: ");
  scanf("%s", product_name);

  //create a new product
  Product *tmp_product = product_new(product_name);
  if(!tmp_product){
    puts("Error while creating a new product (NULL)");
    return false;
  }

  //add this input to the app input list
  app_add_input(p_app, (Input*)tmp_product);

  return true;
}
示例#4
0
static int
main_with_input (const char *cmd,
		 const char *id_str)
{
	unsigned int id;
	List *products;
	Product *product;
	int error = 0;

	products = db_load_products ();

	id = (unsigned int) atoi (id_str);

	product = product_list_find (products, id);
	if (product == NULL && toupper (*cmd) != 'N' && toupper (*cmd) != 'L')
	{
		printf ("Product with ID %d does not exist\n", id);
		error = 1;
	}
	if (product != NULL && toupper (*cmd) == 'N')
	{
		printf ("Product with ID %d already exists\n", id);
		error = 1;
	}

	if (error)
	{
		list_foreach (products, LIST_FUNC (product_free));
		list_free (products);
		return 1;
	}

	switch (toupper (*cmd))
	{
		case 'N':
			product = product_new (id, NULL, 0, 0, 0);
			products = list_append (products, product);
			/* fall through */
		case 'E':
			products = edit_item (products, product);
			db_write_products (products);
			break;
		case 'V':
			view_product (product);
			break;
		case 'D':
			view_product (product);
			if (prompt_y_n ("Delete this item")) {
				products = list_remove (products, product);
				product_free (product);
				db_write_products (products);
			}
			break;
		case 'L':
			menu_list_items (products);
			break;
	}

	list_foreach (products, LIST_FUNC (product_free));
	list_free (products);

	return 0;
}
示例#5
0
/*
 * Edits the given item, with prompts.
 *
 * Pass it a NULL product to prompt to create a new item.
 */
static List *
edit_item (List *products,
	   Product *product)
{
	char input[INPUT_BUFFER_SIZE];
	unsigned int id;
	int is_new;
	int want_edit = 0;

	while (product == NULL)
	{
		printf ("Enter an integer product ID: ");
		fflush (stdout);

		fgets (input, INPUT_BUFFER_SIZE, stdin);

		if (*input == 'Q' || *input == 'q')
		{
			return products;
		}

		id = (unsigned int) atoi (input);

		if (product_list_find (products, id) != NULL)
		{
			printf ("Product #%u already exists\n", id);
			printf ("Try another ID or type 'q' to escape\n");
			continue;
		}

		product = product_new (id, NULL, 0, 0, 0);
		products = list_append (products, product);
	}

	is_new = (product->name == NULL);

	want_edit = is_new;
	if (!is_new)
	{
		printf ("Name: %s\n", product->name);
		want_edit = prompt_y_n ("Change name");
	}
	if (want_edit)
	{
		printf ("Enter the product's name: ");
		fflush (stdout);
		fgets (input, INPUT_BUFFER_SIZE, stdin);
		product->name = strndup (input, strlen (input)); /* minus \n */
	}

	want_edit = is_new;
	if (!is_new)
	{
		printf ("In stock: %d\n", product->in_stock);
		want_edit = prompt_y_n ("Change quantity");
	}
	if (want_edit)
	{
		printf ("Enter quantity in stock: ");
		fflush (stdout);
		fgets (input, INPUT_BUFFER_SIZE, stdin);
		product->in_stock = atoi (input);
	}

	want_edit = is_new;
	if (!is_new)
	{
		printf ("Price: %0.2f\n", product->price);
		want_edit = prompt_y_n ("Change price");
	}
	if (want_edit)
	{
		printf ("Enter price: ");
		fflush (stdout);
		fgets (input, INPUT_BUFFER_SIZE, stdin);
		product->price = (double) atof (input);
	}

	want_edit = is_new;
	if (!is_new)
	{
		printf ("Discount: %0.2f%%\n", product->discount);
		want_edit = prompt_y_n ("Change discount");
	}
	if (want_edit)
	{
		printf ("Enter discount in %%: ");
		fflush (stdout);
		fgets (input, INPUT_BUFFER_SIZE, stdin);
		product->discount = (double) atof (input);
	}

	return products;
}
示例#6
0
int main(){
  //expected values
  const float EXPECTED_HOUSE_STONE = HOUSE_STONE_AMOUNT;
  const float EXPECTED_HOUSE_WOOD = HOUSE_WOOD_AMOUNT + (HOUSE_WINDOW_AMOUNT * WINDOW_WOOD_AMOUNT);
  const float EXPECTED_HOUSE_SAND = HOUSE_WINDOW_AMOUNT * (WINDOW_GLASS_AMOUNT * GLASS_SAND_AMOUNT);

  //optimistic initialization
  int acceptance = 1;
  
  //define the app
  APP_LCA app;
  app_init(&app);
  
  //create some materials
  Material *wood = material_new("Wood");
  app_add_input(&app, (Input*) wood);

  Material *sand = material_new("Sand");
  app_add_input(&app, (Input*) sand);

  Material *stone = material_new("Stone");
  app_add_input(&app, (Input*) stone);

  //create some products
  Product *glass = product_new("Glass");
  app_add_input(&app, (Input*) glass);

  Product *window = product_new("Window");
  app_add_input(&app, (Input*) window);

  Product *house = product_new("House");
  app_add_input(&app, (Input*) house);

  //set amount for products
  //glass
  Input_Amount glass_sand_IA;
  glass_sand_IA.p_input = (Input*)sand;
  glass_sand_IA.amount = GLASS_SAND_AMOUNT;

  
  product_add_input_amount(glass, &glass_sand_IA);

  //window
  Input_Amount window_wood_IA;
  window_wood_IA.p_input = (Input*)wood;
  window_wood_IA.amount = WINDOW_WOOD_AMOUNT;

  Input_Amount window_glass_IA;
  window_glass_IA.p_input = (Input*)glass;
  window_glass_IA.amount = WINDOW_GLASS_AMOUNT;


  product_add_input_amount(window, &window_wood_IA);
  product_add_input_amount(window, &window_glass_IA);
    
  //house
  Input_Amount house_stone_IA;
  house_stone_IA.p_input = (Input*)stone;
  house_stone_IA.amount = HOUSE_STONE_AMOUNT;

  Input_Amount house_wood_IA;
  house_wood_IA.p_input = (Input*)wood;
  house_wood_IA.amount = HOUSE_WOOD_AMOUNT;

  Input_Amount house_window_IA;
  house_window_IA.p_input = (Input*)window;
  house_window_IA.amount = HOUSE_WINDOW_AMOUNT;


  product_add_input_amount(house, &house_stone_IA);
  product_add_input_amount(house, &house_wood_IA);
  product_add_input_amount(house, &house_window_IA);
  
  //calculate how many raw material is needed to build a house
  Input_Amount *p_input_amount_list=NULL;
  int nb_input_amount = 0;
  float parent_amount = 1;
  
  p_input_amount_list = product_extend(house,
				       parent_amount,
				       p_input_amount_list,
				       &nb_input_amount);

  //create a product to display the calculation
  Product product_all_raw_inputs;
  product_all_raw_inputs.nb_input_amount = nb_input_amount;
  input_set_name((Input*)&product_all_raw_inputs, house->name);
  product_all_raw_inputs.input_amount_list = p_input_amount_list;

  //print, no needs to multiply with parent amount here, the calculations are
  //all done
  product_print_inputs(&product_all_raw_inputs, 1);
  printf("\n");
  

  //print expected results
  printf("Expected\n%s %.1f, %s %.1f, %s %.1f\n",
	 "Stone", EXPECTED_HOUSE_STONE,
	 "Sand", EXPECTED_HOUSE_SAND,
	 "Wood", EXPECTED_HOUSE_WOOD
	 );

  //compare values and set return value acceptance
  Input_Amount *house_stone_IA_total =  get_input_amount_by_name(p_input_amount_list,
								 nb_input_amount,
								 "Stone");
  if(house_stone_IA_total->amount != EXPECTED_HOUSE_STONE){
    printf("ERROR ! get %f stone, expected %f\n",
	   house_stone_IA_total->amount,
	   EXPECTED_HOUSE_STONE);
    acceptance = 0;
  }
  else{
    puts("Stone.......[Ok]");
  }


  Input_Amount *house_wood_IA_total =  get_input_amount_by_name(p_input_amount_list,
								 nb_input_amount,
								 "Wood");
  
  if(house_wood_IA_total->amount != EXPECTED_HOUSE_WOOD){
    printf("ERROR ! get %f wood, expected %f\n",
	   house_wood_IA_total->amount,
	   EXPECTED_HOUSE_WOOD);
    acceptance = 0;
  }
  else{
    puts("Wood.......[Ok]");
  }

  
  Input_Amount *house_sand_IA_total =  get_input_amount_by_name(p_input_amount_list,
								nb_input_amount,
								"Sand");
  
  if(house_sand_IA_total->amount != EXPECTED_HOUSE_SAND){
    printf("ERROR ! get %f sand, expected %f\n",
	   house_sand_IA_total->amount,
	   EXPECTED_HOUSE_SAND);
    acceptance = 0;
  }
  else{
    puts("Sand.......[Ok]");
  }
  
  //free memory
  product_clear_inputs(&product_all_raw_inputs);


  if(acceptance)puts("All tests OK ! ");
  else puts("Oops ! Something when wrong, check log");

  app_free(&app);
  
  return acceptance;
}