metalink_parse_file - Man Page

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)

Referenced By

metalink_checksum_t(3), metalink_chunk_checksum_t(3), metalink_file_t(3), metalink_parse_update(3), metalink_piece_hash_t(3), metalink_resource_t(3), metalink_t(3).

The man pages metalink_parse_fd(3), metalink_parse_fp(3) and metalink_parse_memory(3) are aliases of metalink_parse_file(3).

July 2012 libmetalink 0.1.0 libmetalink Manual