home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / maxlib10 / detab.bas < prev    next >
BASIC Source File  |  1994-10-31  |  7KB  |  264 lines

  1. $IF 0  '=================================================================
  2.  
  3.  DETAB.BAS - A sample program showing how to use MAXLIB For PB.
  4.              Written by Brian McLaughlin.  Released to public domain.
  5.  
  6.  
  7.  This is a simple utility for detabbing text files.  You include the
  8.  name of the file to be detabbed on the command line, like so:
  9.  
  10.  
  11.       C:> DETAB \WORD\MYNAME\MYFILE.TXT
  12.  
  13.  
  14.  The original file keeps it's original name.  The detabbed output
  15.  file is given the extension "DTB".  (If the original file had a
  16.  "DTB" extension, the detabbed output is given the extension "$$$".)
  17.  
  18.  DETAB.BAS assumes that tab stops are set every 8 spaces.  You can
  19.  easily change that assumption.  DETAB also trims any trailing
  20.  spaces from the end of each line.
  21.  
  22.  DETAB.BAS could be improved in many ways to make it more useful.  I
  23.  will leave that to you.  It does demonstrate how to open a file,
  24.  read it, close it and do some rudimentary error handling.  It also
  25.  shows how to write to STDOUT as a DOS device.
  26.  
  27.  Before you compile DETAB.BAS, you must either place MAXLIB.PBL
  28.  and MAXLIB.BI into the current directory, or change the $LINK and
  29.  $INCLUDE statements to reflect their whereabouts.
  30.  
  31.  
  32. $ENDIF '==================================================================
  33.  
  34.  
  35. $LIB ALL OFF
  36. $LINK ".\MAXLIB.PBL"          ' <-- assumes file is in current directory
  37. $INCLUDE ".\MAXLIB.BI"        ' <-- assumes file is in current directory
  38.  
  39. DECLARE FUNCTION GetFileName$ ()
  40. DECLARE FUNCTION MakeOutName$ (FileName$)
  41. DECLARE SUB Detab (InFile$, OutFile$, TabWidth%)
  42. DECLARE FUNCTION AllTrim$ (Target$)
  43. DECLARE SUB HandleError ()
  44.  
  45. %TRUE = -1
  46. %FALSE = 0
  47. %STDOUT = 1
  48.  
  49.  
  50. '/////////////////////// START OF MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\
  51.  
  52.  InitMAXFiles                         ' required!
  53.  SetDiskFile %FALSE                   ' make this value explicit
  54.  
  55.  TabFile$ = GetFileName$              ' name returned as upper case
  56.  OutName$ = MakeOutName$(TabFile$)    ' name returned as upper case
  57.  
  58.  Report "Detabbing: " + TabFile$
  59.  
  60.  TabWidth% = 8                        ' set a default value for tab width
  61.  Detab TabFile$, OutName$, TabWidth%
  62.  
  63.  Display$ =  TabFile$ + " was successfully detabbed."
  64.  Report Display$
  65.  
  66.  Display$ =  "The detabbed output file is: " + OutName$
  67.  Report Display$
  68.  
  69. END
  70.  
  71. '///////////////////////// END OF MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  72.  
  73.  
  74. '==========================
  75.  FUNCTION GetFileName$
  76. '==========================
  77.  Cmd$ = AllTrim$(COMMAND$)
  78.  
  79.    IF LEN(Cmd$) THEN
  80.      IF LEN(DIR$(Cmd$)) THEN
  81.         InFile$ = UCASE$(Cmd$)
  82.      ELSE
  83.         Report "Can't find a file named: " + Cmd$
  84.         END
  85.      END IF
  86.    ELSE
  87.      Help$ = "To detab a file use:   DETAB [path\]filename"
  88.      Report Help$
  89.      END
  90.    END IF
  91.  
  92.    GetFileName$ = InFile$      'name returned as upper case
  93.  
  94. END FUNCTION
  95.  
  96.  
  97. '==================================
  98.  FUNCTION MakeOutName$ (FileName$)
  99. '==================================
  100.  
  101.  OutExtension$ = "DTB"
  102.  Dot% = INSTR(FileName$, ".")       'look for file extension
  103.  
  104.  IF Dot% THEN
  105.    IF INSTR(FileName$, OutExtension$) = (Dot% + 1) THEN OutExtension$ = "$$$"
  106.    OutFile$ = UCASE$(LEFT$(FileName$, Dot%)) + OutExtension$
  107.  ELSE
  108.    OutFile$ = UCASE$(FileName$) + OutExtension$
  109.  END IF
  110.  
  111.  MakeOutName$ = OutFile$
  112.  
  113. END FUNCTION
  114.  
  115.  
  116. '==========================================================
  117.  SUB Detab (InFile$, OutFile$, TabWidth%)
  118. '==========================================================
  119.  
  120.  InHandle% = OpenX%(InFile$)
  121.    IF ErrorCode% THEN HandleError
  122.  
  123.  IF LEN(DIR$(OutFile$)) THEN KillX OutFile$
  124.     IF ErrorCode% THEN HandleError
  125.  
  126.  OutHandle% = OpenX%(OutFile$)
  127.    IF ErrorCode% THEN HandleError
  128.  
  129.  TabChar$ = CHR$(9)       'initialize these outside the loop
  130.  CRLF$ = CHR$(13,10)
  131.  
  132.  DO
  133.    ThisLine$ = GetLineX$(InHandle%)
  134.       IF ErrorCode% THEN HandleError     'this would end the program
  135.  
  136.    IF LEN(ThisLine$) THEN
  137.       Start% = 1
  138.       DO
  139.          FoundTab% = INSTR(Start%, ThisLine$, TabChar$)
  140.          IF FoundTab% THEN
  141.             SpacesPastTabStop% = FoundTab% MOD TabWidth%
  142.             SpacesToAdd% = TabWidth% - SpacesPastTabStop%
  143.             INCR SpacesToAdd%
  144.             BeforeTab$ = LEFT$(ThisLine$, FoundTab% - 1)
  145.             AfterTab$ = MID$(ThisLine$, FoundTab% + 1)
  146.             ThisLine$ = BeforeTab$ + SPACE$(SpacesToAdd%)
  147.             Start% = LEN(ThisLine$)
  148.             ThisLine$ = ThisLine$ + AfterTab$
  149.          END IF
  150.       LOOP WHILE FoundTab%
  151.       OutString$ = RTRIM$(ThisLine$) + CRLF$ '<--NOTE: trims trailing spaces!
  152.       PutStX OutHandle%, OutString$
  153.    ELSE
  154.       PutStX OutHandle%, CRLF$
  155.    END IF
  156.  LOOP UNTIL EndX% OR ErrorCode%
  157.  
  158.  IF ErrorCode% THEN HandleError
  159.  
  160.  CloseX InHandle%
  161.     IF ErrorCode% THEN HandleError
  162.  CloseX OutHandle%
  163.     IF ErrorCode% THEN HandleError
  164.  
  165. END SUB
  166.  
  167.  
  168. '=======================
  169.  SUB Report (Display$)
  170. '=======================
  171.  
  172.   CRLF$ = CHR$(13,10)
  173.   IF LEN(Display$) THEN
  174.      Display$ = "  " + AllTrim$(Display$) + CRLF$
  175.   ELSE
  176.      Display$ = CRLF$
  177.   END IF
  178.  
  179.   PutStX %STDOUT, Display$
  180.  
  181. END SUB
  182.  
  183.  
  184. '=============================
  185.  FUNCTION AllTrim$ (Target$)
  186. '=============================
  187.  
  188.    AllTrim$ = RTRIM$(LTRIM$(Target$))
  189.  
  190. END FUNCTION
  191.  
  192.  
  193. '====================
  194.  SUB HandleError
  195. '====================
  196.   'This gives you a cut-and-paste skeleton for your own error handler:
  197.  
  198.   Report "ERROR NUMBER:" + STR$(ErrorCode%)
  199.  
  200.   SELECT CASE ErrorCode%
  201.     CASE -1
  202.        Report "Failed to allocate buffer string in GetLineX$."
  203.     CASE 2
  204.        Report "File not found."
  205.     CASE 3
  206.        Report "Path not found."
  207.     CASE 4
  208.        Report "Out of file handles."
  209.     CASE 5
  210.        Report "Access denied."
  211.     CASE 6
  212.        Report "Invalid file handle."
  213.     CASE 8
  214.        Report "Insufficient memory."
  215.     CASE 15
  216.        Report "Invalid drive."
  217.     CASE 19
  218.        Report "Write-protected disk."
  219.     CASE 21
  220.        Report "Drive not ready. Is drive door open?"
  221.     CASE 25
  222.        Report "Seek error."
  223.     CASE 26
  224.        Report "Unknown media type."
  225.     CASE 27
  226.        Report "Disk sector not found."
  227.     CASE 28
  228.        Report "Printer out of paper or not ready."
  229.     CASE 29
  230.        Report "Write fault."
  231.     CASE 30
  232.        Report "Read fault."
  233.     CASE 31
  234.        Report "General failure."
  235.     CASE 128
  236.        Report "Malfunction in EMM driver."
  237.     CASE 129
  238.        Report "Malfunction in EMS memory hardware."
  239.     CASE 131
  240.        Report "Invalid EMS handle."
  241.     CASE 132
  242.        Report "Invalid EMS function number."
  243.     CASE 133
  244.        Report "No EMS handles available."
  245.     CASE 134
  246.        Report "EMS deallocation error."
  247.     CASE 135
  248.        Report "Not enough EMS memory."
  249.     CASE 136
  250.        Report "More EMS pages requested than exist on system."
  251.     CASE 137
  252.        Report "Requested zero pages during EMS allocation."
  253.     CASE 138
  254.        Report "Tried to map a page not assigned to EMS handle."
  255.     CASE 139
  256.        Report "Invalid page frame number (not 0-3)."
  257.     CASE ELSE
  258.        Report "Unknown error."
  259.   END SELECT
  260.  
  261.   END         'abort the program
  262.  
  263. END SUB
  264.