home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ipo-101.zip / Samples.zip / text.pas < prev    next >
Pascal/Delphi Source File  |  1999-03-09  |  6KB  |  176 lines

  1. (* Copyright (C) 1998, 1999 Stuart King. All rights reserved. *)
  2.  
  3. (***************************************************************************
  4. ** Operating systems vary in how they represent end-of-line in text files.
  5. ** For example Unix and it's variants (such as Linux) use a line feed
  6. ** character to represent end-of-line. DOS, OS/2, Windows use two characters
  7. ** carriage return, and line feed together to represent end-of-line.
  8. ** Irie Pascal can read text files with any combination of carriage return,
  9. ** line feed characters as end-of-line, but it writes text files in the
  10. ** native file format. So converting text files with Irie Pascal is
  11. ** very easy all that is necessary is to use read each line of text
  12. ** using "readln" and write out each line using "writeln".
  13. ****************************************************************************)
  14. program text(output);
  15. var
  16.    param : integer;
  17.    OutDir : filename;
  18.  
  19.    function DirectorySeparator : string;
  20.    begin
  21.       if (platform <> platform_linux) and (platform <> platform_fbsd) then
  22.          DirectorySeparator := '\'
  23.       else
  24.          DirectorySeparator := '/'
  25.    end;
  26.  
  27.    procedure syntax;
  28.    begin
  29.       writeln('Text - Converts text files into the native format');
  30.       writeln('Syntax: ivm text filespec .. filespec output_directory');
  31.       writeln('        The wildcards ''?'' and ''*'' may be used');
  32.       writeln('For example:');
  33.       writeln('        ivm text *.pas ' + DirectorySeparator + 'temp' + DirectorySeparator);
  34.       halt
  35.    end;
  36.  
  37.    procedure ConvertFile(InName, OutName : filename);
  38.    const
  39.       MaxLine = 1024;   (* Maximum length of a line in the text file to be converted *)
  40.    var
  41.       f, g : text;
  42.       line : string[MaxLine];
  43.    begin
  44.       writeln(InName, ' -> ', OutName);
  45.       reset(f, InName);
  46.       rewrite(g, OutName);
  47.       while not eof(f) do
  48.          begin
  49.             readln(f, line);
  50.             writeln(g, line)
  51.          end;
  52.       close(g);
  53.       close(f)
  54.    end;
  55.  
  56.    procedure ConvertTextFiles(f : filename);
  57.    var
  58.       DirPart, NamePart, ExtPart : filename;
  59.       fname : filename;
  60.       d : Dir;
  61.  
  62.       (***************************************************************
  63.       ** PURPOSE: This function tests whether a file name is
  64.       **          specified by a file spec (which may have wild cards).
  65.       **          For example the file spec "test.*" specifies all file
  66.       **          names with "test" as the name and any extension.
  67.       **          So FileMatch("test.*", "test.dat") would return "true".
  68.       ** NOTES: This function may be built-into Irie Pascal in the future
  69.       **        but it's kept separate for now. Before building it in
  70.       **        I want to be reasonably sure I can implement it on all
  71.       **        platforms that Irie Pascal will eventually run on.
  72.       *)
  73.       function FileMatch(fspec, fname : filename) : boolean;
  74.       var
  75.          SpecPos, NamePos : integer;
  76.          next : char;
  77.          done : boolean;
  78.       begin (* FileMatch *)
  79.          (*
  80.          ** File names under Linux and FreeBSD are case sensitive but
  81.          ** they aren't under other platforms. So for those platforms
  82.          ** where file names are not case sensitive the file spec and
  83.          ** file name are converted to all lower case so that their
  84.          ** original case doesn't matter (i.e. "NAME" <> "Name" but by
  85.          ** by converting "NAME" to "name" and "Name" to "name" they match).
  86.          *)
  87.          if (platform <> platform_linux) and (platform <> platform_fbsd) then
  88.             begin
  89.                fspec := lowercase(fspec);
  90.                fname := lowercase(fname)
  91.             end;
  92.          NamePos := 1;
  93.          SpecPos := 1;
  94.          done := false;
  95.          repeat
  96.             if (SpecPos>length(fspec)) and (NamePos>length(fname)) then
  97.                begin
  98.                   FileMatch := true;
  99.                   done := true
  100.                end
  101.             else if (SpecPos>length(fspec)) and (NamePos<=length(fname)) then
  102.                begin
  103.                   FileMatch := false;
  104.                   done := true
  105.                end
  106.             else if (SpecPos<=length(fspec)) and (NamePos>length(fname)) then
  107.                begin
  108.                   FileMatch := false;
  109.                   done := true
  110.                end
  111.             else if fspec[SpecPos] = '?' then
  112.                begin
  113.                   inc(SpecPos);
  114.                   inc(NamePos)
  115.                end
  116.             else if fspec[SpecPos] = '*' then
  117.                begin
  118.                   if SpecPos = length(fspec) then
  119.                      begin
  120.                         FileMatch := true;
  121.                         done := true
  122.                      end
  123.                   else
  124.                      begin
  125.                         next := fspec[SpecPos+1];
  126.                         NamePos := pos(next, fname, NamePos);
  127.                         if NamePos > 0 then
  128.                            inc(SpecPos)
  129.                         else
  130.                            begin
  131.                               FileMatch := false;
  132.                               done := true
  133.                            end
  134.                      end
  135.                end
  136.             else if fspec[SpecPos] = fname[NamePos] then
  137.                begin
  138.                   inc(SpecPos);
  139.                   inc(NamePos)
  140.                end
  141.             else
  142.                begin
  143.                   FileMatch := false;
  144.                   done := true
  145.                end
  146.          until done;
  147.       end; (* FileMatch *)
  148.  
  149.    begin (* ConvertTextFiles *)
  150.       f := fexpand(f);
  151.       writeln(f);
  152.       fsplit(f, DirPart, NamePart, ExtPart);
  153.       NamePart := NamePart + ExtPart;
  154.       OpenDir(d, DirPart);
  155.       repeat
  156.          ReadDir(d, fname);
  157.          if fname <> '' then    (* if there are more files in directory *)
  158.             begin
  159.                if FileMatch(NamePart, fname) then
  160.                   begin
  161.                      ConvertFile(DirPart+fname, OutDir+fname)
  162.                   end
  163.             end
  164.       until fname = '';
  165.       CloseDir(d)
  166.    end; (* ConvertTextFiles *)
  167.  
  168. begin
  169.    if paramcount < 2 then
  170.       syntax;
  171.    OutDir := fexpand(paramstr(paramcount));
  172.    fsplit(OutDir, OutDir, , );
  173.    for param := 1 to paramcount-1 do
  174.       ConvertTextFiles(paramstr(param));
  175. end.
  176.