update for 0.3.3 release
[vala-lang.git] / ccode / valaccodewriter.vala
blob4330e7a48e80120de007463823ee6b449173226f
1 /* valaccodewriter.vala
3 * Copyright (C) 2006-2008 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a writer to write C source files.
28 public class Vala.CCodeWriter : Object {
29 /**
30 * Specifies the file to be written.
32 public string filename {
33 get {
34 return _filename;
36 construct {
37 _filename = value;
41 /**
42 * Specifies whether to emit line directives.
44 public bool line_directives { get; set; }
46 /**
47 * Specifies whether the output stream is at the beginning of a line.
49 public bool bol {
50 get { return _bol; }
53 private string _filename;
54 private string temp_filename;
55 private bool file_exists;
57 private FileStream stream;
59 private int indent;
61 /* at begin of line */
62 private bool _bol = true;
64 public CCodeWriter (string _filename) {
65 filename = _filename;
68 /**
69 * Opens the file.
71 * @return true if the file has been opened successfully,
72 * false otherwise
74 public bool open () {
75 file_exists = FileUtils.test (_filename, FileTest.EXISTS);
76 if (file_exists) {
77 temp_filename = "%s.valatmp".printf (_filename);
78 stream = FileStream.open (temp_filename, "w");
79 } else {
80 stream = FileStream.open (_filename, "w");
83 return (stream != null);
86 /**
87 * Closes the file.
89 public void close () {
90 stream = null;
92 if (file_exists) {
93 var changed = true;
95 try {
96 var old_file = new MappedFile (_filename, false);
97 var new_file = new MappedFile (temp_filename, false);
98 var len = old_file.get_length ();
99 if (len == new_file.get_length ()) {
100 if (Memory.cmp (old_file.get_contents (), new_file.get_contents (), len) == 0) {
101 changed = false;
104 old_file = null;
105 new_file = null;
106 } catch (FileError e) {
107 // assume changed if mmap comparison doesn't work
110 if (changed) {
111 FileUtils.rename (temp_filename, _filename);
112 } else {
113 FileUtils.unlink (temp_filename);
119 * Writes tabs according to the current indent level.
121 public void write_indent (CCodeLineDirective? line = null) {
122 if (line_directives && line != null) {
123 line.write (this);
126 if (!bol) {
127 stream.putc ('\n');
130 for (int i = 0; i < indent; i++) {
131 stream.putc ('\t');
134 _bol = false;
138 * Writes the specified string.
140 * @param s a string
142 public void write_string (string s) {
143 stream.printf ("%s", s);
144 _bol = false;
148 * Writes a newline.
150 public void write_newline () {
151 stream.putc ('\n');
152 _bol = true;
156 * Opens a new block, increasing the indent level.
158 public void write_begin_block () {
159 if (!bol) {
160 stream.putc (' ');
161 } else {
162 write_indent ();
164 stream.putc ('{');
165 write_newline ();
166 indent++;
170 * Closes the current block, decreasing the indent level.
172 public void write_end_block () {
173 assert (indent > 0);
175 indent--;
176 write_indent ();
177 stream.printf ("}");
181 * Writes the specified text as comment.
183 * @param text the comment text
185 public void write_comment (string text) {
186 write_indent ();
187 stream.printf ("/*");
188 bool first = true;
190 /* separate declaration due to missing memory management in foreach statements */
191 var lines = text.split ("\n");
193 foreach (string line in lines) {
194 if (!first) {
195 write_indent ();
196 } else {
197 first = false;
199 stream.printf ("%s", line);
201 stream.printf ("*/");
202 write_newline ();