2018-04-25 19:30:49 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
|
2018-04-25 15:16:12 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "syllable.h"
|
2018-06-03 15:59:16 +02:00
|
|
|
#include "utf8.h"
|
2018-04-25 15:16:12 +02:00
|
|
|
|
|
|
|
|
2018-06-03 15:59:16 +02:00
|
|
|
struct syllable *syllable_alloc(const char *data, unsigned int code)
|
2018-04-25 15:16:12 +02:00
|
|
|
{
|
|
|
|
struct syllable *ptr = malloc(sizeof(*ptr));
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
2018-05-16 14:58:47 +02:00
|
|
|
goto out;
|
2018-04-25 15:16:12 +02:00
|
|
|
|
2018-06-03 15:59:16 +02:00
|
|
|
ptr->data = data != NULL ? strdup(data) : utf8_code_to_string(code);
|
|
|
|
ptr->code = code;
|
2018-04-29 08:42:11 +02:00
|
|
|
ptr->prev = NULL;
|
2018-04-25 15:16:12 +02:00
|
|
|
ptr->next = NULL;
|
|
|
|
|
2018-05-16 14:58:47 +02:00
|
|
|
out:
|
2018-04-25 15:16:12 +02:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void syllable_drop(struct syllable *ptr)
|
|
|
|
{
|
2018-05-16 14:58:47 +02:00
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(ptr->data);
|
2018-04-25 15:16:12 +02:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-04-29 08:21:46 +02:00
|
|
|
unsigned int syllable_chain_length(struct syllable *head)
|
2018-04-25 15:29:24 +02:00
|
|
|
{
|
|
|
|
struct syllable *walk = head;
|
|
|
|
unsigned int length = 0;
|
|
|
|
|
|
|
|
while (walk) {
|
|
|
|
length += strlen(walk->data);
|
|
|
|
walk = walk->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *syllable_chain_to_string(struct syllable *head)
|
|
|
|
{
|
|
|
|
struct syllable *walk = head;
|
|
|
|
unsigned int length = syllable_chain_length(head);
|
|
|
|
char *buffer = malloc(length + 1);
|
|
|
|
char *ptr = buffer;
|
|
|
|
|
|
|
|
while (walk) {
|
|
|
|
strcpy(ptr, walk->data);
|
|
|
|
ptr += strlen(walk->data);
|
|
|
|
walk = walk->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2018-05-18 10:09:06 +02:00
|
|
|
|
|
|
|
void syllable_chain_drop(struct syllable *head)
|
|
|
|
{
|
|
|
|
struct syllable *walk = head, *next;
|
|
|
|
|
|
|
|
while (walk) {
|
|
|
|
next = walk->next;
|
|
|
|
syllable_drop(walk);
|
|
|
|
walk = next;
|
|
|
|
}
|
|
|
|
}
|