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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a writer to write C source files.
28 public class Vala
.CCodeWriter
: Object
{
30 * Specifies the file to be written.
32 public string filename
{
42 * Specifies whether to emit line directives.
44 public bool line_directives
{ get; set; }
47 * Specifies whether the output stream is at the beginning of a line.
53 private string _filename
;
54 private string temp_filename
;
55 private bool file_exists
;
57 private FileStream stream
;
61 /* at begin of line */
62 private bool _bol
= true;
64 public CCodeWriter (string _filename
) {
71 * @return true if the file has been opened successfully,
75 file_exists
= FileUtils
.test (_filename
, FileTest
.EXISTS
);
77 temp_filename
= "%s.valatmp".printf (_filename
);
78 stream
= FileStream
.open (temp_filename
, "w");
80 stream
= FileStream
.open (_filename
, "w");
83 return (stream
!= null);
89 public void close () {
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) {
106 } catch (FileError e
) {
107 // assume changed if mmap comparison doesn't work
111 FileUtils
.rename (temp_filename
, _filename
);
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) {
130 for (int i
= 0; i
< indent
; i
++) {
138 * Writes the specified string.
142 public void write_string (string s
) {
143 stream
.printf ("%s", s
);
150 public void write_newline () {
156 * Opens a new block, increasing the indent level.
158 public void write_begin_block () {
170 * Closes the current block, decreasing the indent level.
172 public void write_end_block () {
181 * Writes the specified text as comment.
183 * @param text the comment text
185 public void write_comment (string text
) {
187 stream
.printf ("/*");
190 /* separate declaration due to missing memory management in foreach statements */
191 var lines
= text
.split ("\n");
193 foreach (string line
in lines
) {
199 stream
.printf ("%s", line
);
201 stream
.printf ("*/");