Files
ivy/common/ident.c

73 lines
1.4 KiB
C
Raw Normal View History

#include <blue/core/queue.h>
#include <blue/core/stringstream.h>
2025-11-06 10:38:23 +00:00
#include <blue/ds/string.h>
#include <ivy/ident.h>
#include <stdlib.h>
#include <string.h>
struct ivy_ident *ivy_ident_create(void)
{
struct ivy_ident *id = malloc(sizeof *id);
if (!id) {
return NULL;
}
memset(id, 0x0, sizeof *id);
return id;
}
void ivy_ident_destroy(struct ivy_ident *ident)
{
2025-11-06 10:38:23 +00:00
b_queue_entry *entry = b_queue_first(&ident->id_parts);
while (entry) {
struct ivy_ident_part *part
2025-11-06 10:38:23 +00:00
= b_unbox(struct ivy_ident_part, entry, p_entry);
b_queue_entry *next = b_queue_next(entry);
b_queue_delete(&ident->id_parts, entry);
free(part->p_str);
free(part);
2025-11-06 10:38:23 +00:00
entry = next;
}
free(ident);
}
void ivy_ident_add_part(struct ivy_ident *ident, const char *s)
{
struct ivy_ident_part *part = malloc(sizeof *part);
if (!part) {
return;
}
memset(part, 0x0, sizeof *part);
part->p_str = b_strdup(s);
b_queue_push_back(&ident->id_parts, &part->p_entry);
}
size_t ivy_ident_to_string(const struct ivy_ident *ident, char *out, size_t max)
{
2025-11-06 10:38:23 +00:00
b_stringstream *strv = b_stringstream_create_with_buffer(out, max);
int i = 0;
2025-11-06 10:38:23 +00:00
b_queue_entry *entry = b_queue_first(&ident->id_parts);
while (entry) {
if (i > 0) {
2025-11-06 10:38:23 +00:00
b_stream_write_char(strv, '.');
}
struct ivy_ident_part *part
2025-11-06 10:38:23 +00:00
= b_unbox(struct ivy_ident_part, entry, p_entry);
b_stream_write_string(strv, part->p_str, NULL);
i++;
}
/* TODO return string length. */
return 0;
}