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

📄 opltrant.cpp

📁 在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己的开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			aLine++;
			aColOffset=0;
			}
		else if (c=='\n') //LF
			{ // Skip it.
			aColOffset--;
			checkPos--;
			}
		}
	CleanupStack::PopAndDestroy(); // the alloc'ed above.
	}

TInt CCrLfTextSource::ConvertL(RFs& aFs,const TDesC& aSourceName)
//
// Export iContents as Symbian OS document - weird form of export I know... 
//
	{
	// Determine the outputfile name, based on the source extension.
	//		.txh	-> .oxh
	//		.tph	-> .oph
	//		.tsg	-> .osg
	//		.tpl	->
	//		.opl	->
	TParse inputname;
	// Ensure the name has an extension.
	TFileName dot(KFileExtensionDot);
	inputname.Set(aSourceName,NULL,&dot);
	TBuf<4> sourceExtn,outputExtn;

	sourceExtn=inputname.Ext().Left(4);
	sourceExtn.LowerCase();
	if (sourceExtn.Find(KFileExtensionTxh)==0)
		outputExtn=KFileExtensionOxh;
	else if (sourceExtn.Find(KFileExtensionTph)==0)
		outputExtn=KFileExtensionOph;
	else if (sourceExtn.Find(KFileExtensionTsg)==0)
		outputExtn=KFileExtensionOsg;
	else if (sourceExtn.Find(KFileExtensionTpl)==0)
		outputExtn=KNullDesC;
	else if (sourceExtn.Find(KFileExtensionOpl)==0)
		outputExtn=KNullDesC;
	else
		outputExtn=KNullDesC; // default no extension

	TParse output;
	TInt r=theFileServer.Parse(outputExtn,OutputPath,output);
	if (r!=KErrNone)
		return r;

	aFs.Delete(output.FullName());
	TRAP(r,SaveL(aFs,output.FullName()));
	return r;
	}

void CCrLfTextSource::SaveL(RFs& aFs,const TDesC& aFileName)
	{
	CDirectFileStore* store=NULL;

	store=CDirectFileStore::CreateLC(aFs,aFileName,EFileShareExclusive|EFileWrite);//pushes store
	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KUidAppDllDoc,KUidTextEdApp));
	// store main in temp
	CStreamDictionary* streamDic=CStreamDictionary::NewL();
	CleanupStack::PushL(streamDic);
	StoreL(*store,*streamDic);
	// write root stream
	TApaAppIdentifier fred;
	fred.iAppUid=KUidTextEdApp;
	CApaProcess::WriteRootStreamL(*store,*streamDic,fred);
	CleanupStack::PopAndDestroy(); // streamDic
	// close the new store
	store->CommitL();
	CleanupStack::PopAndDestroy(); // store
	}

void CCrLfTextSource::StoreL(CStreamStore& aStore,CStreamDictionary& aStreamDic) 
	{
	RStoreWriteStream stream;
	TStreamId streamId=stream.CreateLC(aStore); // pushes stream
	// convert iContents to CGlobalText
	CParaFormatLayer* paraFormatLayer=CParaFormatLayer::NewL();
	CleanupStack::PushL(paraFormatLayer);
	CCharFormatLayer* charFormatLayer=CCharFormatLayer::NewL();
	CleanupStack::PushL(charFormatLayer);
	CGlobalText* textModel=CGlobalText::NewL(paraFormatLayer,charFormatLayer);
	CleanupStack::PushL(textModel);
	textModel->InsertL(0,iContents);
	stream<<*textModel;
	stream.CommitL();
	CleanupStack::PopAndDestroy(4); // textModel,2xlayers,stream
	aStreamDic.AssignL(KUidTextEdApp,streamId);
	}

void CCrLfTextSource::ConvertChars(TDes16& aUnicode, const TChar aPrevChar, const TChar aNewChar)
	{ // convert one Unicode char to another.
	const TChar previousChar=aPrevChar;
	TBuf<1> newChar;
	newChar.Append(aNewChar);
	TInt location=KErrNotFound;
	while ((location=aUnicode.Locate(previousChar))!=KErrNotFound)
		{
		aUnicode.Replace(location,1,newChar);
		}
	}

/////////////////////////////////////////////////////////////////////////////////////////
//
// Symbian OS text methods
//
/////////////////////////////////////////////////////////////////////////////////////////

void CEpocTextSource::OpenL(RFs& aFs,const TDesC& aFileName)
	{
	TInt filesize=0;
	TRAPD(err,(filesize=SizeL(aFs,aFileName)));
	if (err!=KErrNone)
		return;

	CFileStore* store=NULL;
	// Pushes stream dictionary automatically.
	CStreamDictionary* dic=CApaProcess::ReadRootStreamLC(aFs,store,aFileName,EFileShareReadersOnly|EFileRead);
	CleanupStack::PushL(store);
	TStreamId streamId=dic->At(KUidTextEdApp);

	RStoreReadStream instream;
	instream.OpenLC(*store, streamId); // Pushes instream

	// Create format layers for global text
	CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
	CleanupStack::PushL(paraFormatLayer);
	CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
	CleanupStack::PushL(charFormatLayer);
	CGlobalText* textModel = CGlobalText::NewL(paraFormatLayer,charFormatLayer);
	CleanupStack::PushL(textModel);
	instream >> *textModel;

	TInt docSize=textModel->DocumentLength();

	//Ensure the last char is a CR.
	TBool needToAddDelim=EFalse;
	TBuf<2> end;
	textModel->Extract(end,docSize-1,1);
	if (end[0]!=KTextTranLineDelimiter)
		{
		docSize++; // =(sizeof(TText));
		needToAddDelim=ETrue;
		}

	if (iContents.Ptr())
		User::Free((TAny *)iContents.Ptr());

	iContents.Set((TText *)User::AllocL(sizeof(TText)*docSize),0,docSize);
	textModel->Extract(iContents,0,docSize);

	// Ensure there's a CR on the last line.
	if (needToAddDelim)
		iContents.Append(KTextTranLineDelimiter);
	CleanupStack::PopAndDestroy(6); // textModel & 2xformatLayers, instream,store,streamDictionary
	}

void CEpocTextSource::MapPosToLineAndOffsetL(RFs& aFs,const TDesC& aFileName,const TInt aErrorPos,
											 TInt& aLine, TInt& aOffset)
	{
	OpenL(aFs,aFileName);
	// Having filled iContents, parse it for the correct l+c
	if (aErrorPos>iContents.Length())
		User::Leave(KErrEof);
	
	aLine=1;
	aOffset=1;
	TInt readPos=0;
	TChar c;
	while (readPos<aErrorPos)
		{
		c=iContents[readPos++];
		aOffset++;
		if (c==KTextTranLineDelimiter)
			{
			aLine++;
			aOffset=1;
			}
		}
	}

TInt CEpocTextSource::ConvertL(RFs& aFs,const TDesC& aSourceName)
//
// Export the Symbian OS text as ASCII.
//
	{
	// Determine the outputfile name, based on the source extension.
	//		.oxh	-> .txh
	//		.oph	-> .tph
	//		.osg	-> .tsg
	//		.opl	-> .tpl
	//				-> .tpl
	TParse inputname;
	// Ensure the name has an extension.
	TFileName dot(KFileExtensionDot);
	inputname.Set(aSourceName,NULL,&dot);
	TBuf<4> sourceExtn,outputExtn;

	sourceExtn=inputname.Ext().Left(4);
	sourceExtn.LowerCase();
	if (sourceExtn.Find(KFileExtensionOxh)==0)
		outputExtn=KFileExtensionTxh;
	else if (sourceExtn.Find(KFileExtensionOph)==0)
		outputExtn=KFileExtensionTph;
	else if (sourceExtn.Find(KFileExtensionOsg)==0)
		outputExtn=KFileExtensionTsg;
	else if (sourceExtn.Find(KFileExtensionOpl)==0)
		outputExtn=KFileExtensionTpl;
	else // default
		outputExtn=KFileExtensionTpl;

	TParse output;
	TInt r=theFileServer.Parse(outputExtn,OutputPath,output);	
	User::LeaveIfError(r);

	// open the output file
	RFile TextFile;
	TUint mode=EFileShareExclusive|EFileStreamText|EFileWrite;
	r=TextFile.Replace(aFs,output.FullName(),mode);
	User::LeaveIfError(r);
	
	const TInt maximumLength=iContents.Length()*3; // the Unicode characters may be converted to multi-byte characters, hence the multiplication by 3 (which should be plenty)
	TPtr8 contents8((TText8 *)User::AllocL(maximumLength),0,maximumLength);
	// Lose the Unicode delimters.
	ConvertChars(iContents,KUnicodeTextTranLineDelimiter,KNarrowTextTranLineDelimiter);

	ConvertFromUnicodeL(contents8,iContents);
	
	const TInt len=contents8.Length();

	TInt pos=0;
	TInt count=0;
	TBuf8<0x3FF> writebuf(0x3FF);
	writebuf.FillZ(0x3FF);
	writebuf.SetMax();

	TText c;
	while (pos<len)
		{
		c=contents8[pos++];
		if (c==KNarrowTextTranLineDelimiter)
			{
			writebuf[count++]='\r';
			writebuf[count++]='\n';
			TextFile.Write(writebuf,count);
			count=0;
			}
		else
			writebuf[count++]=(char)c;
		}
	if (count)
		TextFile.Write(writebuf,count);

	TextFile.Flush();
	TextFile.Close();
	return r;
	}

void CEpocTextSource::ConvertChars(TDes16& aUnicode, const TChar aPrevChar, const TChar aNewChar)
	{ // convert one Unicode char to another.
	const TChar previousChar=aPrevChar;
	TBuf<1> newChar;
	newChar.Append(aNewChar);
	TInt location=KErrNotFound;
	while ((location=aUnicode.Locate(previousChar))!=KErrNotFound)
		{
		aUnicode.Replace(location,1,newChar);
		}
	}

TInt CEpocTextSource::SizeL(RFs& aFs,const TDesC& aFileName)
	{
	TAutoClose<RFile> file;
	User::LeaveIfError(file.iObj.Open(aFs,aFileName,EFileRead|EFileShareExclusive|EFileStreamText));
	TInt size=0;
	User::LeaveIfError(file.iObj.Size(size));
	size+=sizeof(TChar); // space for belts and braces terminator on the last line
	return size;
	}

⌨️ 快捷键说明

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