⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opltran.cpp

📁 在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己的开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
LOCAL_C void ReadResourceFileL(TDes& aDes, const TInt& aResourceId,const TInt &aFileType)
	{
	TFileName fileName(EpocRoot);
	TInt signature;
	if(aFileType==EOplResource)
		{
		fileName.Append(KOpltranResourceFile);
		signature=KOpltranResourceSignature;
		}
		else
		{
		fileName.Append(KEikResourceFile);
		signature=KEikResourceSignature;
		}
	BaflUtils::NearestLanguageFile(theFileServer,fileName);
	TParse resourceFile;
	resourceFile.Set(fileName,&ExeDrive,NULL);

	RResourceFileU rsc;
	TRAPD(r,rsc.OpenL(theFileServer,(resourceFile.FullName())));
	if (r!=KErrNone)
		{
		_LIT(KUnableToOpenResource,"\nUnable to open resource file %S\n");
		theConsole->PError(KUnableToOpenResource,&(resourceFile.FullName()));
		}
		else
		{
		// !! Need cleanup protection around here?
		TRAPD(ss,rsc.ConfirmSignatureL(signature));
		if (ss!=KErrNone)
			; 
		else
			{
			TRAPD(rr,rsc.ReadL(aDes,aResourceId));
			if (rr!=KErrNone)
				{
				// Can't use ShowError() here.
				_LIT(KErrorReadingResource,"Error %d reading resource file %S\n");
				theConsole->PError(KErrorReadingResource,rr,&(resourceFile.FullName()));
				}
			}
		}	
	rsc.Close();
	}

LOCAL_C TInt SystemResourceFileOffset()
	{
	TFileName rscFileName(EpocRoot);
	rscFileName.Append(KEikResourceFile);
	BaflUtils::NearestLanguageFile(theFileServer,rscFileName);
	TInt offset=0;
	RResourceFile rsc;
	TRAPD(r,rsc.OpenL(theFileServer,rscFileName));
	if (r==KErrNone)
		{
		TRAPD(ss,rsc.ConfirmSignatureL(KEikResourceSignature));
		if (ss==KErrNone)
			{
			offset=rsc.Offset();
			}
		}
	rsc.Close();
	return(offset);
	}

LOCAL_C void GetTranslateErrorText(TDes& aDes,TInt aError)
	{
	if (aError<0)
		{
		if (aError>KErrNotFound || aError<KErrDirFull)
			aError=KErrUnknown;
		TRAPD(r,ReadResourceFileL(aDes,SystemResourceFileOffset()+1-aError,EEikResource));
		if(r!=KErrNone)
			{
			_LIT(KErrorReadingEikonResource,"Error %d reading Eikon resource file\n");
			theConsole->PError(KErrorReadingEikonResource,r);
			}
		}
	else
		{ // must be a translator internal error
		TRAPD(rr,ReadResourceFileL(aDes,GetTranslatorErrorResourceNumber(aError),EOplResource));
		if (rr!=KErrNone)
			{
			_LIT(KErrorReadingOPLTResource,"Error %d reading OPLT resource file\n");
			theConsole->PError(KErrorReadingOPLTResource,rr);
			}
		}
	}

LOCAL_C void ShowError(const TDesC& aMsg,TInt aErr,const TDesC& aExtra/*=KNullDesC*/)
// Reports errors in opltran.exe, *not* translation errors in OPL source.
	{
	TBuf<128> ErrorDes;
	GetTranslateErrorText(ErrorDes,aErr);
	_LIT(KErrorString,"Error %S %S: %S\n");
	theConsole->PError(KErrorString,&aMsg,&aExtra,&ErrorDes);
	}

#ifdef _ERROR_FILE
LOCAL_C void SetErrorFileName(const TDesC& aName,const TFileName& aPath)
	{
	ErrorFileDetails.Set(aName,&aPath,NULL);
	}

LOCAL_C void KillErrorFile()
	{
	TInt r=theFileServer.Delete(ErrorFileDetails.FullName());
	if ((r!=KErrNone) && (r!=KErrNotFound))
		{
		_LIT(KDeletingErrFile,"deleting err file");
		ShowError(KDeletingErrFile,r,ErrorFileDetails.FullName());
		}
	}	

LOCAL_C TInt OpenErrorFile()
	{
	TUint Mode=EFileShareExclusive|EFileStreamText|EFileWrite;
	TInt r=ErrorFile.Replace(theFileServer,ErrorFileDetails.FullName(),Mode);
	
	if (r!=KErrNone)
		{
		_LIT(KCreatingErrFile,"creating err file");
		ShowError(KCreatingErrFile,r,ErrorFileDetails.FullName());
		return r;
		}

	ErrorFileOpen=ETrue;
	return r;
	}

LOCAL_C TInt LogError(const TDesC& aText)
// Attempt to write the error text to the error file.
	{
	if (ErrorFileOpen==EFalse)
		{
		TInt r=OpenErrorFile();
		if (r!=KErrNone)
			return r;
		}
		
	TInt r=ErrorFile.Write(aText);
	if (r!=KErrNone)
		{
		_LIT(KWritingErrFile,"writing err file");
		ShowError(KWritingErrFile,r,ErrorFileDetails.FullName());
		return r;
		}

	r=ErrorFile.Flush();
	if (r!=KErrNone)
		{
		_LIT(KFlushingErrFile,"flushing err file");
		ShowError(KFlushingErrFile,r,ErrorFileDetails.FullName());
		return r;
		}
	return r;
	}

LOCAL_C void TransError(TRefByValue<const TDesC> aFmt,...)
// Errors detected by translator. They are written to error file.
	{
	VA_LIST list;
	VA_START(list,aFmt);
	aFileBuf.Zero();
	aFileBuf.AppendFormatList(aFmt,list);
	TInt r=LogError(aFileBuf);
	if (r==KErrNone)
		{
		// Give the user the bad news.
		_LIT(KTranslationFailed,"Translation failed.\n");
		theConsole->Write(KTranslationFailed);
		}
	else
		theConsole->PError(aFileBuf); // Can't write to file, so stick it on the console.
	}
#endif

LOCAL_C void ConvertFile(const TDesC& aSource)
	{
	_LIT(KConverting,"Converting %S\n");
	theConsole->Printf(KConverting,&aSource);

	MTextSource* textSrc;
	TFileName fileName=aSource;
	TInt err=theTextSystem.OpenSource(fileName,textSrc);
	if (err!=KErrNone)
		{
		_LIT(KInConversion,"in conversion");
		ShowError(KInConversion,err);
		return;
		}

	TRAPD(r,((CFileSource*)textSrc)->ConvertL(theFileServer,aSource));

	if (r!=KErrNone)
		{
		_LIT(KConvertError,"convert error");
		ShowError(KConvertError,r);
		}
	}

LOCAL_C void TranslateFile(const TDesC& aSource)
	{
	if (ConvertFlag!=ENoConvert)
		{
		ConvertFile(aSource);
		return;
		}
	_LIT(KTranslating,"Translating %S\n");
	theConsole->Printf(KTranslating,&aSource);
	TParse output;
	_LIT(KOpo,".opo");
	TInt r=theFileServer.Parse(KOpo,OutputPath,output);

	if (r!=KErrNone)
		{
		_LIT(KParsingOutputFileName,"parsing output file name");
		ShowError(KParsingOutputFileName,r,output.FullName());
		}
	else
		{
		TSourceTranslateError anErr;
		TRAP(r,theTranslator->StartTranslateL(theTextSystem,aSource,output.FullName(),theDebugFlag,anErr));
		if (r!=KErrNone)
			{
			_LIT(KInStartTranslate,"in StartTranslate");
			ShowError(KInStartTranslate,r);
			}
		else
			{
			do
				{
				r=theTranslator->TranslateChunk();
				} while (r==KErrNone);
			if (r!=KErrGeneral || anErr.Error()!=EErrSuccess)
				{
#if !defined(__UIQ__)
				ExitValue=EXIT_FAILURE;
#endif
				TBuf<128> ErrorDes;
				GetTranslateErrorText(ErrorDes,anErr.Error());

				MTextSource* textSrc;
				TFileName fileName=anErr.Source();
				if (theTextSystem.OpenSource(fileName,textSrc)!=KErrNone)
					{
					_LIT(KTranslatingFailed,"\n%S : Translating failed with Error %d, at byte offset %d\n");
					_LIT(KErrorLocationFailed,"Failed to open %S to locate error\n");
					theConsole->PError(KTranslatingFailed,&anErr.Source(),anErr.Error(),anErr.Position());
					theConsole->PError(KErrorLocationFailed,&anErr.Source());
					}
				else
					{
					TInt line=0;
					TInt offset=0;
					TRAP(r,((CFileSource*)textSrc)->MapPosToLineAndOffsetL(theFileServer,anErr.Source(),
						anErr.Position(),line,offset));

					if (r!=KErrNone)
						{
						_LIT(KMappingTranslatorError,"mapping translator error");
						ShowError(KMappingTranslatorError,r);
						}
					else
						{
						if (ErrorFormatFlag==EErrorFormatODE)
							{
							// ODE format "Error LOC::C:\TEMP\TEST.OPL 5: Duplicate Name"
							_LIT(KODEFormatError,"Error LOC::%S %d: %S\n");
							theConsole->PError(KODEFormatError,&anErr.Source(),line,&ErrorDes);
							}
						else
							{
							// MSDEV etc format. "C:\TEMP\TEST.OPL(5) : error 19 : column 18 : Syntax error"
							_LIT(KMSDevFormatError,"%S(%d) : error %d : column %d : %S\n");
							theConsole->PError(KMSDevFormatError,&anErr.Source(),line,anErr.Error(),offset,&ErrorDes);
							}
						}
					}
				}
			else
				{
				_LIT(KTranslatedOK,"%S translated OK\n");
				theConsole->Printf(KTranslatedOK,&aSource);
				}
			}		
		}
	}

LOCAL_D void Translate(TParse& aSource)
// Checks for wild-cards and does the necessary
	{
	if (!aSource.IsWild())
		TranslateFile(aSource.FullName());
	else
		{
		RDir dir;
		// Note that opening "*." will not supply un-extensioned files (like Toolbar) 
		// - a bug in Open?)
		TInt r=dir.Open(theFileServer,aSource.FullName(),KEntryAttNormal);

		if (r!=KErrNone)
			{
			_LIT(OpendingDir,"opening directory");
			ShowError(OpendingDir,r);
			}
		else
			{
			TEntry entry;
			TInt TryOne=EFalse;
			while (dir.Read(entry)==KErrNone)
				{
				TryOne=ETrue;
				// Lose any .* wildcard extensions
				theFileServer.Parse(entry.iName,aSource.DriveAndPath(),aSource);

				// Set output path to match.
				SetOutputPath(aSource);
				TranslateFile(aSource.FullName());
				}
			if (TryOne==EFalse)
				{
				_LIT(LocatingSource,"locating source");
				ShowError(LocatingSource,KErrNotFound,aSource.FullName());
				}
			}
		}
	}

class MAppCallBack : public MTranAppCB
	{
public:
	void OplTargetIsAppL(TFileName& aName);
	};

void MAppCallBack::OplTargetIsAppL(TFileName& aName) // the call back.
	{
	TParse appParse;
	TFileName defaultAppPath(EpocRoot);
	defaultAppPath.Append(KDefaultAppPath);
	User::LeaveIfError(appParse.SetNoWild(defaultAppPath,&aName,NULL));
	User::LeaveIfError(appParse.AddDir(appParse.Name()));
	aName=appParse.FullName();

	TFileName parseName=appParse.Name();
	TParse AllButExt;
	AllButExt.Set(appParse.DriveAndPath(),&parseName,NULL);
	
	_LIT(KVerboseOutputting,"[Outputting to %S]\n");
	theConsole->PVerbose(KVerboseOutputting,&(AllButExt.FullName()));
	}

_LIT(KOldRoot,"epoc32\\");

// _LIT(KTest,"K:\\Crystal6.0\\test\\sdk\\epoc32\\ver1\\epoc32\\release\\winc\\...");

TPtrC FindEpocRootL(const TDesC& aExeName)
	{
	TInt next=0;
	TInt index=0;
	TInt remainder=0;
	TInt step=0;
	TPtrC search(aExeName);
//	search.Set(KTest);
	while ((next=search.FindF(KOldRoot))!=KErrNotFound)
		{
		step=next+KOldRoot().Length();
		index+=step;
		remainder=search.Length()-step;
		if (!(remainder>0))
			break;
		search.Set(search.Right(remainder));
		}
	if (!index) // not found
		User::Leave(KErrBadName);
	return(aExeName.Left(index-KOldRoot().Length()));
	}

void DoE32MainL()
	{
	EpocRoot.Set(FindEpocRootL(RProcess().FileName()));
	TRAPD(err,_WingetcwdL());

	TParse src;

	_LIT(KConsoleName,"OPLTRAN\n");
	// Console::NewL() leaves with 'Not supported' on UIQ WINSCW.
	TRAPD(r, theConsole=REINTERPRET_CAST(CConsoleReporter*,Console::NewL(KConsoleName,TSize(KDefaultConsWidth,KDefaultConsHeight))));
#if !defined(__UIQ__)
	if (r!=KErrNone)
		User::Beep(440,5);
	else
#endif
		{
		theFileServer.Connect();
		theTextSystem.Connect();
		FbsStartup(); // icon embedding during AIF construction.
		RFbsSession::Connect();
		TRAPD(r,theTranslator=COplModuleTranslator::NewL())
		if (r!=KErrNone)
			{
			_LIT(KAllocTranslator,"allocating the translator");
			ShowError(KAllocTranslator,r);
			}
		else
			{
			TRAP(r,theTranslator->SetTarget(EOplTargetER1));
			if (r!=KErrNone)
				{
				_LIT(KSettingTarget,"setting translator target");
				ShowError(KSettingTarget,r);
				}
			else
				{
				MAppCallBack myAppCB;
				TRAP(r,theTranslator->SetAppCB(&myAppCB));
				if (r!=KErrNone)
					{
					_LIT(KSettingCallback,"setting app callback");
					ShowError(KSettingCallback,r);
					}
				else if (ParseCommandLineL(src)) // Passed something plausible on the command line 
					{
#ifdef _ERROR_FILE
					SetErrorFileName(KErrName,InputPath);
					KillErrorFile();
#endif
					Translate(src);
#ifdef _ERROR_FILE
					if (ErrorFileOpen)
						ErrorFile.Close();
#endif
					}
				else
					{

					_LIT(KUsageLine1,"Usage: OPLTRAN infile [-ipath] [-opath] [-s] [-e] [-q] [-v]\n");
					_LIT(KUsageLine2,"      infile may include wild cards e.g. *.opl\n");
					_LIT(KUsageLine3,"      -ipath specifies path for INCLUDE files\n");
					_LIT(KUsageLine4,"      -opath sets the output directory for OPO files\n");
					_LIT(KUsageLine5,"      -s generates symbolic debugging info\n");
					_LIT(KUsageLine6,"      -e use alternate error format\n");
					_LIT(KUsageLine7,"      -q quiet (only report errors)\n");
					_LIT(KUsageLine8,"    Convertor functions (no translation)\n");
					_LIT(KUsageLine9,"      -conv converts ASCII files to Unicode (and vice versa)\n");
					_LIT(KUsageDebugLine1,"      -v verbose (overrides -q)\n");
					//
					theConsole->Printf(KUsageLine1);
					theConsole->Printf(KUsageLine2);
					theConsole->Printf(KUsageLine3);
					theConsole->Printf(KUsageLine4);
					theConsole->Printf(KUsageLine5);
					theConsole->Printf(KUsageLine6);
					theConsole->Printf(KUsageLine7);
#ifdef _DEBUG
					theConsole->Printf(KUsageDebugLine1);
#endif
					theConsole->Printf(KUsageLine8);
					theConsole->Printf(KUsageLine9);
					}
				}
			}
		delete theTranslator;
		}

	RFbsSession::Disconnect();
	theTextSystem.Close();
	theFileServer.Close();

	delete theConsole;
	}

GLDEF_C TInt E32Main()
	{
	CTrapCleanup *trapCleanup=CTrapCleanup::New();
	TRAPD(err,DoE32MainL());
	delete trapCleanup;
#if !defined(__UIQ__)
	if (err)
		ExitValue=EXIT_FAILURE;
 	exit(ExitValue);
#endif
	// no need to return a value as execution never reaches this line.
	return err;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -