tests: Add explicit "move" and "resize" array tests to increase coverage
[vala-gnome.git] / ccode / valaccodewriter.vala
blob281ff6e435fcb349e864f855c83ed48ee8e8c369
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 static GLib.Regex fix_indent_regex;
53 private string temp_filename;
54 private bool file_exists;
56 private FileStream? stream;
58 private int indent;
59 private int current_line_number = 1;
60 private bool using_line_directive;
62 /* at begin of line */
63 private bool _bol = true;
65 public CCodeWriter (string filename, string? source_filename = null) {
66 this.filename = filename;
67 this.source_filename = source_filename;
70 /**
71 * Opens the file.
73 * @return true if the file has been opened successfully,
74 * false otherwise
76 public bool open (bool write_version) {
77 file_exists = FileUtils.test (filename, FileTest.EXISTS);
78 if (file_exists) {
79 temp_filename = "%s.valatmp".printf (filename);
80 stream = FileStream.open (temp_filename, "w");
81 } else {
83 * File doesn't exist. In case of a particular destination (-d flag),
84 * check and create the directory structure.
86 var dirname = Path.get_dirname (filename);
87 DirUtils.create_with_parents (dirname, 0755);
88 stream = FileStream.open (filename, "w");
91 if (stream == null) {
92 return false;
95 var opening = write_version ?
96 "/* %s generated by valac %s, the Vala compiler".printf (Path.get_basename (filename), Config.BUILD_VERSION) :
97 "/* %s generated by valac, the Vala compiler".printf (Path.get_basename (filename));
98 write_string (opening);
100 // Write the file name if known
101 if (source_filename != null) {
102 write_newline ();
103 write_string (" * generated from %s".printf (Path.get_basename (source_filename)));
106 write_string (", do not modify */");
107 write_newline ();
108 write_newline ();
110 return true;
114 * Closes the file.
116 public void close () {
117 stream = null;
119 if (file_exists) {
120 var changed = true;
122 try {
123 var old_file = new MappedFile (filename, false);
124 var new_file = new MappedFile (temp_filename, false);
125 var len = old_file.get_length ();
126 if (len == new_file.get_length ()) {
127 if (Memory.cmp (old_file.get_contents (), new_file.get_contents (), len) == 0) {
128 changed = false;
131 old_file = null;
132 new_file = null;
133 } catch (FileError e) {
134 // assume changed if mmap comparison doesn't work
137 if (changed) {
138 FileUtils.rename (temp_filename, filename);
139 } else {
140 FileUtils.unlink (temp_filename);
146 * Writes tabs according to the current indent level.
148 public void write_indent (CCodeLineDirective? line = null) {
149 if (line_directives) {
150 if (line != null) {
151 line.write (this);
152 using_line_directive = true;
153 } else if (using_line_directive) {
154 // no corresponding Vala line, emit line directive for C line
155 write_string ("#line %d \"%s\"".printf (current_line_number + 1, Path.get_basename (filename)));
156 write_newline ();
157 using_line_directive = false;
161 if (!_bol) {
162 write_newline ();
165 stream.puts (string.nfill (indent, '\t'));
166 _bol = false;
170 * Writes the specified string.
172 * @param s a string
174 public void write_string (string s) {
175 stream.puts (s);
176 _bol = false;
180 * Writes a newline.
182 public void write_newline () {
183 stream.putc ('\n');
184 current_line_number++;
185 _bol = true;
189 * Opens a new block, increasing the indent level.
191 public void write_begin_block () {
192 if (!_bol) {
193 stream.putc (' ');
194 } else {
195 write_indent ();
197 stream.putc ('{');
198 write_newline ();
199 indent++;
203 * Closes the current block, decreasing the indent level.
205 public void write_end_block () {
206 assert (indent > 0);
208 indent--;
209 write_indent ();
210 stream.putc ('}');
214 * Writes the specified text as comment.
216 * @param text the comment text
218 public void write_comment (string text) {
219 try {
220 write_indent ();
221 stream.puts ("/*");
222 bool first = true;
224 // discard tabs at beginning of line
225 if (fix_indent_regex == null)
226 fix_indent_regex = new GLib.Regex ("^\t+");;
228 foreach (unowned string line in text.split ("\n")) {
229 if (!first) {
230 write_indent ();
231 } else {
232 first = false;
235 var lineparts = fix_indent_regex.replace_literal (line, -1, 0, "").split ("*/");
237 for (int i = 0; lineparts[i] != null; i++) {
238 stream.puts (lineparts[i]);
239 if (lineparts[i+1] != null) {
240 stream.puts ("* /");
244 stream.puts ("*/");
245 write_newline ();
246 } catch (RegexError e) {
247 // ignore