IBM Support

How to generate AUTOSAR macros using Post Processing script in Rational Rhapsody

Question & Answer


Question

How do you generate AUTOSAR macros using Post Processing script in IBM Rational Rhapsody?

Cause

You would like to use a C++ script to modify the generated code as per your specific requirement, which is not directly possible using the GUI.

Answer

In order to generate AUTOSAR macros (like VAR and FUNC), you can use the below C++ post processing script as a reference.

This script is indicatively only and you may need to modify the same as per your specific need.

Disclaimer

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


#import "C:\IBM\Rational\Rhapsody\761\rhapsody.tlb" raw_interfaces_only, no_namespace, named_guids

#include "stdio.h"
#include <algorithm>
#include <string>
#include <conio.h>
#include <iostream>
#include <fstream>

using namespace std;

static void process( const char* filename ) {

 string lineString, fileString, clsName;
 int found=0;

 fstream in( filename );

 if ( !in )

  cout << filename << " does not exist!" << endl;

 else {

  while ( getline(in, lineString,'\n')) {

   if(lineString.substr(0,10) == "/*## class") {
    clsName = lineString.substr(11,lineString.find(" */")-11);
    std::transform(clsName.begin(), clsName.end(),clsName.begin(), ::toupper);
    if(clsName.find("TOPLEVEL::") != string::npos)
     clsName.erase(clsName.find("TOPLEVEL::"),10);
    }

    if(lineString.substr(0,14) == "/*## attribute") {

     fileString += lineString + '\n';
     getline(in, lineString,'\n');

     string varType, varName;
     string tmpStr = lineString;
     string bfrStr = "";

     if(lineString.find("extern") != string::npos) {

      bfrStr = lineString.substr(0,lineString.find(' ')+1);
      tmpStr = lineString.substr(lineString.find(' ')+1,lineString.length());
     }

     int ptrPos = tmpStr.find('*');
     if(ptrPos != string::npos) {

      varType = tmpStr.substr(0,ptrPos-1);
      varName = tmpStr.substr(ptrPos+2,tmpStr.length());
      bfrStr = bfrStr + "P2VAR" + "(" + varType + "," + "AUTOMATIC," + clsName + "_VAR) " + varName;
     }
    else {

     int spaPos = tmpStr.find(' ');
     varType = tmpStr.substr(0,spaPos);
     varName = tmpStr.substr(spaPos+1,tmpStr.length());
     bfrStr = bfrStr + "VAR" + "(" + varType + "," + "AUTOMATIC" + ") " + varName;
    }

    lineString = bfrStr + "\n";

   }

   if(lineString.substr(0,14) == "/*## operation") {

    fileString += lineString + '\n';
    getline(in, lineString,'\n');

    string tmpStr = lineString;
    string origLineStr = lineString;
    string bfrStr = "FUNC";
    string origTmpStr;

    int flag = 0, count = 0;

    string kwdStr = tmpStr.substr(0,tmpStr.find(' '));
    while(tmpStr.find(' ') < tmpStr.find('(')) {
     count++;
     origTmpStr = tmpStr;
     tmpStr = tmpStr.substr(tmpStr.find(' ')+1,tmpStr.length());
    }

    tmpStr = origTmpStr;

    int retPos = tmpStr.find(' ');
    string opRetType = tmpStr.substr(0,retPos);

    int namePos = tmpStr.find('(');
    string opName = tmpStr.substr(retPos+1,namePos-retPos-1);

    if(count == 2)
     bfrStr = kwdStr + " " + bfrStr + "(" + opRetType + "," + clsName + "_CODE) " + opName + "\n(\n";
    else
     bfrStr = bfrStr + "(" + opRetType + "," + clsName + "_CODE) " + opName + "\n(\n";

    int argPos = tmpStr.find('(');
    tmpStr.insert(argPos+1," ");
    int tmpPos = tmpStr.find(')');

    while (argPos != string::npos) {

    flag = 1;

    tmpStr = tmpStr.substr(argPos+1,tmpPos+1);

    int argTypePos = tmpStr.find(' ');
    tmpStr = tmpStr.substr(argTypePos+1,tmpPos+1);
    int argTypePosEnd = tmpStr.find(' ');
    string argType = tmpStr.substr(argTypePos,argTypePosEnd-argTypePos);

    tmpStr = tmpStr.substr(argTypePosEnd+1,tmpPos+1);

    int argNamePos = tmpStr.find(',');

    if(argNamePos == string::npos)
    argNamePos = tmpStr.find(')');
    string argName = tmpStr.substr(0,argNamePos);

    if(argType.find('*') != string::npos) {
     argType = argType.substr(0,argType.length()-1);
     bfrStr = bfrStr + "P2VAR(" + argType + ",AUTOMATIC," + clsName + "_VAR) " + argName + ",\n";
    }
    else
     bfrStr = bfrStr + "VAR(" + argType + ",AUTOMATIC) " + argName + ",\n";

    tmpStr = tmpStr.substr(argNamePos,tmpPos+1);
    argPos = tmpStr.find(',');
   }

   if(flag == 1) {
    int bfrPos = bfrStr.find_last_of(',');
    bfrStr = bfrStr.substr(0,bfrPos) + "\n";
   }

   getline(in, lineString,'\n');
   string tmpStr1;
   if(lineString.substr(0,18) == "    /*#[ operation") {
    tmpStr1 = lineString + "\n\n";
    getline(in, lineString,'\n');
    while(lineString.substr(0,10) != "    /*#]*/") {
     tmpStr1 = tmpStr1 + lineString+"\n";
     getline(in, lineString,'\n');
    }
    tmpStr1 = tmpStr1 + "\n" + lineString;
    bfrStr = bfrStr + ")" + "\n" + "{";
   }
   else
    bfrStr = "extern " + bfrStr + ");";

   lineString = bfrStr + "\n" + tmpStr1;
  }

  fileString += lineString + '\n';
 }

 in.close();
 ofstream out(filename, ios::ate );
 out.seekp(ios::beg);
 out << fileString;
 out.close();

 }
}

int main(int argc, char* argv[]) {

 char fileName[80];

 if ( argc > 1 ) {

  strcpy (fileName, argv[1] );
  process(fileName);

 } else

 printf ("Missing arguments\n");
 return 0;

}

You need to convert the above code to an executable using any C++ IDE [say Microsoft Visual Studio 6] and use the same in the property CG::File::InvokePostProcessor

[{"Product":{"code":"SSB2MU","label":"IBM Engineering Systems Design Rhapsody"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"General Information","Platform":[{"code":"PF033","label":"Windows"}],"Version":"7.5;7.5.0.1;7.5.1;7.5.1.1;7.5.2;7.5.2.1;7.5.3;7.5.3.1;7.5.3.2;7.6;7.6.0.1;7.6.1;7.6.1.1;7.6.1.2","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

Product Synonym

Rational Rhapsody

Document Information

Modified date:
27 May 2022

UID

swg21588027