From cfcd98a239833631a6fa70d989422c88f7cbcebd Mon Sep 17 00:00:00 2001 From: rasmus Date: Fri, 24 Jan 2025 15:00:10 +0100 Subject: [PATCH] Add prototype for reading files --- app/main.f90 | 4 ++++ html/index.html | 25 ++++++++++++++++++++++++ src/FileReader.f90 | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 html/index.html create mode 100644 src/FileReader.f90 diff --git a/app/main.f90 b/app/main.f90 index 4f5a644..db5aae1 100644 --- a/app/main.f90 +++ b/app/main.f90 @@ -1,9 +1,13 @@ program main use WebServer, only: get_progress + use FileReader, only: read_file use Greeter, only: say_hello implicit none + character, allocatable :: Data + call say_hello() call get_progress() + call read_file("/home/skingging/Documents/Projects/Fortran/Web-Server-In-Fortran/html/index.html", Data) end program main diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..ea4f298 --- /dev/null +++ b/html/index.html @@ -0,0 +1,25 @@ + + + + + + + + + Hello, world! + + + + + + + + + + + +

Hello, world!

+ + + + diff --git a/src/FileReader.f90 b/src/FileReader.f90 new file mode 100644 index 0000000..f8c2415 --- /dev/null +++ b/src/FileReader.f90 @@ -0,0 +1,48 @@ +module FileReader + !implicit none + + public :: read_file + contains + subroutine read_file (FileName, Data) + integer :: io + integer :: LineLength, iostat + integer :: TotalLength + logical :: res + character (len=*), intent (in) :: FileName + character (len=*), allocatable, intent (out) :: Data + character(len=:), allocatable :: TempData + character(len=256) :: LineBuffer + + inquire( file=trim(FileName), exist=res ) + + if (.not. res) then + print *, "File does not exist." + return + end if + + open(newunit=io, file=trim(FileName), status="old", action="read") + + TotalLength = 0 + + allocate(character(len=0) :: TempData) ! Start with an empty string + + read(io, *, iostat=iostat) + + do + read(io, *, IOSTAT=iostat) LineBuffer + if (iostat /= 0) exit ! Exit loop on end-of-file or error + + ! Append the current line to TempData. CHAR(10) is a newline + TempData = TempData // LineBuffer // CHAR(10) + end do + + ! Allocate Data to match the length of TempData and copy it over + allocate(character(len=len(TempData)) :: Data) + Data = TempData + + print *, TempData + + close(io) + + end subroutine read_file +end module FileReader \ No newline at end of file