PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vcc/Runtime/Stdio.cs

#
C# | 121 lines | 106 code | 7 blank | 8 comment | 29 complexity | da6dbbd4c4f60acba5ad42872b9e5d0b MD5 | raw file
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  4. //
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Text;
  8. // Partial implementation of printf
  9. namespace Microsoft.Research.Vcc {
  10. public static unsafe partial class Runtime {
  11. static int width;
  12. static int width2;
  13. public static int printf(sbyte* _Format, __arglist) {
  14. StringBuilder output = new StringBuilder();
  15. ArgIterator ai = new ArgIterator(__arglist);
  16. string format = new string(_Format);
  17. int i = 0, n = format.Length;
  18. bool after_parse_width = false;
  19. while (i < n) {
  20. char c = format[i];
  21. if (c == '%' || after_parse_width ) {
  22. if (i + 1 <= n) {
  23. i++;
  24. char next = format[i];
  25. switch (next) {
  26. // ignore width for now.
  27. case 'd': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break;
  28. case 'c': i++; output.Append(getChar(ai)); break;
  29. case 'x': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break;
  30. case 's': i++; output.Append(getString(ai)); break;
  31. case 'f': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break;
  32. case 'l': i++;
  33. if (format[i] == 'f') {
  34. i++;
  35. output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg()));
  36. }
  37. else {
  38. output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg()));
  39. }
  40. break;
  41. default:
  42. if (Char.IsDigit(next)) {
  43. StringBuilder sb = new StringBuilder();
  44. while (i < n && Char.IsDigit(format[i])) {
  45. sb.Append(format[i]);
  46. i++;
  47. }
  48. width = int.Parse(sb.ToString());
  49. width2 = 0;
  50. if (format[i] == '.') {
  51. i++;
  52. StringBuilder sb2 = new StringBuilder();
  53. while (i < n && Char.IsDigit(format[i])) {
  54. sb2.Append(format[i]);
  55. i++;
  56. }
  57. width2 = int.Parse(sb2.ToString());
  58. }
  59. }
  60. break;
  61. }
  62. }
  63. else break; // break the loop
  64. }
  65. else if (c == '\\') {
  66. // it is impossible for the next char to be out of bound.
  67. i++;
  68. char escaped = format[i];
  69. output.Append(c);
  70. output.Append(escaped);
  71. i ++;
  72. }
  73. else {
  74. output.Append(c);
  75. i++;
  76. }
  77. }
  78. Console.Write(output.ToString());
  79. return 0;
  80. }
  81. private static int getInt(ArgIterator ai) {
  82. TypedReference tr = ai.GetNextArg();
  83. Type t = __reftype(tr);
  84. if (t == typeof(int)) {
  85. return (int)(__refvalue(tr, int));
  86. }
  87. throw new FormatException("expecting an integer");
  88. }
  89. private static char getChar(ArgIterator ai) {
  90. TypedReference tr = ai.GetNextArg();
  91. Type t = __reftype(tr);
  92. if (t == typeof(sbyte)) {
  93. return (char)(__refvalue(tr, sbyte));
  94. }
  95. throw new FormatException("expecting a char");
  96. }
  97. private unsafe static string getString(ArgIterator ai) {
  98. TypedReference tr = ai.GetNextArg();
  99. Type t = __reftype(tr);
  100. if (t == typeof(sbyte*)) {
  101. return new String(__refvalue(tr, sbyte*));
  102. }
  103. throw new FormatException("expecting a string");
  104. }
  105. private static double getFloat(ArgIterator ai) {
  106. TypedReference tr = ai.GetNextArg();
  107. Type t = __reftype(tr);
  108. if (t == typeof(double)) {
  109. return __refvalue( tr, double);
  110. }
  111. throw new FormatException("expecting a double");
  112. }
  113. }
  114. }