Support GVariant casts and GDBus clients and servers
[vala-lang.git] / ccode / valaccodewriter.vala
blob8f2575437370a7919a005597606bfc06b04a16c0
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
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 {
29 /**
30 * Specifies the file to be written.
32 public string filename { get; set; }
34 /**
35 * Specifies the source file used to generate this one.
37 private string source_filename;
39 /**
40 * Specifies whether to emit line directives.
42 public bool line_directives { get; set; }
44 /**
45 * Specifies whether the output stream is at the beginning of a line.
47 public bool bol {
48 get { return _bol; }
51 private string temp_filename;
52 private bool file_exists;
54 private FileStream? stream;
56 private int indent;
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;
68 /**
69 * Opens the file.
71 * @return true if the file has been opened successfully,
72 * false otherwise
74 public bool open (bool write_version) {
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 {
81 * File doesn't exist. In case of a particular destination (-d flag),
82 * check and create the directory structure.
84 var dirname = Path.get_dirname (filename);
85 DirUtils.create_with_parents (dirname, 0755);
86 stream = FileStream.open (filename, "w");
89 if (stream == null) {
90 return false;
93 var opening = write_version ?
94 "/* %s generated by valac %s, the Vala compiler".printf (Path.get_basename (filename), Config.BUILD_VERSION) :
95 "/* %s generated by valac, the Vala compiler".printf (Path.get_basename (filename));
96 write_string (opening);
98 // Write the file name if known
99 if (source_filename != null) {
100 write_newline ();
101 write_string (" * generated from %s".printf (Path.get_basename (source_filename)));
104 write_string (", do not modify */");
105 write_newline ();
106 write_newline ();
108 return true;
112 * Closes the file.
114 public void close () {
115 stream = null;
117 if (file_exists) {
118 var changed = true;
120 try {
121 var old_file = new MappedFile (filename, false);
122 var new_file = new MappedFile (temp_filename, false);
123 var len = old_file.get_length ();
124 if (len == new_file.get_length ()) {
125 if (Memory.cmp (old_file.get_contents (), new_file.get_contents (), len) == 0) {
126 changed = false;
129 old_file = null;
130 new_file = null;
131 } catch (FileError e) {
132 // assume changed if mmap comparison doesn't work
135 if (changed) {
136 FileUtils.rename (temp_filename, filename);
137 } else {
138 FileUtils.unlink (temp_filename);
144 * Writes tabs according to the current indent level.
146 public void write_indent (CCodeLineDirective? line = null) {
147 if (line_directives) {
148 if (line != null) {
149 line.write (this);
150 using_line_directive = true;
151 } else if (using_line_directive) {
152 // no corresponding Vala line, emit line directive for C line
153 write_string ("#line %d \"%s\"".printf (current_line_number + 1, Path.get_basename (filename)));
154 write_newline ();
155 using_line_directive = false;
159 if (!bol) {
160 write_newline ();
163 for (int i = 0; i < indent; i++) {
164 stream.putc ('\t');
167 _bol = false;
171 * Writes the specified string.
173 * @param s a string
175 public void write_string (string s) {
176 stream.puts (s);
177 _bol = false;
181 * Writes a newline.
183 public void write_newline () {
184 stream.putc ('\n');
185 current_line_number++;
186 _bol = true;
190 * Opens a new block, increasing the indent level.
192 public void write_begin_block () {
193 if (!bol) {
194 stream.putc (' ');
195 } else {
196 write_indent ();
198 stream.putc ('{');
199 write_newline ();
200 indent++;
204 * Closes the current block, decreasing the indent level.
206 public void write_end_block () {
207 assert (indent > 0);
209 indent--;
210 write_indent ();
211 stream.putc ('}');
215 * Writes the specified text as comment.
217 * @param text the comment text
219 public void write_comment (string text) {
220 write_indent ();
221 stream.puts ("/*");
222 bool first = true;
224 /* separate declaration due to missing memory management in foreach statements */
225 var lines = text.split ("\n");
227 foreach (string line in lines) {
228 if (!first) {
229 write_indent ();
230 } else {
231 first = false;
234 var lineparts = line.split ("*/");
236 for (int i = 0; lineparts[i] != null; i++) {
237 stream.puts (lineparts[i]);
238 if (lineparts[i+1] != null) {
239 stream.puts ("* /");
243 stream.puts ("*/");
244 write_newline ();