Skip to content

Commit 21f3745

Browse files
committed
more refactoring and updated unicode build.
1 parent 63abab4 commit 21f3745

File tree

7 files changed

+111
-90
lines changed

7 files changed

+111
-90
lines changed

src/json_file_module.F90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module json_file_module
88

99
use,intrinsic :: iso_fortran_env
1010
use json_kinds
11+
use json_parameters, only: unit2str
1112
use json_string_utilities
1213
use json_value_module
1314

src/json_kinds.F90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ module json_kinds
8383
! Currently only gfortran >= 4.9.2 will correctly support
8484
! UCS4 which is stored in 4 bytes.
8585
! (and perhaps others).
86-
8786
integer,parameter,public :: CK = selected_char_kind(json_fortran_string_kind)
8887
!*********************************************************
8988

src/json_macros.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
!*****************************************************************************************
2+
!>
3+
! JSON-Fortran preprocessor macros.
14

25
!*********************************************************
36
! File encoding preprocessor macro.

src/json_module.F90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module json_module
1313

1414
use json_value_module
1515
use json_file_module
16+
use json_string_utilities
1617

1718
implicit none
1819

src/json_parameters.F90

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
!*****************************************************************************************
2+
!> author: Jacob Williams
3+
! license: BSD
4+
!
5+
! Other parameters used by JSON-Fortran.
6+
! This is a low-level module not meant to be used by a JSON-Fortran user.
7+
8+
module json_parameters
9+
10+
use json_kinds
11+
12+
implicit none
13+
14+
public
15+
16+
character(kind=CDK,len=*),parameter :: json_ext = '.json' !! JSON file extension
17+
18+
!special JSON characters
19+
character(kind=CK,len=*),parameter :: space = ' '
20+
character(kind=CK,len=*),parameter :: start_object = '{'
21+
character(kind=CK,len=*),parameter :: end_object = '}'
22+
character(kind=CK,len=*),parameter :: start_array = '['
23+
character(kind=CK,len=*),parameter :: end_array = ']'
24+
character(kind=CK,len=*),parameter :: delimiter = ','
25+
character(kind=CK,len=*),parameter :: colon_char = ':'
26+
character(kind=CK,len=*),parameter :: bspace = achar(8)
27+
character(kind=CK,len=*),parameter :: horizontal_tab = achar(9)
28+
character(kind=CK,len=*),parameter :: newline = achar(10)
29+
character(kind=CK,len=*),parameter :: formfeed = achar(12)
30+
character(kind=CK,len=*),parameter :: carriage_return = achar(13)
31+
character(kind=CK,len=*),parameter :: quotation_mark = achar(34)
32+
character(kind=CK,len=*),parameter :: slash = achar(47)
33+
character(kind=CK,len=*),parameter :: backslash = achar(92)
34+
35+
!These were parameters, but gfortran bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65141)
36+
!necessitates moving them here to be variables
37+
character(kind=CK,len=4) :: null_str = 'null'
38+
character(kind=CK,len=4) :: true_str = 'true'
39+
character(kind=CK,len=5) :: false_str = 'false'
40+
41+
integer, private :: i_ !! just a counter for control_chars array
42+
character(kind=CK,len=*),dimension(32),parameter :: control_chars = &
43+
[(achar(i_),i_=1,31), achar(127)] !! Control characters, possibly in unicode
44+
45+
!for indenting (Note: this could also be a user input...)
46+
integer(IK),parameter :: spaces_per_tab = 2
47+
48+
!find out the precision of the floating point number system
49+
!and set safety factors
50+
integer(IK),parameter :: rp_safety_factor = 1
51+
integer(IK),parameter :: rp_addl_safety = 1
52+
integer(IK),parameter :: real_precision = rp_safety_factor*precision(1.0_RK) + &
53+
rp_addl_safety
54+
55+
!Get the number of possible digits in the exponent when using decimal number system
56+
integer(IK),parameter :: maxexp = maxexponent(1.0_RK)
57+
integer(IK),parameter :: minexp = minexponent(1.0_RK)
58+
integer(IK),parameter :: real_exponent_digits = floor( 1 + log10( &
59+
real(max(maxexp,abs(maxexp)),&
60+
kind=RK) ) )
61+
62+
integer(IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
63+
!! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
64+
character(kind=CDK,len=*),parameter :: int_fmt = '(ss,I0)' !! minimum width format for integers
65+
66+
integer(IK),parameter :: chunk_size = 100 !! for allocatable strings: allocate chunks of this size
67+
integer(IK),parameter :: unit2str = -1 !! unit number to cause stuff to be
68+
!! output to strings rather than files.
69+
!! See 9.5.6.12 in the F2003/08 standard
70+
71+
end module json_parameters
72+
!*****************************************************************************************

src/json_string_utilities.F90

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
! license: BSD
44
!
55
! JSON-Fortran support module for string manipulation.
6-
! This is a low-level module not meant to be used by a JSON-Fortran user.
76

87
module json_string_utilities
98

@@ -25,30 +24,29 @@ module json_string_utilities
2524
!*************************************************************************************
2625

2726
# ifdef USE_UCS4
28-
! Provide a means to convert to UCS4 while concatenating UCS4 and default strings
27+
!******************************************************
28+
!>
29+
! Provide a means to convert to UCS4 while
30+
! concatenating UCS4 and default strings
2931
interface operator(//)
3032
module procedure ucs4_join_default, default_join_ucs4
3133
end interface
34+
public :: operator(//)
3235

33-
! Provide a string comparison operator that works with mixed kinds
36+
!******************************************************
37+
!>
38+
! Provide a string comparison operator that works
39+
! with mixed kinds
3440
interface operator(==)
3541
module procedure ucs4_comp_default, default_comp_ucs4
3642
end interface
37-
38-
public :: operator(//)
3943
public :: operator(==)
4044
# endif
4145

4246
public :: integer_to_string
4347
public :: real_to_string
4448
public :: compact_real_string
4549
public :: valid_json_hex
46-
public :: to_uni
47-
public :: to_uni_vec
48-
public :: ucs4_join_default
49-
public :: default_join_ucs4
50-
public :: ucs4_comp_default
51-
public :: default_comp_ucs4
5250
public :: to_unicode
5351

5452
contains
@@ -65,7 +63,7 @@ pure subroutine integer_to_string(ival,int_fmt,str)
6563
implicit none
6664

6765
integer(IK),intent(in) :: ival !! integer value.
68-
character(kind=CK,len=*),intent(in) :: int_fmt !! format for integers
66+
character(kind=CDK,len=*),intent(in) :: int_fmt !! format for integers
6967
character(kind=CK,len=*),intent(out) :: str !! ival converted to a string.
7068

7169
integer(IK) :: istat
@@ -96,7 +94,7 @@ subroutine real_to_string(rval,real_fmt,compact_real,str)
9694
implicit none
9795

9896
real(RK),intent(in) :: rval !! real value.
99-
character(kind=CDK,len=*),intent(in) :: real_fmt
97+
character(kind=CDK,len=*),intent(in) :: real_fmt !! format for real numbers
10098
logical(LK),intent(in) :: compact_real !! compact the string so that it is
10199
!! displayed with fewer characters
102100
character(kind=CK,len=*),intent(out) :: str !! rval converted to a string.
@@ -110,11 +108,9 @@ subroutine real_to_string(rval,real_fmt,compact_real,str)
110108
end if
111109

112110
if (istat==0) then
113-
114111
!in this case, the default string will be compacted,
115112
! so that the same value is displayed with fewer characters.
116113
if (compact_real) call compact_real_string(str)
117-
118114
else
119115
str = repeat(star,len(str))
120116
end if
@@ -345,7 +341,7 @@ function default_comp_ucs4(def_str,ucs4_str) result(res)
345341
character(kind=CK, len=*), intent(in) :: ucs4_str
346342
logical(LK) :: res
347343

348-
res = ( to_unicode(def_str) == ucs4_str)
344+
res = (to_unicode(def_str) == ucs4_str)
349345

350346
end function default_comp_ucs4
351347
!*****************************************************************************************

src/json_value_module.F90

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@
8080

8181
module json_value_module
8282

83-
use,intrinsic :: iso_fortran_env, only: iostat_end
83+
use,intrinsic :: iso_fortran_env, only: iostat_end,error_unit,output_unit
8484
use json_kinds
85+
use json_parameters
8586
use json_string_utilities
8687

8788
implicit none
@@ -577,89 +578,30 @@ end subroutine traverse_callback_func
577578
public :: json_update ! update a value in a JSON structure
578579
public :: json_traverse ! to traverse all elements of a JSON structure
579580
public :: json_print_error_message !
580-
581-
!... so it can be used in json_file_module ...
582-
!public :: json_value_print
583581
public :: throw_exception
584582

585-
character(kind=CDK,len=*),parameter,public :: json_ext = '.json' !! JSON file extension
586-
587-
!special JSON characters
588-
character(kind=CK,len=*),parameter :: space = ' '
589-
character(kind=CK,len=*),parameter :: start_object = '{'
590-
character(kind=CK,len=*),parameter :: end_object = '}'
591-
character(kind=CK,len=*),parameter :: start_array = '['
592-
character(kind=CK,len=*),parameter :: end_array = ']'
593-
character(kind=CK,len=*),parameter :: delimiter = ','
594-
character(kind=CK,len=*),parameter :: colon_char = ':'
595-
character(kind=CK,len=*),parameter :: bspace = achar(8)
596-
character(kind=CK,len=*),parameter :: horizontal_tab = achar(9)
597-
character(kind=CK,len=*),parameter :: newline = achar(10)
598-
character(kind=CK,len=*),parameter :: formfeed = achar(12)
599-
character(kind=CK,len=*),parameter :: carriage_return = achar(13)
600-
character(kind=CK,len=*),parameter :: quotation_mark = achar(34)
601-
character(kind=CK,len=*),parameter :: slash = achar(47)
602-
character(kind=CK,len=*),parameter :: backslash = achar(92)
603-
604-
!These were parameters, but gfortran bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65141)
605-
!necessitates moving them here to be variables
606-
character(kind=CK,len=4) :: null_str = 'null'
607-
character(kind=CK,len=4) :: true_str = 'true'
608-
character(kind=CK,len=5) :: false_str = 'false'
609-
610-
! Control characters, possibly in unicode
611-
integer, private :: i_
612-
character(kind=CK,len=*),parameter :: control_chars(32) = [(achar(i_),i_=1,31), achar(127)]
613-
614-
!for indenting (Note: jsonlint.com uses 4 spaces)
615-
integer(IK),parameter :: spaces_per_tab = 2
583+
!
584+
! Note: the following global variables make this module non thread safe.
585+
!
616586

617587
!Variables for real string printing:
618-
619588
logical(LK) :: compact_real = .true. !! to use the "compact" form of real numbers for output
620-
621-
!find out the precision of the floating point number system
622-
!and set safety factors
623-
integer(IK),parameter :: rp_safety_factor = 1
624-
integer(IK),parameter :: rp_addl_safety = 1
625-
integer(IK),parameter :: real_precision = rp_safety_factor*precision(1.0_RK) + &
626-
rp_addl_safety
627-
628-
!Get the number of possible digits in the exponent when using decimal number system
629-
integer(IK),parameter :: maxexp = maxexponent(1.0_RK)
630-
integer(IK),parameter :: minexp = minexponent(1.0_RK)
631-
integer(IK),parameter :: real_exponent_digits = floor( 1 + log10( &
632-
real(max(maxexp,abs(maxexp)),&
633-
kind=RK) ) )
634-
635-
integer(IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
636-
!! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
637-
character(kind=CDK,len=*),parameter :: int_fmt = '(ss,I0)' !! minimum width format for integers
638589
character(kind=CDK,len=:),allocatable :: real_fmt !! the format string to use for real numbers
639590
!! it is set in [[json_initialize]]
640591

641-
!
642-
! Note: the following global variables make this module non thread safe.
643-
!
644-
645592
!exception handling [private variables]
646-
logical(LK) :: is_verbose = .false. !! if true, all exceptions are immediately printed to console
647-
logical(LK),public :: exception_thrown = .true. !! the error flag (by default, this is true to
648-
!! make sure that [[json_initialize]] is called.
593+
logical(LK) :: is_verbose = .false. !! if true, all exceptions are immediately printed to console
594+
logical(LK),public :: exception_thrown = .true. !! the error flag (by default, this is true to
595+
!! make sure that [[json_initialize]] is called.
649596
character(kind=CK,len=:),allocatable :: err_message !! the error message
650597

651598
!temp vars used when parsing lines in file [private variables]
652-
integer(IK) :: char_count = 0 !character position in the current line
653-
integer(IK) :: line_count = 1 !lines read counter
599+
integer(IK) :: char_count = 0 !! character position in the current line
600+
integer(IK) :: line_count = 1 !! lines read counter
654601
integer(IK) :: pushed_index = 0
655602
character(kind=CK,len=10) :: pushed_char = '' !JW : what is this magic number 10??
656603

657-
integer(IK),parameter :: chunk_size = 100 !! for allocatable strings: allocate chunks of this size
658-
integer(IK) :: ipos = 1 !! for allocatable strings: next character to read
659-
660-
integer(IK),parameter,public :: unit2str = -1 !! unit number to cause stuff to be
661-
!! output to strings rather than files.
662-
!! See 9.5.6.12 in the F2003/08 standard
604+
integer(IK) :: ipos = 1 !! for allocatable strings: next character to read
663605

664606
contains
665607
!*****************************************************************************************
@@ -842,6 +784,7 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
842784
!clear any errors from previous runs:
843785
call json_clear_exceptions()
844786

787+
!JW note: do we need this?.....
845788
!Ensure gfortran bug work around "parameters" are set properly
846789
null_str = 'null'
847790
true_str = 'true'
@@ -945,18 +888,24 @@ end subroutine json_clear_exceptions
945888

946889
subroutine json_throw_exception(msg)
947890

891+
#ifdef __INTEL_COMPILER
892+
use ifcore, only: tracebackqq
893+
#endif
894+
948895
implicit none
949896

950-
character(kind=CK,len=*),intent(in) :: msg !the error message
897+
character(kind=CK,len=*),intent(in) :: msg !! the error message
951898

952899
exception_thrown = .true.
953900
err_message = trim(msg)
954901

955902
if (is_verbose) then
956903
write(*,'(A)') '***********************'
957-
write(*,'(A)') 'JSON-Fortran EXCEPTION: '//trim(msg)
904+
write(*,'(A)') 'JSON-Fortran Exception: '//trim(msg)
958905
!call backtrace() ! gfortran (use -fbacktrace -fall-intrinsics flags)
959-
!call tracebackqq(-1) ! intel (requires "use ifcore" in this routine)
906+
#ifdef __INTEL_COMPILER
907+
call tracebackqq(-1) ! print a traceback and return
908+
#endif
960909
write(*,'(A)') '***********************'
961910
end if
962911

@@ -971,7 +920,7 @@ subroutine wrap_json_throw_exception(msg)
971920

972921
implicit none
973922

974-
character(kind=CDK,len=*),intent(in) :: msg !the error message
923+
character(kind=CDK,len=*),intent(in) :: msg !! the error message
975924

976925
call json_throw_exception(to_unicode(msg))
977926

0 commit comments

Comments
 (0)