codegen: Fix floating reference regression with Variants
[vala-gnome.git] / ccode / valaccodeconstant.vala
bloba3377237ae0c71d2d42473eb32de1d711c94db62
1 /* valaccodeconstant.vala
3 * Copyright (C) 2006-2010 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 * A constant C expression.
28 public class Vala.CCodeConstant : CCodeExpression {
29 /**
30 * The name of this constant.
32 public string name { get; set; }
34 public CCodeConstant (string _name) {
35 name = _name;
38 const int LINE_LENGTH = 70;
40 public CCodeConstant.string (string _name) {
41 assert (_name[0] == '\"');
43 if (_name.length <= LINE_LENGTH) {
44 name = _name;
45 return;
48 var builder = new StringBuilder ("\"");
50 char* p = _name;
51 char* end = p + _name.length;
53 // remove quotes
54 p++;
55 end--;
57 int col = 0;
58 while (p < end) {
59 if (col >= LINE_LENGTH) {
60 builder.append ("\" \\\n\"");
61 col = 0;
63 if (*p == '\\') {
64 char* begin_of_char = p;
66 builder.append_c (p[0]);
67 builder.append_c (p[1]);
68 p += 2;
69 switch (p[-1]) {
70 case 'x':
71 // hexadecimal character
72 while (p < end && p->isxdigit ()) {
73 builder.append_c (*p);
74 p++;
76 break;
77 case '0':
78 case '1':
79 case '2':
80 case '3':
81 case '4':
82 case '5':
83 case '6':
84 case '7':
85 // octal character
86 while (p < end && p - begin_of_char <= 3 && *p >= '0' && *p <= '7') {
87 builder.append_c (*p);
88 p++;
90 break;
91 case 'n':
92 // break line at \n
93 col = LINE_LENGTH;
94 break;
96 col += (int) (p - begin_of_char);
97 } else {
98 builder.append_unichar (((string) p).get_char ());
99 p += ((char*) ((string) p).next_char () - p);
100 col++;
104 builder.append_c ('"');
106 this.name = builder.str;
109 public override void write (CCodeWriter writer) {
110 writer.write_string (name);