Hash :
96b055e1
Author :
Thomas de Grivel
Date :
2024-11-14T16:27:55
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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include "buf.h"
#include "buf_inspect.h"
#include "env.h"
#include "facts_cursor.h"
#include "facts_spec.h"
#include "facts_with.h"
#include "facts_with_cursor.h"
void facts_with_cursor_clean (s_facts_with_cursor *cursor)
{
uw i = 0;
assert(cursor);
if (cursor->facts_count) {
while (i < cursor->facts_count) {
if (cursor->levels[i].spec) {
free(cursor->levels[i].spec);
facts_cursor_clean(&cursor->levels[i].cursor);
}
i++;
}
free(cursor->levels);
free(cursor->spec);
if (pthread_mutex_destroy(&cursor->mutex)) {
err_puts("facts_with_cursor_clean: pthread_mutex_destroy");
assert(! "facts_with_cursor_clean: pthread_mutex_destroy");
exit(1);
}
}
}
s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
s_fact **dest)
{
s_fact *fact = NULL;
s_facts_with_cursor_level *level;
p_facts_spec parent_spec;
assert(cursor);
if (! cursor->facts_count) {
*dest = NULL;
return dest;
}
if (pthread_mutex_lock(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_lock");
assert(! "facts_with_cursor_next: pthread_mutex_lock");
exit(1);
return NULL;
}
if (cursor->level == cursor->facts_count) {
level = &cursor->levels[cursor->facts_count - 1];
#ifdef DEBUG_FACTS
err_write_1("[debug] cursor->level=");
err_inspect_uw(&cursor->level);
err_write_1(" level->spec=");
err_inspect_facts_spec(level->spec);
err_write_1(" ");
err_inspect_fact(level->fact);
#endif
if (! facts_cursor_next(&level->cursor, &level->fact))
goto ko;
#ifdef DEBUG_FACTS
err_write_1(" -> ");
err_inspect_fact(level->fact);
err_write_1("\n");
err_flush(err);
#endif
if (level->fact) {
if (pthread_mutex_unlock(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_unlock");
assert(! "facts_with_cursor_next: pthread_mutex_unlock");
exit(1);
}
*dest = level->fact;
return dest;
}
free(level->spec);
level->spec = NULL;
cursor->level--;
if (! cursor->level)
goto not_found;
cursor->level--;
}
while (cursor->level < cursor->facts_count) {
level = &cursor->levels[cursor->level];
if (! level->spec) {
if (cursor->level)
parent_spec = cursor->levels[cursor->level - 1].spec + 4;
else
parent_spec = cursor->spec;
level->spec = facts_spec_new_expand(parent_spec);
if (! facts_with_tags(cursor->facts, &level->cursor,
level->spec[0], level->spec[1],
level->spec[2]))
goto ko;
}
#ifdef DEBUG_FACTS
buf_write_1(&g_c3_env.err, "[debug] cursor->level=");
buf_inspect_uw(&g_c3_env.err, &cursor->level);
buf_write_1(&g_c3_env.err, " level->spec=");
buf_inspect_facts_spec(&g_c3_env.err, level->spec);
buf_write_1(&g_c3_env.err, " ");
buf_inspect_fact(&g_c3_env.err, level->fact);
#endif
if (! facts_cursor_next(&level->cursor, &fact))
goto ko;
level->fact = fact;
#ifdef DEBUG_FACTS
buf_write_1(&g_c3_env.err, " -> ");
buf_inspect_fact(&g_c3_env.err, level->fact);
buf_write_1(&g_c3_env.err, "\n");
buf_flush(&g_c3_env.err);
#endif
if (fact) {
cursor->level++;
continue;
}
free(level->spec);
level->spec = NULL;
if (! cursor->level)
goto not_found;
cursor->level--;
}
if (pthread_mutex_unlock(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_unlock");
assert(! "facts_with_cursor_next: pthread_mutex_unlock");
exit(1);
}
*dest = fact;
return dest;
not_found:
cursor->facts_count = 0;
free(cursor->levels);
free(cursor->spec);
if (pthread_mutex_unlock(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_unlock");
assert(! "facts_with_cursor_next: pthread_mutex_unlock");
abort();
}
if (pthread_mutex_destroy(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_destroy");
assert(! "facts_with_cursor_next: pthread_mutex_destroy");
abort();
}
*dest = NULL;
return dest;
ko:
if (pthread_mutex_unlock(&cursor->mutex)) {
err_puts("facts_with_cursor_next: pthread_mutex_unlock");
assert(! "facts_with_cursor_next: pthread_mutex_unlock");
exit(1);
}
return NULL;
}