aboutsummaryrefslogtreecommitdiff
path: root/files.f90
diff options
context:
space:
mode:
authorJeffrey Armstrong <jeff@approximatrix.com>2020-04-24 21:47:09 -0400
committerJeffrey Armstrong <jeff@approximatrix.com>2020-04-24 21:47:09 -0400
commiteb8d9bc74a0624dbc98ffb75268a3ba57bd43b40 (patch)
tree04e77c082b1c87778f06fe53798db6bb284f834c /files.f90
downloadLR-87-eb8d9bc74a0624dbc98ffb75268a3ba57bd43b40.zip
LR-87-eb8d9bc74a0624dbc98ffb75268a3ba57bd43b40.tar.gz
Initial code commit
Diffstat (limited to 'files.f90')
-rw-r--r--files.f90162
1 files changed, 162 insertions, 0 deletions
diff --git a/files.f90 b/files.f90
new file mode 100644
index 0000000..45bc116
--- /dev/null
+++ b/files.f90
@@ -0,0 +1,162 @@
+module file_handling
+
+ integer, parameter::file_type_plain_text = 1
+ integer, parameter::file_type_gemini = 2
+
+contains
+
+ function read_line_text(unit_number, iostatus) result(res)
+ implicit none
+
+ character(len=:), allocatable::res
+ integer, intent(in)::unit_number
+ integer, intent(out)::iostatus
+ integer::startpos, endpos, length, i
+ character::c
+
+ inquire(unit=unit_number, pos=startpos)
+
+ endpos = startpos
+ read(unit_number, '(A1)', advance='no', iostat=iostatus) c
+ do while(c /= CHAR(10) .AND. iostatus == 0)
+ endpos = endpos + 1
+ read(unit_number, '(A1)', advance='no', iostat=iostatus) c
+ end do
+
+ length = (endpos - startpos + 1)
+ print '(A10, I8)', "allocated", length
+
+ allocate(character(len=length) :: res)
+ res = repeat(' ', length)
+
+ read(unit_number, '(A1)', pos=startpos, advance='no', iostat=iostatus) c
+ if(iostatus == 0) then
+ res(1:1) = c
+ do i=2, length
+ read(unit_number, '(A1)', advance='no', iostat=iostatus) c
+
+ if(iostatus /= 0) then
+ exit
+ end if
+
+ res(i:i) = c
+ end do
+ end if
+
+ end function read_line_text
+
+ subroutine process_line(single_line, file_type, preformatted_on)
+ use layout
+ implicit none
+
+ type(line), intent(inout)::single_line
+ integer, intent(in)::file_type
+ logical, intent(inout)::preformatted_on
+
+ integer::line_length
+
+ if(file_type == file_type_plain_text) then
+
+ single_line%line_type = line_type_preformatted
+ preformatted_on = .TRUE.
+
+ else if(file_type == file_type_gemini) then
+
+ line_length = len_trim(single_line%text)
+
+ if(line_length > 2 .AND. single_line%text(1:2) == "=>") then
+
+ single_line%line_type = line_type_link
+ single_line%text(1:2) = " "
+ single_line%text = adjustl(single_line%text)
+
+ else if(line_length >= 3 .AND. single_line%text(1:3) == "```") then
+
+ preformatted_on = .not. preformatted_on
+ single_line%line_type = line_type_indicator
+
+ else if(preformatted_on) then
+
+ single_line%line_type = line_type_preformatted
+
+ else
+
+ single_line%line_type = line_type_text
+
+ end if
+
+ end if
+
+ end subroutine process_line
+
+ function load_unit(unit_number, file_type) result(first_line)
+ use layout
+ implicit none
+
+ integer, intent(in)::unit_number, file_type
+ type(line), pointer::first_line
+
+ type(line), pointer::walker, next_line
+ character::c
+ integer::iostatus
+ logical::preformatted_on
+
+ preformatted_on = .FALSE.
+
+ allocate(first_line)
+
+ first_line%text = read_line_text(unit_number, iostatus)
+ first_line%next => null()
+
+ walker=>first_line
+
+ call process_line(walker, file_type, preformatted_on)
+
+ do while(iostatus /= -1) ! -1 should be end of file
+
+ allocate(next_line)
+ next_line%next => null()
+
+ walker%next => next_line
+ next_line => null()
+ walker => walker%next
+
+ walker%text = read_line_text(unit_number, iostatus)
+ print *, walker%text
+ call process_line(walker, file_type, preformatted_on)
+
+ end do
+
+ end function load_unit
+
+ function load_filename(filename, iostatus, mimetype) result(first_line)
+ use layout, only: line
+ implicit none
+
+ character(*), intent(in)::filename
+ integer, intent(out)::iostatus
+ character(*), intent(in), optional::mimetype
+
+ type(line), pointer::first_line
+
+ integer::filetype
+
+ open(unit=1097, file=filename, access='stream', iostat=iostatus, form='FORMATTED')
+ if(iostatus == 0) then
+
+ if(present(mimetype)) then
+ if(trim(mimetype) == 'text/gemini') then
+ filetype = file_type_gemini
+ else
+ filetype = file_type_plain_text
+ end if
+ else
+ filetype = file_type_plain_text
+ end if
+
+ first_line => load_unit(1097, filetype)
+ end if
+
+ end function load_filename
+
+end module file_handling \ No newline at end of file