Line data Source code
1 : /* valaccodeconstant.vala
2 : *
3 : * Copyright (C) 2006-2010 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * A constant C expression.
27 : */
28 885 : public class Vala.CCodeConstant : CCodeExpression {
29 : /**
30 : * The name of this constant.
31 : */
32 385776 : public string name { get; set; }
33 :
34 363597 : public CCodeConstant (string _name) {
35 121199 : name = _name;
36 : }
37 :
38 : const int LINE_LENGTH = 70;
39 :
40 14833 : public CCodeConstant.string (string _name) {
41 7390 : assert (_name[0] == '\"');
42 :
43 7390 : if (_name.length <= LINE_LENGTH) {
44 7337 : name = _name;
45 7337 : return;
46 : }
47 :
48 53 : var builder = new StringBuilder ("\"");
49 :
50 53 : char* p = _name;
51 53 : char* end = p + _name.length;
52 :
53 : // remove quotes
54 53 : p++;
55 53 : end--;
56 :
57 53 : int col = 0;
58 6005 : while (p < end) {
59 5952 : if (col >= LINE_LENGTH) {
60 91 : builder.append ("\" \\\n\"");
61 : col = 0;
62 : }
63 5952 : if (*p == '\\') {
64 135 : char* begin_of_char = p;
65 :
66 135 : builder.append_c (p[0]);
67 135 : builder.append_c (p[1]);
68 135 : p += 2;
69 135 : switch (p[-1]) {
70 : case 'x':
71 : // hexadecimal character
72 0 : while (p < end && p->isxdigit ()) {
73 0 : builder.append_c (*p);
74 0 : p++;
75 : }
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 0 : while (p < end && p - begin_of_char <= 3 && *p >= '0' && *p <= '7') {
87 0 : builder.append_c (*p);
88 0 : p++;
89 : }
90 : break;
91 : case 'n':
92 : // break line at \n
93 48 : col = LINE_LENGTH;
94 48 : break;
95 : }
96 135 : col += (int) (p - begin_of_char);
97 : } else {
98 5817 : builder.append_unichar (((string) p).get_char ());
99 5817 : p += ((char*) ((string) p).next_char () - p);
100 5817 : col++;
101 : }
102 : }
103 :
104 53 : builder.append_c ('"');
105 :
106 53 : this.name = builder.str;
107 : }
108 :
109 92526 : public override void write (CCodeWriter writer) {
110 92526 : writer.write_string (name);
111 : }
112 : }
|