/samples/altkey/altkey.cob

https://gitlab.com/iladin/open-cobol-contrib · COBOL · 352 lines · 266 code · 62 blank · 24 comment · 2 complexity · cc446831c832ec4a431cb664a5eafe7e MD5 · raw file

  1. Identification division.
  2. Program-id. altkey.
  3. *> altkey * Alternate Key Demo.
  4. *> Published under GNU General Public License.
  5. Environment division.
  6. Configuration section.
  7. Source-computer. GNU-Cobol.
  8. Object-computer. GNU-Cobol.
  9. Special-names.
  10. * Display x upon std-out, to get 1>file1.txt.
  11. console is std-out
  12. * Display x upon std-err, to get 2>file2.txt
  13. syserr is std-err.
  14. input-output section.
  15. file-control.
  16. Select iftest
  17. assign to "./iftest"
  18. organization is indexed
  19. access mode is dynamic
  20. record key is iftest-key
  21. alternate key is iftest-alternate
  22. with duplicates
  23. suppress when space
  24. file status is ws-file-status.
  25. Data division.
  26. file section.
  27. fd iftest.
  28. 01 iftest-record.
  29. 02 iftest-key pic x(10).
  30. 02 pic x(05).
  31. 02 iftest-alternate pic x(10).
  32. 02 pic x(05).
  33. Working-storage section.
  34. 01 display-message pic x(80).
  35. 01 flag-done pic x(01).
  36. 88 flag-done-no value low-value.
  37. 88 flag-done-yes value high-value.
  38. 01 flag-eof pic x(01).
  39. 88 flag-eof-no value low-value.
  40. 88 flag-eof-yes value high-value.
  41. 01 flag-rewrite pic x(01).
  42. 88 flag-rewrite-no value low-value.
  43. 88 flag-rewrite-yes value high-value.
  44. 01 flag-suppressed pic x(01) value high-value.
  45. 88 flag-suppressed-no value low-value.
  46. 88 flag-suppressed-yes value high-value.
  47. 01 hold-record pic x(30).
  48. 01 table-messages.
  49. 02 table-messages-defined value
  50. " "
  51. & " This program shows how alternate keys are used."
  52. & "The first field in the record is the primary key, "
  53. & "and the second is the alternate key. "
  54. & " "
  55. & " Blank alternate keys do not appear in the read "
  56. & "loop because of the 'suppress when spaces' clause."
  57. & "As an exercise, comment out the clause and see "
  58. & "the difference when the program is run again. "
  59. & " "
  60. & "1) The file is opened output, to create it. "
  61. & "2) Six sample records are written, some with "
  62. & " blank alternate keys. "
  63. & "3) The file is opened i-o, for read, rewrite, "
  64. & " and delete functions. "
  65. & "4) Record 6, with a blank key, is rewritten with "
  66. & " non blank key. "
  67. & "5) The file is started on the alternate key, "
  68. & " so that read next statements read by the "
  69. & " alternate key and not by the primary key. "
  70. & "6) The records are read until there are no more "
  71. & " alternate keys. "
  72. & "7) After all of the records are read, the first "
  73. & " non blank alternate key record is either "
  74. & " rewritten with a blank alternate key or "
  75. & " deleted. "
  76. & "8) The file is started again in step 5) and "
  77. & " the process repeated until there are no "
  78. & " more records with non blank alternate keys. "
  79. & " ".
  80. 02 table-message redefines table-messages-defined
  81. pic x(50) occurs 30 times.
  82. 01 end-table pic 9(04) binary value 30.
  83. 01 sub-table pic 9(04) binary.
  84. 01 ws-file-status pic x(02).
  85. 01 ws-operation pic x(15).
  86. 01 ws-record pic x(30).
  87. Procedure division.
  88. ** Display the table messages.
  89. Perform dm-display-message thru dm-exit
  90. varying sub-table
  91. from 1
  92. by 1
  93. until sub-table is greater than end-table.
  94. ** Open output to create the indexed file.
  95. Open output iftest.
  96. Move "open output" to ws-operation.
  97. Move space to ws-record.
  98. Perform sfo-show-file-operation thru sfo-exit.
  99. if ws-file-status (1:1) not = '0'
  100. goback
  101. end-if
  102. ** Write 6 records, 1, 3, 5, and 6 have space alternate key.
  103. Move "write" to ws-operation.
  104. Move space to iftest-record.
  105. Move "key-1" to iftest-key.
  106. Move space to iftest-alternate.
  107. Move iftest-record to ws-record.
  108. Write iftest-record end-write.
  109. Perform sfo-show-file-operation thru sfo-exit.
  110. Move space to iftest-record.
  111. Move "key-2" to iftest-key.
  112. Move "key-2" to iftest-alternate.
  113. Move iftest-record to ws-record.
  114. Write iftest-record end-write.
  115. Perform sfo-show-file-operation thru sfo-exit.
  116. Move space to iftest-record.
  117. Move "key-3" to iftest-key.
  118. Move space to iftest-alternate.
  119. Move iftest-record to ws-record.
  120. Write iftest-record end-write.
  121. Perform sfo-show-file-operation thru sfo-exit.
  122. Move space to iftest-record.
  123. Move "key-4" to iftest-key.
  124. Move "key-4" to iftest-alternate.
  125. Move iftest-record to ws-record.
  126. Write iftest-record end-write.
  127. Perform sfo-show-file-operation thru sfo-exit.
  128. Move space to iftest-record.
  129. Move "key-5" to iftest-key.
  130. Move space to iftest-alternate.
  131. Move iftest-record to ws-record.
  132. Write iftest-record end-write.
  133. Perform sfo-show-file-operation thru sfo-exit.
  134. Move space to iftest-record.
  135. Move "key-6" to iftest-key.
  136. Move space to iftest-alternate.
  137. Move iftest-record to ws-record.
  138. Write iftest-record end-write.
  139. Perform sfo-show-file-operation thru sfo-exit.
  140. ** Open I/O.
  141. Close iftest.
  142. Move "close" to ws-operation.
  143. Move space to ws-record.
  144. Perform sfo-show-file-operation thru sfo-exit.
  145. Open i-o iftest.
  146. Move "open i-o" to ws-operation.
  147. Move space to ws-record.
  148. Perform sfo-show-file-operation thru sfo-exit.
  149. ** Test rewrite of blank alternate key to non-blank.
  150. Move space to iftest-record.
  151. Move "key-6" to iftest-key.
  152. Move "key-6" to iftest-alternate.
  153. Move iftest-record to ws-record.
  154. Rewrite iftest-record end-rewrite.
  155. Move "rewrite" to ws-operation.
  156. Perform sfo-show-file-operation thru sfo-exit.
  157. ** Read loop, with a rewrite or delete.
  158. Set flag-rewrite-yes to true.
  159. Set flag-done-no to true.
  160. Perform 1-loop thru 1-exit
  161. until flag-done-yes.
  162. ** Finish.
  163. Close iftest.
  164. Move "close" to ws-operation.
  165. Move space to ws-record.
  166. Perform sfo-show-file-operation thru sfo-exit.
  167. Move space to display-message.
  168. Perform sm-show-message thru sm-exit.
  169. If flag-suppressed-no
  170. Move
  171. "* Blank alternate keys are not suppressed "
  172. & "in this run."
  173. to display-message
  174. Perform sm-show-message thru sm-exit
  175. else
  176. Move
  177. "* Blank alternate keys are suppressed in this run."
  178. to display-message
  179. Perform sm-show-message thru sm-exit
  180. end-if.
  181. Move space to display-message.
  182. Perform sm-show-message thru sm-exit.
  183. Goback.
  184. 1-loop.
  185. ** Start the file on the first alternate key.
  186. Move low-values to iftest-alternate.
  187. Start iftest
  188. key not less than iftest-alternate
  189. invalid key Continue
  190. end-start.
  191. Move "start alternate" to ws-operation.
  192. Move space to ws-record.
  193. Perform sfo-show-file-operation thru sfo-exit.
  194. If ws-file-status is equal to "23"
  195. Set flag-done-yes to true
  196. Go to 1-exit
  197. end-if.
  198. ** Read next and show records.
  199. Move high-value to hold-record.
  200. Set flag-eof-no to true.
  201. Perform 11-report thru 11-exit
  202. until flag-eof-yes.
  203. ** Exit if all alternate keys are blank.
  204. If hold-record is equal to high-value
  205. Set flag-done-yes to true
  206. Go to 1-exit
  207. end-if.
  208. ** Blank out rewrite or delete the first non-blank alternate key.
  209. Move hold-record to iftest-record.
  210. If flag-rewrite-yes
  211. * Rewrite, blanking out non-blank alternate key.
  212. Move space to iftest-alternate
  213. Move iftest-record to ws-record
  214. Rewrite iftest-record
  215. invalid key Continue
  216. end-rewrite
  217. Move "rewrite" to ws-operation
  218. Set flag-rewrite-no to true
  219. else
  220. * Delete with non-blank alternate key.
  221. Move iftest-record to ws-record
  222. Delete iftest record
  223. end-delete
  224. Move "delete" to ws-operation
  225. Set flag-rewrite-yes to true
  226. end-if.
  227. Perform sfo-show-file-operation thru sfo-exit.
  228. 1-exit.
  229. Exit.
  230. 11-report.
  231. ** Read next.
  232. Move space to iftest-record.
  233. Read iftest
  234. next record
  235. at end Continue
  236. end-read.
  237. Move "read next" to ws-operation.
  238. Move iftest-record to ws-record.
  239. Perform sfo-show-file-operation thru sfo-exit.
  240. If ws-file-status is equal to "10"
  241. Set flag-eof-yes to true
  242. Go to 11-exit
  243. end-if.
  244. ** Check if read next alternate key has blank keys.
  245. If iftest-alternate is equal to space
  246. Set flag-suppressed-no to true
  247. Go to 11-exit
  248. end-if.
  249. ** Save first record for rewrite or delete.
  250. If hold-record is equal to high-value
  251. Move iftest-record to hold-record
  252. end-if.
  253. 11-exit.
  254. Exit.
  255. ** Display a table message line.
  256. dm-display-message.
  257. Move table-message (sub-table)
  258. to display-message.
  259. Perform sm-show-message thru sm-exit.
  260. dm-exit.
  261. Exit.
  262. ** Display the file operation and output.
  263. sfo-show-file-operation.
  264. Move space to display-message.
  265. String
  266. "Operation "
  267. delimited by size
  268. ws-operation
  269. delimited by size
  270. " Status "
  271. delimited by size
  272. ws-file-status
  273. delimited by size
  274. " Record "
  275. delimited by size
  276. ws-record
  277. delimited by size
  278. into display-message
  279. end-string.
  280. Perform sm-show-message thru sm-exit.
  281. sfo-exit.
  282. Exit.
  283. ** General display message.
  284. sm-show-message.
  285. Display display-message
  286. upon std-out
  287. end-display.
  288. sm-exit.
  289. Exit.
  290. * end of file.