function [vargout]=nic_rsdd_misc(operation,vargin)
% Miscellaneous functions
%-----------------------------------------------------------
%   Copyright (C) 2009
%   Neuroimage Computing Group, State Key Laboratory of
%   Cognitive Neuroscience and Learning, Beijing normal University
%	Written by Guiwen Chen
%	Version=1.0;
%	Release=20090715;
%-----------------------------------------------------------
%

switch upper(operation),
    case 'WHEREIS', 			% the folder of the toolbox
        if nargin>0,
            [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));
        	varargout{1} =pathstr;
        end	
    case 'CURRENTUSER'
        varargout{1} =getenv('USERNAME');	% for Windows
        if isempty(varargout{1}),
            varargout{1} =getenv('USER');% for Unix ...
        end
     case 'MATLABVERSION',		%GetMatlabVersion
         theVer =version;
         varargout{1} =str2num( theVer(1:3) );
    case 'DATESTR',		%GetMatlabVersion
         theDatetime=fix(clock);%[year month day hour minute seconds]
         varargout{1} =sprintf('%.4d%.2d%.2d',theDatetime(1),theDatetime(2),theDatetime(3));
    case 'TIMESTR',		%GetMatlabVersion
         theDatetime=fix(clock);%[year month day hour minute seconds]
         varargout{1} =sprintf('%.4d%.2d%.2d_%.2d%.2d',theDatetime(1),theDatetime(2),theDatetime(3),theDatetime(4),theDatetime(5));	
    case 'EXCEPTION' % display the exception
        if nargout>0,
            varargout{1} =DisplayLastException;
        else
            DisplayLastException;
        end
    case '',
     
    otherwise,
end
    
return;

	
function Result =DisplayLastException()
% display the exception
	theError = lasterror;
	Result =sprintf('\nException occured.\t(%s)\n\t%s', theError.identifier,  theError.message);			
	if isfield(theError, 'stack')	%Matlab 6.5 compatible
		for x=1:length(theError.stack)
			Result =sprintf('%s\n\t<a href="error:%s,%d,0">%d#line</a>,\t\t%s,\tin "%s"', ...
						Result, ...
						getfield(theError.stack(x,1), 'file'), ...
						getfield(theError.stack(x,1), 'line'), ...
						getfield(theError.stack(x,1), 'line'), ...
						getfield(theError.stack(x,1), 'name'), ...
						getfield(theError.stack(x,1), 'file'));			
		end
	end
	fprintf('%s\n', Result);	
    
    
