change run to use meson/ninja and then exec. - remove libvala code from application...
[roobuilder] / src / ccode / valaccodeconstant.vala
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 public class Vala.CCodeConstant : CCodeExpression {
29         /**
30          * The name of this constant.
31          */
32         public string name { get; set; }
33
34         public CCodeConstant (string _name) {
35                 name = _name;
36         }
37
38         const int LINE_LENGTH = 70;
39
40         public CCodeConstant.string (string _name) {
41                 assert (_name[0] == '\"');
42
43                 if (_name.length <= LINE_LENGTH) {
44                         name = _name;
45                         return;
46                 }
47
48                 var builder = new StringBuilder ("\"");
49
50                 char* p = _name;
51                 char* end = p + _name.length;
52
53                 // remove quotes
54                 p++;
55                 end--;
56
57                 int col = 0;
58                 while (p < end) {
59                         if (col >= LINE_LENGTH) {
60                                 builder.append ("\" \\\n\"");
61                                 col = 0;
62                         }
63                         if (*p == '\\') {
64                                 char* begin_of_char = p;
65
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++;
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                                         while (p < end && p - begin_of_char <= 3 && *p >= '0' && *p <= '7') {
87                                                 builder.append_c (*p);
88                                                 p++;
89                                         }
90                                         break;
91                                 case 'n':
92                                         // break line at \n
93                                         col = LINE_LENGTH;
94                                         break;
95                                 }
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++;
101                         }
102                 }
103
104                 builder.append_c ('"');
105
106                 this.name = builder.str;
107         }
108
109         public override void write (CCodeWriter writer) {
110                 writer.write_string (name);
111         }
112 }