PartMC  2.3.0
spec_line.F90
Go to the documentation of this file.
1 ! Copyright (C) 2007-2010 Nicole Riemer and Matthew West
2 ! Licensed under the GNU General Public License version 2 or (at your
3 ! option) any later version. See the file COPYING for details.
4 
5 !> \file
6 !> The pmc_spec_line module.
7 
8 !> A single line of formatted test for input.
10 
11  use pmc_util
12 
13  !> Maximum size of a single line.
14  integer, parameter :: SPEC_LINE_MAX_LEN = 10000
15  !> Maximum size of a variable.
16  integer, parameter :: SPEC_LINE_MAX_VAR_LEN = 300
17 
18  !> A single line of input data, split at whitespace.
19  !!
20  !! Input lines are assumed to be in the format
21  !! <pre>
22  !! # stand-alone comment
23  !! &lt;name&gt; &lt;whitespace&gt; &lt;data1&gt; &lt;whitespace&gt; &lt;data2&gt; ... # optional comment
24  !! </pre>
25  !! An spec_line_t structure stores the name and data split at
26  !! whitespace.
28  !> Variable name.
29  character(len=SPEC_LINE_MAX_VAR_LEN) :: name
30  !> Array of data as strings.
31  character(len=SPEC_LINE_MAX_VAR_LEN), pointer :: data(:)
32  end type spec_line_t
33 
34 contains
35 
36 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37 
38  !> Allocates memory for a spec_line.
39  subroutine spec_line_allocate(spec_line)
40 
41  !> Struct to alloc.
42  type(spec_line_t), intent(out) :: spec_line
43 
44  allocate(spec_line%data(0))
45 
46  end subroutine spec_line_allocate
47 
48 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49 
50  !> Allocates memory for a spec_line of the given size.
51  subroutine spec_line_allocate_size(spec_line, n_data)
52 
53  !> Struct to alloc.
54  type(spec_line_t), intent(out) :: spec_line
55  !> Number of data items.
56  integer, intent(in) :: n_data
57 
58  allocate(spec_line%data(n_data))
59 
60  end subroutine spec_line_allocate_size
61 
62 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
63 
64  !> Frees all storage.
65  subroutine spec_line_deallocate(spec_line)
66 
67  !> Struct to free.
68  type(spec_line_t), intent(inout) :: spec_line
69 
70  deallocate(spec_line%data)
71 
72  end subroutine spec_line_deallocate
73 
74 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
75 
76  !> Copies a spec_line.
77  subroutine spec_line_copy(from_spec_line, to_spec_line)
78 
79  !> Original spec_line.
80  type(spec_line_t), intent(in) :: from_spec_line
81  !> Destination, already alloced.
82  type(spec_line_t), intent(inout) :: to_spec_line
83 
84  if (size(to_spec_line%data) /= size(from_spec_line%data)) then
85  call spec_line_deallocate(to_spec_line)
86  call spec_line_allocate_size(to_spec_line, &
87  size(from_spec_line%data))
88  end if
89  to_spec_line%name = from_spec_line%name
90  to_spec_line%data = from_spec_line%data
91 
92  end subroutine spec_line_copy
93 
94 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
95 
96  !> Strip the comments from a string. Comments are everything after
97  !> the first # character.
98  subroutine spec_line_strip_comment(string)
99 
100  !> Complete input string.
101  character(len=*), intent(inout) :: string
102 
103  integer :: hash_index
104 
105  hash_index = index(string, '#')
106  if (hash_index > 0) then
107  string = string(1:(hash_index - 1))
108  end if
109 
110  end subroutine spec_line_strip_comment
111 
112 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
113 
114  !> Expand all tabs in a string into single spaces (one tab makes one
115  !> space).
116  subroutine spec_line_tabs_to_spaces(string)
117 
118  !> Complete input string.
119  character(len=*), intent(inout) :: string
120 
121  integer :: i
122 
123  do i = 1,len(string)
124  if (ichar(string(i:i)) == 9) then
125  string(i:i) = ' '
126  end if
127  end do
128 
129  end subroutine spec_line_tabs_to_spaces
130 
131 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
132 
133  !> Strip leading spaces from a string.
135 
136  !> Complete input string.
137  character(len=*), intent(inout) :: string
138 
139  integer :: i
140 
141  if (len_trim(string) > 0) then
142  i = verify(string, ' ') ! first character not a space
143  string = string(i:)
144  end if
145 
146  end subroutine spec_line_strip_leading_spaces
147 
148 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
149 
150 end module pmc_spec_line
subroutine spec_line_strip_comment(string)
Strip the comments from a string. Comments are everything after the first # character.
Definition: spec_line.F90:98
subroutine spec_line_strip_leading_spaces(string)
Strip leading spaces from a string.
Definition: spec_line.F90:134
A single line of input data, split at whitespace.
Definition: spec_line.F90:27
subroutine spec_line_copy(from_spec_line, to_spec_line)
Copies a spec_line.
Definition: spec_line.F90:77
Common utility subroutines.
Definition: util.F90:9
subroutine spec_line_tabs_to_spaces(string)
Expand all tabs in a string into single spaces (one tab makes one space).
Definition: spec_line.F90:116
A single line of formatted test for input.
Definition: spec_line.F90:9
subroutine spec_line_allocate_size(spec_line, n_data)
Allocates memory for a spec_line of the given size.
Definition: spec_line.F90:51
subroutine spec_line_allocate(spec_line)
Allocates memory for a spec_line.
Definition: spec_line.F90:39
subroutine spec_line_deallocate(spec_line)
Frees all storage.
Definition: spec_line.F90:65