%kkgeneric 'Generic Routine Interface (glue)'
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kgeneric.pane file
%
% Parameters: 
% String: prefix 'Prefix', default: ' ': 'prefix string'
% InputFile: i 'Input Image ', optional: 'input image'
% OutputFile: o 'Output Image', optional: 'output image or file'
% String: iname1 'Input name #1 ', default: ' ': 'name of input argument #1'
% InputFile: i1 'Input Filename #1 ', optional: 'input filename #1'
% String: iname2 'Input name #2 ', default: ' ': 'name of input argument #2'
% InputFile: i2 'Input Filename #2 ', optional: 'input filename #2'
% String: iname3 'Input name #3 ', default: ' ': 'name of input argument #3'
% InputFile: i3 'Input Filename #3 ', optional: 'input filename #3'
% String: oname1 'Output name #1', default: ' ': 'name of output argument #1'
% OutputFile: o1 'Output Filename #1', optional: 'output filename #1'
% String: oname2 'Output name #2', default: ' ': 'name of output argument #2'
% OutputFile: o2 'Output Filename #2', optional: 'output filename #2'
% String: oname3 'Output name #3', default: ' ': 'name of output argument #3'
% OutputFile: o3 'Output Filename #3', optional: 'output filename #3'
% String: name1 'Arg name #1', default: ' ': 'Name of argument #1'
% String: val1 'Argument #1 value', default: ' ': 'argument #1 value'
% String: name2 'Arg name #2', default: ' ': 'Name of argument #2'
% String: val2 'Argument #2 value', default: ' ': 'argument #2 value'
% String: name3 'Arg name #3', default: ' ': 'Name of argument #3'
% String: val3 'Argument #3 value', default: ' ': 'argument #3 value'
% String: name4 'Arg name #4', default: ' ': 'Name of argument #4'
% String: val4 'Argument #4 value', default: ' ': 'argument #4 value'
% String: name5 'Arg name #5', default: ' ': 'Name of argument #5'
% String: val5 'Argument #5 value', default: ' ': 'argument #5 value'
% String: name6 'Arg name #6', default: ' ': 'Name of argument #6'
% String: val6 'Argument #6 value', default: ' ': 'argument #6 value'
% String: name7 'Arg name #7', default: ' ': 'Name of argument #7'
% String: val7 'Argument #7 value', default: ' ': 'argument #7 value'
% String: name8 'Arg name #8', default: ' ': 'Name of argument #8'
% String: val8 'Argument #8 value', default: ' ': 'argument #8 value'
% String: trailer 'Trailer', default: ' ': 'trailer string'
%
% Example: [o, o1, o2, o3] = kkgeneric({i, i1, i2, i3}, {'prefix',' ';'i','';'o','';'iname1',' ';'i1','';'iname2',' ';'i2','';'iname3',' ';'i3','';'oname1',' ';'o1','';'oname2',' ';'o2','';'oname3',' ';'o3','';'name1',' ';'val1',' ';'name2',' ';'val2',' ';'name3',' ';'val3',' ';'name4',' ';'val4',' ';'name5',' ';'val5',' ';'name6',' ';'val6',' ';'name7',' ';'val7',' ';'name8',' ';'val8',' ';'trailer',' '})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% kgeneric - Generic Routine Interface (glue)
%
%  DESCRIPTION
% .I kgeneric
% is a programmable interface that allows a generalized Un*x command line
% to be executed by cantata.
% 
% Entries on the pane are spit out to a string that is then system()'d
% off. The string is built up by appending the contents of the SELECTED
% entries together in scan-line order.
% 
% Note: All entries are strings. Unfortunately, the forms don't
% seem to like multiple-worded strings, so you have to build up your
% command piece at a time. You CAN use a multiple-worded string in the
% prefix and trailer.
% 
% Note: Anything entered into a FILENAME box will be treated as a file
% using the normal Khoros file access checking. This means the UI will
% try to open the filename for access. If it is a Un*x command or other
% object then things won't work.
% 
% Note: There appears to be no way to protect special characters from
% the shell.
% 
% Note: The oval button to the left of argument name selection allows
% you to put a minus sign ("-") immediately in front of the name itself
% in the command string that is executed. The parser won't let you
% put a minus sign in the string field itself - it will complain about
% unknown arguments. So if you want an argument name that looks like
% 
% -blah
% 
% then click the button to get the - to show and enter blah as the name.
% The actual command string executed will look like that above.
% 
% Note: There is no job control from this routine. Hitting the
% kill button in cantata is not guaranteed to kill the command that was
% system()'d off. Additionally, status reporting is unsupported.
%
%  
%
%  EXAMPLES
% 
% To execute:
% 
% ls -al > snarf
% 
% using this routine, do:
% 
% % kgeneric -prefix ls -iname1 al -iname2 > -o1 snarf -di1 1 -di2 0 -di3 0 -do1 0
% -do2 0 -do3 0 -dn1 0 -dn2 0 -dn3 0 -dn4 0 -dn5 0 -dn6 0 -dn7 0 -dn8 0
% 
% and you should get the desired result. Note that this routine is not much good
% for use on the command line. It is for cantata.
%
%  "SEE ALSO"
% None. Yet.
%
%  RESTRICTIONS 
% Kgeneric executes the command via a ksystem() call, and thus there is no
% reporting of errors from the command. If nothing happens when you run a command,
% try  executing the whole command from command line. If the command line fails,
% fix the problem before trying to use kgeneric.
% 
% No data type or range checking is available when using this interface.
%
%  REFERENCES 
% None.
%
%  COPYRIGHT
% Copyright (C) 1993 - 1996, Khoral Research, Inc.  All rights reserved.
% 


function varargout = kkgeneric(varargin)
if nargin ==0
  Inputs={};arglist={'',''};
elseif nargin ==1
  Inputs=varargin{1};arglist={'',''};
elseif nargin ==2
  Inputs=varargin{1}; arglist=varargin{2};
else error('Usage: [out1,..] = kkgeneric(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'prefix', ' ';'i', '__input';'o', '__output';'iname1', ' ';'i1', '__input';'iname2', ' ';'i2', '__input';'iname3', ' ';'i3', '__input';'oname1', ' ';'o1', '__output';'oname2', ' ';'o2', '__output';'oname3', ' ';'o3', '__output';'name1', ' ';'val1', ' ';'name2', ' ';'val2', ' ';'name3', ' ';'val3', ' ';'name4', ' ';'val4', ' ';'name5', ' ';'val5', ' ';'name6', ' ';'val6', ' ';'name7', ' ';'val7', ' ';'name8', ' ';'val8', ' ';'trailer', ' '};
maxval={0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
minval={0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
istoggle=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
was_set=istoggle * 0;
paramtype={'String','InputFile','OutputFile','String','InputFile','String','InputFile','String','InputFile','String','OutputFile','String','OutputFile','String','OutputFile','String','String','String','String','String','String','String','String','String','String','String','String','String','String','String','String','String'};
% identify the input arrays and assign them to the arguments as stated by the user
if ~iscell(Inputs)
Inputs = {Inputs};
end
NumReqOutputs=0; nextinput=1; nextoutput=1;
  for ii=1:size(arglist,1)
  wasmatched=0;
  for jj=1:size(narglist,1)
   if strcmp(arglist{ii,1},narglist{jj,1})  % a given argument was matched to the possible arguments
     wasmatched = 1;
     was_set(jj) = 1;
     if strcmp(narglist{jj,2}, '__input')
      if (nextinput > length(Inputs)) 
        error(['Input ' narglist{jj,1} ' has no corresponding input!']); 
      end
      narglist{jj,2} = 'OK_in';
      nextinput = nextinput + 1;
     elseif strcmp(narglist{jj,2}, '__output')
      if (nextoutput > nargout) 
        error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']); 
      end
      if (isempty(arglist{ii,2}))
        narglist{jj,2} = 'OK_out';
      else
        narglist{jj,2} = arglist{ii,2};
      end

      nextoutput = nextoutput + 1;
      if (minval{jj} == 0)  
         NumReqOutputs = NumReqOutputs - 1;
      end
     elseif isstr(arglist{ii,2})
      narglist{jj,2} = arglist{ii,2};
     else
        if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
            error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
        end
        if (minval{jj} ~= 0 | maxval{jj} ~= 0)
          if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
            error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
          elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
            error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
          elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
            error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
          elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
            error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
            error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
            error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
          end
        end
     end
     if ~strcmp(narglist{jj,2},'OK_out') &  ~strcmp(narglist{jj,2},'OK_in') 
       narglist{jj,2} = arglist{ii,2};
     end
   end
   end
   if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
        error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
   end
end
% match the remaining inputs/outputs to the unused arguments and test for missing required inputs
 for jj=1:size(narglist,1)
     if  strcmp(paramtype{jj}, 'Toggle')
        if (narglist{jj,2} ==0)
          narglist{jj,1} = ''; 
        end;
        narglist{jj,2} = ''; 
     end;
     if  ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
          narglist{jj,1} = ''; 
          narglist{jj,2} = ''; 
     end;
     if strcmp(narglist{jj,2}, '__input')
      if (minval{jj} == 0)  % meaning this input is required
        if (nextinput > size(Inputs)) 
           error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']); 
        else
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        end
      else  % this is an optional input
        if (nextinput <= length(Inputs)) 
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end;
     else 
     if strcmp(narglist{jj,2}, '__output')
      if (minval{jj} == 0) % this is a required output
        if (nextoutput > nargout & nargout > 1) 
           error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']); 
        else
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
          NumReqOutputs = NumReqOutputs-1;
        end
      else % this is an optional output
        if (nargout - nextoutput >= NumReqOutputs) 
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end
     end
  end
end
if nargout
   varargout = cell(1,nargout);
else
  varargout = cell(1,1);
end
global KhorosRoot
if exist('KhorosRoot') && ~isempty(KhorosRoot)
w=['"' KhorosRoot];
else
if ispc
  w='"C:\Program Files\dip\khorosBin\';
else
[s,w] = system('which cantata');
w=['"' w(1:end-8)];
end
end
[varargout{:}]=callKhoros([w 'kgeneric"  '],Inputs,narglist);
