/***************************************************************
** Qt based generic FORTRAN 77 conversion into C++ Pseudocode **
** written by John Coe III -- July 21, 2007 **
** Version 1.1 jciii@earthlink.net **
****************************************************************/
#include "main.h"
int main( int argc, char ** argv )
{
// Must start the Qt application first
QApplication a( argc, argv );
// Load the configuration from the information file
loadInfoFile( "convert.info" );
// Allow an command line argument to override selection dialog
QStringList fileNameList;
if ( argc >= 2 )
fileNameList << argv[1];
else
fileNameList = QFileDialog::getOpenFileNames( "Program File (*.F *.f *.h)", QString::null, 0 );
// If something was selected proceed
if ( fileNameList.size() > 0 ) {
// If no command line arguments, ask user if overwrite is ok if Cpp file exists
if ( argc == 1 && QFile::exists( makeIntoCppFile( fileNameList[0] ) ) ) {
switch( QMessageBox::question( 0, "FORTRAN to Cpp:",
"Overwrite existing Cpp file(s)?",
"&Yes", "&Abort",
0, 1 ) ) {
case 0:
break;
case 1:
default:
return -1;
break;
}
}
// Iterate over entire filename list
for ( QStringList::const_iterator it = fileNameList.begin(); it != fileNameList.end(); it++ ) {
if ( !convertFortran( *it ) )
qDebug( "File IO error during %s", *it );
}
}
return 0;
}
bool convertFortran(const QString fileName)
{
QStringList fortranCode;
// Read entire file into memory
QFile infile( fileName );
if ( !infile.open(IO_ReadOnly) )
return false;
QTextStream in(&infile);
while ( !in.atEnd() ) {
fortranCode << in.readLine();
}
infile.close();
// Set up file output for the new Cpp program
QString fileNameNew = makeIntoCppFile( fileName );
qDebug( fileNameNew );
QFile outFile( fileNameNew );
if ( !outFile.open(IO_WriteOnly) )
return false;
QTextStream cppOut(&outFile);
// Write the file header out to the Cpp file first
for ( QStringList::iterator it = fileHeader.begin(); it != fileHeader.end(); it++ )
cppOut << *it << "\n";
// Intialize
int lineNo = 0;
QString currentCode, nextCode, code, label, ignore;
bool contLine = false;
FortranType myType = eUnknown;
int indent = 0;
int doFlag = 0;
int indentNext = 0;
bool insideComment = false;
bool ignoreLongLine = false;
bool isLocalsWritten= true; // version 1.1 addition
QStringList::iterator next;
// Holds local variables - version 1.1 addition
QStringList localVariables;
// Temporary
QStringList::iterator it2, it3;
QString temp, common;
int tempInt;
FortranType type;
QStringList parts;
// Parse file data
for ( QStringList::iterator it = fortranCode.begin(); it != fortranCode.end(); it++ ) {
lineNo++;
// Initialize for each loop
indentNext = 0;
ignoreLongLine = false;
// Process a line of Fortran code
myType = determineFortranType( (*it), currentCode, label, contLine );
// Ignore this, it was already processed
if ( contLine ) continue;
// Put all continuation lines into one big one (skip comments if any)
if ( myType != eComment && myType != eBlank ) {
next = it;
next++;
for ( ; next != fortranCode.end(); next++ ) {
temp = *next;
type = determineFortranType( temp, nextCode, ignore, contLine );
// Ignore comment and keep going
if ( type == eComment ) continue;
// If continuation line, add into existing line
if ( contLine ) {
currentCode += " " + nextCode;
continue;
}
// Stop processing...
break;
}
}
/* **********Process the Fortran type********** */
// Is this the end of a comment?
if ( insideComment && myType != eComment ) {
cppOut << "*/\n";
insideComment = false;
}
// If code is reached, write out local variables first
if ( !isLocalsWritten &&
( myType != eComment && myType != eBlank &&
myType != eEquivalence && myType != eImplicit &&
myType != eIntrinsic && myType != eStructure &&
myType != eEndStructure && myType != eUnion &&
myType != eEndUnion && myType != eBlock &&
myType != eCommon && myType != eInclude &&
myType != eVariable && myType != eParameter &&
myType != eData && myType != eSave &&
myType != eExternal ) ) {
if ( localVariables.size() > 0 ) {
cppOut << "\n";
for ( QStringList::iterator var = localVariables.begin(); var != localVariables.end(); var++ ) {
cppOut << *var << "\n";
}
cppOut << "\n";
localVariables.clear();
}
isLocalsWritten = true;
}
// Set the flag that allows variables to be written if this is the start of a program
if ( myType == eBeginProgram || myType == eSubroutine )
isLocalsWritten = false;
switch( myType ) {
case eDo:
doFlag++;
indentNext = 1;
currentCode = processDo( currentCode );
break;
case eComment:
if ( !insideComment ) {
cppOut << "/*" << "\n";
insideComment = true;
}
currentCode = "**" + currentCode;
ignoreLongLine = true;
break;
case eBeginProgram:
currentCode = currentCode.replace( "program", "", FALSE ).stripWhiteSpace();
case eSubroutine:
currentCode = currentCode.replace( "subroutine", "", FALSE ).stripWhiteSpace();
for ( it2 = programHeader.begin(); it2 != programHeader.end(); it2++ )
cppOut << *it2 << "\n";
if ( currentCode.find("(") == -1 )
currentCode += "()";
indentNext = 1;
break;
case eEndProgram:
currentCode = "}";
indent--;
break;
case eInclude:
currentCode.replace( "INCLUDE", "include", TRUE );
break;
case eCode:
swapKeywords( currentCode );
currentCode = swapVariables( currentCode, TRUE );
currentCode = swapConditionals( currentCode );
currentCode += ENDCHAR;
break;
case eReturn:
currentCode.replace( "RETURN", "return", TRUE );
currentCode += ENDCHAR;
break;
case eVariable:
// Swap from () to []
currentCode.replace( "(", "[" );
currentCode.replace( ")", "]" );
// version 1.1 addition to remove the '::'
currentCode.replace( "::", "" );
// See if next line is a parameter
next = it;
next++;
temp = *next;
type = determineFortranType( temp, temp, ignore, contLine );
if ( type == eParameter ) {
currentCode = currentCode.upper();
parts = QStringList::split( "=", temp, FALSE );
currentCode = currentCode + " (" + parts[1];
currentCode = cleanCode( currentCode );
swapKeywords( currentCode );
currentCode = swapVariables( currentCode, FALSE );
currentCode = "#define " + currentCode;
} else {
swapKeywords( currentCode );
currentCode = swapVariables( currentCode, TRUE );
// Clean the variable list
currentCode = currentCode.simplifyWhiteSpace();
parts = QStringList::split( " ", currentCode, FALSE );
tempInt = _CppVarLength - parts[0].length();
if ( tempInt < 0 )
tempInt = 0;
temp.fill( ' ', tempInt );
temp = parts[0] + temp;
currentCode = currentCode.right( currentCode.length() - parts[0].length() );
currentCode = temp + currentCode + ENDCHAR;
// version 1.1 addition
localVariables << currentCode;
// this skips the below code, as variables are added before the first code line
continue;
}
break;
case eDoWhile:
currentCode.replace( "WHILE", "while", TRUE );
currentCode = swapConditionals( currentCode );
currentCode = currentCode.replace( "then", "", FALSE );
currentCode = currentCode.simplifyWhiteSpace();
indentNext = 1;
break;
case eIf:
currentCode.replace( "IF", "if", TRUE );
currentCode = swapConditionals( currentCode );
//currentCode = currentCode.replace( "then", "", FALSE );
currentCode = currentCode.simplifyWhiteSpace();
//indentNext
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
convert_Fortran_Code.zip (5个子文件)
convertFortranCode
convertFortran.sln 917B
convertFortran.vcproj 5KB
main.cpp 23KB
main.h 4KB
convert.info 1012B
共 5 条
- 1
资源评论
- wangxiaolong8862013-10-07我是帮朋友下载的,具体没有使用过,没有发言权,不过有就比没有好,这是网络上少有的此类语言转换脚本
- bluemoon13162012-05-08不好用,有些函数不能转.
SwordShadow
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功