FixAutoGenCCode.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function FixAutoGenCCode(fileName)
  2. %% Initialize variables
  3. delimiter = '';
  4. %% Format string for each line of text:
  5. % column1: text (%s)
  6. % For more information, see the TEXTSCAN documentation.
  7. formatSpec = '%s%[^\n\r]';
  8. %% Open the text file.
  9. fileID = fopen(strcat(fileName,'.c'),'r');
  10. %% Read columns of data according to format string.
  11. % This call is based on the structure of the file used to generate this
  12. % code. If an error occurs for a different file, try regenerating the code
  13. % from the Import Tool.
  14. dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
  15. %% Close the text file.
  16. fclose(fileID);
  17. %% Create output variable
  18. SymbolicOutput = [dataArray{1:end-1}];
  19. %% Clear temporary variables
  20. clearvars filename delimiter formatSpec fileID dataArray ans;
  21. %% Convert 1 based indexes in symbolic array variables
  22. for lineIndex = 1:length(SymbolicOutput)
  23. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_l_', '(');
  24. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_c_', ',');
  25. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_r_', ')');
  26. end
  27. % replace 2-D left indexes
  28. for arrayIndex = 1:99
  29. strIndex = int2str(arrayIndex);
  30. strRep = sprintf('[%d,',(arrayIndex-1));
  31. strPat = strcat('\(',strIndex,'\,');
  32. for lineIndex = 1:length(SymbolicOutput)
  33. str = char(SymbolicOutput(lineIndex));
  34. SymbolicOutput(lineIndex) = {regexprep(str, strPat, strRep)};
  35. end
  36. end
  37. % replace 2-D right indexes
  38. for arrayIndex = 1:99
  39. strIndex = int2str(arrayIndex);
  40. strRep = sprintf(',%d]',(arrayIndex-1));
  41. strPat = strcat('\,',strIndex,'\)');
  42. for lineIndex = 1:length(SymbolicOutput)
  43. str = char(SymbolicOutput(lineIndex));
  44. SymbolicOutput(lineIndex) = {regexprep(str, strPat, strRep)};
  45. end
  46. end
  47. % replace commas
  48. for lineIndex = 1:length(SymbolicOutput)
  49. str = char(SymbolicOutput(lineIndex));
  50. SymbolicOutput(lineIndex) = {regexprep(str, '\,', '][')};
  51. end
  52. %% add float declarations in front of temporary variables
  53. expression = 't(\w*) =';
  54. replace = 'float t$1 =';
  55. for lineIndex = 1:length(SymbolicOutput)
  56. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex),expression,replace);
  57. end
  58. %% replace (1.0/2.0) with 0.5f
  59. expression = '\(1.0/2.0\)';
  60. replace = '0.5f';
  61. for lineIndex = 1:length(SymbolicOutput)
  62. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex),expression,replace);
  63. end
  64. %% replace 2.0
  65. expression = '2\.0';
  66. replace = '2.0f';
  67. for lineIndex = 1:length(SymbolicOutput)
  68. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex),expression,replace);
  69. end
  70. %% Write to file
  71. fileName = strcat(fileName,'.cpp');
  72. fid = fopen(fileName,'wt');
  73. for lineIndex = 1:length(SymbolicOutput)
  74. fprintf(fid,char(SymbolicOutput(lineIndex)));
  75. fprintf(fid,'\n');
  76. end
  77. fclose(fid);
  78. clear all;
  79. end