1METALINK_PARSE_UPDATE(3)      libmetalink Manual      METALINK_PARSE_UPDATE(3)
2
3
4

NAME

6       metalink_parse_update,    metalink_parse_final,    metalink_parser_con‐
7       text_new, metalink_parser_context_delete - Parse Metalink file and cre‐
8       ate metalink_t object.
9
10

SYNOPSIS

12       #include <metalink/metalink.h>
13
14       metalink_error_t  metalink_parse_update(metalink_parser_context_t *ctx,
15                                               const char *buf, size_t len);
16       metalink_error_t  metalink_parse_final(metalink_parser_context_t  *ctx,
17                                               const  char  *buf,  size_t len,
18                                               metalink_t **res);
19
20       metalink_parser_context_t* metalink_parser_context_new();
21       void metalink_parser_context_delete(metalink_parser_context_t *ctx);
22
23

DESCRIPTION

25       These functions provide a  push  interface  for  parsing  Metalink  XML
26       files.
27
28       Before calling metalink_parse_update() and metalink_parse_final(), met‐
29       alink_parse_context_t  has  to  be  created   by   metalink_parser_con‐
30       text_new().
31
32       In  each  call  of  metalink_parse_update(),  len bytes of buf are pro‐
33       cessed.  At the last piece of data, call metalink_parse_final() to  get
34       metalink_t(3) structure as a result.  Giving 0 as len is permitted.
35
36       metalink_parse_final()  calls  metalink_parser_context_delete()  inter‐
37       nally to deallocate the memory  for  passed  metalink_parser_context_t.
38       Therefore  you  don't  have to call metlaink_parser_context_delete() if
39       you call metalink_parse_final().  Otherwise  call  metalink_parser_con‐
40       text_delete() to free the allocated resource.
41
42       You  don't  have  to allocate memory for metalink_t(3) structure.  met‐
43       alink_parse_final() takes the  pointer  of  metalink_t(3)  pointer  and
44       allocates memory for that pointer.
45
46       The  caller  must free the memory allocated for metalink_t(3) structure
47       using metalink_delete(3) if it is no longer used.
48
49

RETURN VALUE

51       metalink_parse_update(), metalink_parse_final() return 0  for  success.
52       When  error  occurred, non-zero value error code is returned.  If error
53       occurred, metalink_parse_final() does  not  allocate  memory  for  met‐
54       alink_t. The error codes are described in metalink_error.h.
55
56       In  case of success, metalink_parser_context_new() allocates memory for
57       metalink_parser_context_t() and returns the pointer to it.  In case  of
58       failure, metalink_parser_context_new() returns NULL.
59
60       metalink_parser_context_delete() returns no value.
61
62

EXAMPLE

64       #include <stdio.h>
65       #include <stdlib.h>
66       #include <sys/types.h>
67       #include <sys/stat.h>
68       #include <fcntl.h>
69       #include <errno.h>
70       #include <string.h>
71       #include <unistd.h>
72       #include <metalink/metalink.h>
73
74       int main(int argc, char** argv)
75       {
76         metalink_error_t r;
77         metalink_t* metalink;
78         metalink_file_t* file;
79         metalink_checksum_t** checksums;
80         metalink_parser_context_t* context;
81         int fd;
82         char buf[BUFSIZ];
83         ssize_t length;
84
85         context = metalink_parser_context_new();
86
87         if(context == NULL) {
88           fprintf(stderr,
89                   "ERROR: failed to create metalink_parser_context_t\n");
90           exit(EXIT_FAILURE);
91         }
92
93         fd = open("sample.metalink", O_RDONLY);
94         if(fd == -1) {
95           fprintf(stderr, "ERROR: open():%s\n", strerror(errno));
96           exit(EXIT_FAILURE);
97         }
98         while((length = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)))) != 0){
99           r = metalink_parse_update(context, buf, length);
100           if(r != 0) {
101             fprintf(stderr, "ERROR: code=%d\n", r);
102             metalink_parser_context_delete(context);
103             exit(EXIT_FAILURE);
104           }
105         }
106         if(length == -1) {
107           fprintf(stderr, "ERROR: read():%s\n", strerror(errno));
108           metalink_parser_context_delete(context);
109           exit(EXIT_FAILURE);
110         }
111
112         r = metalink_parse_final(context, NULL, 0, &metalink);
113
114         if(r != 0) {
115           fprintf(stderr, "ERROR: code=%d\n", r);
116           exit(EXIT_FAILURE);
117         }
118
119         TEMP_FAILURE_RETRY(close(fd));
120
121         file = metalink->files[0];
122         printf("name: %s\n", file->name);
123         printf("size: %lld\n", file->size);
124         printf("os  : %s\n", file->os);
125
126         if(file->checksums) {
127           checksums = file->checksums;
128           while(*checksums) {
129             printf("hash: %s %s\n", (*checksums)->type, (*checksums)->hash);
130             ++checksums;
131           }
132         }
133         if(file->chunk_checksum) {
134           size_t count = 0;
135           metalink_piece_hash_t** piece_hashes;
136           printf("chunk checksum: size=%d, type=%s\n",
137                  file->chunk_checksum->length,
138                  file->chunk_checksum->type);
139           printf("first 5 piece hashes...\n");
140           piece_hashes = file->chunk_checksum->piece_hashes;
141           while(*piece_hashes && count < 5) {
142             printf("piece=%d, hash=%s\n", (*piece_hashes)->piece,
143                                            (*piece_hashes)->hash);
144             ++piece_hashes;
145             ++count;
146           }
147           printf("...\n");
148         }
149         if(file->resources) {
150           size_t count = 0;
151           metalink_resource_t** resources;
152           printf("first 5 resources...\n");
153           resources = file->resources;
154           while(*resources && count < 5) {
155             printf("type=%s, location=%s, preference=%d, url=%s\n",
156                    (*resources)->type, (*resources)->location,
157                    (*resources)->preference, (*resources)->url);
158             ++resources;
159             ++count;
160           }
161           printf("...\n");
162         }
163
164         /* delete metalink_t */
165         metalink_delete(metalink);
166
167         return EXIT_SUCCESS;
168       }
169
170

SEE ALSO

172       metalink_delete(3), metalink_parse_file(3), metalink_t(3)
173
174
175
176libmetalink 0.1.0                  July 2012          METALINK_PARSE_UPDATE(3)
Impressum