home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l196 / 3.ddi / CRLF.BA$ / CRLF.bin
Encoding:
Text File  |  1990-06-24  |  3.8 KB  |  135 lines

  1. DEFINT A-Z             ' Default variable type is integer.
  2.  
  3. ' The Backup$ FUNCTION makes a backup file with
  4. ' the same base as FileName$ plus a .BAK extension:
  5. DECLARE FUNCTION Backup$ (FileName$)
  6.  
  7. ' Initialize symbolic constants and variables:
  8. CONST FALSE = 0, TRUE = NOT FALSE
  9.  
  10. CarReturn$ = CHR$(13)
  11. LineFeed$ = CHR$(10)
  12.  
  13. DO
  14.    CLS
  15.  
  16.    ' Input the name of the file to change:
  17.    INPUT "Which file do you want to convert"; OutFile$
  18.  
  19.    InFile$ = Backup$(OutFile$)  ' Get backup file's name.
  20.  
  21.    ON ERROR GOTO ErrorHandler   ' Turn on error trapping.
  22.  
  23.    NAME OutFile$ AS InFile$     ' Rename input file as
  24.                 ' backup file.
  25.  
  26.    ON ERROR GOTO 0              ' Turn off error trapping.
  27.  
  28.    ' Open backup file for input and old file for output:
  29.    OPEN InFile$ FOR INPUT AS #1
  30.    OPEN OutFile$ FOR OUTPUT AS #2
  31.  
  32.    ' The PrevCarReturn variable is a flag set to TRUE
  33.    ' whenever the program reads a carriage-return character:
  34.    PrevCarReturn = FALSE
  35. ' Read from input file until reaching end of file:
  36.    DO UNTIL EOF(1)
  37.  
  38.       ' This is not end of file, so read a character:
  39.       FileChar$ = INPUT$(1, #1)
  40.  
  41.       SELECT CASE FileChar$
  42.  
  43.      CASE CarReturn$        ' The character is a CR.
  44.  
  45.         ' If the previous character was also a
  46.         ' CR, put a LF before the character:
  47.         IF PrevCarReturn THEN
  48.         FileChar$ = LineFeed$ + FileChar$
  49.         END IF
  50.  
  51.         ' In any case, set the PrevCarReturn
  52.         ' variable to TRUE:
  53.         PrevCarReturn = TRUE
  54.  
  55.      CASE LineFeed$         ' The character is a LF.
  56.  
  57.         ' If the previous character was not a
  58.         ' CR, put a CR before the character:
  59.         IF NOT PrevCarReturn THEN
  60.         FileChar$ = CarReturn$ + FileChar$
  61.         END IF
  62.  
  63.         ' Set the PrevCarReturn variable to FALSE:
  64.         PrevCarReturn = FALSE
  65.  
  66.      CASE ELSE              ' Neither a CR nor a LF.
  67.  
  68.         ' If the previous character was a CR,
  69.         ' set the PrevCarReturn variable to FALSE
  70.         ' and put a LF before the current character:
  71.         IF PrevCarReturn THEN
  72.                PrevCarReturn = FALSE
  73.                FileChar$ = LineFeed$ + FileChar$
  74.         END IF
  75.  
  76.       END SELECT
  77.  
  78.       ' Write the character(s) to the new file:
  79.       PRINT #2, FileChar$;
  80.    LOOP
  81.  
  82.    ' Write a LF if the last character in the file was a CR:
  83.    IF PrevCarReturn THEN PRINT #2, LineFeed$;
  84. CLOSE                        ' Close both files.
  85.    PRINT "Another file (Y/N)?"  ' Prompt to continue.
  86.  
  87.    ' Change the input to uppercase (capital letter):
  88.    More$ = UCASE$(INPUT$(1))
  89.  
  90. ' Continue the program if the user entered a "Y" or a "Y":
  91. LOOP WHILE More$ = "Y"
  92. END
  93.  
  94. ErrorHandler:           ' Error-handling routine
  95.    CONST NOFILE = 53, FILEEXISTS = 58
  96.  
  97.    ' The ERR function returns the error code for last error:
  98.    SELECT CASE ERR
  99.       CASE NOFILE       ' Program couldn't find file
  100.             ' with input name.
  101.  
  102.          PRINT "No such file in current directory."
  103.      INPUT "Enter new name: ", OutFile$
  104.      InFile$ = Backup$(OutFile$)
  105.      RESUME
  106.       CASE FILEEXISTS   ' There is already a file named
  107.             ' <filename>.BAK in this directory:
  108.             ' remove it, then continue.
  109.      KILL InFile$
  110.      RESUME
  111.       CASE ELSE         ' An unanticipated error occurred:
  112.             ' stop the program.
  113.      ON ERROR GOTO 0
  114.    END SELECT
  115.  
  116. ' ======================== BACKUP$ =========================
  117. '   This procedure returns a file name that consists of the
  118. '   base name of the input file (everything before the ".")
  119. '   plus the extension ".BAK"
  120. ' ==========================================================
  121. FUNCTION Backup$ (FileName$) STATIC
  122.  
  123.    ' Look for a period:
  124.    Extension = INSTR(FileName$, ".")
  125.  
  126.    ' If there is a period, add .BAK to the base:
  127.    IF Extension > 0 THEN
  128.       Backup$ = LEFT$(FileName$, Extension - 1) + ".BAK"
  129.    ' Otherwise, add .BAK to the whole name:
  130.    ELSE
  131.       Backup$ = FileName$ + ".BAK"
  132.    END IF
  133. END FUNCTION
  134.  
  135.