1 /* <!-- copyright */ 2 /* 3 * libmetalink 4 * 5 * Copyright (c) 2008 Tatsuhiro Tsujikawa 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 /* copyright --> */ 26 #ifndef _D_METALINK_PARSER_H_ 27 #define _D_METALINK_PARSER_H_ 28 29 #include <stdio.h> 30 31 #include <metalink/metalink_types.h> 32 #include <metalink/metalink_error.h> 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /* 39 * Parses metalink XML file. 40 * @param filename path to Metalink XML file to be parsed. 41 * @param res a dynamically allocated metalink_t structure as a result of 42 * parsing. 43 * @return 0 for success, non-zero for error. See metalink_error.h for 44 * the meaning of error code. 45 */ 46 metalink_error_t metalink_parse_file(const char* filename, metalink_t** res); 47 48 /* 49 * Parses metalink XML file. 50 * @param docfp file stream for Metalink XML file to be parsed. 51 * @param res a dynamically allocated metalink_t structure as a result of 52 * parsing. 53 * @return 0 for success, non-zero for error. See metalink_error.h for 54 * the meaning of error code. 55 */ 56 metalink_error_t metalink_parse_fp(FILE *docfp, metalink_t** res); 57 58 /* 59 * Parses metalink XML from a file descriptor. 60 * @param docfd file descriptor of Metalink XML file to be parsed. 61 * @param res a dynamically allocated metalink_t structure as a result of 62 * parsing. 63 * @return 0 for success, non-zero for error. See metalink_error.h for 64 * the meaning of error code. 65 */ 66 metalink_error_t metalink_parse_fd(int docfd, metalink_t** res); 67 68 /* 69 * Parses metalink XML stored in buf and its length is len. 70 * @param buf a pointer to the XML data. 71 * @param len length of XML data in byte. 72 * @param res a dynamically allocated metalink_t structure as a result of 73 * parsing. 74 * @return 0 for success, non-zero for error. See metalink_error.h for 75 * the meaning of error code. 76 */ 77 metalink_error_t metalink_parse_memory(const char* buf, size_t len, metalink_t** res); 78 79 /** 80 * a parser context to keep current progress of XML parser. 81 */ 82 typedef struct _metalink_parser_context metalink_parser_context_t; 83 84 /* 85 * Allocates, initializes and returns a parser context. 86 * @return a parser context on success, otherwise NULL. 87 */ 88 metalink_parser_context_t* metalink_parser_context_new(void); 89 90 /** 91 * Deallocates a parser context ctx. 92 * @param ctx a parser context to deallocate. If ctx is NULL, this function does 93 * nothing. 94 */ 95 void metalink_parser_context_delete(metalink_parser_context_t* ctx); 96 97 /** 98 * Processes len bytes of data at buf. This function can be called several times 99 * to parse entire XML data. 100 * @param ctx a parser context. 101 * @param buf a pointer to the XML data. 102 * @param len length of XML data in bytes. 103 * @return 0 on success, non-zero for error. See metalink_error.h for the 104 * meaning of error code. 105 */ 106 metalink_error_t metalink_parse_update(metalink_parser_context_t* ctx, 107 const char* buf, size_t len); 108 109 /** 110 * Processes len bytes of data at buf and places metalink_t to res. 111 * Inside this function, ctx is cleaned up, so you don't need to call 112 * delete_metalink_parser_context. len can be 0. 113 * @param ctx a parser context. 114 * @param buf a pointer to the XML data. 115 * @param len length of XML data in bytes. 116 * @param res a dynamically allocated metalink_t structure as a result of 117 * parsing. 118 * @return 0 on success, non-zero for error. See metalink_error_h for 119 * the meaning of error code. 120 */ 121 metalink_error_t metalink_parse_final(metalink_parser_context_t* ctx, 122 const char* buf, size_t len, metalink_t** res); 123 124 #ifdef __cplusplus 125 } 126 #endif 127 128 #endif /* _D_METALINK_PARSER_H_ */ 129