function unix2pc(FileIn, FileOut)
% UNIX2PC   - Convert UNIX format ascii files to PC format
%
% Use as: unix2pc(FileIn)
%         unix2pc(FileIn, FileOut)
%
% Inputs: FileIn  = Name of the file to convert
%         FileOut = Name of converted file. If not specified FileIn is 
%                   overwritten.
%
% See also: PC2UNIX

% Brian Schlining
% 10 June 1999

if nargin < 2
   FileOut = FileIn;
end

%======================
% Read the source file
%======================
fidIn  = fopen(FileIn,'rt');
x      = fread(fidIn);
fclose(fidIn);

%=================
% Find line feeds
%=================
i      = find(x == 10); 

%========================================================================
% Write it back out with carriage returns inserted before each line feed
%========================================================================
fidOut = fopen(FileOut,'w');

if ~isempty(i) 
   
   % Write the characters before the first line feed
   if i(1) ~= 1
      fwrite(fidOut, [x(1:i(1)-1)], 'int8');
   end
   
   % Write the bulk of the file
   for n = 1:length(i)-1
      fwrite(fidOut, [13; [x(i(n):i(n+1)-1)]], 'int8');   
   end
   
   % Write the characters after the last line feed
   if (i(end) ~= length(x))
      fwrite(fidOut, [13; x(i(end):length(x))], 'int8');
   end
   
else
   % If no line feeds are present, simply copy the file
   fwrite(fidOut, x, 'int8');    
end


fclose(fidOut);

