sanskrit-iast/syllable.c

83 lines
1.3 KiB
C
Raw Normal View History

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"
struct syllable *syllable_alloc(const char *data)
{
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
ptr->data = strdup(data);
ptr->code = 0;
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);
}
struct syllable *syllable_append(struct syllable *tail, const char *data)
{
struct syllable *ptr;
ptr = syllable_alloc(data);
2018-04-29 08:42:11 +02:00
ptr->prev = tail;
2018-04-25 15:16:12 +02:00
tail->next = ptr;
return ptr;
}
unsigned int syllable_chain_length(struct syllable *head)
{
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;
}
void syllable_chain_drop(struct syllable *head)
{
struct syllable *walk = head, *next;
while (walk) {
next = walk->next;
syllable_drop(walk);
walk = next;
}
}