METALINK_PARSE_FILE(3)

πŸ“¦ version 0.1.3, in epel 9, opensuse-leap 16.0

Okay bestie, you are reading the libmetalink manual page METALINK_PARSE_FILE(3) β€” and it is literally the β€œparse a Metalink file and create a metalink_t object” page. πŸ’…

πŸ’– NAME πŸ’–

metalink_parse_file, metalink_parse_fp, metalink_parse_fd, metalink_parse_memory β€” parse a Metalink file and create a metalink_t object. ✨

✨ SYNOPSIS ✨

First, include this header like the good little C bestie you are:

#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 construct a metalink_t structure. You do not have to allocate memory for the metalink_t structure β€” iconic, right? They take the pointer of a metalink_t pointer and allocate memory for that pointer. 🫢

  • metalink_parse_file() β€” parses the Metalink file denoted by filename and constructs a metalink_t structure.
  • metalink_parse_fp() β€” reads data from file stream docfp and constructs a metalink_t structure.
  • metalink_parse_fd() β€” reads data from file descriptor docfd and constructs a metalink_t structure.
  • metalink_parse_memory() β€” parses len bytes of buf and constructs a metalink_t structure.
The caller must free the memory allocated for the metalink_t structure using metalink_delete(3) if it is no longer used. No leaks, bestie. πŸ’€

πŸŽ€ RETURN VALUE πŸŽ€

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

πŸ¦‹ EXAMPLE πŸ¦‹

Here’s the whole main-character example, exactly as the page gives it:

#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 🌈


This iconic page is serving libmetalink 0.1.0, dated July 2012 β€” periodt, bestie. πŸ’…πŸ‘‘