Home > atmlab > handy > sanitise.m

sanitise

PURPOSE ^

sanitise If you must pass something to system, do this first

SYNOPSIS ^

function new = sanitise(old, varargin)

DESCRIPTION ^

 sanitise If you must pass something to system, do this first

 Often, calls to system (or exec_system_cmd) can be avoided. Avoid them if
 you can. If you must use system, and you pass on input coming from the
 user, pass your string through this function first. It will sanitise the
 input, replacing any undesired characters.

 Dangerous characters are: :=, ()*/<>!?%;

 IN

   old     string to be sanitised
   rep     character to replace with; defaults to _
   action  relates to messages:
           1 = print warning message if string changed (default)
           2 = error if string changed
           anything else: silently replace names
   danger  dangerous characters; defaults to ':=, ()*<>!?%;'

 OUT

   new     new sanitised string

 See also: exec_system_cmd

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

sanitise.m

SOURCE CODE ^

0001 function new = sanitise(old, varargin)
0002 
0003 % sanitise If you must pass something to system, do this first
0004 %
0005 % Often, calls to system (or exec_system_cmd) can be avoided. Avoid them if
0006 % you can. If you must use system, and you pass on input coming from the
0007 % user, pass your string through this function first. It will sanitise the
0008 % input, replacing any undesired characters.
0009 %
0010 % Dangerous characters are: :=, ()*/<>!?%;
0011 %
0012 % IN
0013 %
0014 %   old     string to be sanitised
0015 %   rep     character to replace with; defaults to _
0016 %   action  relates to messages:
0017 %           1 = print warning message if string changed (default)
0018 %           2 = error if string changed
0019 %           anything else: silently replace names
0020 %   danger  dangerous characters; defaults to ':=, ()*<>!?%;'
0021 %
0022 % OUT
0023 %
0024 %   new     new sanitised string
0025 %
0026 % See also: exec_system_cmd
0027 
0028 % $Id: sanitise.m 8744 2013-11-27 07:14:23Z seliasson $
0029 
0030 WARN = 1;
0031 ERROR = 2;
0032 
0033 [repl, action, danger] = optargs(varargin, {'_', WARN, ':=, ()*<>!?%;&$'});
0034 
0035 new = regexprep(old,['[' danger ']'],repl);
0036 
0037 if ~strcmp(new, old)
0038     switch action
0039         case WARN
0040             logtext(atmlab('ERR'), ...
0041                 'Warning: string changed from %s to %s.\n', ...
0042                 old, new);
0043         case ERROR
0044             error(['atmlab:' mfilename ':wrongname'], ...
0045                 ['Encountered dangerous string %s, should not be used with ' ...
0046                  'exec_system_cmd/system or similar! Maybe use %s instead?'], ...
0047                  old, new);
0048     end
0049 end
0050 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005