Path: utzoo!utgpu!attcan!uunet!peregrine!elroy!ames!pasteur!ucbvax!hplabs!hp-sdd!ncr-sd!babel!shehzad
From: shehzad@babel.SanDiego.NCR.COM (Mevawalla Shezad)
Newsgroups: comp.binaries.ibm.pc.d
Subject: Re: UNIX Like Split Utility
Message-ID: <298@babel.SanDiego.NCR.COM>
Date: 9 Aug 88 19:53:54 GMT
References: <290@babel.SanDiego.NCR.COM> <486@happym.UUCP>
Reply-To: shehzad@babel.SanDiego.NCR.COM (Mevawalla Shezad)
Organization: NCR Corporation, Rancho Bernardo, CA
Lines: 172

In article <486@happym.UUCP> irv@happym.UUCP (Irving Wolfe) writes:
>Please post the source.  A few people don't have Turbo Pascal, but EVERYONE


OK. Here is the source for the Split utility. Sorry for the lack of comments,
but I wrote this program for my own use and therefore didn't bother to put them
in. However the code is pretty straight forward and people should have no
problems customizing it to their liking.

Even though this program is written for TP 4.0 I don't think it will be too
difficult to port to another compiler.

Right now the program doesn't work with binary files and doesn't accept 
command line parameters. Something you might want to add (pretty trivial).

------  Cut Here  -----

Program Split (input,output);

uses crt;

Type

   Instring=Packed Array [1..30] of char;
   TempType=Packed Array [1..3] of char;
Var
   Num_Of_Lines:Integer;
   Filename:Instring;
   File1:Text;
   File2:Text;

  Procedure Get_File_Name(var error:Boolean;var TempFile:TempType;
                          var Loop:Integer; var Done:Integer);
  Var
  	I:Integer;
  Begin
       If (Loop=1) and (Done=1) then
       Begin
            TempFile[1]:='X';
            TempFile[2]:='A';
            TempFile[3]:='A';
       End
       else if (Loop=27) and (Done=676) then
       Begin
       		Error:=true;
                For i:=1 to 25 do write(' ');
       		Writeln('ERROR TOO MANY FILES');
       	End
       else if Loop=27 then
       Begin
       		I:=ord(TempFile[2]);
       		I:=I+1;
       		TempFile[2]:=chr(I);
       		TempFile[3]:='A';
                Loop:=1;
                Done:=Done+1;
  	End
  	else Begin
  		I:=ord(TempFile[3]);
  		I:=I+1;
  		TempFile[3]:=Chr(I);
                Done:=Done+1;
  	     end;
  end;
  Procedure Readin(var Filename:Instring;Var Num_Of_lines:Integer);
  Var
  	I,J:Integer;
  	Temp:Integer;
  	X,Y,Loop:Integer;
  	TempFile:TempType;
  	Ch:Char;
        Done:Integer;
  	Finish,Bad,Error:Boolean;
  Begin
        TextBackGround(4);
        ClrScr;
        Writeln;Writeln;
        Writeln;Writeln;
        Finish:=False;
  	I:=1;
  	Loop:=0;
        Done:=1;
  	Error:=False;
  	Bad:=True;
        X:=WhereX;
        Y:=Wherey;
        {$I-}
        While Bad do
        Begin
        	Write('Enter Number of Lines : ');
  		Read(Num_Of_Lines);
                If IOResult<>0 then
		Begin
		     GotoXY(X,Y);
		     Write('                                         ');
                     Write('                                         ');
		     GotoXY(X,Y);
		End
		Else Bad:=False;
	End;
        {$I+}
  	Temp:=Num_Of_Lines;
        Bad:=True;
        X:=WhereX;
        Y:=WhereY;
        {$I-}
        While Bad do
        Begin
              Readln;
              Write('Enter Filename or  to exit : ');
  	      While not(Eoln) and (not(I>30)) do
  	      Begin
  		   Read(Filename[I]);
                   I:=I+1;
  	      End;
              Writeln;
  	      For J:=30 Downto I do Filename[J]:=' ';
  	      If I=1 then Error:=True
  	      Else Begin
  	      		Assign(File1,Filename);
  	      		Reset(File1);
  	      	   End;
              If IOResult <> 0 then
              Begin
                    GotoXY(X,Y);
                    Write('File not found !!                       ');
                    Write('                                        ');
                    Delay(1000);
                    GotoXY(X,Y);
                    I:=1;
              End
              Else Bad:=False;
        End;
        {$I+}
      If not(Error) then
      Begin
        Repeat
        Loop:=Loop+1;
        Get_File_Name(Error,TempFile,Loop,Done);
        If not(Error) then
        Begin
        Assign(File2,TempFile);
        Rewrite(File2);
  	While not(Finish) and (not(Temp=0)) do
  	Begin
  	   While not(eoln(File1)) do
  	   Begin
  		Read(File1,Ch);
  		Write(File2,Ch)
  	   End;
  	   Readln(File1);
  	   Writeln(File2);
           If Eof(File1) then finish:=true;
  	   Temp:=Temp-1;
  	 End;
         Close(File2);
         Temp:=Num_Of_Lines;
         End
         else Finish:=True;
  	 Until Finish;
         Close(File1);
  End;
  End;


  Begin  {MAIN}
  Readin(Filename,Num_Of_Lines);
  HighVideo;
  Writeln;Writeln;
  Write('                  *************  Split is done  *************');
  LowVideo;
  End.   {MAIN}