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