Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PrettyPrinter.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.IO;
5 
6 namespace SiliconStudio.Paradox
7 {
8  public static class PrettyPrinter
9  {
10  static string EscapeString(string s)
11  {
12  return s.Replace("\"", "\\\"");
13  }
14 
15  static void Print(TextWriter output, string s)
16  {
17  output.Write(s);
18  }
19  static void EscapeChar(TextWriter output, char c)
20  {
21  if (c == '\'')
22  {
23  output.Write("'\\''");
24  return;
25  }
26  if (c > 32)
27  {
28  output.Write("'{0}'", c);
29  return;
30  }
31  switch (c)
32  {
33  case '\a':
34  output.Write("'\\a'");
35  break;
36 
37  case '\b':
38  output.Write("'\\b'");
39  break;
40 
41  case '\n':
42  output.Write("'\\n'");
43  break;
44 
45  case '\v':
46  output.Write("'\\v'");
47  break;
48 
49  case '\r':
50  output.Write("'\\r'");
51  break;
52 
53  case '\f':
54  output.Write("'\\f'");
55  break;
56 
57  case '\t':
58  output.Write("'\\t");
59  break;
60 
61  default:
62  output.Write("'\\x{0:x}", (int)c);
63  break;
64  }
65  }
66 
67  public static void PrettyPrint(TextWriter output, object result)
68  {
69  if (result == null)
70  {
71  Print(output, "null");
72  return;
73  }
74 
75  if (result is Array)
76  {
77  Array a = (Array)result;
78 
79  Print(output, "{ ");
80  int top = a.GetUpperBound(0);
81  for (int i = a.GetLowerBound(0); i <= top; i++)
82  {
83  PrettyPrint(output, a.GetValue(i));
84  if (i != top)
85  Print(output, ", ");
86  }
87  Print(output, " }");
88  }
89  else if (result is bool)
90  {
91  if ((bool)result)
92  Print(output, "true");
93  else
94  Print(output, "false");
95  }
96  else if (result is string)
97  {
98  Print(output, String.Format("\"{0}\"", EscapeString((string)result)));
99  }
100  else if (result is System.Collections.IDictionary)
101  {
102  var dict = (System.Collections.IDictionary)result;
103  int top = dict.Count, count = 0;
104 
105  Print(output, "{");
106  foreach (System.Collections.DictionaryEntry entry in dict)
107  {
108  count++;
109  Print(output, "{ ");
110  PrettyPrint(output, entry.Key);
111  Print(output, ", ");
112  PrettyPrint(output, entry.Value);
113  if (count != top)
114  Print(output, " }, ");
115  else
116  Print(output, " }");
117  }
118  Print(output, "}");
119  }
120  else if (result is System.Collections.IEnumerable)
121  {
122  int i = 0;
123  Print(output, "{ ");
124  foreach (object item in (System.Collections.IEnumerable)result)
125  {
126  if (i++ != 0)
127  Print(output, ", ");
128 
129  PrettyPrint(output, item);
130  }
131  Print(output, " }");
132  }
133  else if (result is char)
134  {
135  EscapeChar(output, (char)result);
136  }
137  else
138  {
139  Print(output, result.ToString());
140  }
141  }
142  }
143 }
function a
_In_ size_t count
Definition: DirectXTexP.h:174
function s(a)
static void PrettyPrint(TextWriter output, object result)