7 int get_line(FILE * input, char *line);
8 char *get_anchor(char sigil, char *line, char *anchor);
9 char *get_tag(char *line, char *tag);
10 void get_value(char *line, char *value, int *style);
12 int main(int argc, char *argv[])
15 yaml_emitter_t emitter;
25 input = fopen(argv[1], "rb");
27 fprintf(stderr, "Usage: libyaml-emitter [<input-file>]\n");
32 if (!yaml_emitter_initialize(&emitter)) {
33 fprintf(stderr, "Could not initalize the emitter object\n");
36 yaml_emitter_set_output_file(&emitter, stdout);
37 yaml_emitter_set_canonical(&emitter, canonical);
38 yaml_emitter_set_unicode(&emitter, unicode);
40 while (get_line(input, line)) {
46 if (strncmp(line, "+STR", 4) == 0) {
47 ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
49 else if (strncmp(line, "-STR", 4) == 0) {
50 ok = yaml_stream_end_event_initialize(&event);
52 else if (strncmp(line, "+DOC", 4) == 0) {
53 implicit = strncmp(line, "+DOC ---", 8) != 0;
54 ok = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, implicit);
56 else if (strncmp(line, "-DOC", 4) == 0) {
57 implicit = strncmp(line, "-DOC ...", 8) != 0;
58 ok = yaml_document_end_event_initialize(&event, implicit);
60 else if (strncmp(line, "+MAP", 4) == 0) {
61 ok = yaml_mapping_start_event_initialize(&event, (yaml_char_t *)
62 get_anchor('&', line, anchor), (yaml_char_t *)
63 get_tag(line, tag), 0, YAML_BLOCK_MAPPING_STYLE);
65 else if (strncmp(line, "-MAP", 4) == 0) {
66 ok = yaml_mapping_end_event_initialize(&event);
68 else if (strncmp(line, "+SEQ", 4) == 0) {
69 ok = yaml_sequence_start_event_initialize(&event, (yaml_char_t *)
70 get_anchor('&', line, anchor), (yaml_char_t *)
71 get_tag(line, tag), 0, YAML_BLOCK_SEQUENCE_STYLE);
73 else if (strncmp(line, "-SEQ", 4) == 0) {
74 ok = yaml_sequence_end_event_initialize(&event);
76 else if (strncmp(line, "=VAL", 4) == 0) {
80 get_value(line, value, &style);
81 implicit = (get_tag(line, tag) == NULL);
83 ok = yaml_scalar_event_initialize(&event, (yaml_char_t *)
84 get_anchor('&', line, anchor), (yaml_char_t *) get_tag(line, tag), (yaml_char_t *) value, -1, implicit, implicit, style);
86 else if (strncmp(line, "=ALI", 4) == 0) {
87 ok = yaml_alias_event_initialize(&event, (yaml_char_t *)
88 get_anchor('*', line, anchor)
92 fprintf(stderr, "Unknown event: '%s'\n", line);
99 if (!yaml_emitter_emit(&emitter, &event))
103 assert(!fclose(input));
104 yaml_emitter_delete(&emitter);
110 switch (emitter.error) {
111 case YAML_MEMORY_ERROR:
112 fprintf(stderr, "Memory error: Not enough memory for emitting\n");
114 case YAML_WRITER_ERROR:
115 fprintf(stderr, "Writer error: %s\n", emitter.problem);
117 case YAML_EMITTER_ERROR:
118 fprintf(stderr, "Emitter error: %s\n", emitter.problem);
124 fprintf(stderr, "Internal error\n");
127 yaml_emitter_delete(&emitter);
131 fprintf(stderr, "Memory error: Not enough memory for creating an event\n");
132 yaml_emitter_delete(&emitter);
136 int get_line(FILE * input, char *line)
140 if (!fgets(line, 1024 - 1, input))
143 if ((newline = strchr(line, '\n')) == NULL) {
144 fprintf(stderr, "Line too long: '%s'", line);
152 char *get_anchor(char sigil, char *line, char *anchor)
156 if ((start = strchr(line, sigil)) == NULL)
159 if ((end = strchr(start, ' ')) == NULL)
160 end = line + strlen(line);
161 memcpy(anchor, start, end - start);
162 anchor[end - start] = '\0';
166 char *get_tag(char *line, char *tag)
170 if ((start = strchr(line, '<')) == NULL)
172 if ((end = strchr(line, '>')) == NULL)
174 memcpy(tag, start + 1, end - start - 1);
175 tag[end - start - 1] = '\0';
179 void get_value(char *line, char *value, int *style)
184 char *end = line + strlen(line);
186 for (c = line + 4; c < end; c++) {
190 *style = YAML_PLAIN_SCALAR_STYLE;
191 else if (*start == '\'')
192 *style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
193 else if (*start == '"')
194 *style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
195 else if (*start == '|')
196 *style = YAML_LITERAL_SCALAR_STYLE;
197 else if (*start == '>')
198 *style = YAML_FOLDED_SCALAR_STYLE;
210 for (c = start; c < end; c++) {