| | 200 | |
| | 201 | === Emitter API Synopsis === |
| | 202 | |
| | 203 | {{{ |
| | 204 | #!c |
| | 205 | #include <yaml.h> |
| | 206 | |
| | 207 | yaml_emitter_t *emitter; |
| | 208 | yaml_event_t *event; |
| | 209 | |
| | 210 | /* Create the Emitter object. */ |
| | 211 | emitter = yaml_emitter_new(); |
| | 212 | |
| | 213 | /* Set a file output. */ |
| | 214 | FILE *output = fopen("...", "wb"); |
| | 215 | |
| | 216 | yaml_emitter_set_file_output(emitter, output); |
| | 217 | |
| | 218 | /* Set a generic writer. */ |
| | 219 | void *ext = ...; |
| | 220 | int write_handler(void *ext, char *buffer, int size) { |
| | 221 | /* |
| | 222 | ... |
| | 223 | Write `size` bytes. |
| | 224 | ... |
| | 225 | */ |
| | 226 | return error ? 0 : 1; |
| | 227 | } |
| | 228 | |
| | 229 | yaml_emitter_set_output(emitter, write_handler, ext); |
| | 230 | |
| | 231 | /* Create the STREAM-START event. */ |
| | 232 | event = yaml_event_new_stream_start(YAML_UTF8_ENCODING); |
| | 233 | |
| | 234 | /* Emit the event. The application is no longer responsible for the event object. */ |
| | 235 | int result = yaml_emitter_emit(emitter, event); |
| | 236 | |
| | 237 | if (!result) goto error; |
| | 238 | |
| | 239 | /* |
| | 240 | ... |
| | 241 | Emit more events. |
| | 242 | ... |
| | 243 | */ |
| | 244 | |
| | 245 | /* Create and emit the STREAM-END event. */ |
| | 246 | if (!yaml_emitter_emit(emitter, yaml_event_new_event_end())) goto error; |
| | 247 | |
| | 248 | /* Destroy the Emitter object. */ |
| | 249 | yaml_emitter_delete(emitter); |
| | 250 | |
| | 251 | /* On error. */ |
| | 252 | error: |
| | 253 | |
| | 254 | /* Error type: YAML_WRITER_ERROR, YAML_EMITTER_ERROR. */ |
| | 255 | yaml_error_t error; |
| | 256 | |
| | 257 | /* The problem description. */ |
| | 258 | char *problem; |
| | 259 | |
| | 260 | /* Get the error data. */ |
| | 261 | yaml_emitter_get_error(emitter, &error, &problem); |
| | 262 | |
| | 263 | /* |
| | 264 | ... |
| | 265 | Report the problem to the user. |
| | 266 | ... |
| | 267 | */ |
| | 268 | |
| | 269 | /* Destroy the Emitter object. */ |
| | 270 | yaml_emitter_delete(emitter); |
| | 271 | }}} |
| | 272 | |
| | 273 | == Feedback == |
| | 274 | |
| | 275 | ''Leave your comments here.'' |