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 static GLib.Regex fix_indent_regex;
53 private string temp_filename;
54 private bool file_exists;
56 private FileStream? stream;
59 private int current_line_number = 1;
60 private bool using_line_directive;
62 /* at begin of line */
63 private bool _bol = true;
64 /* at begin after empty line */
65 private bool _bael = false;
67 public CCodeWriter (string filename, string? source_filename = null) {
68 this.filename = filename;
69 this.source_filename = source_filename;
75 * @return true if the file has been opened successfully,
78 public bool open (bool write_version) {
79 file_exists = FileUtils.test (filename, FileTest.EXISTS);
81 temp_filename = "%s.valatmp".printf (filename);
82 stream = FileStream.open (temp_filename, "w");
85 * File doesn't exist. In case of a particular destination (-d flag),
86 * check and create the directory structure.
88 var dirname = Path.get_dirname (filename);
89 DirUtils.create_with_parents (dirname, 0755);
90 stream = FileStream.open (filename, "w");
97 var opening = write_version ?
98 "/* %s generated by valac %s, the Vala compiler".printf (Path.get_basename (filename), Vala.BUILD_VERSION) :
99 "/* %s generated by valac, the Vala compiler".printf (Path.get_basename (filename));
100 write_string (opening);
102 // Write the file name if known
103 if (source_filename != null) {
105 write_string (" * generated from %s".printf (Path.get_basename (source_filename)));
108 write_string (", do not modify */");
118 public void close () {
125 var old_file = new MappedFile (filename, false);
126 var new_file = new MappedFile (temp_filename, false);
127 var len = old_file.get_length ();
128 if (len == new_file.get_length ()) {
129 if (Memory.cmp (old_file.get_contents (), new_file.get_contents (), len) == 0) {
135 } catch (FileError e) {
136 // assume changed if mmap comparison doesn't work
140 FileUtils.rename (temp_filename, filename);
142 FileUtils.unlink (temp_filename);
143 if (source_filename != null) {
144 var stats = Stat (source_filename);
145 var target_stats = Stat (filename);
146 if (stats.st_mtime >= target_stats.st_mtime) {
147 UTimBuf timebuf = { stats.st_atime + 1, stats.st_mtime + 1 };
148 FileUtils.utime (filename, timebuf);
156 * Writes tabs according to the current indent level.
158 public void write_indent (CCodeLineDirective? line = null) {
159 if (line_directives) {
162 using_line_directive = true;
163 } else if (using_line_directive) {
164 // no corresponding Vala line, emit line directive for C line
165 write_string ("#line %d \"%s\"".printf (current_line_number + 1, Path.get_basename (filename)));
167 using_line_directive = false;
175 stream.puts (string.nfill (indent, '\t'));
182 public void write_nspaces (uint n) {
183 stream.puts (string.nfill (n, ' '));
187 * Writes the specified string.
191 public void write_string (string s) {
199 public void write_newline () {
208 current_line_number++;
213 * Opens a new block, increasing the indent level.
215 public void write_begin_block () {
227 * Closes the current block, decreasing the indent level.
229 public void write_end_block () {
238 * Writes the specified text as comment.
240 * @param text the comment text
242 public void write_comment (string text) {
248 // discard tabs at beginning of line
249 if (fix_indent_regex == null)
250 fix_indent_regex = new GLib.Regex ("^\t+");;
252 foreach (unowned string line in text.split ("\n")) {
259 var lineparts = fix_indent_regex.replace_literal (line, -1, 0, "").split ("*/");
261 for (int i = 0; lineparts[i] != null; i++) {
262 stream.puts (lineparts[i]);
263 if (lineparts[i+1] != null) {
270 } catch (RegexError e) {