Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "commandlineargs.h"
23 #include "sntpclientengine.h"
26 LOCAL_C void RunSNTPClientL();
27 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs);
30 GLDEF_C TInt E32Main() // main function called by E32
33 CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
34 TRAPD(err, RunSNTPClientL());
35 delete cleanup; // destroy clean-up stack
37 return err; // and return
42 LOCAL_C void RunSNTPClientL()
44 TRAP_IGNORE(Util::SetAppropriateTimezoneL());
46 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
47 CleanupStack::PushL(scheduler);
48 CActiveScheduler::Install(scheduler);
50 // Parse the command line
52 TCommandLineArgs args;
53 ParseCommandLineL(args);
55 // Create the client object, and allow it to run to completion
57 CSNTPClient* engine = CSNTPClient::NewLC(args);
59 CActiveScheduler::Start();
61 args.iServers.ResetAndDestroy();
63 CActiveScheduler::Install(NULL);
65 if (engine->State() == EStateFailed)
67 User::Leave(KErrGeneral);
69 else if (engine->State() == EStateAborted)
71 User::Leave(KErrTimedOut);
74 CleanupStack::PopAndDestroy(2, scheduler);
78 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs)
81 /* Get and parse the command line arguments */
83 TBool parsingOffset(EFalse);
84 TBool enoughArguments(EFalse);
87 aArgs.iApplyDaylightSavings = EFalse;
89 CCommandLineArguments* args = CCommandLineArguments::NewLC();
90 TInt argCount = args->Count();
92 /* Ignore the first argument, this is the executable name */
94 for (TInt i = 1; i < argCount; ++i)
97 TPtrC arg = args->Arg(i);
103 if (arg.Length() != 3)
105 User::Leave(KErrArgument);
109 TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0'));
113 aArgs.iOffset = -temp;
117 aArgs.iOffset = temp;
120 parsingOffset = EFalse;
123 else if (arg.CompareF(KCommandLineDaylightSavings) == 0)
125 aArgs.iApplyDaylightSavings = ETrue;
127 else if (arg.CompareF(KCommandLineOffset) == 0)
129 parsingOffset = ETrue;
133 // This argument must be the IP address/hostname
134 // sanity check the name
136 if (arg.Length() > 256)
138 User::Leave(KErrArgument);
141 HBufC* server = arg.AllocLC();
142 User::LeaveIfError(aArgs.iServers.Append(server));
143 CleanupStack::Pop(server);
145 enoughArguments = ETrue;
150 // Sanity check the arguments
151 if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13)
153 User::Leave(KErrArgument);
156 CleanupStack::PopAndDestroy(args);