-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge_1_Intermediate.f
More file actions
350 lines (298 loc) · 10.5 KB
/
Challenge_1_Intermediate.f
File metadata and controls
350 lines (298 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
!> Program global constants
blockdata globals
implicit none
integer RECORD_SIZE
character*(9) RECORD_COUNT_FMT,RECORD_FMT
common /RECORD_COUNT_FMT/RECORD_COUNT_FMT
common /RECORD_FMT/RECORD_FMT
common /RECORD_SIZE/RECORD_SIZE
data RECORD_SIZE/30/
data RECORD_COUNT_FMT/'(I10,20X)'/
data RECORD_FMT/'(A20,I10)'/
end
!> Transform a character from lowercase to uppercase
!! Clears the 6th bit of a character to make it uppercase
!! @param[in] c Character to transform
!! @return Returns the uppercase character
character function up_case(c)
implicit none
character c
if(c.ge.'a'.and.c.le.'z')then
up_case = CHAR(IBCLR(ICHAR(c), 5))
else
up_case = c
endif
end
!> Checks if returned status code from a read/write is end-of-file
!! @param[in] status IOSTAT status
!! @return Returns if the status is end-of-file
logical function is_eof(status)
implicit none
integer status
is_eof = status.lt.0
end
!> Checks if returned status code from a read/write is an error
!! @param[in] status IOSTAT status
!! @return Returns if the status is an error
logical function is_error(status)
implicit none
integer status
is_error = status.gt.0
end
!> Checks if returned status code from a read/write is ok
!! @param[in] status IOSTAT status
!! @return Returns if the status is ok
logical function is_ok(status)
implicit none
integer status
is_ok = status.eq.0
end
!> Reads the number of records from database
!! @return Returns true if no error
logical function db_read_record_count()
implicit none
integer unit,record_count,status
common /database/ unit,record_count
logical db_write_record_count,is_eof,is_error,is_ok
character*(9) FMT
common /RECORD_COUNT_FMT/FMT
read(unit,FMT,IOSTAT=status,REC=1) record_count
if(is_eof(status)) then
record_count=0
if(.not.db_write_record_count()) then
print *,'FATAL ERROR: cannot access database'
stop
endif
status = 0
endif
db_read_record_count = is_ok(status)
if (is_error(status)) then
print *,'FATAL ERROR: cannot read record count from database'
stop
endif
end
!> Writes the number of records to database
!! @return Returns true if no error
logical function db_write_record_count()
implicit none
integer unit,record_count,status
common /database/ unit, record_count
logical is_ok,is_error
character*(9) FMT
common /RECORD_COUNT_FMT/ FMT
write(unit,FMT,IOSTAT=status,REC=1) record_count
db_write_record_count = is_ok(status)
if (is_error(status)) then
print *,'ERROR writing record count to database'
return
endif
end
!> Reads the record data from database
!! @param[out] name The name
!! @param[out] hour The hour
!! @param[in] position The position to read the record
!! @return Returns true if no error
logical function db_read_record(name, hour, position)
implicit none
character*(*) name
integer hour,position,status,unit,record_count
common /database/ unit, record_count
logical is_ok,is_error
character*(9) FMT
common /RECORD_FMT/FMT
read(unit,FMT,IOSTAT=status,REC=position + 1) name, hour
db_read_record = is_ok(status)
if (is_error(status)) then
print *,'ERROR reading record ',position,' from database'
return
endif
end
!> Writes the record data to database
!! @param[in] name The name
!! @param[in] hour The hour
!! @param[in] position The position to write the record
!! @return Returns true if no error
logical function db_write_record(name, hour, position)
implicit none
character*(*) name
integer hour,position,unit,record_count,status
common /database/ unit, record_count
logical is_ok,is_error
character*(9) FMT
common /RECORD_FMT/FMT
write(unit,FMT,IOSTAT=status,REC=position + 1) name,hour
db_write_record = is_ok(status)
if (is_error(status)) then
print *,'ERROR writing record ',position,' to database'
return
endif
end
!> Lists all records
subroutine do_list()
implicit none
integer unit,record_count
common /database/ unit, record_count
character*(100) name_buffer
integer hour
logical db_read_record
integer position
do position=1,record_count
if(.not.db_read_record(name_buffer, hour, position))return
print '(I3,A,A20,A,I10)',
_ position, '. ', name_buffer, ' -> ', hour
enddo
print *,'Records read: ', record_count
end
!> Asks user the record data
!! @param[out] name The name
!! @param[out] hour The hour
subroutine input_record(name, hour)
implicit none
character*(*) name
integer hour
print '(A$)', 'Event name: '
read *,name
do
print '(A$)', 'Event hour: '
read *,hour
if(hour.ge.0.and.hour.le.24) exit
print *, 'ERROR: Invalid hour'
enddo
end
!> Asks user a record position
!! @param[out] position Record position
!! @return Returns if the position is valid
logical function input_position(position)
implicit none
integer unit
integer record_count
common /database/ unit, record_count
integer position
print '(A$)', 'Enter record #: '
read *,position
input_position = position.ge.1.and.position.le.record_count
if(.not.input_position)then
print *, 'ERROR: invalid record #'
endif
end
!> Asks user and commits record data
!! @param[in] position The record's position
!! @return Returns true if no error
logical function asks_and_write_user_input(position)
implicit none
integer position
character*(100) name_buffer
integer hour
logical db_write_record
call input_record(name_buffer, hour)
asks_and_write_user_input = db_write_record(name_buffer, hour,
_ position)
end
!> Adds a new record at the end of database
subroutine do_add()
implicit none
integer unit,record_count
common /database/ unit, record_count
logical asks_and_write_user_input, db_write_record_count
if(.not.asks_and_write_user_input(record_count + 1)) then
print *,'ERROR writing record to database'
return
endif
record_count = record_count + 1
if(.not.db_write_record_count()) return
end
!> Edits a record
subroutine do_edit()
implicit none
integer unit
integer record_count
common /database/ unit, record_count
integer position
logical asks_and_write_user_input, input_position
if(.not.input_position(position)) return
if(.not.asks_and_write_user_input(position)) then
print *,'ERROR writing record to database'
return
endif
end
!> Deletes a record
subroutine do_delete()
implicit none
integer unit,record_count
common /database/ unit, record_count
integer position,i
character*(100) name_buffer
integer hour
logical db_read_record,db_write_record,db_write_record_count
logical input_position
if(.not.input_position(position)) return
do i=position, record_count - 1
if(.not.db_read_record(name_buffer, hour, position + 1))return
if(.not.db_write_record(name_buffer, hour, position)) return
enddo
name_buffer=' '
hour=-1
if(.not.db_write_record(name_buffer, hour, record_count)) return
record_count = record_count - 1
if(.not.db_write_record_count()) return
end
!> Opens the database
subroutine db_open()
implicit none
character*(*) DATABASE_FILE
parameter(DATABASE_FILE='Challenge_1_Intermediate.db')
integer unit,record_count
common /database/ unit, record_count
logical db_read_record_count
data unit/10/
integer RECORD_SIZE
common /RECORD_SIZE/RECORD_SIZE
open(unit, FILE=DATABASE_FILE, ACCESS='DIRECT',
_ FORM='FORMATTED', RECL=RECORD_SIZE)
if(.not.db_read_record_count()) stop
end
!> Closes the database
subroutine db_close()
implicit none
integer unit
integer record_count
common /database/ unit, record_count
close(unit)
end
!> http://www.reddit.com/r/dailyprogrammer/comments/pihtx/intermediate_challenge_1/
program intermediate_1
implicit none
character key, up_case
integer*1 key_code
integer*1 LIST, ADD, EDIT, DEL, QUIT
parameter(LIST=X'4C',ADD=X'41',EDIT=X'45',DEL=X'44',QUIT=X'51')
call db_open()
do
print '(A)', 'L. List events;'
print '(A)', 'A. Add event;'
print '(A)', 'E. Edit event;'
print '(A)', 'D. Delete events;'
print '(A)', 'Q. Quit.'
print '(A$)','Select an option (LAEDQ): '
read *, key
key = up_case(key)
key_code = ICHAR(key)
select case (key_code)
case (LIST)
call do_list()
case (ADD)
call do_add()
case (EDIT)
call do_edit()
case (DEL)
call do_delete()
case (QUIT)
exit
case default
print '(A,A)', key, ' is not a valid option!'
end select
enddo
call db_close()
print '(A$)','Press ENTER to exit.'
read *
end