function Value=quoting(Action,Value)
%QUOTING Handle string quoting issues

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.7 $ $Date: 1997/11/21 23:49:26 $

switch Action,
  case 'DeQuotify',Value=LocalDeQuotify(Value);
  case 'Quotify',  Value=LocalQuotify(Value);  
end  

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalDeQuotify %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function Value=LocalDeQuotify(Value)
% Everything coming into here is a string array of 1 row
if ~isempty(Value),
  if Value(1)=='''',Value(1)=[];end
end
if ~isempty(Value),
  if Value(end)=='''',Value(end)=[];end
end

Value=LocalNoOverlap(Value,'''''','''');

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalQuotify %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
function Value=LocalQuotify(MatValue)
% Everything coming in here is a string array of 1 row

if ~isempty(MatValue),
  len=prod(size(MatValue));
  MatValue=MatValue([sort([1:len find(MatValue=='''')])]);
  MatValue=num2cell(MatValue,2);
  Quotes(1:size(MatValue,1),1)={''''};  
  Value=strcat(Quotes,MatValue,Quotes);  
else,
  Value={''''''};    
end  
Value=char(Value);

%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LocalNoOverlap %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function t = LocalNoOverlap(s,q,s3)
% This function does a strrep that checks for overlapping strings
% i.e. aaaaaa with normal strrep('aaaaaa','aa','b') returns 'bbbbb'
% This function returns 'bbb'

k = findstr(s,q);
dk = diff(k);
while any(dk<length(q));
  p = min(find(dk<length(q)));
  k(p+1) = [];
  dk = diff(k);
end

s2len = length(q);

s2pos = k-1;
s1 = s;
% Build resulting string
s=[];
s1pos=1;
for i=1:length(s2pos),
  s=[s,s1(s1pos:s2pos(i)),s3];
  s1pos=s2pos(i)+s2len+1;
end;
t=[s,s1(s1pos:length(s1))];

if isempty(t),t='';end
