Code covered by the MathWorks Limited License

Highlights from
Simulink Library for Zumo Robot

image thumbnail

Simulink Library for Zumo Robot

by

 

29 Apr 2014 (Updated )

A library of blocks to get started with the Zumo Robot

renc2cpp(sfname, invertedMode)
function renc2cpp(sfname, invertedMode)
% renc2cpp(sfname) given an s-function name sfname, it looks for
% the .c wrapper file generated by the s-function builder and:
% 1) inserts extern "C" before occurrences of void
% 2) copies the file to a .cpp file
% 3) deletes the original .c file
% this needs to be run after building the some s-functions that rely
% on cpp libraries. Google for "Simulink Drivers Guide" for more info.
% 
% This does the inversion of the above if the invertedMode is set to true.
% 1) Removes the extern "C" and creates a .c file 
% Relies on a function by P. Kumpulainen. G. Campa, P. Kassebaum, 10-2013.  
% Copyright 2013 - The Mathworks

if nargin < 2
    invertedMode = false;
end



% attach wrapper and look for files
if ~invertedMode
    fname = fullfile(pwd,[sfname '_wrapper.c']);
else
    fname = fullfile(pwd,[sfname '_wrapper.cpp']);
end

if ~invertedMode
    % insert extern "C" before void and create .cpp file
    replaceInFile('void','extern \"C\" void',fname,[fname 'pp']);
else
    replaceInFile('extern \"C\" void','void',fname,fname(1:end-2));
end
% delete .c/cpp file
delete(fname);
end

function [s, msg] = replaceInFile(str1, str2, infile, outfile)
%REPLACEINFILE replaces characters in ASCII file using PERL
%
% [s, msg] = replaceinfile(str1, str2, infile)
%    replaces str1 with str2 in infile, original file is saved as "infile.bak"
%
% [s, msg] = replaceinfile(str1, str2, infile, outfile)
%    writes contents of infile to outfile, str1 replaced with str2
%    NOTE! if outputfile is '-nobak' the backup file will be deleted
%
% [s, msg] = replaceinfile(str1, str2)
%    opens gui for the infile, replaces str1 with str2 in infile, original file is saved as "infile.bak"
%
% in:  str1      string to be replaced
%      str2      string to replace with
%      infile    file to search in
%      outfile   outputfile (optional) if '-nobak'
%
% out: s         status information, 0 if succesful
%      msg       messages from calling PERL

% Pekka Kumpulainen 30.08.2000
% 16.11.2008 fixed for paths having whitespaces,
% 16.11.2008 dos rename replaced by "movefile" to force overwrite
% 08.01.2009 '-nobak' option to remove backup file, fixed help a little..
%
% TAMPERE UNIVERSITY OF TECHNOLOGY
% Measurement and Information Technology
% www.mit.tut.fi

% message = nargchk(2,4,nargin); % old method
% if ~isempty(message)
%   error(message)
% end
narginchk(2,4); % new method

%% check inputs
if ~(ischar(str1) && ischar(str2))
  error('Invalid string arguments.')
end
% in case of single characters, escape special characters
% (at least someof them)
switch str1
  case {'\' '.'}
    str1 = ['\' str1];
end

%% uigetfile if none given
if nargin < 3;
  [fn, fpath] = uigetfile('*.*','Select file');
  if ~ischar(fn)
    return
  end
  infile = fullfile(fpath,fn);
end

%% The PERL stuff
if strcmp(computer,'MACI64')
    perlCmd = ['perl'];
else
    perlCmd = sprintf('"%s"',fullfile(matlabroot, 'sys\perl\win32\bin\perl'));
end

perlstr = sprintf('%s -i.bak -pe"s/%s/%s/g" "%s"', perlCmd, str1, str2,infile);

[s,msg] = dos(perlstr);

%% rename files if outputfile given
if ~isempty(msg)
  error(msg)
else
  if nargin > 3 % rename files
    if strcmp('-nobak',outfile)
      delete(sprintf('%s.bak',infile));
    else
      movefile(infile, outfile);
      movefile(sprintf('%s.bak',infile), infile);
    end
  end
end
end

Contact us