symport/symuser/exec.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <stdio.h>
       
    17 #include <stdlib.h>
       
    18 #include <time.h>
       
    19 #include <string.h>
       
    20 
       
    21 #ifndef _WIN32
       
    22 #include <unistd.h>
       
    23 #else
       
    24 #include <windows.h>
       
    25 #endif
       
    26 
       
    27 #include "e32base.h"
       
    28 
       
    29 extern int gArgc;
       
    30 extern char **gArgv;
       
    31 
       
    32 // The exec class implements low level functionality needed by the TOOLS2 port
       
    33 // These are normally kernel executive calls
       
    34 class Exec
       
    35 	{
       
    36 	static TInt TimeNow(TInt64&, TInt&);
       
    37 	static TInt TimeNowSecure(TInt64&, TInt&);
       
    38 	static void DebugPrint(TAny* aPtr, TInt);
       
    39 	static TUint32 MathRandom();
       
    40 	static void ProcessCommandLine(TInt, TDes8&);
       
    41 	static TInt ProcessCommandLineLength(TInt);
       
    42 	static TUint TickCount();
       
    43 	static TUint32 NTickCount();
       
    44 	static void After(TInt, TRequestStatus&);
       
    45 	static void AfterHighRes(TInt, TRequestStatus&);
       
    46 	};
       
    47 
       
    48 // ***
       
    49 // after
       
    50 //
       
    51 
       
    52 void Exec::After(TInt aInterval, TRequestStatus&)
       
    53 	{
       
    54 #ifdef _WIN32
       
    55 	Sleep(aInterval/1000);
       
    56 #else
       
    57 	sleep(aInterval/1000000);
       
    58 #endif
       
    59 	}
       
    60 
       
    61 void Exec::AfterHighRes(TInt aInterval, TRequestStatus&)
       
    62 	{
       
    63 #ifdef _WIN32
       
    64 	Sleep(aInterval/1000);
       
    65 #else
       
    66 	sleep(aInterval/1000000);
       
    67 #endif
       
    68 	}
       
    69 
       
    70 // ***
       
    71 // tick
       
    72 //
       
    73 TUint Exec::TickCount()
       
    74 	{
       
    75 	return (TUint)clock();
       
    76 	}
       
    77 
       
    78 TUint32 Exec::NTickCount()
       
    79 	{
       
    80 	return (TUint32)clock();
       
    81 	}
       
    82 
       
    83 // ***
       
    84 // command line
       
    85 //
       
    86 void Exec::ProcessCommandLine(TInt, TDes8& aCmd)
       
    87 	{
       
    88 	TPtr16 aCommand16((TUint16*)aCmd.Ptr(),aCmd.MaxLength()>>1);
       
    89 	for(TInt i = 1; i < gArgc; i++)
       
    90 		{
       
    91 		if (i > 0)
       
    92 			aCommand16.Append(' ');
       
    93 
       
    94 		for(TUint s = 0; s < strlen(gArgv[i]); s++)
       
    95 			{
       
    96 			aCommand16.Append(gArgv[i][s]);
       
    97 			}
       
    98 		}
       
    99 	aCmd.SetLength(aCommand16.Length()<<1);
       
   100 	}
       
   101 
       
   102 TInt Exec::ProcessCommandLineLength(TInt)
       
   103 	{
       
   104 	TInt len = 0;
       
   105 	for(TInt i = 1; i < gArgc; i++)
       
   106 		{
       
   107 		if (i > 0)
       
   108 			len++;
       
   109 		len += strlen(gArgv[i]);
       
   110 		}
       
   111 	return len;
       
   112 	}
       
   113 
       
   114 // ***
       
   115 // debug
       
   116 //
       
   117 void Exec::DebugPrint(TAny* aPtr, TInt)
       
   118 	{
       
   119 	TPtr8* p = (TPtr8*)aPtr;
       
   120 	for(TInt i = 0; i < p->Length(); i++)
       
   121 		putchar((*p)[i]);
       
   122 
       
   123 	}
       
   124 
       
   125 // ***
       
   126 // rand
       
   127 //
       
   128 TUint32 Exec::MathRandom()
       
   129 	{
       
   130 	return rand();
       
   131 	}
       
   132 
       
   133 // ***
       
   134 // time
       
   135 //
       
   136 TInt Exec::TimeNow(TInt64& aUniversalTime, TInt& aUniversalTimeOffset)
       
   137 	{
       
   138 	aUniversalTimeOffset = 0;
       
   139 
       
   140 	time_t t = time(NULL);
       
   141     struct tm *tm = gmtime(&t);
       
   142     TTime tt = TDateTime(1900 + tm->tm_year, TMonth(tm->tm_mon), tm->tm_mday - 1, tm->tm_hour, tm->tm_min, tm->tm_sec, 0);
       
   143     aUniversalTime = tt.Int64();
       
   144 
       
   145     return KErrNone;
       
   146 	}
       
   147 
       
   148 TInt Exec::TimeNowSecure(TInt64& aUniversalTime, TInt& aUniversalTimeOffset)
       
   149 	{
       
   150 	return Exec::TimeNow(aUniversalTime, aUniversalTimeOffset);
       
   151 	}