Removal of items

Should be fairly obvious what I'm trying to do with the below, posting it here on the off chance someone wants to try fixing it themselves while I take a break from debugging/programming. The full code is at https://github.com/awsdert
*Note IUP will need to be installed and a few Environment variables will need to be set if you want to compile it.
Edit: Just the resize function works fine, only added so you can understand what it does with the data before passing it back to the calling function.
Edit: Moving the resize function to a reply post.
Edit: Code below now reflects the fixed code.
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
typedef uchar hack_t;
#define HACK_T_MAX UCHAR_MAX
typedef struct _HACK
{
  uchar   use : 1;
  // Is Radio List
  uchar   irl : 1;
  // ID (codelist file usage only)
  ulong   id;
  // Current Index
  hack_t _ci;
  // Parent Index
  hack_t _pi;
  // 1st Child Index
  hack_t _fi;
  // Next Sibling Index
  hack_t _ni;
} HACK;
typedef struct _HACKS
{
  hack_t i;
  hack_t c;
  hack_t _c;
  size_t s;
  HACK   a[1];
} HACKS;
void guiHacks_OnRemUpdate ( hack_t i, hack_t c, hack_t r )
{
  HACK *hack = &tmpHacks.hacks->a[i];
  hack->_ci = i;
  if ( hack->_fi )
  {
    guiHacks_OnRemUpdate ( hack->_fi > r ? hack->_fi -= c : hack->_fi, c, r );
  }
  if ( hack->_ni )
  {
    guiHacks_OnRemUpdate ( hack->_ni > r ? hack->_ni -= c : hack->_ni, c, r );
  }
}
void guiHacks_OnRem ( void )
{
  if ( !tmpIndex || tmpHacks.hacks->i < 1 || tmpHacks.hacks->c <= 1 )
  {
    return;
  }
  hack_t i = tmpHacks.hacks->i, c = tmpHacks.hacks->c;
  HACK *hack = &tmpHacks.hacks->a[i];
  char *name = tmpHacks.names[i].a;
  HACK *ownr = &tmpHacks.hacks->a[hack->_pi];
  HACK *prev = &tmpHacks.hacks->a[i - 1];
  hack_t n = hack->_ni ? hack->_ni : ownr->_ni;
  HACK *next = n ? &tmpHacks.hacks->a[n] : NULL;
  if ( !hack->_ni )
  {
    if ( prev == ownr )
    {
      ownr->_fi = 0;
    }
    else
    {
      prev->_ni = 0;
    }
  }
  if ( next )
  {
    c -= n;
    memmove ( &tmpIndex[i], &tmpIndex[n], c * sizeof ( hack_t ) );
    memmove ( name, tmpHacks.names[n].a, c * NAME_MAX );
    memmove ( hack, next, c * sizeof ( HACK ) );
    n -= i;
    c = tmpHacks.hacks->c - n;
  }
  else
  {
    n = 1;
    c = i;
  }
  guiHacks_OnRemUpdate ( 0, n, i );
  hacksReSize ( &tmpHacks, &tmpIndex, c );
  guiHacks_OnShow ( guiHacks.main.fset );
}
Last edited on
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
void hacksReSize( HACKL *hl, hack_t **indexList, hack_t count )
{
  count = count ? count : 1;
  hack_t c =  ( hl->hacks ? hl->hacks->c : 0);
  hack_t _c = ( hl->hacks ? hl->hacks->_c : 0);
  size_t s = (count - 1) * sizeof(HACK);
  if ( count <= _c && hl->hacks )
  {
    hl->hacks->c = count;
    if ( count >= c )
      return;
    c = _c - count;
    memset( &hl->names[count], 0, c * NAME_MAX );
    memset( &hl->hacks->a[count], 0, c * sizeof(HACK) );
    if ( indexList )
      memset( &(*indexList)[count], 0, c * sizeof(hack_t) );
    return;
  }
  if ( !hl->hacks )
  {
    hl->hacks = malloc( sizeof(HACKS) );
    hl->hacks->c = 0;
    hl->hacks->i = 0;
  }
  hl->hacks = realloc( hl->hacks, sizeof(HACKS) + s );
  hl->names = realloc( hl->names, count * NAME_MAX );
  hl->hacks->_c = count;
  hl->hacks->s  = s;
  hack_t i = hl->hacks->c;
  c = count - i;
  memset( &hl->names[i], 0, c * NAME_MAX );
  memset( &hl->hacks->a[i], 0, c * sizeof(HACK) );
  if ( indexList )
  {
    *indexList = realloc( *indexList, count * sizeof(hack_t) );
    memset( &(*indexList)[i], 0, c * sizeof(hack_t) );
  }
  hl->hacks->c = count;
}
Last edited on
Suppose I should mention this, when I try removing via my GUI I end up with everything after the removed item disappearing until I add a new item (which forces all items after the last identified item to be forced as the next item after the selected index - workaround for an issue during inserting/appending items)
Last edited on
Just spotted a fatal error, had been using wrong update function in the guiHacks_OnRemUpdate function, corrected and now have an infinite loop so will figure that 1 next.
Fixed my code, updated the above to reflect so others can use my efforts as a reference.
For those who are curious what I did wrong here it is:

The update function got into a infinite loop because it was causing the _fi/_ni to = _ci when ever it found them to be equal to or greater than the selected index to remove so by setting this to capture only those greater than the index the infinite loop ceased to be, obviously this would've created a problem if the index happened to be the last in the branch so by forcing prev->_ni or ownr->_fi to be 0 beforehand if hack->_ni was 0 then that potential problem also ceased to be before it ever had a chance to do anything. I also noticed that I had forgotten to dereference indexList in my resize function when reducing the count which resulted in my tree trying to use a null pointer which inevitably caused segmentation faults I was having trouble figuring out the root cause of.
Topic archived. No new replies allowed.