📄 opltrant.cpp
字号:
// OPLTRANT.CPP
//
// Copyright (c) 1998-2002 Symbian Ltd. All rights reserved.
// Based on TEXTFILE.CPP
// Classes to read ASCII and Symbian OS native files into memory as MTextSources
#include <f32file.h>
#include <s32file.h> // file store
#include <txtglobl.h> // global text
#include <txtfmlyr.h> // paraformatlevel
#include <apparc.h> // streamdictionary
#include "winutil.h"
#include "opltrant.h"
GLREF_D TFileName IncludePath; // For INCLUDE'd files.
GLREF_D TFileName InputPath;
GLREF_D RFs theFileServer;
GLREF_D TFileName OutputPath;
GLREF_D TConvertFlag ConvertFlag;
GLREF_D TPtrC EpocRoot;
const TUid KUidTextEdApp={0x10003A63};
_LIT(KSystemIncDir,"epoc32\\wins\\c\\System\\OPL\\");
_LIT(KTextIncDir,"epoc32\\winc\\OPL\\");
_LIT(KFileExtensionDot,".");
_LIT(KFileExtensionTpl,".tpl");
_LIT(KFileExtensionTph,".tph");
_LIT(KFileExtensionTxh,".txh");
_LIT(KFileExtensionTsg,".tsg");
_LIT(KFileExtensionOpl,".opl");
_LIT(KFileExtensionOph,".oph");
_LIT(KFileExtensionOxh,".oxh");
_LIT(KFileExtensionOsg,".osg");
/////////////////////////////////////////////////////////////////////////////////
//
// ROplTextSystem
//
/////////////////////////////////////////////////////////////////////////////////
//!!TODO demo.tpl comes up Error in StartTranslate: "not found"
TInt ROplTextSystem::OpenSource(TDes& aFileName, MTextSource*& aSource)
{
TParse name;
TInt ret=KErrNoMemory;
// Try the programs own folder
name.Set(aFileName,&InputPath,NULL);
ret=DoOpenSource(name.FullName(),aSource);
if (ret!=KErrNone)
{
// Include folder from command line.
name.Set(aFileName,&IncludePath,NULL);
ret=DoOpenSource(name.FullName(),aSource);
if (ret!=KErrNone)
{
// Text WINC folder.
TFileName textIncPath(EpocRoot);
textIncPath.Append(KTextIncDir);
name.Set(aFileName,&textIncPath,&IncludePath); // Use IncludePath to get drive name.
ret=DoOpenSource(name.FullName(),aSource);
if (ret!=KErrNone)
{
// 'ROM'
// Was Z: ROM, now it's C: System
TFileName systemPath(EpocRoot);
systemPath.Append(KSystemIncDir);
name.Set(aFileName,&systemPath,&IncludePath);
ret=DoOpenSource(name.FullName(),aSource);
}
}
}
aFileName=name.FullName();
return ret;
}
TBool ROplTextSystem::IsOplSourceFileL(const TDesC& aFileName)
//
// Check for Symbian OS native OplSourceUidType file.
//
{
CFileStore *store;
const TInt fileMode=EFileStream|EFileRead|EFileShareReadersOnly;
store=CDirectFileStore::OpenLC(theFileServer,aFileName,fileMode);
const TUidType CheckUidType=store->Type();
// Destroy the permanent file store object (closes the file)
CleanupStack::PopAndDestroy();//store
TBool ret;
//!!TODO is this TUidType ctored from the triple ctor?
if (CheckUidType==TUidType(KDirectFileStoreLayoutUid,KUidAppDllDoc,KUidTextEdApp))
ret=ETrue;
else
ret=EFalse;
return ret;
}
TInt ROplTextSystem::DoOpenSource(const TDesC& aFileName, MTextSource*& aSource)
{
CFileSource *pT;
TBool nativeFormat=ETrue;
TRAPD(err,(nativeFormat=IsOplSourceFileL(aFileName)));
//test for missing file
if (err==KErrNotFound || err==KErrPathNotFound)
return err;
// err may set for ASCII file.
if ((err==KErrNone) && (nativeFormat))
pT=new CEpocTextSource(ConvertFlag);
else
pT=new CCrLfTextSource(ConvertFlag);
TInt ret=KErrNone;
if (pT!=NULL)
{
TRAP(ret,pT->OpenL(*this,aFileName));
if (ret==KErrNone)
aSource=pT;
else
delete pT;
}
return ret;
}
//////////////////////////////////////////////////////////////////////////////////
//
// CFileSource
//
//////////////////////////////////////////////////////////////////////////////////
TInt CFileSource::Read(TDes& aBuf,TInt aPos)
//
// Reads as much as will fit (or remains) from aPos
//
{
TInt ret=KErrEof;
TInt len=iContents.Length()-aPos;
if (len>0)
{
if (len>aBuf.MaxLength())
len=aBuf.MaxLength();
aBuf.Copy(iContents.Mid(aPos,len));
iPos=aPos+len;
ret=KErrNone;
}
return ret;
}
TInt CFileSource::Read(TDes& aBuf)
//
// Reads as much as will fit (or remains) from immediately after the last read.
//
{
return Read(aBuf,iPos);
}
void CFileSource::Close()
{
delete this;
}
CFileSource::CFileSource(TConvertFlag aFlag) : iContents(NULL,0),iConvert(aFlag)
{
}
CFileSource::~CFileSource()
//
// Deletes the buffer
//
{
User::Free((TAny *)iContents.Ptr());
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// ASCII Text methods
//
/////////////////////////////////////////////////////////////////////////////////////////
void CCrLfTextSource::OpenL(RFs& aFs,const TDesC& aFileName)
//
// Loads up the contents of the file, making it into TText and replacing CrLfs with 0's
//
{
// Open the file and make room for it
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
TPtr8 contents8((TText8 *)User::AllocL(size),0,size);
// Read it in reasonable chunks replacing Cr with a KTextTranLineDelimiter
// but skipping the Lf.
TPtr8 readBuf((TUint8 *)User::AllocLC(0x1000*sizeof(TUint8)),0x1000); // push
TChar c=NULL;
FOREVER
{
TInt ret=file.iObj.Read(readBuf);
TInt readLen=readBuf.Length();
if (ret==KErrEof || readLen==0) // Cos read on an empty file is a bit odd.
break;
User::LeaveIfError(ret);
for (TInt index=0;index<readLen;index++)
{
c=readBuf[index];
if (c=='\n'); // skip it
else
{
if (c=='\r')
c=KNarrowTextTranLineDelimiter;
contents8.Append(c);
}
}
}
// Make sure the last line ends correctly, for translation only!
if (iConvert==ENoConvert && c!=KNarrowTextTranLineDelimiter)
contents8.Append(KNarrowTextTranLineDelimiter);
User::Free((TAny *)iContents.Ptr());
iContents.Set((TText *)User::AllocL(sizeof(TText)*size),0,size);
ConvertToUnicodeL(iContents,contents8);
ConvertChars(iContents,KNarrowTextTranLineDelimiter,KUnicodeTextTranLineDelimiter);
CleanupStack::PopAndDestroy(); // the alloc'd
}
void CCrLfTextSource::MapPosToLineAndOffsetL(RFs& aFs,const TDesC& aFileName,
const TInt aErrorPos,TInt& aLine, TInt& aColOffset)
{
// aErrorPos uses normal indexing -- 0 for the first char etc.
TAutoClose<RFile> file;
User::LeaveIfError(file.iObj.Open(aFs,aFileName,EFileRead|EFileShareExclusive|EFileStreamText));
TInt size=0;
User::LeaveIfError(file.iObj.Size(size));
// Allow for the CR we append
//!!TODO Support aErrorPos still being one char beyond the end of file!
TInt errorPos=aErrorPos;
if (errorPos>size)
{
if (errorPos==size+1)
errorPos=size; // Hit error on last char in file.
else
User::Leave(KErrEof);
}
size=errorPos;
// Read it in reasonable chunks replacing Cr with a KTextTranLineDelimiter
// and remembering to fudge the lost Lf chars.
TPtr8 readBuf((TUint8 *)User::AllocLC(0x1000*sizeof(TUint8)),0x1000);//push
TChar c;
TInt bufferRemaining=0;
TInt index=0;
TInt checkPos=0;
aColOffset=0;
aLine=1;
FOREVER
{
// buffer check.
if (bufferRemaining==0)
{
// buffer read, test for eof and set index to 0.
index=0;
TInt ret=file.iObj.Read(readBuf);
bufferRemaining=readBuf.Length();
if (ret==KErrEof||bufferRemaining==0)
User::Leave(KErrEof);
}
c=readBuf[index++]; bufferRemaining--;
aColOffset++;
if (checkPos++==errorPos)
break; // found it
if (c=='\r') // CR
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -