|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 */ |
|
16 |
|
17 |
|
18 //------------------------------------ System Header Files ------------------------------------------------ |
|
19 #include <e32cons.h> // ConsoleBase |
|
20 //------------------------------------ Local Header Files ------------------------------------------------- |
|
21 #include "openc.h" |
|
22 |
|
23 //-------------------------------- Constants, global variables and Macro Definitions ---------------------------------------- |
|
24 _LIT(KConsoleName, "NIST Test Console"); |
|
25 CConsoleBase* gConsole = NULL; |
|
26 |
|
27 |
|
28 int PrintToScreen(const char* aString); |
|
29 |
|
30 const TInt KIntStringLen = 10; |
|
31 RFs gFileSession; |
|
32 |
|
33 enum TDataType |
|
34 { |
|
35 EInteger, |
|
36 EFloat, |
|
37 EUnknownType = 0x10000 |
|
38 }; |
|
39 |
|
40 const TInt KMaxReadSize = 50; |
|
41 const TInt KMaxScreenBufferSize = 100; |
|
42 |
|
43 TBuf<KMaxScreenBufferSize> gScreenBuffer; |
|
44 |
|
45 class GlobalInitilizer |
|
46 { |
|
47 public: |
|
48 GlobalInitilizer() |
|
49 { |
|
50 TInt err = gFileSession.Connect(); |
|
51 |
|
52 if(err != KErrNone) |
|
53 { |
|
54 User::Exit(err); |
|
55 } |
|
56 |
|
57 TRAP(err, gConsole = Console::NewL(KConsoleName, TSize(KConsFullScreen,KConsFullScreen))); |
|
58 if(err != KErrNone) |
|
59 { |
|
60 User::Exit(err); |
|
61 } |
|
62 } |
|
63 ~GlobalInitilizer() |
|
64 { |
|
65 gFileSession.Close(); |
|
66 delete gConsole; |
|
67 } |
|
68 }; |
|
69 |
|
70 GlobalInitilizer globalObj; |
|
71 |
|
72 //------------------------------------ Function Definitions ----------------------------------------------- |
|
73 |
|
74 double log(double aSource) |
|
75 { |
|
76 double result = 0.0; |
|
77 Math::Ln(result, aSource); |
|
78 return result; |
|
79 } |
|
80 |
|
81 double exp(double aSource) |
|
82 { |
|
83 double result = 0.0; |
|
84 Math::Exp(result, aSource); |
|
85 return result; |
|
86 } |
|
87 |
|
88 double fabs(double aSource) |
|
89 { |
|
90 return (aSource >= 0.0)? aSource: -aSource; |
|
91 } |
|
92 |
|
93 |
|
94 double floor(double aSource) |
|
95 { |
|
96 if(aSource >= 0.0 || aSource == (TInt64)aSource) |
|
97 { |
|
98 return (double)(TInt64)aSource; |
|
99 } |
|
100 |
|
101 return (double)((TInt64)aSource - 1); |
|
102 } |
|
103 |
|
104 double sqrt(double aSource) |
|
105 { |
|
106 double result = 0.0; |
|
107 Math::Sqrt(result, aSource); |
|
108 return result; |
|
109 } |
|
110 |
|
111 double pow(double aSource, double aPower) |
|
112 { |
|
113 double result = 0.0; |
|
114 Math::Pow(result, aSource, aPower); |
|
115 return result; |
|
116 } |
|
117 |
|
118 // Math trigonometric functions |
|
119 double sin(double aSource) |
|
120 { |
|
121 double result = 0.0; |
|
122 Math::Sin(result, aSource); |
|
123 return result; |
|
124 } |
|
125 |
|
126 double cos(double aSource) |
|
127 { |
|
128 double result = 0.0; |
|
129 Math::Cos(result, aSource); |
|
130 return result; |
|
131 } |
|
132 |
|
133 // Stdio functions |
|
134 int printf(const char* aFormatString, ...) |
|
135 { |
|
136 TUint length = User::StringLength((TUint8*)aFormatString) + 100; |
|
137 HBufC8* buffer = HBufC8::New(length); |
|
138 if(NULL == buffer) |
|
139 { |
|
140 return KErrNoMemory; |
|
141 } |
|
142 |
|
143 TPtr8 targetPtr = buffer->Des(); |
|
144 TPtrC8 formatPtr((TUint8*)aFormatString); |
|
145 |
|
146 VA_LIST list; |
|
147 VA_START(list, aFormatString); |
|
148 |
|
149 targetPtr.FormatList(formatPtr, list); |
|
150 |
|
151 PrintToScreen((const char*)targetPtr.PtrZ()); |
|
152 |
|
153 delete buffer; |
|
154 |
|
155 return targetPtr.Length(); |
|
156 } |
|
157 |
|
158 int puts(const char* aString) |
|
159 { |
|
160 int ret = PrintToScreen(aString); |
|
161 gConsole->Printf(_L("\n")); |
|
162 |
|
163 return ret; |
|
164 } |
|
165 |
|
166 int putchar(int aChar) |
|
167 { |
|
168 gConsole->Printf(_L("%c"), aChar); |
|
169 return aChar; |
|
170 } |
|
171 |
|
172 char* strcpy(char* aDst, const char* aSrc) |
|
173 { |
|
174 char* cp = aDst; |
|
175 |
|
176 while((*cp++ = *aSrc++) != 0) |
|
177 ; // Copy src over dst |
|
178 |
|
179 return(aDst); |
|
180 } |
|
181 |
|
182 int scanf(const char* aFormatString, ...) |
|
183 { |
|
184 TDataType type = EUnknownType; |
|
185 TBool byteRead = EFalse; |
|
186 |
|
187 if(Mem::Compare((const unsigned char*)aFormatString, 2, (const unsigned char*)"%d", 2) == 0) |
|
188 { |
|
189 type = EInteger; |
|
190 } |
|
191 else if(Mem::Compare((const unsigned char*)aFormatString, 2, (const unsigned char*)"%f", 2) == 0) |
|
192 { |
|
193 type = EFloat; |
|
194 } |
|
195 else if(Mem::Compare((const unsigned char*)aFormatString, 3, (const unsigned char*)"%1d", 3) == 0) |
|
196 { |
|
197 type = EInteger; |
|
198 byteRead = ETrue; |
|
199 } |
|
200 else |
|
201 { |
|
202 User::Panic(_L("NIST TestSuit Error"), KErrArgument); |
|
203 } |
|
204 |
|
205 if(!byteRead || (gScreenBuffer.Length() == 0)) |
|
206 { |
|
207 ReadStringFromConsole(gScreenBuffer); |
|
208 } |
|
209 |
|
210 TLex parser(gScreenBuffer); |
|
211 parser.SkipSpace(); |
|
212 |
|
213 VA_LIST list; |
|
214 VA_START(list, aFormatString); |
|
215 |
|
216 switch(type) |
|
217 { |
|
218 case EInteger: |
|
219 { |
|
220 TInt* ptr = VA_ARG(list, TInt*); |
|
221 if(byteRead) |
|
222 { |
|
223 TChar ch(gScreenBuffer[0]); |
|
224 gScreenBuffer.Delete(0, 1); |
|
225 *ptr = ch.GetNumericValue(); |
|
226 } |
|
227 else |
|
228 { |
|
229 parser.Val(*ptr); |
|
230 gScreenBuffer.Zero(); |
|
231 } |
|
232 break; |
|
233 } |
|
234 case EFloat: |
|
235 { |
|
236 float* ptr = VA_ARG(list, float*); |
|
237 parser.Val(*ptr); |
|
238 gScreenBuffer.Zero(); |
|
239 break; |
|
240 } |
|
241 case EUnknownType: |
|
242 { |
|
243 User::Panic(_L("NIST TestSuit Error"), KErrArgument); |
|
244 } |
|
245 } |
|
246 |
|
247 return 1; |
|
248 } |
|
249 |
|
250 int sprintf(char *aBuffer, const char* aFormatString, ...) |
|
251 { |
|
252 TUint length = User::StringLength((TUint8*)aFormatString) + 100; |
|
253 TPtr8 aTargetPtr((TUint8*)aBuffer, length); |
|
254 TPtrC8 formatPtr((TUint8*)aFormatString); |
|
255 |
|
256 VA_LIST list; |
|
257 VA_START(list, aFormatString); |
|
258 |
|
259 aTargetPtr.FormatList(formatPtr, list); |
|
260 aTargetPtr.ZeroTerminate(); |
|
261 |
|
262 return User::StringLength((TUint8*)aBuffer);; |
|
263 } |
|
264 |
|
265 int GetFileMode(const char* aModeStr, TFileMode& aFileMode, TBool& aIsAppend) |
|
266 { |
|
267 aIsAppend = EFalse; |
|
268 switch (*aModeStr) |
|
269 { |
|
270 case 'r': |
|
271 aFileMode = EFileRead; |
|
272 break; |
|
273 |
|
274 case 'w': |
|
275 aFileMode = EFileWrite; |
|
276 break; |
|
277 |
|
278 case 'a': |
|
279 aFileMode = EFileWrite; |
|
280 aIsAppend = ETrue; |
|
281 break; |
|
282 |
|
283 default: |
|
284 return KErrArgument; |
|
285 } |
|
286 |
|
287 return KErrNone; |
|
288 } |
|
289 |
|
290 FILE *fopen(const char *aFileName, const char *aMode) |
|
291 { |
|
292 TPtrC8 fileNamePtr(reinterpret_cast<const unsigned char*>(aFileName)); |
|
293 TFileName fileName; |
|
294 fileName.Copy(fileNamePtr); |
|
295 RFile* file = new RFile; |
|
296 if(NULL == file) |
|
297 { |
|
298 return NULL; |
|
299 } |
|
300 |
|
301 TFileMode mode = EFileRead; |
|
302 TBool isAppend = EFalse; |
|
303 GetFileMode(aMode, mode, isAppend); |
|
304 int err = KErrArgument; |
|
305 switch(mode) |
|
306 { |
|
307 case EFileRead: |
|
308 { |
|
309 err = file->Open(gFileSession, fileName, mode); |
|
310 break; |
|
311 } |
|
312 case EFileWrite: |
|
313 { |
|
314 if(isAppend) |
|
315 { |
|
316 err = file->Open(gFileSession, fileName, mode); |
|
317 if(err == KErrNone) |
|
318 { |
|
319 TInt offset = 0; |
|
320 err = file->Seek(ESeekEnd, offset); |
|
321 break; |
|
322 } |
|
323 } |
|
324 |
|
325 err = file->Replace(gFileSession, fileName, mode); |
|
326 break; |
|
327 } |
|
328 default: |
|
329 err = KErrArgument; |
|
330 } |
|
331 |
|
332 if(KErrNone != err) |
|
333 { |
|
334 file->Close(); |
|
335 delete file; |
|
336 file = NULL; |
|
337 } |
|
338 return file; |
|
339 } |
|
340 |
|
341 |
|
342 |
|
343 int fclose(FILE *aFp) |
|
344 { |
|
345 if(NULL != aFp) |
|
346 { |
|
347 aFp->Close(); |
|
348 delete aFp; |
|
349 } |
|
350 return KErrNone; |
|
351 } |
|
352 |
|
353 int fprintf(FILE* aFile, const char* aFormatString, ...) |
|
354 { |
|
355 TUint length = User::StringLength((TUint8*)aFormatString) + 100; |
|
356 HBufC8* buffer = HBufC8::New(length); |
|
357 if(NULL == buffer) |
|
358 { |
|
359 return KErrNoMemory; |
|
360 } |
|
361 |
|
362 TPtr8 targetPtr = buffer->Des(); |
|
363 TPtrC8 formatPtr((TUint8*)aFormatString); |
|
364 |
|
365 VA_LIST list; |
|
366 VA_START(list, aFormatString); |
|
367 |
|
368 targetPtr.FormatList(formatPtr, list); |
|
369 targetPtr.ZeroTerminate(); |
|
370 |
|
371 aFile->Write(targetPtr); |
|
372 |
|
373 delete buffer; |
|
374 |
|
375 return targetPtr.Length(); |
|
376 } |
|
377 |
|
378 int fscanf(FILE* aFp, const char * aFormatString, ...) |
|
379 { |
|
380 TDataType type = EUnknownType; |
|
381 TBool byteRead = EFalse; |
|
382 |
|
383 if(Mem::Compare((const unsigned char*)aFormatString, 2, (const unsigned char*)"%d", 2) == 0) |
|
384 { |
|
385 type = EInteger; |
|
386 } |
|
387 else if(Mem::Compare((const unsigned char*)aFormatString, 2, (const unsigned char*)"%f", 2) == 0) |
|
388 { |
|
389 type = EFloat; |
|
390 } |
|
391 else if(Mem::Compare((const unsigned char*)aFormatString, 3, (const unsigned char*)"%1d", 3) == 0) |
|
392 { |
|
393 type = EInteger; |
|
394 byteRead = ETrue; |
|
395 } |
|
396 else |
|
397 { |
|
398 User::Panic(_L("NIST TestSuit Error"), KErrArgument); |
|
399 } |
|
400 |
|
401 TInt initialOffset = 0; |
|
402 aFp->Seek(ESeekCurrent, initialOffset); |
|
403 TBuf8<KMaxReadSize + 1> readBuffer; |
|
404 aFp->Read(readBuffer, KMaxReadSize); |
|
405 readBuffer.ZeroTerminate(); |
|
406 TLex8 parser(readBuffer); |
|
407 parser.SkipSpace(); |
|
408 |
|
409 VA_LIST list; |
|
410 VA_START(list, aFormatString); |
|
411 |
|
412 switch(type) |
|
413 { |
|
414 case EInteger: |
|
415 { |
|
416 TInt* ptr = VA_ARG(list, TInt*); |
|
417 TChar ch = parser.Peek(); |
|
418 |
|
419 if(!ch.IsDigit()) |
|
420 { |
|
421 break; |
|
422 } |
|
423 |
|
424 if(byteRead) |
|
425 { |
|
426 ch = parser.Get(); |
|
427 *ptr = ch.GetNumericValue(); |
|
428 } |
|
429 else |
|
430 { |
|
431 parser.Val(*ptr); |
|
432 } |
|
433 |
|
434 break; |
|
435 } |
|
436 case EFloat: |
|
437 { |
|
438 float* ptr = VA_ARG(list, float*); |
|
439 parser.Val(*ptr); |
|
440 break; |
|
441 } |
|
442 case EUnknownType: |
|
443 { |
|
444 User::Panic(_L("NIST TestSuit Error"), KErrArgument); |
|
445 } |
|
446 } |
|
447 |
|
448 TInt len = initialOffset + parser.Offset(); |
|
449 aFp->Seek(ESeekStart, len); |
|
450 |
|
451 return 1; |
|
452 } |
|
453 |
|
454 TUint32 fread(void* aPtr, TUint32 aSize, TUint32 aCount, FILE* aFile) |
|
455 { |
|
456 TUint32 size = aSize * aCount; |
|
457 TPtr8 dataPtr((TUint8*)aPtr, size); |
|
458 TInt err = aFile->Read(dataPtr); |
|
459 if(KErrNone != err) |
|
460 { |
|
461 size = (TUint32)dataPtr.Length(); |
|
462 } |
|
463 return size; |
|
464 } |
|
465 |
|
466 int fseek(FILE* aFile, long aOffset, int aWhence) |
|
467 { |
|
468 int ret = KErrNone; |
|
469 int fileOffset = aOffset; |
|
470 switch(aWhence) |
|
471 { |
|
472 case SEEK_SET: |
|
473 { |
|
474 ret = aFile->Seek(ESeekStart, fileOffset); |
|
475 break; |
|
476 } |
|
477 case SEEK_CUR: |
|
478 { |
|
479 ret = aFile->Seek(ESeekCurrent, fileOffset); |
|
480 break; |
|
481 } |
|
482 case SEEK_END: |
|
483 { |
|
484 ret = aFile->Seek(ESeekEnd, fileOffset); |
|
485 break; |
|
486 } |
|
487 default: |
|
488 User::Panic(_L("NIST TestSuit Error"), KErrArgument); |
|
489 } |
|
490 |
|
491 return ret; |
|
492 } |
|
493 |
|
494 |
|
495 |
|
496 int fflush(FILE *aFile) |
|
497 { |
|
498 TInt err = aFile->Flush(); |
|
499 if(err != KErrNone) |
|
500 { |
|
501 err = EOF; |
|
502 } |
|
503 return err; |
|
504 } |
|
505 |
|
506 |
|
507 // Conio functions |
|
508 void* calloc(TUint32 aElementCount, TUint32 aSize) |
|
509 { |
|
510 aSize *= aElementCount; |
|
511 return User::AllocZ(aSize); |
|
512 } |
|
513 |
|
514 void free(void *aMemory) |
|
515 { |
|
516 User::Free(aMemory); |
|
517 } |
|
518 |
|
519 TBool IsCharacterKey(TKeyCode aKey) |
|
520 { |
|
521 if(aKey > EKeyEscape && aKey < EKeyDelete) |
|
522 { |
|
523 return ETrue; |
|
524 } |
|
525 |
|
526 return EFalse; |
|
527 } |
|
528 |
|
529 _LIT(KLineBreaker, "\r\n"); |
|
530 |
|
531 void ReadStringFromConsole(TDes& aString) |
|
532 { |
|
533 // This infinte loop terminates when user hits an "enter" key |
|
534 FOREVER |
|
535 { |
|
536 // Get a key(character) from the console |
|
537 TKeyCode ch = gConsole->Getch(); |
|
538 |
|
539 switch(ch) |
|
540 { |
|
541 case EKeyEnter: |
|
542 { |
|
543 if(aString.Length() == 0) |
|
544 { |
|
545 break;// At least one character should be read. |
|
546 } |
|
547 gConsole->Printf(KLineBreaker); |
|
548 return; |
|
549 } |
|
550 case EKeyBackspace: |
|
551 { |
|
552 if(0 != aString.Length()) |
|
553 { |
|
554 // Back-space only takes the cursor one position back |
|
555 // So to delete a character blank-space is inserted at |
|
556 // that position and later cursor is again adjusted. |
|
557 gConsole->Printf(_L("%c%c%c"), EKeyBackspace, |
|
558 EKeySpace, |
|
559 EKeyBackspace); |
|
560 // Delete the character from the target string also. |
|
561 aString.Delete(aString.Length() - 1, 1); |
|
562 } |
|
563 break; |
|
564 } |
|
565 default: |
|
566 { |
|
567 TInt maxBufferLength = aString.MaxLength(); |
|
568 // IsCharacterKey will return true if ch is a displayable |
|
569 // character else it will return false. |
|
570 if(IsCharacterKey(ch) && aString.Length() != maxBufferLength) |
|
571 { |
|
572 gConsole->Printf(_L("%c"), ch); |
|
573 aString.Append(ch); |
|
574 } |
|
575 } |
|
576 } |
|
577 } |
|
578 } |
|
579 |
|
580 TInt ReadIntL(TInt& aValue) |
|
581 { |
|
582 TBuf<KIntStringLen> string; |
|
583 ReadStringFromConsole(string); |
|
584 TLex lexObj(string); |
|
585 return lexObj.Val(aValue); |
|
586 } |
|
587 |
|
588 int PrintToScreen(const char* aString) |
|
589 { |
|
590 TUint length = User::StringLength((TUint8*)aString); |
|
591 HBufC* buffer = HBufC::New(length); |
|
592 if(NULL == buffer) |
|
593 { |
|
594 return EOF; |
|
595 } |
|
596 |
|
597 TPtr targetPtr = buffer->Des(); |
|
598 |
|
599 TPtrC8 stringPtr((TUint8*)aString); |
|
600 |
|
601 targetPtr.Copy(stringPtr); |
|
602 gConsole->Printf(targetPtr); |
|
603 |
|
604 delete buffer; |
|
605 |
|
606 return KErrNone; |
|
607 } |
|
608 |
|
609 void exit ( int status ) |
|
610 { |
|
611 User::Exit(status); |
|
612 for(;;){} // So that GCC compiler don't complain about noreturn function returns. |
|
613 } |
|
614 |
|
615 //------------------------------------------ E O F ----------------------------------------------------- |
|
616 |