Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Add support for string class
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristopherson committed Aug 11, 2020
1 parent 156c9e9 commit bd7ba86
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
35 changes: 30 additions & 5 deletions src/file_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ module file_io
procedure, public :: open => tw_open
!> @brief Writes text to the file, but does not advance to the next
!! line.
procedure, public :: write => tw_write_txt
generic, public :: write => tw_write_txt, tw_write_txt_str
!> @brief Writes text to the file, but does advance to the next line.
procedure, public :: write_line => tw_write_txt_line
generic, public :: write_line => tw_write_txt_line, &
tw_write_txt_line_str

procedure :: tw_write_txt
procedure :: tw_write_txt_str
procedure :: tw_write_txt_line
procedure :: tw_write_txt_line_str
end type

! ------------------------------------------------------------------------------
Expand Down Expand Up @@ -138,7 +144,7 @@ module file_io
bw_append_i64, bw_append_i64_array, bw_append_i64_matrix, &
bw_append_c64, bw_append_c64_array, bw_append_c64_matrix, &
bw_append_c32, bw_append_c32_array, bw_append_c32_matrix, &
bw_append_string
bw_append_char, bw_append_str

procedure :: bw_append_byte
procedure :: bw_append_byte_array
Expand All @@ -163,7 +169,8 @@ module file_io
procedure :: bw_append_c32
procedure :: bw_append_c32_array
procedure :: bw_append_c32_matrix
procedure :: bw_append_string
procedure :: bw_append_char
procedure :: bw_append_str
end type

! ------------------------------------------------------------------------------
Expand Down Expand Up @@ -295,11 +302,23 @@ module subroutine tw_write_txt(this, txt, err)
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine tw_write_txt_str(this, txt, err)
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine tw_write_txt_line(this, txt, err)
class(text_writer), intent(in) :: this
character(len = *), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine tw_write_txt_line_str(this, txt, err)
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine
end interface

! ------------------------------------------------------------------------------
Expand Down Expand Up @@ -533,12 +552,18 @@ module subroutine bw_append_c32_matrix(this, x, err)
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine bw_append_string(this, x, err)
module subroutine bw_append_char(this, x, err)
class(binary_writer), intent(inout) :: this
character(len = *), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine bw_append_str(this, x, err)
class(binary_writer), intent(inout) :: this
class(string), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine

module subroutine bw_clean_up(this)
type(binary_writer), intent(inout) :: this
end subroutine
Expand Down
28 changes: 27 additions & 1 deletion src/file_io_binary.f90
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ module subroutine bw_append_c32_matrix(this, x, err)
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
module subroutine bw_append_string(this, x, err)
module subroutine bw_append_char(this, x, err)
! Arguments
class(binary_writer), intent(inout) :: this
character(len = *), intent(in) :: x
Expand All @@ -910,6 +910,32 @@ module subroutine bw_append_string(this, x, err)
call this%push(buffer, err)
end subroutine

! ------------------------------------------------------------------------------
!> @brief Pushes a character string onto the buffer.
!!
!! @param[in,out] this The binary_writer object.
!! @param[in] x The array to push onto the buffer.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
module subroutine bw_append_str(this, x, err)
! Arguments
class(binary_writer), intent(inout) :: this
class(string), intent(in) :: x
class(errors), intent(inout), optional, target :: err

! Local Variables
integer(int8), allocatable, dimension(:) :: buffer

! Process
buffer = transfer(x, buffer)
call this%push(buffer, err)
end subroutine

! ------------------------------------------------------------------------------
!> @brief Forces a write operation on all buffer contents, closes the file,
!! and performs any necessary clean-up operations.
Expand Down
42 changes: 42 additions & 0 deletions src/file_io_text.f90
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ module subroutine tw_write_txt(this, txt, err)
write(this%get_unit(), '(A)', advance = 'no') txt
end subroutine

! --------------------
!> @brief Writes text to the file, but does not advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt_str(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err

! Process
call this%write(txt%str, err)
end subroutine

! ------------------------------------------------------------------------------
!> @brief Writes text to the file, but does advance to the next line.
!!
Expand Down Expand Up @@ -141,6 +162,27 @@ module subroutine tw_write_txt_line(this, txt, err)
write(this%get_unit(), '(A)') txt
end subroutine

! --------------------
!> @brief Writes text to the file, but does advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt_line_str(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err

! Process
call this%write_line(txt%str, err)
end subroutine

! ******************************************************************************
!> @brief Opens a text file for reading.
!!
Expand Down

0 comments on commit bd7ba86

Please sign in to comment.