* Parameters.cs (ProcessProperty): Handle invalid syntax.
[mono.git] / mcs / tools / cilc / CodeWriter.cs
1 // cilc -- a CIL-to-C binding generator
2 // Copyright (C) 2003, 2004, 2005, 2006, 2007 Alp Toker <alp@atoker.com>
3 // Licensed under the terms of the MIT License
4
5 using System;
6 using System.IO;
7
8 class CodeWriter
9 {
10         private StreamWriter w;
11
12         public CodeWriter (string fname)
13         {
14                 Init (fname);
15         }
16
17         public bool IsDuplicate = false;
18
19         void Init (string fname)
20         {
21                 if (File.Exists (fname)) {
22                         string newfname = fname + ".x";
23                         //Console.WriteLine ("Warning: File " + fname + " already exists, using " + newfname);
24                         IsDuplicate = true;
25                         Init (newfname);
26                         return;
27                 }
28
29                 FileStream fs = new FileStream (fname, FileMode.OpenOrCreate, FileAccess.Write);
30                 w = new StreamWriter (fs);
31         }
32
33         public string Indenter = "  ";
34         string cur_indent = String.Empty;
35         int level = 0;
36
37         public void Indent ()
38         {
39                 level++;
40                 cur_indent = String.Empty;
41                 for (int i = 0; i != level ; i++) cur_indent += Indenter;
42         }
43
44         public void Outdent ()
45         {
46                 level--;
47                 cur_indent = String.Empty;
48                 for (int i = 0; i != level ; i++) cur_indent += Indenter;
49         }
50
51         public void Write (string text)
52         {
53                 w.Write (text);
54         }
55
56         public void WriteLine (string text)
57         {
58                 WriteLine (text, true);
59         }
60
61         public void WriteLine (string text, bool autoindent)
62         {
63                 char[] opentags = {'{', '('};
64                 char[] closetags = {'}', ')'};
65
66                 if (autoindent && text.TrimStart (closetags) != text)
67                         Outdent ();
68
69                 w.Write (cur_indent);
70                 w.WriteLine (text);
71
72                 if (autoindent && text.TrimEnd (opentags) != text)
73                         Indent ();
74         }
75
76         public void WriteLine (string text, CodeWriter cc)
77         {
78                 WriteLine (text, String.Empty, cc, String.Empty);
79         }
80
81         public void WriteLine (string text, CodeWriter cc, string suffix)
82         {
83                 WriteLine (text, String.Empty, cc, suffix);
84         }
85
86         public void WriteLine (string text, string prefix, CodeWriter cc)
87         {
88                 WriteLine (text, prefix, cc, String.Empty);
89         }
90
91         public void WriteLine (string text, string prefix, CodeWriter cc, string suffix)
92         {
93                 WriteLine (text);
94                 cc.WriteLine (prefix + text + suffix);
95         }
96
97         public void WriteComment (string text)
98         {
99                 w.WriteLine ("/* " + text + " */");
100         }
101
102         public void WriteLine ()
103         {
104                 w.WriteLine (String.Empty);
105         }
106
107         public void Close ()
108         {
109                 w.Flush ();
110                 w.Close ();
111         }
112 }