2014-05-14 23:52:45 -04:00
|
|
|
/*
|
|
|
|
* token.c
|
2014-06-27 01:24:40 -04:00
|
|
|
* Copyright (C) 2014 c9s <yoanlin93@gmail.com>
|
2014-05-14 23:52:45 -04:00
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
2015-11-17 07:59:17 -05:00
|
|
|
#include "config.h"
|
2014-05-14 23:52:45 -04:00
|
|
|
#include <stdlib.h>
|
2014-05-15 00:47:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-05-26 09:39:36 -04:00
|
|
|
#include "r3.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "r3_slug.h"
|
2014-05-28 09:08:06 -04:00
|
|
|
#include "str_array.h"
|
2014-05-20 13:50:15 -04:00
|
|
|
#include "zmalloc.h"
|
2014-05-14 23:52:45 -04:00
|
|
|
|
2014-05-16 06:03:52 -04:00
|
|
|
str_array * str_array_create(int cap) {
|
2014-05-20 13:50:15 -04:00
|
|
|
str_array * list = (str_array*) zmalloc( sizeof(str_array) );
|
2014-05-31 13:25:34 -04:00
|
|
|
if (!list)
|
|
|
|
return NULL;
|
2014-05-14 23:52:45 -04:00
|
|
|
list->len = 0;
|
2016-03-08 01:19:54 -05:00
|
|
|
list->slugs_len = 0;
|
2014-05-14 23:52:45 -04:00
|
|
|
list->cap = cap;
|
2016-03-08 01:19:54 -05:00
|
|
|
list->slugs = (char**) zmalloc( sizeof(char*) * cap);
|
2014-05-20 13:50:15 -04:00
|
|
|
list->tokens = (char**) zmalloc( sizeof(char*) * cap);
|
2014-05-14 23:52:45 -04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2014-05-16 06:03:52 -04:00
|
|
|
void str_array_free(str_array *l) {
|
2014-05-31 13:26:36 -04:00
|
|
|
assert(l);
|
2014-05-21 06:22:11 -04:00
|
|
|
for ( int i = 0; i < l->len ; i++ ) {
|
2014-05-26 13:07:33 -04:00
|
|
|
if (l->tokens[ i ]) {
|
|
|
|
zfree(l->tokens[i]);
|
|
|
|
}
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
2016-03-08 01:19:54 -05:00
|
|
|
zfree(l->slugs);
|
2015-09-11 03:14:07 -04:00
|
|
|
zfree(l->tokens);
|
2014-05-21 06:22:11 -04:00
|
|
|
zfree(l);
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 01:19:54 -05:00
|
|
|
bool str_array_slugs_full(const str_array * l) {
|
|
|
|
return l->slugs_len >= l->cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool str_array_tokens_full(const str_array * l) {
|
2014-05-14 23:52:45 -04:00
|
|
|
return l->len >= l->cap;
|
|
|
|
}
|
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
bool str_array_resize(str_array * l, int new_cap) {
|
2016-03-08 01:19:54 -05:00
|
|
|
l->slugs = zrealloc(l->slugs, sizeof(char**) * new_cap);
|
2014-05-20 13:50:15 -04:00
|
|
|
l->tokens = zrealloc(l->tokens, sizeof(char**) * new_cap);
|
2014-05-14 23:52:45 -04:00
|
|
|
l->cap = new_cap;
|
2016-03-08 01:19:54 -05:00
|
|
|
return l->tokens != NULL && l->slugs != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool str_array_append_slug(str_array * l, char * slug) {
|
|
|
|
if ( str_array_slugs_full(l) ) {
|
|
|
|
bool ret = str_array_resize(l, l->cap + 20);
|
|
|
|
if (ret == false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l->slugs[ l->slugs_len++ ] = slug;
|
|
|
|
return true;
|
2014-05-14 23:52:45 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 06:03:52 -04:00
|
|
|
bool str_array_append(str_array * l, char * token) {
|
2016-03-08 01:19:54 -05:00
|
|
|
if ( str_array_tokens_full(l) ) {
|
2014-05-16 06:03:52 -04:00
|
|
|
bool ret = str_array_resize(l, l->cap + 20);
|
2015-11-17 08:43:10 -05:00
|
|
|
if (ret == false ) {
|
|
|
|
return false;
|
2014-05-14 23:52:45 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 00:47:14 -04:00
|
|
|
l->tokens[ l->len++ ] = token;
|
2015-11-17 08:43:10 -05:00
|
|
|
return true;
|
2014-05-14 23:52:45 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 01:19:54 -05:00
|
|
|
void str_array_dump_slugs(const str_array *l) {
|
|
|
|
printf("[");
|
|
|
|
for ( int i = 0; i < l->slugs_len ; i++ ) {
|
|
|
|
printf("\"%s\"", l->slugs[i] );
|
|
|
|
if ( i + 1 != l->slugs_len ) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
void str_array_dump(const str_array *l) {
|
2014-05-15 00:47:14 -04:00
|
|
|
printf("[");
|
|
|
|
for ( int i = 0; i < l->len ; i++ ) {
|
|
|
|
printf("\"%s\"", l->tokens[i] );
|
|
|
|
if ( i + 1 != l->len ) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:52:45 -04:00
|
|
|
|
|
|
|
|
2014-05-15 00:47:14 -04:00
|
|
|
|