-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomlc99.f90
1506 lines (1120 loc) · 46.4 KB
/
tomlc99.f90
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module tomlc99
use, intrinsic :: iso_fortran_env, only : stdout=>output_unit, &
stderr=>error_unit, &
int32, int64, real64
use iso_c_binding
implicit none
integer, parameter :: ucs4 = selected_char_kind('ISO_10646')
integer(int32), parameter :: maxStrLen = 262144
type, bind(c) :: bufferType
integer(c_int) :: year, month, day
integer(c_int) :: hour, minute, second, millisec
character(kind=c_char) :: z(10)
end type
type, bind(c) :: timestampType
type(bufferType) :: buffer
type(c_ptr) :: year, month, day
type(c_ptr) :: hour, minute, second, millisec
type(c_ptr) :: z
end type
type :: toml_time
integer :: year, month, day
integer :: hour, minute, second, millisec
character(len=10) :: offset = ""
character(len=1) :: timeType
logical :: has_millisec = .false.
end type
interface
! interfaces to standard C library functions
function c_fopen(fileName, mode) bind(C,name="fopen")
import :: c_ptr, c_char
implicit none
type(c_ptr) :: c_fopen
character(kind=c_char) :: fileName, mode
end function
function c_fclose(filePtr) bind(C,name="fclose")
import :: c_ptr, c_int
implicit none
type(c_ptr), value :: filePtr
integer(c_int) :: c_fclose
end function
subroutine c_free(ptr) bind(C,name="free")
import :: c_ptr
implicit none
type(c_ptr), value :: ptr
end subroutine
! interfaces to tomlc99 functions
function tomlc99_toml_parse_file(filePtr, errBuf, errBufSz) &
bind(C,name="toml_parse_file")
import :: c_ptr, c_char, c_int
implicit none
type(c_ptr) :: tomlc99_toml_parse_file
type(c_ptr), value :: filePtr
character(kind=c_char) :: errBuf(*)
integer(c_int) :: errBufSz
end function
subroutine tomlc99_toml_free(tblPtr) bind(C,name="toml_free")
import :: c_ptr
implicit none
type(c_ptr), value :: tblPtr
end subroutine
function tomlc99_toml_key_in(tblPtr, keyIdx) bind(C,name="toml_key_in")
import :: c_ptr, c_int
implicit none
type(c_ptr) :: tomlc99_toml_key_in
type(c_ptr), value :: tblPtr
integer(c_int), value :: keyIdx
end function
function tomlc99_toml_raw_in(dataPtr, keyName) bind(C,name="toml_raw_in")
import :: c_ptr, c_char
implicit none
type(c_ptr) :: tomlc99_toml_raw_in
type(c_ptr), value :: dataPtr
character(kind=c_char) :: keyName(*)
end function
function tomlc99_toml_table_in(dataPtr, tableName) &
bind(C,name="toml_table_in")
import :: c_ptr, c_char
implicit none
type(c_ptr) :: tomlc99_toml_table_in
type(c_ptr), value :: dataPtr
character(kind=c_char) :: tableName(*)
end function
function tomlc99_toml_table_key(tblPtr) &
bind(C,name="toml_table_key")
import :: c_ptr
implicit none
type(c_ptr) :: tomlc99_toml_table_key
type(c_ptr), value :: tblPtr
end function
function tomlc99_toml_table_nkval(tblPtr) &
bind(C,name="toml_table_nkval")
import :: c_ptr, c_int
implicit none
type(c_ptr), value :: tblPtr
integer(c_int) :: tomlc99_toml_table_nkval
end function
function tomlc99_toml_table_narr(tblPtr) &
bind(C,name="toml_table_narr")
import :: c_ptr, c_int
implicit none
type(c_ptr), value :: tblPtr
integer(c_int) :: tomlc99_toml_table_narr
end function
function tomlc99_toml_table_ntab(tblPtr) &
bind(C,name="toml_table_ntab")
import :: c_ptr, c_int
implicit none
type(c_ptr), value :: tblPtr
integer(c_int) :: tomlc99_toml_table_ntab
end function
function tomlc99_toml_array_in(dataPtr, arrayName) &
bind(C,name="toml_array_in")
import :: c_ptr, c_char
implicit none
type(c_ptr) :: tomlc99_toml_array_in
type(c_ptr), value :: dataPtr
character(kind=c_char) :: arrayName(*)
end function
function tomlc99_toml_array_kind(arrPtr) &
bind(C,name="toml_array_kind")
import :: c_ptr, c_char
implicit none
type(c_ptr), value :: arrPtr
character(kind=c_char) :: tomlc99_toml_array_kind
end function
function tomlc99_toml_array_type(arrPtr) &
bind(C,name="toml_array_type")
import :: c_ptr, c_char
implicit none
type(c_ptr), value :: arrPtr
character(kind=c_char) :: tomlc99_toml_array_type
end function
function tomlc99_toml_array_key(tblPtr) &
bind(C,name="toml_array_key")
import :: c_ptr
implicit none
type(c_ptr) :: tomlc99_toml_array_key
type(c_ptr), value :: tblPtr
end function
function tomlc99_toml_array_nelem(arrPtr) &
bind(C,name="toml_array_nelem")
import :: c_ptr, c_int
implicit none
integer(c_int) :: tomlc99_toml_array_nelem
type(c_ptr), value :: arrPtr
end function
function tomlc99_toml_raw_at(arrPtr, idx) &
bind(C,name="toml_raw_at")
import :: c_ptr, c_int
implicit none
type(c_ptr) :: tomlc99_toml_raw_at
type(c_ptr), value :: arrPtr
integer(c_int), value :: idx
end function
function tomlc99_toml_array_at(arrPtr, idx) &
bind(C,name="toml_array_at")
import :: c_ptr, c_int
implicit none
type(c_ptr) :: tomlc99_toml_array_at
type(c_ptr), value :: arrPtr
integer(c_int), value :: idx
end function
function tomlc99_toml_table_at(arrPtr, idx) &
bind(C,name="toml_table_at")
import :: c_ptr, c_int
implicit none
type(c_ptr) :: tomlc99_toml_table_at
type(c_ptr), value :: arrPtr
integer(c_int), value :: idx
end function
function tomlc99_toml_rtos(raw, outStr) bind(C,name="toml_rtos")
import :: c_int, c_ptr, c_char
implicit none
integer(c_int) :: tomlc99_toml_rtos
type(c_ptr), value :: raw
type(c_ptr) :: outStr
end function
function tomlc99_toml_rtoi(raw, outInt) bind(C,name="toml_rtoi")
import :: c_ptr, c_int, c_int64_t
implicit none
integer(c_int) :: tomlc99_toml_rtoi
type(c_ptr), value :: raw
integer(c_int64_t) :: outInt
end function
function tomlc99_toml_rtod(raw, outDbl) bind(C,name="toml_rtod")
import :: c_ptr, c_int, c_double
implicit none
integer(c_int) :: tomlc99_toml_rtod
type(c_ptr), value :: raw
real(c_double) :: outDbl
end function
function tomlc99_toml_rtob(raw, outBool) bind(C,name="toml_rtob")
import :: c_ptr, c_int, c_bool
implicit none
integer(c_int) :: tomlc99_toml_rtob
type(c_ptr), value :: raw
logical(c_bool) :: outBool
end function
function tomlc99_toml_rtots(raw, outTime) bind(C,name="toml_rtots")
import :: c_ptr, c_int, timestampType
implicit none
integer(c_int) :: tomlc99_toml_rtots
type(c_ptr), value :: raw
type(timestampType) :: outTime
end function
function tomlc99_toml_utf8_to_ucs(orig, length, outVal) &
bind(C,name="toml_utf8_to_ucs")
import :: c_char, c_int, c_int64_t
implicit none
integer(c_int) :: tomlc99_toml_utf8_to_ucs
character(kind=c_char) :: orig(*)
integer(c_int), value :: length
integer(c_int64_t) :: outVal
end function
function tomlc99_toml_ucs_to_utf8(codeVal, buffer) &
bind(C,name="toml_ucs_to_utf8")
import :: c_char, c_int, c_int64_t
implicit none
integer(c_int) :: tomlc99_toml_ucs_to_utf8
integer(c_int64_t), value :: codeVal
character(kind=c_char) :: buffer(6)
end function
end interface
contains
function toml_parse_file(fileName)
! description: opens a TOML file named "fileName" and parses the data;
! returns a c pointer to the root-level "toml_table_t" data
! structure. Errors are fatal.
type(c_ptr) :: toml_parse_file
character(len=*), intent(in) :: fileName
type(c_ptr) :: fh
character(len=512, kind=c_char) :: errBuf
fh = c_fopen(trim(fileName) // c_null_char, &
c_char_"r" // c_null_char)
if (c_associated(fh) .eqv. .false.) then
write(stderr,101) trim(fileName)
error stop
endif
toml_parse_file = tomlc99_toml_parse_file(fh, errBuf, &
len(errBuf, kind=c_int))
if (c_associated(toml_parse_file) .eqv. .false.) then
write(stderr,102) trim(fileName)
call write_error_buffer(errBuf)
error stop
endif
if (c_fclose(fh) /= 0) then
write(stderr,103) trim(fileName)
error stop
endif
101 format ('ERROR: Failed to open ',a)
102 format ('ERROR: Failed to parse ',a)
103 format ('ERROR: Failed to close ',a)
end function
subroutine write_error_buffer(errBuf)
! description: writes error messages associated with the "parse" operation
character(len=*, kind=c_char), intent(in) :: errBuf
integer :: idx
do idx=1,len(errBuf)
if (errBuf(idx:idx) == c_null_char) exit
enddo
write(stderr, '(a)') errBuf(1:idx)
end subroutine
subroutine toml_free(inTblPtr)
! description: free the memory associated with the root-level "toml_table_t"
! data structure
type(c_ptr), intent(in) :: inTblPtr
call tomlc99_toml_free(inTblPtr)
end subroutine
function toml_table_in(inTblPtr, tblName)
! description: returns a c pointer to the table with name "tblName"
! contained in the "toml_table_t" structure referenced
! by "inTblPtr". Returns c_null_ptr if "tblName" is not found.
type(c_ptr) :: toml_table_in
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: tblName
toml_table_in = tomlc99_toml_table_in(inTblPtr, &
tblName // c_null_char)
end function
subroutine toml_table_key(inTblPtr, keyName)
! description: accepts a pointer to a "toml_table_t" data structure
! and returns the string value of the key name.
type(c_ptr), intent(in) :: inTblPtr
character(len=:), &
allocatable, intent(out) :: keyName
type(c_ptr) :: tmpKey
character(len=maxStrLen), &
pointer :: fstring
integer(int32) :: keyLen
tmpKey = tomlc99_toml_table_key(inTblPtr)
call c_f_pointer(tmpKey, fstring)
keyLen = index(fstring, c_null_char)-1
keyName = fstring(1:keyLen)
end subroutine
function toml_table_nkval(inTblPtr)
! description: returns the integer number of key-value pairs in the
! "toml_table_t" structure referenced by "inTblPtr".
integer(int32) :: toml_table_nkval
type(c_ptr), intent(in) :: inTblPtr
integer(c_int) :: c_outVal
c_outVal = tomlc99_toml_table_nkval(inTblPtr)
toml_table_nkval = c_outVal
end function
function toml_table_narr(inTblPtr)
! description: returns the integer number of arrays in the
! "toml_table_t" structure referenced by "inTblPtr".
integer(int32) :: toml_table_narr
type(c_ptr), intent(in) :: inTblPtr
integer(c_int) :: c_outVal
c_outVal = tomlc99_toml_table_narr(inTblPtr)
toml_table_narr = c_outVal
end function
function toml_table_ntab(inTblPtr)
! description: returns the integer number of sub-tables in the
! "toml_table_t" structure referenced by "inTblPtr".
integer(int32) :: toml_table_ntab
type(c_ptr), intent(in) :: inTblPtr
integer(c_int) :: c_outVal
c_outVal = tomlc99_toml_table_ntab(inTblPtr)
toml_table_ntab = c_outVal
end function
function toml_array_in(inTblPtr, arrayName)
! description: returns a c pointer to the array with name "arrayName"
! contained in the "toml_table_t" structure referenced
! by "inTblPtr". Returns c_null_ptr if "arrayName" is not found.
type(c_ptr) :: toml_array_in
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: arrayName
toml_array_in = tomlc99_toml_array_in(inTblPtr, &
arrayName // c_null_char)
end function
function toml_array_kind(inArrPtr)
! description: accepts a pointer to a "toml_array_t" data structure
! and returns the kind; 'v' for value, 'a' for array, and
! 't' for table.
character :: toml_array_kind
type(c_ptr), intent(in) :: inArrPtr
character(kind=c_char) :: c_kind
c_kind = tomlc99_toml_array_kind(inArrPtr)
toml_array_kind = c_kind
end function
function toml_array_type(inArrPtr)
! description: for array type 'v', accepts a pointer to a "toml_array_t"
! data structure and returns the value type; 'i' for int, 'd'
! for double, 'b' for bool, 's' for string
character :: toml_array_type
type(c_ptr), intent(in) :: inArrPtr
character(kind=c_char) :: c_kind
c_kind = tomlc99_toml_array_type(inArrPtr)
toml_array_type = c_kind
end function
function toml_array_nelem(inArrPtr)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns the number of elements in the array
integer :: toml_array_nelem
type(c_ptr), intent(in) :: inArrPtr
integer(c_int) :: c_nelem
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
toml_array_nelem = c_nelem
end function
subroutine toml_array_key(inArrPtr, keyName)
! description: accepts a pointer to a "toml_array_t" data structure
! and returns the string value of the key name.
type(c_ptr), intent(in) :: inArrPtr
character(len=:), &
allocatable, intent(out) :: keyName
type(c_ptr) :: tmpKey
character(len=maxStrLen), &
pointer :: fstring
integer(int32) :: keyLen
tmpKey = tomlc99_toml_array_key(inArrPtr)
call c_f_pointer(tmpKey, fstring)
keyLen = index(fstring, c_null_char)-1
keyName = fstring(1:keyLen)
end subroutine
subroutine toml_get_array_int(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an integer array. A fatal error
! is issued if the parameters do not match
type(c_ptr), intent(in) :: inArrPtr
integer(int64), dimension(:), &
allocatable, intent(out) :: outArray
integer(c_int64_t), dimension(:), allocatable :: c_outArray
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(c_ptr) :: tmpRaw
integer :: idx
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 'i') then
write(stderr,102) c_type, 'i'
error stop
endif
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
allocate(outArray(1:c_nelem)); outArray = 0
allocate(c_outArray(0:c_nelem-1)); c_outArray = 0
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtoi(tmpRaw, c_outArray(c_idx))
enddo
outArray = c_outArray
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_array_dbl(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an double array. A fatal error
! is issued if the parameters do not match
type(c_ptr), intent(in) :: inArrPtr
real(real64), dimension(:), &
allocatable, intent(out) :: outArray
real(c_double), dimension(:), allocatable :: c_outArray
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(c_ptr) :: tmpRaw
integer :: idx
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 'd') then
write(stderr,102) c_type, 'd'
error stop
endif
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
allocate(outArray(1:c_nelem)); outArray = 0.0
allocate(c_outArray(0:c_nelem-1)); c_outArray = 0.0
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtod(tmpRaw, c_outArray(c_idx))
enddo
outArray = c_outArray
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_array_time(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an type(toml_time) array. A fatal error
! is issued if the parameters do not match
type(c_ptr), intent(in) :: inArrPtr
type(toml_time), dimension(:), &
allocatable, intent(out) :: outArray
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(timestampType) :: c_outTime
type(c_ptr) :: tmpRaw
integer :: idx
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 't' .and. c_type /= "D" .and. c_type /= "T") then
write(stderr,102) c_type, "t || T || D"
error stop
endif
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
allocate(outArray(1:c_nelem))
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtots(tmpRaw, c_outTime)
call toml_c_f_timestamp(c_outTime, outArray(idx))
enddo
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_array_bool(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an logical array. A fatal error
! is issued if the parameters do not match
type(c_ptr), intent(in) :: inArrPtr
logical, dimension(:), allocatable, &
intent(out) :: outArray
logical(kind=c_bool),&
dimension(:), allocatable :: c_outArray
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(c_ptr) :: tmpRaw
integer :: idx
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 'b') then
write(stderr,102) c_type, 'b'
error stop
endif
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
allocate(outArray(1:c_nelem)); outArray = .false.
allocate(c_outArray(0:c_nelem-1)); c_outArray = .false.
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtob(tmpRaw, c_outArray(c_idx))
enddo
outArray = c_outArray
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end subroutine
function toml_array_strlen(inArrPtr)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns the maximum string length in the
! array. A fatal error is issued if the parameters do not match
integer :: toml_array_strlen
type(c_ptr), intent(in) :: inArrPtr
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(c_ptr) :: tmpRaw, c_outStr
integer :: idx, tmpStrLen
character(len=maxStrLen), &
pointer :: fstring
toml_array_strlen = 0
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 's') then
write(stderr,102) c_type, 's'
error stop
endif
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtos(tmpRaw, c_outStr)
call c_f_pointer(c_outStr, fstring)
tmpStrLen = index(fstring, c_null_char)-1
call c_free(c_outStr)
toml_array_strlen = max(toml_array_strlen, tmpStrLen)
enddo
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end function
subroutine toml_get_array_str(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns a string array. A fatal error is
! issued if the parameters do not match
type(c_ptr), intent(in) :: inArrPtr
character(len=:), dimension(:), &
allocatable, intent(out) :: outArray
integer(c_int) :: c_nelem, c_idx, c_ierr
character(kind=c_char) :: c_kind, c_type
type(c_ptr) :: tmpRaw, c_outStr
integer :: idx, tmpStrLen, maxLen
character(len=maxStrLen), pointer :: fstring
c_kind = tomlc99_toml_array_kind(inArrPtr)
c_type = tomlc99_toml_array_type(inArrPtr)
if (c_kind /= 'v') then
write(stderr,101) c_kind, 'v'
error stop
endif
if (c_type /= 's') then
write(stderr,102) c_type, 's'
error stop
endif
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
maxLen = toml_array_strlen(inArrPtr)
allocate(character(maxLen) :: outArray(int(c_nelem, int32)))
do idx=1,c_nelem
c_idx = idx - 1
tmpRaw = tomlc99_toml_raw_at(inArrPtr, c_idx)
c_ierr = tomlc99_toml_rtos(tmpRaw, c_outStr)
call c_f_pointer(c_outStr, fstring)
tmpStrLen = index(fstring, c_null_char)-1
outArray(idx) = fstring(1:tmpStrLen)
call c_free(c_outStr)
enddo
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
102 format ('ERROR: array has type "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_array_tbl(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an array of pointers to the enclosed
! tables. A fatal error is issued if the parameters do not
! match.
type(c_ptr), intent(in) :: inArrPtr
type(c_ptr), dimension(:), &
allocatable, intent(out) :: outArray
integer(c_int) :: c_nelem, c_idx
character(kind=c_char) :: c_kind
integer :: idx
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
c_kind = tomlc99_toml_array_kind(inArrPtr)
if (c_kind /= 't') then
write(stderr,101) c_kind, 't'
error stop
endif
allocate(outArray(1:c_nelem)); outArray = c_null_ptr
do idx=1,c_nelem
c_idx = idx - 1
outArray(idx) = tomlc99_toml_table_at(inArrPtr, c_idx)
enddo
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_array_arr(inArrPtr, outArray)
! description: accepts a pointer to a "toml_array_t" data structure
! structure and returns an array of pointers to the enclosed
! arrays. A fatal error is issued if the parameters do not
! match.
type(c_ptr), intent(in) :: inArrPtr
type(c_ptr), dimension(:), &
allocatable, intent(out) :: outArray
integer(c_int) :: c_nelem, c_idx
character(kind=c_char) :: c_kind
integer :: idx
c_nelem = tomlc99_toml_array_nelem(inArrPtr)
c_kind = tomlc99_toml_array_kind(inArrPtr)
if (c_kind /= 'a') then
write(stderr,101) c_kind, 'a'
error stop
endif
allocate(outArray(1:c_nelem)); outArray = c_null_ptr
do idx=1,c_nelem
c_idx = idx - 1
outArray(idx) = tomlc99_toml_array_at(inArrPtr, c_idx)
enddo
101 format ('ERROR: array has kind "',a,'" but "',a,'" is required.')
end subroutine
subroutine toml_get_val_str(inTblPtr, keyName, outVal)
! description: accepts a pointer to a "toml_table_t" data structure
! and a key name for a string; returns the string value.
! If the parameters do not match, a fatal error is issued.
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: keyName
character(len=:), &
allocatable, intent(out) :: outVal
type(c_ptr) :: tmpRaw
character :: valType
integer(c_int) :: c_ierr = 0
type(c_ptr) :: c_outStr
character(len=maxStrLen), pointer :: fstring
integer :: strLen = 0
tmpRaw = tomlc99_toml_raw_in(inTblPtr, trim(keyName) // c_null_char)
if (c_associated(tmpRaw) .eqv. .false.) then
write(stderr,101) trim(keyName)
error stop
endif
valType = toml_inquire_val_type(inTblPtr, trim(keyName) // c_null_char)
if (valType /= "s") then
write(stderr,102) trim(keyName), valType, "s"
error stop
endif
c_ierr = tomlc99_toml_rtos(tmpRaw, c_outStr)
call c_f_pointer(c_outStr, fstring)
strLen = index(fstring, c_null_char)-1
allocate(character(strLen) :: outVal)
outVal = fstring(1:strLen)
call c_free(c_outStr)
101 format ('ERROR: Failed to find key: ',a)
102 format ('ERROR: Key "',a,'" has type "',a,&
'", but the output array is type "',a,'"')
103 format ('ERROR: Output string length does not match TOML data for key: ',a)
end subroutine
subroutine toml_get_val_int(inTblPtr, keyName, outVal)
! description: accepts a pointer to a "toml_table_t" data structure
! and a key name for an int; returns the int value.
! If the parameters do not match, a fatal error is issued.
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: keyName
integer(int64), intent(out) :: outVal
type(c_ptr) :: tmpRaw
character :: valType
integer(c_int) :: c_ierr = 0
integer(c_int64_t) :: c_outVal = 0
outVal = 0
tmpRaw = tomlc99_toml_raw_in(inTblPtr, trim(keyName) // c_null_char)
if (c_associated(tmpRaw) .eqv. .false.) then
write(stderr,101) trim(keyName)
error stop
endif
valType = toml_inquire_val_type(inTblPtr, trim(keyName) // c_null_char)
if (valType /= "i") then
write(stderr,102) trim(keyName), valType, "i"
error stop
endif
c_ierr = tomlc99_toml_rtoi(tmpRaw, c_outVal)
if (c_ierr == -1) then
write(stderr,103) trim(keyName)
error stop
endif
outVal = c_outVal
101 format ('ERROR: Failed to find key: ',a)
102 format ('ERROR: Key "',a,'" has type "',a,&
'", but the output array is type "',a,'"')
103 format ('ERROR: Failed integer conversion for key: ',a)
end subroutine
subroutine toml_get_val_bool(inTblPtr, keyName, outVal)
! description: accepts a pointer to a "toml_table_t" data structure
! and a key name for a bool; returns the logical value.
! If the parameters do not match, a fatal error is issued.
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: keyName
logical, intent(out) :: outVal
type(c_ptr) :: tmpRaw
character :: valType
integer(c_int) :: c_ierr = 0
logical(kind=c_bool) :: c_outVal
tmpRaw = tomlc99_toml_raw_in(inTblPtr, trim(keyName) // c_null_char)
if (c_associated(tmpRaw) .eqv. .false.) then
write(stderr,101) trim(keyName)
error stop
endif
valType = toml_inquire_val_type(inTblPtr, trim(keyName) // c_null_char)
if (valType /= "b") then
write(stderr,102) trim(keyName), valType, "b"
error stop
endif
c_ierr = tomlc99_toml_rtob(tmpRaw, c_outVal)
if (c_ierr == -1) then
write(stderr,103) trim(keyName)
error stop
endif
outVal = c_outVal
101 format ('ERROR: Failed to find key: ',a)
102 format ('ERROR: Key "',a,'" has type "',a,&
'", but the output array is type "',a,'"')
103 format ('ERROR: Failed bool conversion for key: ',a)
end subroutine
subroutine toml_get_val_dbl(inTblPtr, keyName, outVal)
! description: accepts a pointer to a "toml_table_t" data structure
! and a key name for a double; returns the double value.
! If the parameters do not match, a fatal error is issued.
type(c_ptr), intent(in) :: inTblPtr
character(len=*), intent(in) :: keyName