1 /* valaccodewriter.vala
3 * Copyright (C) 2006-2009 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
{
30 * Specifies the file to be written.
32 public string filename
{ get; set; }
35 * Specifies the source file used to generate this one.
37 private string source_filename
;
40 * Specifies whether to emit line directives.
42 public bool line_directives
{ get; set; }
45 * Specifies whether the output stream is at the beginning of a line.
51 private string temp_filename
;
52 private bool file_exists
;
54 private FileStream stream
;
57 private int current_line_number
= 1;
58 private bool using_line_directive
;
60 /* at begin of line */
61 private bool _bol
= true;
63 public CCodeWriter (string filename
, string? source_filename
= null) {
64 this
.filename
= filename
;
65 this
.source_filename
= source_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 write_string ("/* %s generated by valac, the Vala compiler".printf (Path
.get_basename (filename
)));
85 // Write the file name if known
86 if (source_filename
!= null) {
88 write_string (" * generated from %s".printf (Path
.get_basename (source_filename
)));
91 write_string (", do not modify */");
95 return (stream
!= null);
101 public void close () {
108 var old_file
= new
MappedFile (filename
, false);
109 var new_file
= new
MappedFile (temp_filename
, false);
110 var len
= old_file
.get_length ();
111 if (len
== new_file
.get_length ()) {
112 if (Memory
.cmp (old_file
.get_contents (), new_file
.get_contents (), len
) == 0) {
118 } catch (FileError e
) {
119 // assume changed if mmap comparison doesn't work
123 FileUtils
.rename (temp_filename
, filename
);
125 FileUtils
.unlink (temp_filename
);
131 * Writes tabs according to the current indent level.
133 public void write_indent (CCodeLineDirective? line
= null) {
134 if (line_directives
) {
137 using_line_directive
= true;
138 } else if (using_line_directive
) {
139 // no corresponding Vala line, emit line directive for C line
140 write_string ("#line %d \"%s\"".printf (current_line_number
+ 1, Path
.get_basename (filename
)));
142 using_line_directive
= false;
150 for (int i
= 0; i
< indent
; i
++) {
158 * Writes the specified string.
162 public void write_string (string s
) {
170 public void write_newline () {
172 current_line_number
++;
177 * Opens a new block, increasing the indent level.
179 public void write_begin_block () {
191 * Closes the current block, decreasing the indent level.
193 public void write_end_block () {
202 * Writes the specified text as comment.
204 * @param text the comment text
206 public void write_comment (string text
) {
211 /* separate declaration due to missing memory management in foreach statements */
212 var lines
= text
.split ("\n");
214 foreach (string line
in lines
) {
221 var lineparts
= line
.split ("*/");
223 for (int i
= 0; lineparts
[i
] != null; i
++) {
224 stream
.puts (lineparts
[i
]);
225 if (lineparts
[i
+1] != null) {