Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for crlf.rex

(view source code of crlf.rex as plain text)

  1. /* CrLf,  Version 2.10                                              */
  2. /* Replace orphaned carriage returns and linefeeds with CRLF pairs  */
  3. /* Written by Rob van der Woude                                     */
  4. /* http://www.robvanderwoude.com                                    */
  5. /* Original version:          March     7, 1998                     */
  6. /* Latest update:             February 27, 2003                     */
  7.  
  8. /* Initialize RexxUtil */
  9. call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
  10. call sysloadfuncs
  11.  
  12. /* Check if file to be converted does exist */
  13. parse upper arg filespec outfile .
  14. if filespec = "" | filespec = "/?" then call Syntax
  15. call SysFileTree filespec, "dir.", "F", , "**---"
  16. parse value dir.1 with . . filesize . filename
  17. filename = translate( strip( filename ) )
  18. filesize = filesize + 0
  19.  
  20. /* Check if output file exists */
  21. if outfile = "" then outfile = filename||".CLP"
  22. call SysFileTree outfile, "exist.", "F"
  23.  
  24. /* Generate error message if necessary */
  25. select
  26. 	when dir.0 = 0 then do
  27. 		msg = "File "||filespec( "N", filename )||" not found"
  28. 		call ErrorMessage
  29. 	end
  30. 	when dir.0 > 1 then do
  31. 		msg = "Wildcards not allowed"
  32. 		call ErrorMessage
  33. 	end
  34. 	when exist.0 <> 0 then do
  35. 		msg = "Output file "||filespec( "N", outfile )||,
  36. 		      " already exists"
  37. 		call ErrorMessage
  38. 	end
  39. 	otherwise nop
  40. end
  41.  
  42. /* Read file */
  43. say "Reading file . . ."
  44. filestring = charin( filename, 1, filesize )
  45. call stream filename, "C", "CLOSE"
  46.  
  47. /* Convert CRLF pairs to CR only */
  48. say "Converting CRLF pairs to CR only . . ."
  49. do forever
  50. 	charpos    = pos( "0D0A"X, filestring )
  51. 	if charpos = 0 then leave
  52. 	filestring = substr( filestring, 1, charpos )||,
  53. 	             substr( filestring, charpos + 2 )
  54. end
  55.  
  56. /* Translate CR to LF */
  57. say "Converting CR to LF . . ."
  58. filestring = translate( filestring, "0A"X, "0D"X )
  59.  
  60. /* Replace LF with CRLF */
  61. say "Converting LF to CRLF pairs . . ."
  62. startpos = 1
  63. do forever
  64. 	charpos    = pos( "0A"X, filestring, startpos )
  65. 	if charpos = 0 then leave
  66. 	startpos   = charpos + 2
  67. 	filestring = substr( filestring, 1, charpos - 1 )||"0D"X||,
  68. 	             substr( filestring, charpos )
  69. end
  70.  
  71. /* Write result to new file */
  72. say "Writing result to file . . ."
  73. rc = charout( outfile, filestring )
  74. call stream outfile, "C", "CLOSE"
  75. if rc > 0 then do
  76. 	msg = "Error writing output to file "||filespec( "N", outfile )
  77. 	call ErrorMessage
  78. end
  79. say "The file "||filespec( "N", filename )||,
  80.     " was successfully converted"
  81. say "and saved as "||filespec( "N", outfile )
  82.  
  83. /* End of main program */
  84. EXIT 0
  85.  
  86.  
  87.  
  88. ErrorMessage: procedure expose msg
  89. 	call SysCls
  90. 	call beep 220, 240
  91. 	say
  92. 	say msg
  93. 	say
  94. 	Exit 1
  95. Return
  96.  
  97.  
  98. Syntax: procedure expose msg
  99. 	call SysCls
  100. 	say
  101. 	say "CRLF, Version 2.10"
  102. 	say "Replaces orphaned carriage returns (CR, 0Dh) and line feeds (LF, 0Ah) with"
  103. 	Say "CRLF pairs (0D0Ah)"
  104. 	say
  105. 	say "Usage:  CRLF infile [ outfile ]"
  106. 	say
  107. 	say 'Where:  "infile"  is the ASCII file with improper line terminations'
  108. 	say '        "outfile" is the corrected ASCII file (default: infile.CLP)'
  109. 	say
  110. 	say "Written by Rob van der Woude"
  111. 	say "http://www.robvanderwoude.com"
  112. 	Exit 1
  113. Return
  114.  

page last uploaded: 2017-08-21, 14:26