29 #define KEY_STR_LEN 255
30 #define LVL2KEY_SIZE LVL2KEY_NUM*KEY_STR_LEN
34 struct fmsyamloutkeys {
35 char key1 [KEY_STR_LEN];
36 char key2 [KEY_STR_LEN];
37 char key3 [KEY_STR_LEN];
38 char key4 [KEY_STR_LEN];
39 char key5 [KEY_STR_LEN];
40 char key6 [KEY_STR_LEN];
41 char key7 [KEY_STR_LEN];
42 char key8 [KEY_STR_LEN];
43 char key9 [KEY_STR_LEN];
44 char key10 [KEY_STR_LEN];
45 char key11 [KEY_STR_LEN];
46 char key12 [KEY_STR_LEN];
47 char key13 [KEY_STR_LEN];
48 char key14 [KEY_STR_LEN];
49 char key15 [KEY_STR_LEN];
50 char key16 [KEY_STR_LEN];
52 char level2key [LVL2KEY_SIZE];
54 struct fmsyamloutvalues {
55 char val1 [KEY_STR_LEN];
56 char val2 [KEY_STR_LEN];
57 char val3 [KEY_STR_LEN];
58 char val4 [KEY_STR_LEN];
59 char val5 [KEY_STR_LEN];
60 char val6 [KEY_STR_LEN];
61 char val7 [KEY_STR_LEN];
62 char val8 [KEY_STR_LEN];
63 char val9 [KEY_STR_LEN];
64 char val10 [KEY_STR_LEN];
65 char val11 [KEY_STR_LEN];
66 char val12 [KEY_STR_LEN];
67 char val13 [KEY_STR_LEN];
68 char val14 [KEY_STR_LEN];
69 char val15 [KEY_STR_LEN];
70 char val16 [KEY_STR_LEN];
79 void error(
char * yamlname ,yaml_event_t * event, yaml_emitter_t * emitter, FILE * yamlout){
81 fprintf(
stderr,
"WARNING: YAML_OUTPUT: No output %s written. Failed to emit event %d: %s\n", yamlname, event->type, emitter->problem);
82 fprintf(
stdout,
"WARNING: YAML_OUTPUT: No output %s written. Failed to emit event %d: %s\n", yamlname, event->type, emitter->problem);
83 yaml_emitter_delete(emitter);
86 void keyerror(yaml_event_t * event, yaml_emitter_t * emitter){
88 fprintf(
stderr,
"WARNING: YAML_OUTPUT: Failed to emit event %d: %s\n", event->type, emitter->problem);
89 fprintf(
stdout,
"WARNING: YAML_OUTPUT: Failed to emit event %d: %s\n", event->type, emitter->problem);
98 void write_keys_vals_yaml (yaml_emitter_t * emitter, yaml_event_t * event ,
int aindex,
struct fmsyamloutkeys *keys,
struct fmsyamloutvalues *vals){
100 if (keys[aindex].key1[0] !=
'\0') {
102 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
103 (yaml_char_t *)keys[aindex].key1, strlen(keys[aindex].key1), 1, 0, YAML_PLAIN_SCALAR_STYLE);
105 if (!yaml_emitter_emit(emitter, event)){
106 keyerror(event, emitter);
110 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
111 (yaml_char_t *)vals[aindex].val1, strlen(vals[aindex].val1), 1, 0, YAML_PLAIN_SCALAR_STYLE);
113 if (!yaml_emitter_emit(emitter, event)){
114 keyerror(event, emitter);
119 if (keys[aindex].key2[0] !=
'\0') {
120 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
121 (yaml_char_t *)keys[aindex].key2, strlen(keys[aindex].key2), 1, 0, YAML_PLAIN_SCALAR_STYLE);
122 if (!yaml_emitter_emit(emitter, event)){
123 keyerror(event, emitter);
126 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
127 (yaml_char_t *)vals[aindex].val2, strlen(vals[aindex].val2), 1, 0, YAML_PLAIN_SCALAR_STYLE);
128 if (!yaml_emitter_emit(emitter, event)){
129 keyerror(event, emitter);
133 if (keys[aindex].key3[0] !=
'\0') {
134 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
135 (yaml_char_t *)keys[aindex].key3, strlen(keys[aindex].key3), 1, 0, YAML_PLAIN_SCALAR_STYLE);
136 if (!yaml_emitter_emit(emitter, event)){
137 keyerror(event, emitter);
140 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
141 (yaml_char_t *)vals[aindex].val3, strlen(vals[aindex].val3), 1, 0, YAML_PLAIN_SCALAR_STYLE);
142 if (!yaml_emitter_emit(emitter, event)){
143 keyerror(event, emitter);
147 if (keys[aindex].key4[0] !=
'\0') {
148 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
149 (yaml_char_t *)keys[aindex].key4, strlen(keys[aindex].key4), 1, 0, YAML_PLAIN_SCALAR_STYLE);
150 if (!yaml_emitter_emit(emitter, event)){
151 keyerror(event, emitter);
154 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
155 (yaml_char_t *)vals[aindex].val4, strlen(vals[aindex].val4), 1, 0, YAML_PLAIN_SCALAR_STYLE);
156 if (!yaml_emitter_emit(emitter, event)){
157 keyerror(event, emitter);
161 if (keys[aindex].key5[0] !=
'\0') {
162 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
163 (yaml_char_t *)keys[aindex].key5, strlen(keys[aindex].key5), 1, 0, YAML_PLAIN_SCALAR_STYLE);
164 if (!yaml_emitter_emit(emitter, event)){
165 keyerror(event, emitter);
168 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
169 (yaml_char_t *)vals[aindex].val5, strlen(vals[aindex].val5), 1, 0, YAML_PLAIN_SCALAR_STYLE);
170 if (!yaml_emitter_emit(emitter, event)){
171 keyerror(event, emitter);
175 if (keys[aindex].key6[0] !=
'\0') {
176 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
177 (yaml_char_t *)keys[aindex].key6, strlen(keys[aindex].key6), 1, 0, YAML_PLAIN_SCALAR_STYLE);
178 if (!yaml_emitter_emit(emitter, event)){
179 keyerror(event, emitter);
182 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
183 (yaml_char_t *)vals[aindex].val6, strlen(vals[aindex].val6), 1, 0, YAML_PLAIN_SCALAR_STYLE);
184 if (!yaml_emitter_emit(emitter, event)){
185 keyerror(event, emitter);
189 if (keys[aindex].key7[0] !=
'\0') {
190 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
191 (yaml_char_t *)keys[aindex].key7, strlen(keys[aindex].key7), 1, 0, YAML_PLAIN_SCALAR_STYLE);
192 if (!yaml_emitter_emit(emitter, event)){
193 keyerror(event, emitter);
196 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
197 (yaml_char_t *)vals[aindex].val7, strlen(vals[aindex].val7), 1, 0, YAML_PLAIN_SCALAR_STYLE);
198 if (!yaml_emitter_emit(emitter, event)){
199 keyerror(event, emitter);
203 if (keys[aindex].key8[0] !=
'\0') {
204 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
205 (yaml_char_t *)keys[aindex].key8, strlen(keys[aindex].key8), 1, 0, YAML_PLAIN_SCALAR_STYLE);
206 if (!yaml_emitter_emit(emitter, event)){
207 keyerror(event, emitter);
210 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
211 (yaml_char_t *)vals[aindex].val8, strlen(vals[aindex].val8), 1, 0, YAML_PLAIN_SCALAR_STYLE);
212 if (!yaml_emitter_emit(emitter, event)){
213 keyerror(event, emitter);
217 if (keys[aindex].key9[0] !=
'\0') {
218 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
219 (yaml_char_t *)keys[aindex].key9, strlen(keys[aindex].key9), 1, 0, YAML_PLAIN_SCALAR_STYLE);
220 if (!yaml_emitter_emit(emitter, event)){
221 keyerror(event, emitter);
224 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
225 (yaml_char_t *)vals[aindex].val9, strlen(vals[aindex].val9), 1, 0, YAML_PLAIN_SCALAR_STYLE);
226 if (!yaml_emitter_emit(emitter, event)){
227 keyerror(event, emitter);
231 if (keys[aindex].key10[0] !=
'\0') {
232 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
233 (yaml_char_t *)keys[aindex].key10, strlen(keys[aindex].key10), 1, 0, YAML_PLAIN_SCALAR_STYLE);
234 if (!yaml_emitter_emit(emitter, event)){
235 keyerror(event, emitter);
238 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
239 (yaml_char_t *)vals[aindex].val10, strlen(vals[aindex].val10), 1, 0, YAML_PLAIN_SCALAR_STYLE);
240 if (!yaml_emitter_emit(emitter, event)){
241 keyerror(event, emitter);
245 if (keys[aindex].key11[0] !=
'\0') {
246 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
247 (yaml_char_t *)keys[aindex].key11, strlen(keys[aindex].key11), 1, 0, YAML_PLAIN_SCALAR_STYLE);
248 if (!yaml_emitter_emit(emitter, event)){
249 keyerror(event, emitter);
252 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
253 (yaml_char_t *)vals[aindex].val11, strlen(vals[aindex].val11), 1, 0, YAML_PLAIN_SCALAR_STYLE);
254 if (!yaml_emitter_emit(emitter, event)){
255 keyerror(event, emitter);
259 if (keys[aindex].key12[0] !=
'\0') {
260 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
261 (yaml_char_t *)keys[aindex].key12, strlen(keys[aindex].key12), 1, 0, YAML_PLAIN_SCALAR_STYLE);
262 if (!yaml_emitter_emit(emitter, event)){
263 keyerror(event, emitter);
266 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
267 (yaml_char_t *)vals[aindex].val12, strlen(vals[aindex].val12), 1, 0, YAML_PLAIN_SCALAR_STYLE);
268 if (!yaml_emitter_emit(emitter, event)){
269 keyerror(event, emitter);
273 if (keys[aindex].key13[0] !=
'\0') {
274 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
275 (yaml_char_t *)keys[aindex].key13, strlen(keys[aindex].key13), 1, 0, YAML_PLAIN_SCALAR_STYLE);
276 if (!yaml_emitter_emit(emitter, event)){
277 keyerror(event, emitter);
280 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
281 (yaml_char_t *)vals[aindex].val13, strlen(vals[aindex].val13), 1, 0, YAML_PLAIN_SCALAR_STYLE);
282 if (!yaml_emitter_emit(emitter, event)){
283 keyerror(event, emitter);
287 if (keys[aindex].key14[0] !=
'\0') {
288 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
289 (yaml_char_t *)keys[aindex].key14, strlen(keys[aindex].key14), 1, 0, YAML_PLAIN_SCALAR_STYLE);
290 if (!yaml_emitter_emit(emitter, event)){
291 keyerror(event, emitter);
294 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
295 (yaml_char_t *)vals[aindex].val14, strlen(vals[aindex].val14), 1, 0, YAML_PLAIN_SCALAR_STYLE);
296 if (!yaml_emitter_emit(emitter, event)){
297 keyerror(event, emitter);
301 if (keys[aindex].key15[0] !=
'\0') {
302 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
303 (yaml_char_t *)keys[aindex].key15, strlen(keys[aindex].key15), 1, 0, YAML_PLAIN_SCALAR_STYLE);
304 if (!yaml_emitter_emit(emitter, event)){
305 keyerror(event, emitter);
308 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
309 (yaml_char_t *)vals[aindex].val15, strlen(vals[aindex].val15), 1, 0, YAML_PLAIN_SCALAR_STYLE);
310 if (!yaml_emitter_emit(emitter, event)){
311 keyerror(event, emitter);
315 if (keys[aindex].key16[0] !=
'\0') {
316 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
317 (yaml_char_t *)keys[aindex].key16, strlen(keys[aindex].key16), 1, 0, YAML_PLAIN_SCALAR_STYLE);
318 if (!yaml_emitter_emit(emitter, event)){
319 keyerror(event, emitter);
322 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
323 (yaml_char_t *)vals[aindex].val16, strlen(vals[aindex].val16), 1, 0, YAML_PLAIN_SCALAR_STYLE);
324 if (!yaml_emitter_emit(emitter, event)){
325 keyerror(event, emitter);
353 void write_yaml_from_struct_3 (
char *yamlname,
int asize,
struct fmsyamloutkeys *topkeys,
struct fmsyamloutvalues *topvals,
int a2size,
struct fmsyamloutkeys *l2keys,
354 struct fmsyamloutvalues *l2vals,
int a3size,
int * n3each,
struct fmsyamloutkeys *l3keys,
struct fmsyamloutvalues *l3vals,
356 yaml_emitter_t emitter;
364 int ws_ind = strlen(yamlname)-1;
365 while(*(yamlname+ws_ind) ==
' ') ws_ind--;
366 if( ws_ind != strlen(yamlname)-1) yamlname[ws_ind+1] =
'\0';
369 yamlout = fopen(yamlname,
"w");
371 yaml_emitter_initialize(&emitter);
372 yaml_emitter_set_output_file(&emitter, yamlout);
373 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
374 if (!yaml_emitter_emit(&emitter, &event)){
375 error(yamlname, &event, &emitter, yamlout);
379 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
380 if (!yaml_emitter_emit(&emitter, &event)){
381 error(yamlname, &event, &emitter, yamlout);
385 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
386 1, YAML_ANY_MAPPING_STYLE);
387 if (!yaml_emitter_emit(&emitter, &event)){
388 error(yamlname, &event, &emitter, yamlout);
393 write_keys_vals_yaml (&emitter, &event , 0, topkeys, topvals);
394 char* curr_topkey = topkeys->level2key;
398 for (top_ind=0; top_ind < topkeys->level2key_offset; top_ind++) {
400 yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
401 (yaml_char_t *)curr_topkey, strlen(curr_topkey), 1, 0,
402 YAML_PLAIN_SCALAR_STYLE);
403 if (!yaml_emitter_emit(&emitter, &event)){
404 error(yamlname, &event, &emitter, yamlout);
408 yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
409 1, YAML_ANY_SEQUENCE_STYLE);
410 if (!yaml_emitter_emit(&emitter, &event)){
411 error(yamlname, &event, &emitter, yamlout);
416 for (s2 = 0 ; s2 < lvl2keyeach[top_ind]; s2++){
417 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
418 1, YAML_ANY_MAPPING_STYLE);
419 if (!yaml_emitter_emit(&emitter, &event)){
420 error(yamlname, &event, &emitter, yamlout);
424 write_keys_vals_yaml (&emitter, &event , s2count, l2keys, l2vals);
427 char * curr_l2key = (&l2keys[s2count])->level2key;
429 for (l2_ind = 0; l2_ind < (&l2keys[s2count])->level2key_offset; l2_ind++) {
431 yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
432 (yaml_char_t *)curr_l2key, strlen(curr_l2key), 1, 0,
433 YAML_PLAIN_SCALAR_STYLE);
434 if (!yaml_emitter_emit(&emitter, &event)){
435 error(yamlname, &event, &emitter, yamlout);
439 yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
440 1, YAML_ANY_SEQUENCE_STYLE);
441 if (!yaml_emitter_emit(&emitter, &event)){
442 error(yamlname, &event, &emitter, yamlout);
446 int s3start = s3count;
447 int s3end = s3start + n3each[i_n3];
450 for (s3 = s3start ; s3 < s3end ; s3++){
451 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
452 1, YAML_ANY_MAPPING_STYLE);
453 if (!yaml_emitter_emit(&emitter, &event)){
454 error(yamlname, &event, &emitter, yamlout);
458 write_keys_vals_yaml (&emitter, &event , s3, l3keys, l3vals);
459 yaml_mapping_end_event_initialize(&event);
460 if(!yaml_emitter_emit(&emitter, &event)){
461 error(yamlname, &event, &emitter, yamlout);
466 yaml_sequence_end_event_initialize(&event);
467 if (!yaml_emitter_emit(&emitter, &event)){
468 error(yamlname, &event, &emitter, yamlout);
471 curr_l2key = (&l2keys[s2count])->level2key + ((l2_ind+1) * KEY_STR_LEN);
473 yaml_mapping_end_event_initialize(&event);
474 if (!yaml_emitter_emit(&emitter, &event)){
475 error(yamlname, &event, &emitter, yamlout);
482 yaml_sequence_end_event_initialize(&event);
483 if (!yaml_emitter_emit(&emitter, &event)){
484 error(yamlname, &event, &emitter, yamlout);
487 curr_topkey = topkeys->level2key + ((top_ind+1) * KEY_STR_LEN);
491 yaml_mapping_end_event_initialize(&event);
492 if (!yaml_emitter_emit(&emitter, &event)){
493 error(yamlname, &event, &emitter, yamlout);
497 yaml_document_end_event_initialize(&event, 0);
498 if (!yaml_emitter_emit(&emitter, &event)){
499 error(yamlname, &event, &emitter, yamlout);
502 yaml_stream_end_event_initialize(&event);
503 if (!yaml_emitter_emit(&emitter, &event)){
504 error(yamlname, &event, &emitter, yamlout);
507 yaml_emitter_delete(&emitter);
516 void add_level2key(
int key_length,
char* key_name,
struct fmsyamloutkeys* keys){
519 char kname_loc[key_length + 1];
521 strncpy(kname_loc, key_name, key_length);
522 kname_loc[key_length] =
'\0';
524 if ( strlen(kname_loc) > KEY_STR_LEN){
525 fprintf(
stderr,
"WARNING: YAML_OUTPUT: invalid level two key passed to add_level2key. Max string size is %d, passed in string: %s", KEY_STR_LEN, key_name);
526 fprintf(
stdout,
"WARNING: YAML_OUTPUT: invalid level two key passed to add_level2key. Max string size is %d, passed in string: %s", KEY_STR_LEN, key_name);
528 if( keys->level2key_offset >= LVL2KEY_NUM ){
529 fprintf(
stderr,
"WARNING: YAML_OUTPUT: max amount of level 2 keys (%d) has been exceeded", LVL2KEY_NUM);
530 fprintf(
stdout,
"WARNING: YAML_OUTPUT: max amount of level 2 keys (%d) has been exceeded", LVL2KEY_NUM);
533 if ( keys->level2key[0] ==
'\0'){
534 keys->level2key_offset = 0;
537 int offset = keys->level2key_offset * KEY_STR_LEN;
539 curr_key = keys->level2key + offset;
540 strcpy(curr_key, kname_loc);
542 keys->level2key_offset++;
545 printf(
"key length: %d \n key_name:", key_length);
548 printf(
"offset: %d \n", offset);
549 printf(
"kname_loc:");
552 printf(keys->level2key);
553 printf(
"\nl2key+offset:");
554 printf(keys->level2key + offset);
integer function stdout()
This function returns the current standard fortran unit numbers for output.
integer function stderr()
This function returns the current standard fortran unit numbers for error messages.