Add prototype for reading files

This commit is contained in:
Rasmus Rasmussen 2025-01-24 15:00:10 +01:00
parent f47f2e738f
commit cfcd98a239
3 changed files with 77 additions and 0 deletions

View File

@ -1,9 +1,13 @@
program main program main
use WebServer, only: get_progress use WebServer, only: get_progress
use FileReader, only: read_file
use Greeter, only: say_hello use Greeter, only: say_hello
implicit none implicit none
character, allocatable :: Data
call say_hello() call say_hello()
call get_progress() call get_progress()
call read_file("/home/skingging/Documents/Projects/Fortran/Web-Server-In-Fortran/html/index.html", Data)
end program main end program main

25
html/index.html Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Hello, world!</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="" />
<link rel="icon" href="favicon.png">
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>

48
src/FileReader.f90 Normal file
View File

@ -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