The Software cannot constitute the primary value of any new software derived
from or incorporating
源代码在线查看: response.cpp
/*
* ============================================================================
* Name : CResponse from Response.cpp
* Part of : TaskManager
* Created : 08/31/2005 by Forum Nokia
* Version : 1.1
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include "Response.h"
#include "TaskManager.pan"
// CONSTANTS
_LIT(KError, "#Error:");
_LIT(KOkMessage, "#OK");
_LIT(KTab, "\t");
_LIT(KSeparator, "#");
// ================= MEMBER FUNCTIONS =======================
// constructor
CResponse::CResponse()
{
}
// destructor
CResponse::~CResponse()
{
iIds.Close();
delete iDescriptions;
}
// ----------------------------------------------------
// CResponse::NewL()
// Two-phased constructor.
// ----------------------------------------------------
//
CResponse* CResponse::NewL()
{
CResponse* self = CResponse::NewLC();
CleanupStack::Pop(self);
return self;
}
// ----------------------------------------------------
// CResponse::NewLC()
// Two-phased constructor.
// ----------------------------------------------------
//
CResponse* CResponse::NewLC()
{
CResponse* self = new (ELeave) CResponse;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// ----------------------------------------------------
// CResponse::ConstructL()
// Symbian OS default constructor can leave.
// ----------------------------------------------------
//
void CResponse::ConstructL()
{
iDescriptions = new (ELeave) CDesCArrayFlat(1);
}
// ----------------------------------------------------
// CResponse::InputDataL()
// This functions constructs a response object from the
// data received from the server. E.g. if aData contains
// tasks, tasks are parsed to arrays, where they can be
// easily fetched.
// ----------------------------------------------------
//
void CResponse::InputDataL(const TDesC& aData)
{
// error occurred in the server side (i.e. the aData begins #Error: ).
if (KError() == aData.Left(7))
{
// remove the #-character.
iError = aData.Mid(1);
return;
}
// response for completing task.
if (aData.Find(KOkMessage) != KErrNotFound)
{
iResponseType = ETaskComplete;
return;
}
TLex lex(aData);
TTaskReadStatus status = EStart;
// Tasks are in form: #id#description#id#description#
while (!lex.Eos())
{
if (lex.Peek() == TChar(KSeparator()[0]))
{
switch (status)
{
// first #-character found
case EStart:
{
status = EReadId;
break;
}
// read the id of the tasks.
case EReadId:
{
status = EReadTask;
TInt id;
TLex tmp(lex.MarkedToken());
User::LeaveIfError(tmp.Val(id));
User::LeaveIfError(iIds.Append(id));
break;
}
// read the description of the task.
case EReadTask:
{
status = EReadId;
TPtrC task = lex.MarkedToken();
iDescriptions->AppendL(task);
break;
}
}
lex.Inc();
lex.Mark();
}
else
{
lex.Inc();
}
}
}
// ----------------------------------------------------
// CResponse::HasError()
// Returns whether errors occurred in the server side
// or not.
// ----------------------------------------------------
//
TBool CResponse::HasError() const
{
TBool retval = EFalse;
if (iError.Length() > 0)
{
retval = ETrue;
}
return retval;
}
// ----------------------------------------------------
// CResponse::Error()
// Returns the error description.
// ----------------------------------------------------
//
TBuf CResponse::Error() const
{
return iError;
}
// ----------------------------------------------------
// CResponse::TaskCount()
// Returns the amount of tasks received from the server.
// ----------------------------------------------------
//
TInt CResponse::TaskCount() const
{
return iIds.Count();
}
// ----------------------------------------------------
// CResponse::TaskDescription()
// Returns a task description.
// ----------------------------------------------------
//
TBuf CResponse::TaskDescription(const TInt& aIndex) const
{
if (aIndex >= iDescriptions->Count() || 0 > aIndex)
{
Panic(ETaskManagerInvalidTaskIndex);
}
return (*iDescriptions)[aIndex];
}
// ----------------------------------------------------
// CResponse::TaskId()
// Returns a task id.
// ----------------------------------------------------
//
TInt CResponse::TaskId(const TInt& aIndex) const
{
if (aIndex >= iDescriptions->Count() || 0 > aIndex)
{
Panic(ETaskManagerInvalidTaskIndex);
}
return iIds[aIndex];
}
// ----------------------------------------------------
// CResponse::ResponseType()
// Returns the type of this response.
// ----------------------------------------------------
//
CResponse::TResponseType CResponse::ResponseType() const
{
return iResponseType;
}
// End of file