1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include <yaml.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
bool get_line(FILE * input, char *line);
char *get_anchor(char sigil, char *line, char *anchor);
char *get_tag(char *line, char *tag);
void get_value(char *line, char *value, int *style);
int main(int argc, char *argv[])
{
FILE *input;
yaml_emitter_t emitter;
yaml_event_t event;
int canonical = 0;
int unicode = 0;
char line[1024];
if (argc == 1)
input = stdin;
else if (argc == 2)
input = fopen(argv[1], "rb");
else {
fprintf(stderr, "Usage: libyaml-emitter [<input-file>]\n");
return 1;
}
assert(input);
if (!yaml_emitter_initialize(&emitter)) {
fprintf(stderr, "Could not initalize the emitter object\n");
return 1;
}
yaml_emitter_set_output_file(&emitter, stdout);
yaml_emitter_set_canonical(&emitter, canonical);
yaml_emitter_set_unicode(&emitter, unicode);
while (get_line(input, line)) {
int ok;
char anchor[256];
char tag[256];
int implicit;
if (strncmp(line, "+STR", 4) == 0) {
ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
}
else if (strncmp(line, "-STR", 4) == 0) {
ok = yaml_stream_end_event_initialize(&event);
}
else if (strncmp(line, "+DOC", 4) == 0) {
implicit = strncmp(line, "+DOC ---", 8) != 0;
ok = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, implicit);
}
else if (strncmp(line, "-DOC", 4) == 0) {
implicit = strncmp(line, "-DOC ...", 8) != 0;
ok = yaml_document_end_event_initialize(&event, implicit);
}
else if (strncmp(line, "+MAP", 4) == 0) {
ok = yaml_mapping_start_event_initialize(&event, (yaml_char_t *)
get_anchor('&', line, anchor), (yaml_char_t *)
get_tag(line, tag), 0, YAML_BLOCK_MAPPING_STYLE);
}
else if (strncmp(line, "-MAP", 4) == 0) {
ok = yaml_mapping_end_event_initialize(&event);
}
else if (strncmp(line, "+SEQ", 4) == 0) {
ok = yaml_sequence_start_event_initialize(&event, (yaml_char_t *)
get_anchor('&', line, anchor), (yaml_char_t *)
get_tag(line, tag), 0, YAML_BLOCK_SEQUENCE_STYLE);
}
else if (strncmp(line, "-SEQ", 4) == 0) {
ok = yaml_sequence_end_event_initialize(&event);
}
else if (strncmp(line, "=VAL", 4) == 0) {
char value[1024];
int style;
get_value(line, value, &style);
implicit = (get_tag(line, tag) == NULL);
ok = yaml_scalar_event_initialize(&event, (yaml_char_t *)
get_anchor('&', line, anchor), (yaml_char_t *) get_tag(line, tag), (yaml_char_t *) value, -1, implicit, implicit, style);
}
else if (strncmp(line, "=ALI", 4) == 0) {
ok = yaml_alias_event_initialize(&event, (yaml_char_t *)
get_anchor('*', line, anchor)
);
}
else {
fprintf(stderr, "Unknown event: '%s'\n", line);
fflush(stdout);
return 1;
}
if (!ok)
goto event_error;
if (!yaml_emitter_emit(&emitter, &event))
goto emitter_error;
}
assert(!fclose(input));
yaml_emitter_delete(&emitter);
fflush(stdout);
return 0;
emitter_error:
switch (emitter.error) {
case YAML_MEMORY_ERROR:
fprintf(stderr, "Memory error: Not enough memory for emitting\n");
break;
case YAML_WRITER_ERROR:
fprintf(stderr, "Writer error: %s\n", emitter.problem);
break;
case YAML_EMITTER_ERROR:
fprintf(stderr, "Emitter error: %s\n", emitter.problem);
break;
default:
/*
* Couldn't happen.
*/
fprintf(stderr, "Internal error\n");
break;
}
yaml_emitter_delete(&emitter);
return 1;
event_error:
fprintf(stderr, "Memory error: Not enough memory for creating an event\n");
yaml_emitter_delete(&emitter);
return 1;
}
bool get_line(FILE * input, char *line)
{
char *newline;
if (!fgets(line, 1024 - 1, input))
return false;
if ((newline = strchr(line, '\n')) == NULL) {
fprintf(stderr, "Line too long: '%s'", line);
abort();
}
*newline = '\0';
return true;
}
char *get_anchor(char sigil, char *line, char *anchor)
{
char *start;
char *end;
if ((start = strchr(line, sigil)) == NULL)
return NULL;
start++;
if ((end = strchr(start, ' ')) == NULL)
end = line + strlen(line);
memcpy(anchor, start, end - start);
anchor[end - start] = '\0';
return anchor;
}
char *get_tag(char *line, char *tag)
{
char *start;
char *end;
if ((start = strchr(line, '<')) == NULL)
return NULL;
if ((end = strchr(line, '>')) == NULL)
return NULL;
memcpy(tag, start + 1, end - start - 1);
tag[end - start - 1] = '\0';
return tag;
}
void get_value(char *line, char *value, int *style)
{
int i = 0;
char *c;
char *start = NULL;
char *end = line + strlen(line);
for (c = line + 4; c < end; c++) {
if (*c == ' ') {
start = c + 1;
if (*start == ':')
*style = YAML_PLAIN_SCALAR_STYLE;
else if (*start == '\'')
*style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
else if (*start == '"')
*style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
else if (*start == '|')
*style = YAML_LITERAL_SCALAR_STYLE;
else if (*start == '>')
*style = YAML_FOLDED_SCALAR_STYLE;
else {
start = NULL;
continue;
}
start++;
break;
}
}
if (!start)
abort();
for (c = start; c < end; c++) {
if (*c == '\\') {
if (*++c == '\\')
value[i++] = '\\';
else if (*c == '0')
value[i++] = '\0';
else if (*c == 'b')
value[i++] = '\b';
else if (*c == 'n')
value[i++] = '\n';
else if (*c == 'r')
value[i++] = '\r';
else if (*c == 't')
value[i++] = '\t';
else
abort();
}
else
value[i++] = *c;
}
value[i] = '\0';
}
|