/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
1
/*
2
 * The contects of this file is in the Public Domain.
3
 *
4
 * Created by Gustav Hartivgsson.
5
 */
6
1 by Gustav Hartvigsson
* Initial code - far from done
7
namespace VQDR.Common.Utils {
8
  public int str_cmp (string a, string b) {
9
    return a.collate (b);
10
  }
11
  
12
  public void print_ln (string str, ...) {
13
    var va = va_list ();
14
    
15
    // Reallocate the string as it is not owned by the function.
16
    var tmp = str + "\n";
17
    
18
    // Hopefully this does not create any memony leaks. :-)
19
    print (tmp.vprintf (va));
20
  }
21
  
22
  public string object_to_string (GLib.Object obj) {
23
    GLib.ObjectClass real_obj = (GLib.ObjectClass) obj.get_type ().class_ref ();
24
    
25
    var str_builder = new GLib.StringBuilder ();
26
    
27
    
28
    str_builder.append ("(")
29
               .append (obj.get_type().name ())
30
               .append ("):\n");
31
    
32
    foreach (GLib.ParamSpec ps in real_obj.list_properties ()) {
33
      
34
      if (ps.value_type == 0 || ps.value_type == GLib.Type.INVALID) {
35
        continue;
36
      }
37
      
38
      var prop_name = ps.get_name ();
39
      
40
      Value prop_val = GLib.Value (ps.value_type);
41
      
42
      if (prop_val.type () == GLib.Type.INVALID || prop_val.type () == 0) {
43
        continue;
44
      }
45
      
46
      obj.get_property (prop_name, ref prop_val);
47
      
48
      
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
49
      str_builder.append ("\t(")
1 by Gustav Hartvigsson
* Initial code - far from done
50
                 .append (prop_val.type_name ())
51
                 .append (") ")
52
                 .append (prop_name)
53
                 .append (": ");
54
      
55
      
56
      switch (prop_val.type ()) {
57
        case (GLib.Type.STRING):
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
58
          if (prop_val.dup_string () == null) {
59
            str_builder.append ("(null)");
8 by Gustav Hartvigsson
* Fixed string handling in gobject_to_string ().
60
          } else {
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
61
            str_builder.append (prop_val.dup_string ());
8 by Gustav Hartvigsson
* Fixed string handling in gobject_to_string ().
62
          }
1 by Gustav Hartvigsson
* Initial code - far from done
63
        break;
64
        case (GLib.Type.INT):
65
          str_builder.append (prop_val.get_int ().to_string ());
66
        break;
67
        case (GLib.Type.BOOLEAN):
68
          str_builder.append (prop_val.get_boolean ().to_string ());
69
        break;
70
        case (GLib.Type.CHAR):
71
          var v = prop_val.get_schar ();
72
          str_builder.append_c (v)
73
                     .append (" (")
74
                     .append_printf ("%hhx", v);
75
          str_builder.append (")");
76
        break;
77
        case (GLib.Type.DOUBLE):
78
          str_builder.append (prop_val.get_double ().to_string ());
79
        break;
80
        case (GLib.Type.ENUM):
81
          str_builder.append (prop_val.get_enum ().to_string ());
82
        break;
83
        case (GLib.Type.FLAGS):
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
84
          // TODO: Probobly needs better handling, but this will do.
1 by Gustav Hartvigsson
* Initial code - far from done
85
          str_builder.append (prop_val.get_flags ().to_string ());
86
        break;
87
        case (GLib.Type.FLOAT):
88
          str_builder.append (prop_val.get_float ().to_string ());
89
        break;
90
        case (GLib.Type.INT64):
91
          str_builder.append (prop_val.get_int64 ().to_string ());
92
        break;
93
        case (GLib.Type.LONG):
94
          str_builder.append (prop_val.get_long ().to_string ());
95
        break;
96
        case (GLib.Type.OBJECT):
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
97
          str_builder.append_printf ("%llX", (((long)((pointer)prop_val.dup_object ()))));
1 by Gustav Hartvigsson
* Initial code - far from done
98
        break;
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
99
        /* /!\ NOTE: Invalid case /!\
100
         * A ParamSpec can't "contain" a ParamSpec.
1 by Gustav Hartvigsson
* Initial code - far from done
101
        case (GLib.Type.PARAM):
102
          var spsc = prop_val.get_param ();
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
103
          if (spsc == null) {
104
            str_builder.append ("(null)");
105
          } else {
106
            str_builder.append ("name: ")
107
                       .append (spsc.name)
108
                       .append (" type: ")
109
                       .append (spsc.value_type.name ());
110
          }
1 by Gustav Hartvigsson
* Initial code - far from done
111
        break;
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
112
        */
1 by Gustav Hartvigsson
* Initial code - far from done
113
        case (GLib.Type.POINTER):
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
114
          str_builder.append ("(")
115
                     .append_printf ("%llX", (((long)prop_val.get_pointer ())));
116
          str_builder.append (")");
117
        break;
118
        case (GLib.Type.BOXED):
119
          str_builder.append ("(")
120
                     .append_printf ("%llX", (((long)prop_val.get_boxed ())));
121
          str_builder.append (")");
1 by Gustav Hartvigsson
* Initial code - far from done
122
        break;
123
        case (GLib.Type.UCHAR):
124
          var v = prop_val.get_uchar ();
125
          str_builder.append_c ((char) v)
126
                     .append (" (")
127
                     .append_printf ("%hhx", v);
128
          str_builder.append (")");
129
        break;
130
        case (GLib.Type.UINT):
131
          str_builder.append (prop_val.get_uint ().to_string ());
132
        break;
133
        case (GLib.Type.UINT64):
134
          str_builder.append (prop_val.get_uint64 ().to_string ());
135
        break;
136
        case (GLib.Type.ULONG):
137
          str_builder.append (prop_val.get_ulong ().to_string ());
138
        break;
139
        case (GLib.Type.VARIANT):
9 by Gustav Hartvigsson
* object_to_string () - Prittier prints,
140
          GLib.Variant v = prop_val.dup_variant ();
141
          GLib.Variant? tv = null;
142
          unowned string ts1 = v.get_type_string ();
143
          str_builder.append ("")
144
                     .append (ts1)
145
                     .append ("\n\t(\n");
146
          GLib.VariantIter iter = v.iterator ();
147
          tv = iter.next_value ();
148
          while (tv != null) {
149
            unowned string ts2 = tv.get_type_string ();
150
            string tp = tv.print (true);
151
            str_builder.append ("\t\t((")
152
                       .append (ts2)
153
                       .append ("): ")
154
                       .append (tp)
155
                       .append (")\n");
156
            tv = iter.next_value ();
157
          }
158
          str_builder.append ("\t)");
159
          
1 by Gustav Hartvigsson
* Initial code - far from done
160
        break;
161
      }
162
      str_builder.append ("\n");
163
    }
164
    
165
    return str_builder.str;
166
  }
167
  
168
}