Advertisement
Guest User

Untitled

a guest
Dec 13th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.63 KB | None | 0 0
  1. program read_input
  2.   ! Maximilian Kubillus, 2013
  3.   ! Reads a file, removes all comments and trims the trailing whitespace
  4.   ! into a new file. If you want a variable or simple output instead:
  5.   ! Change 'write(write_id,*) trim(buffer)' in line 54 accordingly.
  6.   implicit none
  7.  
  8.   ! Variable for the file identifier number
  9.   integer :: read_id, write_id
  10.   ! IO-status for open() and read()
  11.   integer :: read_ios, read_status, write_ios
  12.   ! Index variables for loops
  13.   integer :: ii, jj
  14.   ! Line buffer for the file
  15.   character(len=1024) :: buffer
  16.   ! Line with dynamic length (so trailing spaces are cut)
  17.   character :: line
  18.   ! Comment symbol
  19.   character :: comment
  20.   ! Filenames to read and write
  21.   character(len=:), allocatable :: in_file, out_file
  22.   ! Format variable for buffer
  23.   character(len=*), parameter :: lineformat = '(A1024)'
  24.  
  25.   ! Set up some variables. You might want to change these!
  26.   comment = '#'
  27.   in_file = 'dummy.in'
  28.   out_file = 'dummy.parsed'
  29.  
  30.   ! This numbers are completely arbitrary... ;-)
  31.   read_id = 42
  32.   write_id = 43
  33.   open(read_id, file=in_file, form='formatted', iostat=read_ios, action='read')
  34.   open(write_id, file=out_file, iostat=write_ios, action='write')
  35.  
  36.   if(read_ios == 0) then
  37.      do
  38.         read(read_id, fmt=lineformat, iostat=read_status) buffer
  39.         ! If we are at the end of the line, we exit the loop.
  40.         if(read_status /= 0) exit
  41.         ! Here we check if the line contains a comment.
  42.         ii = index(buffer, comment)
  43.         if(ii == 0) then
  44.            ! If there is no comment in the line, we align the line to the left.
  45.            buffer = adjustl(buffer)
  46.         else
  47.            ! We cut out the comment part of the line if one exists and
  48.            ! then align to the left again.
  49.            buffer = adjustl(buffer(1:ii-1))
  50.         endif
  51.         ! Now, if the length of a buffer (w/o trailing whitespace) is 0, we
  52.         ! won't print it.
  53.        if(len(trim(buffer)) /= 0) then
  54.           ! Check if opening the output file was successful
  55.           if(write_ios == 0) then
  56.              write (write_id,*) trim(buffer)
  57.              ! If you want to print the output
  58.              ! into the terminal, uncomment this:
  59.              ! write (*,*) trim(buffer)
  60.           else
  61.              STOP 'IOError: Problem opening output file!'
  62.           endif
  63.        endif
  64.     enddo
  65.  else if(read_ios < 0) then
  66.     ! If we are at the end of the file, we stop the program
  67.     STOP
  68.  else
  69.     ! If the ios is something > 0 we exit with an error message.
  70.     STOP 'IOError: Could not open input file!'
  71.  endif
  72.  
  73.  close(read_id)
  74.  
  75. end program read_input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement