/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/common/utils.vala

  • Committer: Gustav Hartvigsson
  • Date: 2020-11-04 21:39:01 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20201104213901-6m5r3h8qj0unfysz
* object_to_string () - Prittier prints,
                       - use Value.dup_* () where applicable
                       - made sure the value string is not null before appending
* Added more tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contects of this file is in the Public Domain.
 
3
 *
 
4
 * Created by Gustav Hartivgsson.
 
5
 */
 
6
 
1
7
namespace VQDR.Common.Utils {
2
8
  public int str_cmp (string a, string b) {
3
9
    return a.collate (b);
40
46
      obj.get_property (prop_name, ref prop_val);
41
47
      
42
48
      
43
 
      str_builder.append ("\t (")
 
49
      str_builder.append ("\t(")
44
50
                 .append (prop_val.type_name ())
45
51
                 .append (") ")
46
52
                 .append (prop_name)
49
55
      
50
56
      switch (prop_val.type ()) {
51
57
        case (GLib.Type.STRING):
52
 
          str_builder.append (prop_val.get_string ());
 
58
          if (prop_val.dup_string () == null) {
 
59
            str_builder.append ("(null)");
 
60
          } else {
 
61
            str_builder.append (prop_val.dup_string ());
 
62
          }
53
63
        break;
54
64
        case (GLib.Type.INT):
55
65
          str_builder.append (prop_val.get_int ().to_string ());
71
81
          str_builder.append (prop_val.get_enum ().to_string ());
72
82
        break;
73
83
        case (GLib.Type.FLAGS):
74
 
          // Probobly needs better handling, but this will do.
 
84
          // TODO: Probobly needs better handling, but this will do.
75
85
          str_builder.append (prop_val.get_flags ().to_string ());
76
86
        break;
77
87
        case (GLib.Type.FLOAT):
84
94
          str_builder.append (prop_val.get_long ().to_string ());
85
95
        break;
86
96
        case (GLib.Type.OBJECT):
87
 
          str_builder.append_printf ("%llX", (((long)((pointer)prop_val.get_object ()))));
 
97
          str_builder.append_printf ("%llX", (((long)((pointer)prop_val.dup_object ()))));
88
98
        break;
 
99
        /* /!\ NOTE: Invalid case /!\
 
100
         * A ParamSpec can't "contain" a ParamSpec.
89
101
        case (GLib.Type.PARAM):
90
102
          var spsc = prop_val.get_param ();
91
 
          str_builder.append ("name: ")
92
 
                     .append (spsc.name)
93
 
                     .append (" type: ")
94
 
                     .append (spsc.value_type.name ());
 
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
          }
95
111
        break;
 
112
        */
96
113
        case (GLib.Type.POINTER):
97
 
          str_builder.append_printf ("%llX", (((long)prop_val.get_pointer ())));
 
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 (")");
98
122
        break;
99
123
        case (GLib.Type.UCHAR):
100
124
          var v = prop_val.get_uchar ();
113
137
          str_builder.append (prop_val.get_ulong ().to_string ());
114
138
        break;
115
139
        case (GLib.Type.VARIANT):
116
 
          GLib.Variant v = prop_val.get_variant ();
117
 
          str_builder.append ("(\n")
118
 
                     .append (v.print (true))
119
 
                     .append ("\n)");
 
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
          
120
160
        break;
121
161
      }
122
162
      str_builder.append ("\n");
123
 
      
124
163
    }
125
164
    
126
165
    return str_builder.str;