1METALINK_PARSE_FILE(3) libmetalink Manual METALINK_PARSE_FILE(3)
2
3
4
6 metalink_parse_file, metalink_parse_fp, metalink_parse_fd, met‐
7 alink_parse_memory - Parse Metalink file and create metalink_t object.
8
10 #include <metalink/metalink_parser.h>
11
12 metalink_error_t metalink_parse_file(const char *filename, metalink_t
13 **res);
14 metalink_error_t metalink_parse_fp(FILE *docfp, metalink_t **res);
15 metalink_error_t metalink_parse_fd(int docfd, metalink_t **res);
16 metalink_error_t metalink_parse_memory(const char *buf, size_t len,
17 metalink_t **res);
18
19
21 These functions parse Metalink file data and constructs metalink_t
22 structure. You don't have to allocate memory for metalink_t structure.
23 They take the pointer of metalink_t pointer and allocate memory for
24 that pointer.
25
26 metalink_parse_file() parses Metalink file denoted by filename and con‐
27 structs metalink_t structure.
28
29 metalink_parse_fp() reads data from file stream docfp and construtcts
30 metalink_t structure.
31
32 metalink_parse_fd() reads data from file descriptor docfd and con‐
33 structs metalink_t structure.
34
35 metalink_parse_memory() parses len bytes of buf and constructs met‐
36 alink_t structure.
37
38 The caller must free the memory allocated for metalink_t structure
39 using metalink_delete(3) if it is no longer used.
40
41
43 All functions return 0 for success. When error occurred, non-zero value
44 error code is returned and metalink_t structure is not allocated. The
45 error codes are described in metalink_error.h.
46
47
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <metalink/metalink_parser.h>
52
53 int main(int argc, char** argv)
54 {
55 metalink_error_t r;
56 metalink_t* metalink;
57 metalink_file_t* file;
58 metalink_checksum_t** checksums;
59
60 r = metalink_parse_file("sample.metalink", &metalink);
61
62 if(r != 0) {
63 fprintf(stderr, "ERROR: code=%d\n", r);
64 exit(EXIT_FAILURE);
65 }
66
67 file = metalink->files[0];
68 printf("name: %s\n", file->name);
69 printf("size: %lld\n", file->size);
70 printf("os : %s\n", file->os);
71
72 if(file->checksums) {
73 checksums = file->checksums;
74 while(*checksums) {
75 printf("hash: %s %s\n", (*checksums)->type, (*checksums)->hash);
76 ++checksums;
77 }
78 }
79 if(file->chunk_checksum) {
80 size_t count = 0;
81 metalink_piece_hash_t** piece_hashes;
82 printf("chunk checksum: size=%d, type=%s\n",
83 file->chunk_checksum->length,
84 file->chunk_checksum->type);
85 printf("first 5 piece hashes...\n");
86 piece_hashes = file->chunk_checksum->piece_hashes;
87 while(*piece_hashes && count < 5) {
88 printf("piece=%d, hash=%s\n", (*piece_hashes)->piece,
89 (*piece_hashes)->hash);
90 ++piece_hashes;
91 ++count;
92 }
93 printf("...\n");
94 }
95 if(file->resources) {
96 size_t count = 0;
97 metalink_resource_t** resources;
98 printf("first 5 resources...\n");
99 resources = file->resources;
100 while(*resources && count < 5) {
101 printf("type=%s, location=%s, preference=%d, url=%s\n",
102 (*resources)->type, (*resources)->location,
103 (*resources)->preference, (*resources)->url);
104 ++resources;
105 ++count;
106 }
107 printf("...\n");
108 }
109
110 /* delete metalink_t */
111 metalink_delete(metalink);
112
113 return EXIT_SUCCESS;
114 }
115
116
118 metalink_delete(3), metalink_parse_update(3), metalink_t(3)
119
120
121
122libmetalink 0.0.3 10/21/2008 METALINK_PARSE_FILE(3)