summaryrefslogtreecommitdiff
path: root/ccode/valaccodeconstant.vala
blob: a3377237ae0c71d2d42473eb32de1d711c94db62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* valaccodeconstant.vala
 *
 * Copyright (C) 2006-2010  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
 * A constant C expression.
 */
public class Vala.CCodeConstant : CCodeExpression {
	/**
	 * The name of this constant.
	 */
	public string name { get; set; }

	public CCodeConstant (string _name) {
		name = _name;
	}

	const int LINE_LENGTH = 70;

	public CCodeConstant.string (string _name) {
		assert (_name[0] == '\"');

		if (_name.length <= LINE_LENGTH) {
			name = _name;
			return;
		}

		var builder = new StringBuilder ("\"");

		char* p = _name;
		char* end = p + _name.length;

		// remove quotes
		p++;
		end--;

		int col = 0;
		while (p < end) {
			if (col >= LINE_LENGTH) {
				builder.append ("\" \\\n\"");
				col = 0;
			}
			if (*p == '\\') {
				char* begin_of_char = p;

				builder.append_c (p[0]);
				builder.append_c (p[1]);
				p += 2;
				switch (p[-1]) {
				case 'x':
					// hexadecimal character
					while (p < end && p->isxdigit ()) {
						builder.append_c (*p);
						p++;
					}
					break;
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
					// octal character
					while (p < end && p - begin_of_char <= 3 && *p >= '0' && *p <= '7') {
						builder.append_c (*p);
						p++;
					}
					break;
				case 'n':
					// break line at \n
					col = LINE_LENGTH;
					break;
				}
				col += (int) (p - begin_of_char);
			} else {
				builder.append_unichar (((string) p).get_char ());
				p += ((char*) ((string) p).next_char () - p);
				col++;
			}
		}

		builder.append_c ('"');

		this.name = builder.str;
	}

	public override void write (CCodeWriter writer) {
		writer.write_string (name);
	}
}