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
|
// Authors:
// Stephan Sundermann <stephansundermann@gmail.com>
//
// Copyright (c) 2013 Stephan Sundermann
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
using System;
using System.Xml;
using System.IO;
namespace GtkSharp.Generation
{
public class Constant
{
private readonly string name;
private readonly string value;
private readonly string ctype;
public Constant (XmlElement elem)
{
this.name = elem.GetAttribute ("name");
this.value = elem.GetAttribute ("value");
this.ctype = elem.GetAttribute ("ctype");
}
public string Name {
get {
return this.name;
}
}
public string ConstType {
get {
if (IsString)
return "string";
// gir registers all integer values as gint even for numbers which do not fit into a gint
// if the number is too big for an int, try to fit it into a long
if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length < 20 && long.Parse (value) > Int32.MaxValue)
return "long";
return SymbolTable.Table.GetMarshalType (ctype);
}
}
public bool IsString {
get {
return (SymbolTable.Table.GetCSType (ctype) == "string");
}
}
public virtual bool Validate (LogWriter log)
{
if (ConstType == String.Empty) {
log.Warn ("{0} type is missing or wrong", Name);
return false;
}
if (SymbolTable.Table.GetMarshalType (ctype) == "int" && value.Length >= 20) {
return false;
}
return true;
}
public virtual void Generate (GenerationInfo gen_info, string indent)
{
StreamWriter sw = gen_info.Writer;
sw.WriteLine ("{0}public const {1} {2} = {3}{4}{5};",
indent,
ConstType,
Name,
IsString ? "@\"": String.Empty,
value,
IsString ? "\"": String.Empty);
}
}
}
|