/bitfield/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/bitfield/trunk
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
namespace BitField {
  
  private static GLib.Tree<string, Type?> list_of_types;
  
  private static GLib.Tree<FieldInfo?, uint16> mask_cash;
  
  void init () {
    list_of_types = new GLib.Tree<string, Type?> ((a, b) => {
      return (GLib.strcmp (a,b));
    });
    
    mask_cash = new GLib.Tree<FieldInfo?, uint16> ((a, b) => {
       return a.compare (b);
    });
  }
  
  void deinit () {
    list_of_types.foreach ((_key, _val) => {
      list_of_types.remove (_key);
      
      return false;
    });
  }
  
  /**
   * @Return true on error.
   */
  public bool add_type (string name, FieldInfo first_field, ...) {
    var va = va_list ();
    
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
    
    lst.append (first_field);
    for (FieldInfo? fi = va.arg<FieldInfo> (); fi != null;
                                               fi = va.arg<FieldInfo> ()) {
      lst.append (fi);
    }
    
    if (lst.length () >= 16) {
      return true;
    }
    
    lst.sort ((a,b) => {return a.compare (b);});
    
    
    for (uint8 i = 0; i < lst.length (); i++) {
      var a = lst.nth_data (i); 
      // We valitade the items whilst we are at it.
      if (a.validate ()) {
        return true;
      }
      for (uint8 j = i + 1; i < lst.length (); j++) {
        var b = lst.nth_data (i);
        
        
        if (a.overlap (b)) {
          GLib.critical ("Overlappinng fields in %s: (%s) (%s).\n" +
                         "\t Will not add bitmap type defitions.",
                         lst.nth_data (i).to_string (),
                         lst.nth_data (j).to_string ());
          return true;
        }
      }
    }
    
    Type t = Type ();
    for (uint8 i = 0; i < lst.length (); i++) {
      t.fields[i] = lst.nth_data (i);
    }
    
    return false;
  }
  
  public void set_8 (ref uint8 field_id, string type_name, uint8 data) {
    
    
    
  }
  
  public struct FieldInfo {
    uint8 field_id;
    uint8 start;
    uint8 end;
    uint8 length;
    
    public int compare (FieldInfo other) {
      if (this.field_id != other.field_id) {
        return other.field_id - this.field_id; 
      } else if (this.start != other.start) {
        return other.start - this.start;
      } else if (this.end != other.end) {
        return other.end - this.end;
      } else if (this.length != other.length) {
        return other.length - this.length;
      }
      
      #if 0
      if (this.start > other.start) {
        return -1;
      } else if (this.start < other.start) {
        return 1;
      } else {
        if (this.end > other.end) {
          return -1;
        } else if (this.end < other.end) {
          return 1;
        } else {
          if (this.length > other.length) {
            return -1;
          } else if (this.length < other.length) {
            return 1;
          }
        }
      }
      #endif
      
      return 0;
    }
    
    [CCode (cname = "bit_field_field_info_compare")]
    public static extern int static_campare (FieldInfo a, FieldInfo b);
    
    
    public bool overlap (FieldInfo other) {
      return (!((this.start < other.end) || (this.end > other.start)));
    }
    
    [CCode (cname = "bit_field_field_info_overlap")]
    public static extern int static_overlap (FieldInfo a, FieldInfo b);
    
    
    public string to_string () {
      return "start: %i, end: %i, length: %i".printf (this.start,
                                                      this.end,
                                                      this.length);
    }
    
    /**
     * returns true on error;
     */
    public bool validate () {
      var distance = this.start - this.end;
      if (distance < 1 || distance != this.length) {
        GLib.critical ("Validtion if FieldInfo object failed: (%s)",
                       this.to_string ());
        return true;
      }
      return false;
    }
    
    [CCode (cname = "bit_field_field_info_validate")]
    public extern static bool static_validate (FieldInfo info);
    
    public uint16 generate_mask () {
      uint16 mask = 0;
      for (size_t i = 0; i < this.length; i++) {
        mask += 0x8000; // set the left-most bit in the field
        mask >> 1; // shit it over to the right one.
      }
      
      mask >> this.start;
      
      return mask;
    }
    
    [CCode (cname = "bit_field_field_generate_mask")]
    public extern static uint16 static_generate_mask (FieldInfo info);
  }
  
  private struct Type {
    FieldInfo[] fields;
    
    Type () {
        fields = new FieldInfo[16];
    }
    
    public string to_string () {
      var sb = new GLib.StringBuilder ();
      
      sb.append (typeof (Type).name ())
        .append (": (\n");
      for (size_t i = 0; i < fields.length; i++) {
          sb.append ("\t (")
            .append (fields[i].to_string ())
            .append (")\n");
      }
      
      sb.append (")\n");
      return sb.str;
    }
  }
  
  
  
  
}