r3/src/slug.c

190 lines
4 KiB
C
Raw Normal View History

/*
* slug.c
2014-06-27 01:24:40 -04:00
* Copyright (C) 2014 c9s <yoanlin93@gmail.com>
*
* Distributed under terms of the MIT license.
*/
2014-06-01 18:58:02 -04:00
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "r3.h"
#include "r3_slug.h"
#include "slug.h"
#include "r3_debug.h"
#include "zmalloc.h"
2014-06-01 17:02:37 -04:00
2014-06-03 09:47:52 -04:00
r3_slug_t * r3_slug_new(const char * path, int path_len) {
r3_slug_t * s = zmalloc(sizeof(r3_slug_t));
if (!s)
return NULL;
2014-06-03 09:47:52 -04:00
s->path = (char*) path;
s->path_len = path_len;
s->begin = NULL;
s->end = NULL;
s->len = 0;
s->pattern = NULL;
s->pattern_len = 0;
return s;
}
void r3_slug_free(r3_slug_t * s) {
zfree(s);
}
/**
* Return 1 means OK
* Return 0 means Empty
* Return -1 means Error
*/
2014-06-01 18:27:35 -04:00
int r3_slug_check(r3_slug_t *s) {
// if it's empty
if (s->begin == NULL && s->len == 0) {
return 0;
}
if (s->begin && s->begin == s->end && s->len == 0) {
return 0;
}
// if the head is defined, we should also have end pointer
if (s->begin && s->end == NULL) {
return -1;
}
return 0;
}
2014-06-01 17:55:45 -04:00
char * r3_slug_to_str(const r3_slug_t *s) {
char *str = NULL;
asprintf(&str, "slug: '%.*s', pattern: '%.*s', path: '%.*s'", s->len, s->begin, s->pattern_len, s->pattern, s->path_len, s->path);
return str;
}
2014-06-01 17:02:37 -04:00
/*
r3_slug_t * r3_slug_parse_next(r3_slug_t *s, char **errstr) {
return r3_slug_parse(s->end, s->path_len - (s->end - s->begin), errstr);
}
2014-06-02 04:12:27 -04:00
Return 0 => Empty, slug not found
Return 1 => Slug found
Return -1 => Slug parsing error
*/
2014-06-04 01:45:28 -04:00
int r3_slug_parse(r3_slug_t *s, const char *needle, int needle_len, const char *offset, char **errstr) {
2014-06-03 09:47:52 -04:00
s->path = (char*) needle;
s->path_len = needle_len;
2014-06-01 17:02:37 -04:00
2014-06-01 21:48:00 -04:00
if (offset == NULL) {
2014-06-01 17:02:37 -04:00
offset = (char*) needle; // from the begining of the needle
}
// there is no slug
2014-06-01 18:27:35 -04:00
if (!r3_path_contains_slug_char(offset)) {
return 0;
2014-06-01 17:02:37 -04:00
}
int cnt = 0;
int state = 0;
2014-06-03 10:15:59 -04:00
const char * p = offset;
while( (p-needle) < needle_len) {
2014-06-01 17:02:37 -04:00
// escape one character
if (*p == '\\' ) {
p++; p++;
2014-06-01 17:02:37 -04:00
continue;
}
2014-06-01 17:02:37 -04:00
// slug starts with '{'
if (state == 0 && *p == '{') {
2014-06-01 17:02:37 -04:00
s->begin = ++p;
state++;
continue;
}
2014-06-01 17:02:37 -04:00
// in the middle of the slug (pattern)
if (state == 1 && *p == ':') {
// start from next
2014-06-01 17:02:37 -04:00
s->pattern = ++p;
continue;
}
2014-06-01 17:02:37 -04:00
// slug closed.
if (state == 1 && *p == '}') {
s->end = p;
s->len = s->end - s->begin;
if (s->pattern) {
s->pattern_len = p - s->pattern;
}
cnt++;
state--;
2014-06-01 17:02:37 -04:00
p++;
break;
}
2014-06-01 17:02:37 -04:00
// might be inside the pattern
if ( *p == '{' ) {
state++;
} else if ( *p == '}' ) {
state--;
}
p++;
};
2014-06-01 21:48:00 -04:00
if (state != 0) {
if (errstr) {
2014-06-03 11:42:40 -04:00
char *err = NULL;
asprintf(&err, "Incomplete slug pattern. PATH (%d): '%s', OFFSET: %ld, STATE: %d", needle_len, needle, p - needle, state);
*errstr = err;
}
return -1;
}
2014-06-01 21:48:00 -04:00
info("found slug\n");
return 1;
}
2014-06-01 18:32:30 -04:00
/**
* provide a quick way to count slugs, simply search for '{'
*/
int r3_slug_count(const char * needle, int len, char **errstr) {
2014-06-01 18:32:30 -04:00
int cnt = 0;
int state = 0;
char * p = (char*) needle;
while( (p-needle) < len) {
if (*p == '\\' ) {
p++; p++;
2014-06-01 19:10:31 -04:00
continue;
2014-06-01 18:32:30 -04:00
}
if (state == 1 && *p == '}') {
cnt++;
}
if ( *p == '{' ) {
state++;
} else if ( *p == '}' ) {
state--;
}
p++;
};
info("FOUND PATTERN: '%s' (%d), STATE: %d\n", needle, len, state);
if (state != 0) {
if (errstr) {
2014-06-03 11:42:40 -04:00
char *err = NULL;
asprintf(&err, "Incomplete slug pattern. PATTERN (%d): '%s', OFFSET: %ld, STATE: %d", len, needle, p - needle, state);
*errstr = err;
2014-06-01 18:32:30 -04:00
}
return -1;
2014-06-01 18:32:30 -04:00
}
return cnt;
}