Ejemplo n.º 1
0
void unpack_from_file()
{
	FILE *fp = NULL;
	char buf[MAX_LEN];
	int sum, total;
	AMessage *amsg = NULL;

	if (NULL == (fp = fopen(FILE_NAME, "r"))) {
		fprintf(stderr, "fopen failed");
		exit(-1);
	}

	total = 0;
	while (0 < (sum = fread(buf+total, 1, MAX_LEN, fp))) {
		total += sum;
		if (total >= MAX_LEN) {
			fprintf(stderr, "read more than MAX_LEN");
			exit(-1);
		}
	}

	if (NULL == (amsg = amessage__unpack(NULL, total, buf))) {
		fprintf(stderr, "amessage__unpack failed");
		exit(-1);
	}

	printf("amsg->a = %d\n", amsg->a);
	if(amsg->has_b) {
		printf("amsg->b = %d\n", amsg->b);
	}

	amessage__free_unpacked(amsg, NULL);

	fclose(fp);
}
Ejemplo n.º 2
0
int main (int argc, const char * argv[]) 
{
  AMessage *msg;

  // Read packed message from standard-input.
  uint8_t buf[MAX_MSG_SIZE];
  size_t msg_len = read_buffer (MAX_MSG_SIZE, buf);

  // Unpack the message using protobuf-c.
  msg = amessage__unpack(NULL, msg_len, buf);   
  if (msg == NULL)
    {
      fprintf(stderr, "error unpacking incoming message\n");
      exit(1);
    }

  // display the message's fields.
  printf("Received: a=%d",msg->a);  // required field
  if (msg->has_b)                   // handle optional field
    printf("  b=%d",msg->b);
  printf("\n");

  // Free the unpacked message
  amessage__free_unpacked(msg, NULL);
  return 0;
}