versit
: Versit example
// VersitExample.h
//
// Copyright (c) 2006 Symbian Software Ltd. All rights reserved.
//
//
#if (!defined __VERSITEXAMPLE_H__)
#define __VERSITEXAMPLE_H__
#include <vcard.h>
#include <VERSIT.H>
class CExampleVersit : public CBase
{
public:
static CExampleVersit* NewL();
void EgVersitL();
~CExampleVersit();
private:
void ConstructL();
void ExternalizeToFileL(const TDesC& aFile);
void InternalizeFromFileL(RFile& aFile);
void CreateAndExternalizeVCardL();
void InternalizeVCardL();
private:
CVersitParser* iParser;
RFs iFsSession;
};
#endif /* __VERSITEXAMPLE_H__ */
// VersitExample.cpp
//
// Copyright (c) 2006 Symbian Software Ltd. All rights reserved.
// This example demonstrates a vCard parser
#include <e32base.h>
#include <e32cons.h>
#include <vcard.h>
#include <versit.h>
#include "VersitExample.h"
// Global definition
static CConsoleBase* gConsole;
// name of file to write the vCard to
_LIT(KVCardFileJIS,"c:\\Private\\E8000094\\charsetJIS.vcf");
// string for display
_LIT(KMsgPressAnyKey,"\n\nPress any key to continue\n");
CExampleVersit* CExampleVersit::NewL()
{
CExampleVersit* self = new (ELeave) CExampleVersit();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CExampleVersit::ConstructL()
{
iParser = CParserVCard::NewL();
}
CExampleVersit::~CExampleVersit()
{
delete iParser;
iFsSession.Close();
}
// Externalises a vCard to a file
void CExampleVersit::ExternalizeToFileL(const TDesC& aFileName)
{
RFile file;
iFsSession.Connect();
// create the private directory (c:\Private\E8000094\)
User::LeaveIfError(iFsSession.CreatePrivatePath(EDriveC));
User::LeaveIfError(file.Replace(iFsSession, aFileName, EFileWrite));
CleanupClosePushL(file);
iParser->ExternalizeL(file);
CleanupStack::PopAndDestroy(&file);
}
// Internalises a vCard from a file
void CExampleVersit::InternalizeFromFileL(RFile& aInputFile)
{
TInt size;
if (aInputFile.Size(size)==KErrNone)
{
delete iParser;
iParser = NULL;
iParser = CParserVCard::NewL();
RFileReadStream stream(aInputFile);
CleanupClosePushL(stream);
iParser->InternalizeL(stream);
CleanupStack::PopAndDestroy(&stream);
}
}
// Creates a vCard containing a note property and a character set property parameter.
// Then externalizes the vCard to a file.
void CExampleVersit::CreateAndExternalizeVCardL()
{
//create a property value to hold some text
_LIT(KNote,"\x4e79\x4f19\x5032");
CParserPropertyValue* value=CParserPropertyValueHBufC::NewL(KNote);
CleanupStack::PushL(value);
CArrayPtr<CParserParam>* arrayOfParams = new(ELeave)CArrayPtrFlat<CParserParam>(5);
CleanupStack::PushL(arrayOfParams);
// Add a character set property parameter
CParserParam* parserParam=CParserParam::NewL(KVersitTokenCHARSET,KVersitTokenJIS);
CleanupStack::PushL(parserParam);
arrayOfParams->AppendL(parserParam);
CleanupStack::Pop(parserParam);
// create the NOTE property
CParserGroupedProperty* property=CParserGroupedProperty::NewL(*value,KVersitTokenNOTE,NULL,arrayOfParams);
CleanupStack::Pop(2,value); // value, arrayOfParams
CleanupStack::PushL(property);
// Add the property to the vCard
iParser->AddPropertyL(property);
CleanupStack::Pop(property);
//Sets the default transformation format
iParser->SetDefaultCharSet(Versit::EJISCharSet);
ExternalizeToFileL(KVCardFileJIS);
}
//Internalize the VCard
void CExampleVersit::InternalizeVCardL()
{
RFile file;
TInt err=file.Open(iFsSession,KVCardFileJIS,EFileRead);
CleanupClosePushL(file);
InternalizeFromFileL(file);
CleanupStack::PopAndDestroy(&file);
_LIT(KConsoleMessage1, "vCard has been successfully internalised from a file");
gConsole->Printf(KConsoleMessage1);
}
void CExampleVersit::EgVersitL()
{
CreateAndExternalizeVCardL();
InternalizeVCardL();
}
static void DoExampleL()
{
// Create the console to print the messages to.
_LIT(KConsoleMessageDisplay, "Versit Example");
_LIT(KConsoleStars,"\n*************************\n");
gConsole = Console::NewL(KConsoleMessageDisplay,TSize(KConsFullScreen,KConsFullScreen));
CleanupStack::PushL(gConsole);
gConsole->Printf(KConsoleMessageDisplay);
gConsole->Printf(KConsoleStars);
CExampleVersit* egVersit= CExampleVersit::NewL();
TRAPD(err, egVersit->EgVersitL());
if (err)
{
_LIT(KFailed,"\n\nExample failed: leave code=%d");
gConsole->Printf(KFailed, err);
}
delete egVersit;
// wait for user to press a key before destroying gConsole
gConsole->Printf(KMsgPressAnyKey);
gConsole->Getch();
CleanupStack::PopAndDestroy(gConsole);
}
// Standard entry point function
TInt E32Main()
{
__UHEAP_MARK;
// Active scheduler required as this is a console app
CActiveScheduler* scheduler=new CActiveScheduler;
// If active scheduler has been created, install it.
if (scheduler)
{
CActiveScheduler::Install(scheduler);
// Cleanup stack needed
CTrapCleanup* cleanup=CTrapCleanup::New();
if (cleanup)
{
TRAPD(err,DoExampleL());
delete cleanup;
}
delete scheduler;
}
__UHEAP_MARKEND;
return KErrNone;
}
// BLD.INF
// Component description file
//
// Copyright (c) 2006 Symbian Software Ltd. All rights reserved.
PRJ_MMPFILES
VersitExample.mmp
// VersitExample.mmp
//
// Copyright (c) 2006 Symbian Software Ltd. All rights reserved.
TARGET VersitExample.exe
TARGETTYPE exe
UID 0 0xE8000094
// using relative paths for source and user includes
SOURCEPATH .
SOURCE VersitExample.cpp
USERINCLUDE .
SYSTEMINCLUDE \Epoc32\include
library CHARCONV.LIB EUSER.LIB EFSRV.LIB ESTOR.LIB
library VERSIT.LIB VCARD.LIB
This example code first creates a vCard parser using the
CParserVCard
class. Then it creates a NOTE property with a
string literal as its value, using CParserGroupedProperty
and CParserPropertyValue
classes. The property is
associated with the vCard parser using the
CParserVCard::AddPropertyL()
method. Finally, the vCard is
externalized or exported to a file by calling the
CParserVCard::ExternalizeL()
method.
The example also shows how to internalize or import a vCard from a
file. A session with the file server is established using the
RFs
class to read the vCard from the file using a file
read stream (RFileReadStream
class).
Note that the procedure to create a vCal parser using the
CParserVCal
class is similar to that of creating a vCard
parser.
This is a console application and it builds an executable called
VersitExample.exe
in the standard location
(\epoc32\release\winscw\
<build_variant> for
CodeWarrior). After launching the executable, depending on the emulator you are
using, you may need to navigate from the app launcher/shell screen to view the
console. Alternatively, adding the string textshell
to the
emulator configuration file epoc.ini
causes the emulator to start
in text mode.