30 #define KEY_STR_LEN 255
31 #define LVL2KEY_SIZE LVL2KEY_NUM*KEY_STR_LEN
35 struct fmsyamloutkeys {
36 char key1 [KEY_STR_LEN];
37 char key2 [KEY_STR_LEN];
38 char key3 [KEY_STR_LEN];
39 char key4 [KEY_STR_LEN];
40 char key5 [KEY_STR_LEN];
41 char key6 [KEY_STR_LEN];
42 char key7 [KEY_STR_LEN];
43 char key8 [KEY_STR_LEN];
44 char key9 [KEY_STR_LEN];
45 char key10 [KEY_STR_LEN];
46 char key11 [KEY_STR_LEN];
47 char key12 [KEY_STR_LEN];
48 char key13 [KEY_STR_LEN];
49 char key14 [KEY_STR_LEN];
50 char key15 [KEY_STR_LEN];
51 char key16 [KEY_STR_LEN];
53 char level2key [LVL2KEY_SIZE];
55 struct fmsyamloutvalues {
56 char val1 [KEY_STR_LEN];
57 char val2 [KEY_STR_LEN];
58 char val3 [KEY_STR_LEN];
59 char val4 [KEY_STR_LEN];
60 char val5 [KEY_STR_LEN];
61 char val6 [KEY_STR_LEN];
62 char val7 [KEY_STR_LEN];
63 char val8 [KEY_STR_LEN];
64 char val9 [KEY_STR_LEN];
65 char val10 [KEY_STR_LEN];
66 char val11 [KEY_STR_LEN];
67 char val12 [KEY_STR_LEN];
68 char val13 [KEY_STR_LEN];
69 char val14 [KEY_STR_LEN];
70 char val15 [KEY_STR_LEN];
71 char val16 [KEY_STR_LEN];
80 void error(
char * yamlname ,yaml_event_t * event, yaml_emitter_t * emitter, FILE * yamlout){
82 fprintf(
stderr,
"WARNING: YAML_OUTPUT: No output %s written. Failed to emit event %d: %s\n", yamlname, event->type, emitter->problem);
83 fprintf(
stdout,
"WARNING: YAML_OUTPUT: No output %s written. Failed to emit event %d: %s\n", yamlname, event->type, emitter->problem);
84 yaml_emitter_delete(emitter);
87 void keyerror(yaml_event_t * event, yaml_emitter_t * emitter){
89 fprintf(
stderr,
"WARNING: YAML_OUTPUT: Failed to emit event %d: %s\n", event->type, emitter->problem);
90 fprintf(
stdout,
"WARNING: YAML_OUTPUT: Failed to emit event %d: %s\n", event->type, emitter->problem);
99 void write_keys_vals_yaml (yaml_emitter_t * emitter, yaml_event_t * event ,
int aindex,
struct fmsyamloutkeys *keys,
struct fmsyamloutvalues *vals){
101 if (keys[aindex].key1[0] !=
'\0') {
103 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
104 (yaml_char_t *)keys[aindex].key1, strlen(keys[aindex].key1), 1, 0, YAML_PLAIN_SCALAR_STYLE);
106 if (!yaml_emitter_emit(emitter, event)){
107 keyerror(event, emitter);
111 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
112 (yaml_char_t *)vals[aindex].val1, strlen(vals[aindex].val1), 1, 0, YAML_PLAIN_SCALAR_STYLE);
114 if (!yaml_emitter_emit(emitter, event)){
115 keyerror(event, emitter);
120 if (keys[aindex].key2[0] !=
'\0') {
121 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
122 (yaml_char_t *)keys[aindex].key2, strlen(keys[aindex].key2), 1, 0, YAML_PLAIN_SCALAR_STYLE);
123 if (!yaml_emitter_emit(emitter, event)){
124 keyerror(event, emitter);
127 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
128 (yaml_char_t *)vals[aindex].val2, strlen(vals[aindex].val2), 1, 0, YAML_PLAIN_SCALAR_STYLE);
129 if (!yaml_emitter_emit(emitter, event)){
130 keyerror(event, emitter);
134 if (keys[aindex].key3[0] !=
'\0') {
135 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
136 (yaml_char_t *)keys[aindex].key3, strlen(keys[aindex].key3), 1, 0, YAML_PLAIN_SCALAR_STYLE);
137 if (!yaml_emitter_emit(emitter, event)){
138 keyerror(event, emitter);
141 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
142 (yaml_char_t *)vals[aindex].val3, strlen(vals[aindex].val3), 1, 0, YAML_PLAIN_SCALAR_STYLE);
143 if (!yaml_emitter_emit(emitter, event)){
144 keyerror(event, emitter);
148 if (keys[aindex].key4[0] !=
'\0') {
149 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
150 (yaml_char_t *)keys[aindex].key4, strlen(keys[aindex].key4), 1, 0, YAML_PLAIN_SCALAR_STYLE);
151 if (!yaml_emitter_emit(emitter, event)){
152 keyerror(event, emitter);
155 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
156 (yaml_char_t *)vals[aindex].val4, strlen(vals[aindex].val4), 1, 0, YAML_PLAIN_SCALAR_STYLE);
157 if (!yaml_emitter_emit(emitter, event)){
158 keyerror(event, emitter);
162 if (keys[aindex].key5[0] !=
'\0') {
163 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
164 (yaml_char_t *)keys[aindex].key5, strlen(keys[aindex].key5), 1, 0, YAML_PLAIN_SCALAR_STYLE);
165 if (!yaml_emitter_emit(emitter, event)){
166 keyerror(event, emitter);
169 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
170 (yaml_char_t *)vals[aindex].val5, strlen(vals[aindex].val5), 1, 0, YAML_PLAIN_SCALAR_STYLE);
171 if (!yaml_emitter_emit(emitter, event)){
172 keyerror(event, emitter);
176 if (keys[aindex].key6[0] !=
'\0') {
177 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
178 (yaml_char_t *)keys[aindex].key6, strlen(keys[aindex].key6), 1, 0, YAML_PLAIN_SCALAR_STYLE);
179 if (!yaml_emitter_emit(emitter, event)){
180 keyerror(event, emitter);
183 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
184 (yaml_char_t *)vals[aindex].val6, strlen(vals[aindex].val6), 1, 0, YAML_PLAIN_SCALAR_STYLE);
185 if (!yaml_emitter_emit(emitter, event)){
186 keyerror(event, emitter);
190 if (keys[aindex].key7[0] !=
'\0') {
191 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
192 (yaml_char_t *)keys[aindex].key7, strlen(keys[aindex].key7), 1, 0, YAML_PLAIN_SCALAR_STYLE);
193 if (!yaml_emitter_emit(emitter, event)){
194 keyerror(event, emitter);
197 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
198 (yaml_char_t *)vals[aindex].val7, strlen(vals[aindex].val7), 1, 0, YAML_PLAIN_SCALAR_STYLE);
199 if (!yaml_emitter_emit(emitter, event)){
200 keyerror(event, emitter);
204 if (keys[aindex].key8[0] !=
'\0') {
205 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
206 (yaml_char_t *)keys[aindex].key8, strlen(keys[aindex].key8), 1, 0, YAML_PLAIN_SCALAR_STYLE);
207 if (!yaml_emitter_emit(emitter, event)){
208 keyerror(event, emitter);
211 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
212 (yaml_char_t *)vals[aindex].val8, strlen(vals[aindex].val8), 1, 0, YAML_PLAIN_SCALAR_STYLE);
213 if (!yaml_emitter_emit(emitter, event)){
214 keyerror(event, emitter);
218 if (keys[aindex].key9[0] !=
'\0') {
219 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
220 (yaml_char_t *)keys[aindex].key9, strlen(keys[aindex].key9), 1, 0, YAML_PLAIN_SCALAR_STYLE);
221 if (!yaml_emitter_emit(emitter, event)){
222 keyerror(event, emitter);
225 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
226 (yaml_char_t *)vals[aindex].val9, strlen(vals[aindex].val9), 1, 0, YAML_PLAIN_SCALAR_STYLE);
227 if (!yaml_emitter_emit(emitter, event)){
228 keyerror(event, emitter);
232 if (keys[aindex].key10[0] !=
'\0') {
233 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
234 (yaml_char_t *)keys[aindex].key10, strlen(keys[aindex].key10), 1, 0, YAML_PLAIN_SCALAR_STYLE);
235 if (!yaml_emitter_emit(emitter, event)){
236 keyerror(event, emitter);
239 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
240 (yaml_char_t *)vals[aindex].val10, strlen(vals[aindex].val10), 1, 0, YAML_PLAIN_SCALAR_STYLE);
241 if (!yaml_emitter_emit(emitter, event)){
242 keyerror(event, emitter);
246 if (keys[aindex].key11[0] !=
'\0') {
247 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
248 (yaml_char_t *)keys[aindex].key11, strlen(keys[aindex].key11), 1, 0, YAML_PLAIN_SCALAR_STYLE);
249 if (!yaml_emitter_emit(emitter, event)){
250 keyerror(event, emitter);
253 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
254 (yaml_char_t *)vals[aindex].val11, strlen(vals[aindex].val11), 1, 0, YAML_PLAIN_SCALAR_STYLE);
255 if (!yaml_emitter_emit(emitter, event)){
256 keyerror(event, emitter);
260 if (keys[aindex].key12[0] !=
'\0') {
261 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
262 (yaml_char_t *)keys[aindex].key12, strlen(keys[aindex].key12), 1, 0, YAML_PLAIN_SCALAR_STYLE);
263 if (!yaml_emitter_emit(emitter, event)){
264 keyerror(event, emitter);
267 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
268 (yaml_char_t *)vals[aindex].val12, strlen(vals[aindex].val12), 1, 0, YAML_PLAIN_SCALAR_STYLE);
269 if (!yaml_emitter_emit(emitter, event)){
270 keyerror(event, emitter);
274 if (keys[aindex].key13[0] !=
'\0') {
275 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
276 (yaml_char_t *)keys[aindex].key13, strlen(keys[aindex].key13), 1, 0, YAML_PLAIN_SCALAR_STYLE);
277 if (!yaml_emitter_emit(emitter, event)){
278 keyerror(event, emitter);
281 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
282 (yaml_char_t *)vals[aindex].val13, strlen(vals[aindex].val13), 1, 0, YAML_PLAIN_SCALAR_STYLE);
283 if (!yaml_emitter_emit(emitter, event)){
284 keyerror(event, emitter);
288 if (keys[aindex].key14[0] !=
'\0') {
289 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
290 (yaml_char_t *)keys[aindex].key14, strlen(keys[aindex].key14), 1, 0, YAML_PLAIN_SCALAR_STYLE);
291 if (!yaml_emitter_emit(emitter, event)){
292 keyerror(event, emitter);
295 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
296 (yaml_char_t *)vals[aindex].val14, strlen(vals[aindex].val14), 1, 0, YAML_PLAIN_SCALAR_STYLE);
297 if (!yaml_emitter_emit(emitter, event)){
298 keyerror(event, emitter);
302 if (keys[aindex].key15[0] !=
'\0') {
303 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
304 (yaml_char_t *)keys[aindex].key15, strlen(keys[aindex].key15), 1, 0, YAML_PLAIN_SCALAR_STYLE);
305 if (!yaml_emitter_emit(emitter, event)){
306 keyerror(event, emitter);
309 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
310 (yaml_char_t *)vals[aindex].val15, strlen(vals[aindex].val15), 1, 0, YAML_PLAIN_SCALAR_STYLE);
311 if (!yaml_emitter_emit(emitter, event)){
312 keyerror(event, emitter);
316 if (keys[aindex].key16[0] !=
'\0') {
317 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
318 (yaml_char_t *)keys[aindex].key16, strlen(keys[aindex].key16), 1, 0, YAML_PLAIN_SCALAR_STYLE);
319 if (!yaml_emitter_emit(emitter, event)){
320 keyerror(event, emitter);
323 yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
324 (yaml_char_t *)vals[aindex].val16, strlen(vals[aindex].val16), 1, 0, YAML_PLAIN_SCALAR_STYLE);
325 if (!yaml_emitter_emit(emitter, event)){
326 keyerror(event, emitter);
354 void write_yaml_from_struct_3 (
char *yamlname,
int asize,
struct fmsyamloutkeys *topkeys,
struct fmsyamloutvalues *topvals,
int a2size,
struct fmsyamloutkeys *l2keys,
355 struct fmsyamloutvalues *l2vals,
int a3size,
int * n3each,
struct fmsyamloutkeys *l3keys,
struct fmsyamloutvalues *l3vals,
357 yaml_emitter_t emitter;
365 int ws_ind = strlen(yamlname)-1;
366 while(*(yamlname+ws_ind) ==
' ') ws_ind--;
367 if( ws_ind != strlen(yamlname)-1) yamlname[ws_ind+1] =
'\0';
370 yamlout = fopen(yamlname,
"w");
372 yaml_emitter_initialize(&emitter);
373 yaml_emitter_set_output_file(&emitter, yamlout);
374 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
375 if (!yaml_emitter_emit(&emitter, &event)){
376 error(yamlname, &event, &emitter, yamlout);
380 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
381 if (!yaml_emitter_emit(&emitter, &event)){
382 error(yamlname, &event, &emitter, yamlout);
386 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
387 1, YAML_ANY_MAPPING_STYLE);
388 if (!yaml_emitter_emit(&emitter, &event)){
389 error(yamlname, &event, &emitter, yamlout);
394 write_keys_vals_yaml (&emitter, &event , 0, topkeys, topvals);
395 char* curr_topkey = topkeys->level2key;
399 for (top_ind=0; top_ind < topkeys->level2key_offset; top_ind++) {
401 yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
402 (yaml_char_t *)curr_topkey, strlen(curr_topkey), 1, 0,
403 YAML_PLAIN_SCALAR_STYLE);
404 if (!yaml_emitter_emit(&emitter, &event)){
405 error(yamlname, &event, &emitter, yamlout);
409 yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
410 1, YAML_ANY_SEQUENCE_STYLE);
411 if (!yaml_emitter_emit(&emitter, &event)){
412 error(yamlname, &event, &emitter, yamlout);
417 for (s2 = 0 ; s2 < lvl2keyeach[top_ind]; s2++){
418 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
419 1, YAML_ANY_MAPPING_STYLE);
420 if (!yaml_emitter_emit(&emitter, &event)){
421 error(yamlname, &event, &emitter, yamlout);
425 write_keys_vals_yaml (&emitter, &event , s2count, l2keys, l2vals);
428 char * curr_l2key = (&l2keys[s2count])->level2key;
430 for (l2_ind = 0; l2_ind < (&l2keys[s2count])->level2key_offset; l2_ind++) {
432 yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
433 (yaml_char_t *)curr_l2key, strlen(curr_l2key), 1, 0,
434 YAML_PLAIN_SCALAR_STYLE);
435 if (!yaml_emitter_emit(&emitter, &event)){
436 error(yamlname, &event, &emitter, yamlout);
440 yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
441 1, YAML_ANY_SEQUENCE_STYLE);
442 if (!yaml_emitter_emit(&emitter, &event)){
443 error(yamlname, &event, &emitter, yamlout);
447 int s3start = s3count;
448 int s3end = s3start + n3each[i_n3];
451 for (s3 = s3start ; s3 < s3end ; s3++){
452 yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
453 1, YAML_ANY_MAPPING_STYLE);
454 if (!yaml_emitter_emit(&emitter, &event)){
455 error(yamlname, &event, &emitter, yamlout);
459 write_keys_vals_yaml (&emitter, &event , s3, l3keys, l3vals);
460 yaml_mapping_end_event_initialize(&event);
461 if(!yaml_emitter_emit(&emitter, &event)){
462 error(yamlname, &event, &emitter, yamlout);
467 yaml_sequence_end_event_initialize(&event);
468 if (!yaml_emitter_emit(&emitter, &event)){
469 error(yamlname, &event, &emitter, yamlout);
472 curr_l2key = (&l2keys[s2count])->level2key + ((l2_ind+1) * KEY_STR_LEN);
474 yaml_mapping_end_event_initialize(&event);
475 if (!yaml_emitter_emit(&emitter, &event)){
476 error(yamlname, &event, &emitter, yamlout);
483 yaml_sequence_end_event_initialize(&event);
484 if (!yaml_emitter_emit(&emitter, &event)){
485 error(yamlname, &event, &emitter, yamlout);
488 curr_topkey = topkeys->level2key + ((top_ind+1) * KEY_STR_LEN);
492 yaml_mapping_end_event_initialize(&event);
493 if (!yaml_emitter_emit(&emitter, &event)){
494 error(yamlname, &event, &emitter, yamlout);
498 yaml_document_end_event_initialize(&event, 0);
499 if (!yaml_emitter_emit(&emitter, &event)){
500 error(yamlname, &event, &emitter, yamlout);
503 yaml_stream_end_event_initialize(&event);
504 if (!yaml_emitter_emit(&emitter, &event)){
505 error(yamlname, &event, &emitter, yamlout);
508 yaml_emitter_delete(&emitter);
517 void add_level2key(
int key_length,
char* key_name,
struct fmsyamloutkeys* keys){
520 char kname_loc[key_length + 1];
522 strncpy(kname_loc, key_name, key_length);
523 kname_loc[key_length] =
'\0';
525 if ( strlen(kname_loc) > KEY_STR_LEN){
526 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);
527 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);
529 if( keys->level2key_offset >= LVL2KEY_NUM ){
530 fprintf(
stderr,
"WARNING: YAML_OUTPUT: max amount of level 2 keys (%d) has been exceeded", LVL2KEY_NUM);
531 fprintf(
stdout,
"WARNING: YAML_OUTPUT: max amount of level 2 keys (%d) has been exceeded", LVL2KEY_NUM);
534 if ( keys->level2key[0] ==
'\0'){
535 keys->level2key_offset = 0;
538 int offset = keys->level2key_offset * KEY_STR_LEN;
540 curr_key = keys->level2key + offset;
541 strcpy(curr_key, kname_loc);
543 keys->level2key_offset++;
546 printf(
"key length: %d \n key_name:", key_length);
549 printf(
"offset: %d \n", offset);
550 printf(
"kname_loc:");
553 printf(keys->level2key);
554 printf(
"\nl2key+offset:");
555 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.