You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

io.c 5.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <inttypes.h> // for PRId64
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <wctype.h>
  5. #include "std_testcase.h"
  6. #ifndef _WIN32
  7. #include <wchar.h>
  8. #endif
  9. void printLine (const char * line)
  10. {
  11. if(line != NULL)
  12. {
  13. printf("%s\n", line);
  14. }
  15. }
  16. void printWLine (const wchar_t * line)
  17. {
  18. if(line != NULL)
  19. {
  20. wprintf(L"%ls\n", line);
  21. }
  22. }
  23. void printIntLine (int intNumber)
  24. {
  25. printf("%d\n", intNumber);
  26. }
  27. void printShortLine (short shortNumber)
  28. {
  29. printf("%hd\n", shortNumber);
  30. }
  31. void printFloatLine (float floatNumber)
  32. {
  33. printf("%f\n", floatNumber);
  34. }
  35. void printLongLine (long longNumber)
  36. {
  37. printf("%ld\n", longNumber);
  38. }
  39. void printLongLongLine (int64_t longLongIntNumber)
  40. {
  41. printf("%" PRId64 "\n", longLongIntNumber);
  42. }
  43. void printSizeTLine (size_t sizeTNumber)
  44. {
  45. printf("%zu\n", sizeTNumber);
  46. }
  47. void printHexCharLine (char charHex)
  48. {
  49. printf("%02x\n", charHex);
  50. }
  51. void printWcharLine(wchar_t wideChar)
  52. {
  53. /* ISO standard dictates wchar_t can be ref'd only with %ls, so we must make a
  54. * string to print a wchar */
  55. wchar_t s[2];
  56. s[0] = wideChar;
  57. s[1] = L'\0';
  58. printf("%ls\n", s);
  59. }
  60. void printUnsignedLine(unsigned unsignedNumber)
  61. {
  62. printf("%u\n", unsignedNumber);
  63. }
  64. void printHexUnsignedCharLine(unsigned char unsignedCharacter)
  65. {
  66. printf("%02x\n", unsignedCharacter);
  67. }
  68. void printDoubleLine(double doubleNumber)
  69. {
  70. printf("%g\n", doubleNumber);
  71. }
  72. void printStructLine (const twoIntsStruct * structTwoIntsStruct)
  73. {
  74. printf("%d -- %d\n", structTwoIntsStruct->intOne, structTwoIntsStruct->intTwo);
  75. }
  76. void printBytesLine(const unsigned char * bytes, size_t numBytes)
  77. {
  78. size_t i;
  79. for (i = 0; i < numBytes; ++i)
  80. {
  81. printf("%02x", bytes[i]);
  82. }
  83. puts(""); /* output newline */
  84. }
  85. /* Decode a string of hex characters into the bytes they represent. The second
  86. * parameter specifies the length of the output buffer. The number of bytes
  87. * actually written to the output buffer is returned. */
  88. size_t decodeHexChars(unsigned char * bytes, size_t numBytes, const char * hex)
  89. {
  90. size_t numWritten = 0;
  91. /* We can't sscanf directly into the byte array since %02x expects a pointer to int,
  92. * not a pointer to unsigned char. Also, since we expect an unbroken string of hex
  93. * characters, we check for that before calling sscanf; otherwise we would get a
  94. * framing error if there's whitespace in the input string. */
  95. while (numWritten < numBytes && isxdigit(hex[2 * numWritten]) && isxdigit(hex[2 * numWritten + 1]))
  96. {
  97. int byte;
  98. sscanf(&hex[2 * numWritten], "%02x", &byte);
  99. bytes[numWritten] = (unsigned char) byte;
  100. ++numWritten;
  101. }
  102. return numWritten;
  103. }
  104. /* Decode a string of hex characters into the bytes they represent. The second
  105. * parameter specifies the length of the output buffer. The number of bytes
  106. * actually written to the output buffer is returned. */
  107. size_t decodeHexWChars(unsigned char * bytes, size_t numBytes, const wchar_t * hex)
  108. {
  109. size_t numWritten = 0;
  110. /* We can't swscanf directly into the byte array since %02x expects a pointer to int,
  111. * not a pointer to unsigned char. Also, since we expect an unbroken string of hex
  112. * characters, we check for that before calling swscanf; otherwise we would get a
  113. * framing error if there's whitespace in the input string. */
  114. while (numWritten < numBytes && iswxdigit(hex[2 * numWritten]) && iswxdigit(hex[2 * numWritten + 1]))
  115. {
  116. int byte;
  117. swscanf(&hex[2 * numWritten], L"%02x", &byte);
  118. bytes[numWritten] = (unsigned char) byte;
  119. ++numWritten;
  120. }
  121. return numWritten;
  122. }
  123. /* The two functions always return 1 or 0, so a tool should be able to
  124. identify that uses of these functions will always return these values */
  125. int globalReturnsTrue()
  126. {
  127. return 1;
  128. }
  129. int globalReturnsFalse()
  130. {
  131. return 0;
  132. }
  133. int globalReturnsTrueOrFalse()
  134. {
  135. return (rand() % 2);
  136. }
  137. /* The variables below are declared "const", so a tool should
  138. be able to identify that reads of these will always return their
  139. initialized values. */
  140. const int GLOBAL_CONST_TRUE = 1; /* true */
  141. const int GLOBAL_CONST_FALSE = 0; /* false */
  142. const int GLOBAL_CONST_FIVE = 5;
  143. /* The variables below are not defined as "const", but are never
  144. assigned any other value, so a tool should be able to identify that
  145. reads of these will always return their initialized values. */
  146. int globalTrue = 1; /* true */
  147. int globalFalse = 0; /* false */
  148. int globalFive = 5;
  149. /* define a bunch of these as empty functions so that if a test case forgets
  150. to make their's statically scoped, we'll get a linker error */
  151. void good1() { }
  152. void good2() { }
  153. void good3() { }
  154. void good4() { }
  155. void good5() { }
  156. void good6() { }
  157. void good7() { }
  158. void good8() { }
  159. void good9() { }
  160. /* shouldn't be used, but just in case */
  161. void bad1() { }
  162. void bad2() { }
  163. void bad3() { }
  164. void bad4() { }
  165. void bad5() { }
  166. void bad6() { }
  167. void bad7() { }
  168. void bad8() { }
  169. void bad9() { }
  170. /* define global argc and argv */
  171. #ifdef __cplusplus
  172. extern "C" {
  173. #endif
  174. int globalArgc = 0;
  175. char** globalArgv = NULL;
  176. #ifdef __cplusplus
  177. }
  178. #endif

No Description

Contributors (1)