8 bool get_line(FILE * input, char *line);
9 char *get_anchor(char sigil, char *line, char *anchor);
10 char *get_tag(char *line, char *tag);
11 void get_value(char *line, char *value, int *style);
13 int main(int argc, char *argv[])
16 yaml_emitter_t emitter;
26 input = fopen(argv[1], "rb");
28 fprintf(stderr, "Usage: libyaml-emitter [<input-file>]\n");
33 if (!yaml_emitter_initialize(&emitter)) {
34 fprintf(stderr, "Could not initalize the emitter object\n");
37 yaml_emitter_set_output_file(&emitter, stdout);
38 yaml_emitter_set_canonical(&emitter, canonical);
39 yaml_emitter_set_unicode(&emitter, unicode);
41 while (get_line(input, line)) {
47 if (strncmp(line, "+STR", 4) == 0) {
48 ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
50 else if (strncmp(line, "-STR", 4) == 0) {
51 ok = yaml_stream_end_event_initialize(&event);
53 else if (strncmp(line, "+DOC", 4) == 0) {
54 implicit = strncmp(line, "+DOC ---", 8) != 0;
55 ok = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, implicit);
57 else if (strncmp(line, "-DOC", 4) == 0) {
58 implicit = strncmp(line, "-DOC ...", 8) != 0;
59 ok = yaml_document_end_event_initialize(&event, implicit);
61 else if (strncmp(line, "+MAP", 4) == 0) {
62 ok = yaml_mapping_start_event_initialize(&event, (yaml_char_t *)
63 get_anchor('&', line, anchor), (yaml_char_t *)
64 get_tag(line, tag), 0, YAML_BLOCK_MAPPING_STYLE);
66 else if (strncmp(line, "-MAP", 4) == 0) {
67 ok = yaml_mapping_end_event_initialize(&event);
69 else if (strncmp(line, "+SEQ", 4) == 0) {
70 ok = yaml_sequence_start_event_initialize(&event, (yaml_char_t *)
71 get_anchor('&', line, anchor), (yaml_char_t *)
72 get_tag(line, tag), 0, YAML_BLOCK_SEQUENCE_STYLE);
74 else if (strncmp(line, "-SEQ", 4) == 0) {
75 ok = yaml_sequence_end_event_initialize(&event);
77 else if (strncmp(line, "=VAL", 4) == 0) {
81 get_value(line, value, &style);
82 implicit = (get_tag(line, tag) == NULL);
84 ok = yaml_scalar_event_initialize(&event, (yaml_char_t *)
85 get_anchor('&', line, anchor), (yaml_char_t *) get_tag(line, tag), (yaml_char_t *) value, -1, implicit, implicit, style);
87 else if (strncmp(line, "=ALI", 4) == 0) {
88 ok = yaml_alias_event_initialize(&event, (yaml_char_t *)
89 get_anchor('*', line, anchor)
93 fprintf(stderr, "Unknown event: '%s'\n", line);
100 if (!yaml_emitter_emit(&emitter, &event))
104 assert(!fclose(input));
105 yaml_emitter_delete(&emitter);
111 switch (emitter.error) {
112 case YAML_MEMORY_ERROR:
113 fprintf(stderr, "Memory error: Not enough memory for emitting\n");
115 case YAML_WRITER_ERROR:
116 fprintf(stderr, "Writer error: %s\n", emitter.problem);
118 case YAML_EMITTER_ERROR:
119 fprintf(stderr, "Emitter error: %s\n", emitter.problem);
125 fprintf(stderr, "Internal error\n");
128 yaml_emitter_delete(&emitter);
132 fprintf(stderr, "Memory error: Not enough memory for creating an event\n");
133 yaml_emitter_delete(&emitter);
137 bool get_line(FILE * input, char *line)
141 if (!fgets(line, 1024 - 1, input))
144 if ((newline = strchr(line, '\n')) == NULL) {
145 fprintf(stderr, "Line too long: '%s'", line);
153 char *get_anchor(char sigil, char *line, char *anchor)
157 if ((start = strchr(line, sigil)) == NULL)
160 if ((end = strchr(start, ' ')) == NULL)
161 end = line + strlen(line);
162 memcpy(anchor, start, end - start);
163 anchor[end - start] = '\0';
167 char *get_tag(char *line, char *tag)
171 if ((start = strchr(line, '<')) == NULL)
173 if ((end = strchr(line, '>')) == NULL)
175 memcpy(tag, start + 1, end - start - 1);
176 tag[end - start - 1] = '\0';
180 void get_value(char *line, char *value, int *style)
185 char *end = line + strlen(line);
187 for (c = line + 4; c < end; c++) {
191 *style = YAML_PLAIN_SCALAR_STYLE;
192 else if (*start == '\'')
193 *style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
194 else if (*start == '"')
195 *style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
196 else if (*start == '|')
197 *style = YAML_LITERAL_SCALAR_STYLE;
198 else if (*start == '>')
199 *style = YAML_FOLDED_SCALAR_STYLE;
211 for (c = start; c < end; c++) {