METALINK_PARSE_FILE(3)

NAME
metalink_parse_file, metalink_parse_fp, metalink_parse_fd, metalink_parse_memory - Parse Metalink file and create metalink_t object.

SYNOPSIS
#include <metalink/metalink.h>

metalink_error_t metalink_parse_file(const char *filename, metalink_t **res);
metalink_error_t metalink_parse_fp(FILE *docfp, metalink_t **res);
metalink_error_t metalink_parse_fd(int docfd, metalink_t **res);
metalink_error_t metalink_parse_memory(const char *buf, size_t len, metalink_t **res);

DESCRIPTION
These functions parse Metalink file data and constructs metalink_t structure. You don't have to allocate memory for metalink_t structure. They take the pointer of metalink_t pointer and allocate memory for that pointer. metalink_parse_file() parses Metalink file denoted by filename and constructs metalink_t structure. metalink_parse_fp() reads data from file stream docfp and construtcts metalink_t structure. metalink_parse_fd() reads data from file descriptor docfd and constructs metalink_t structure. metalink_parse_memory() parses len bytes of buf and constructs metalink_t structure. The caller must free the memory allocated for metalink_t structure using metalink_delete(3) if it is no longer used.

RETURN VALUE
All functions return 0 for success. When error occurred, non-zero value error code is returned and metalink_t structure is not allocated. The error codes are described in metalink_error.h.

EXAMPLE
#include <stdio.h>
#include <stdlib.h>
#include <metalink/metalink.h>
int main(int argc, char** argv)
{
  metalink_error_t r;
  metalink_t* metalink;
  metalink_file_t* file;
  metalink_checksum_t** checksums;

  r = metalink_parse_file("sample.metalink", &metalink);
  if(r != 0) {
    fprintf(stderr, "ERROR: code=%d\n", r);
    exit(EXIT_FAILURE);
  }

  file = metalink->files[0];
  printf("name: %s\n", file->name);
  printf("size: %lld\n", file->size);
  printf("os  : %s\n", file->os);
  if(file->checksums) {
    checksums = file->checksums;
    while(*checksums) {
      printf("hash: %s %s\n", (*checksums)->type, (*checksums)->hash);
      ++checksums;
    }
  }
  if(file->chunk_checksum) {
    size_t count = 0;
    metalink_piece_hash_t** piece_hashes;
    printf("chunk checksum: size=%d, type=%s\n",
           file->chunk_checksum->length,
           file->chunk_checksum->type);
    printf("first 5 piece hashes...\n");
    piece_hashes = file->chunk_checksum->piece_hashes;
    while(*piece_hashes && count < 5) {
      printf("piece=%d, hash=%s\n", (*piece_hashes)->piece,
                                     (*piece_hashes)->hash);
      ++piece_hashes;
      ++count;
    }
    printf("...\n");
  }
  if(file->resources) {
    size_t count = 0;
    metalink_resource_t** resources;
    printf("first 5 resources...\n");
    resources = file->resources;
    while(*resources && count < 5) {
      printf("type=%s, location=%s, preference=%d, url=%s\n",
             (*resources)->type, (*resources)->location,
             (*resources)->preference, (*resources)->url);
      ++resources;
      ++count;
    }
    printf("...\n");
  }
  /* delete metalink_t */
  metalink_delete(metalink);
  return EXIT_SUCCESS;
}

SEE ALSO
metalink_delete(3), metalink_parse_update(3), metalink_t(3)