2014-06-01 16:52:41 -04:00
|
|
|
/*
|
|
|
|
* slug.c
|
2014-06-27 01:24:40 -04:00
|
|
|
* Copyright (C) 2014 c9s <yoanlin93@gmail.com>
|
2014-06-01 16:52:41 -04:00
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
2014-06-01 18:58:02 -04:00
|
|
|
#include "config.h"
|
2014-06-01 17:48:27 -04:00
|
|
|
#include <stdio.h>
|
2014-06-01 16:52:41 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "r3.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "r3_slug.h"
|
2014-06-01 16:52:41 -04:00
|
|
|
#include "slug.h"
|
2015-11-17 08:24:36 -05:00
|
|
|
#include "r3_debug.h"
|
2014-06-01 16:52:41 -04:00
|
|
|
#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) {
|
2014-06-01 16:52:41 -04:00
|
|
|
r3_slug_t * s = zmalloc(sizeof(r3_slug_t));
|
2014-06-01 17:48:27 -04:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2014-06-03 09:47:52 -04:00
|
|
|
s->path = (char*) path;
|
2014-06-01 16:52:41 -04:00
|
|
|
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) {
|
2014-06-01 16:52:41 -04:00
|
|
|
// 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) {
|
2014-06-01 16:52:41 -04:00
|
|
|
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
|
|
|
|
2014-06-01 16:52:41 -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-01 16:52:41 -04:00
|
|
|
*/
|
|
|
|
|
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;
|
2014-06-01 21:09:41 -04:00
|
|
|
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)) {
|
2014-06-01 21:09:41 -04:00
|
|
|
return 0;
|
2014-06-01 17:02:37 -04:00
|
|
|
}
|
2014-06-01 16:52:41 -04:00
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
int state = 0;
|
2014-06-03 10:15:59 -04:00
|
|
|
const char * p = offset;
|
2014-06-01 16:52:41 -04:00
|
|
|
|
|
|
|
while( (p-needle) < needle_len) {
|
2014-06-01 17:02:37 -04:00
|
|
|
// escape one character
|
2014-06-01 16:52:41 -04:00
|
|
|
if (*p == '\\' ) {
|
|
|
|
p++; p++;
|
2014-06-01 17:02:37 -04:00
|
|
|
continue;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
2014-06-01 17:02:37 -04:00
|
|
|
|
|
|
|
// slug starts with '{'
|
2014-06-01 16:52:41 -04:00
|
|
|
if (state == 0 && *p == '{') {
|
2014-06-01 17:02:37 -04:00
|
|
|
s->begin = ++p;
|
|
|
|
state++;
|
|
|
|
continue;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 17:02:37 -04:00
|
|
|
// in the middle of the slug (pattern)
|
2014-06-01 16:52:41 -04:00
|
|
|
if (state == 1 && *p == ':') {
|
|
|
|
// start from next
|
2014-06-01 17:02:37 -04:00
|
|
|
s->pattern = ++p;
|
|
|
|
continue;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 17:02:37 -04:00
|
|
|
// slug closed.
|
2014-06-01 16:52:41 -04:00
|
|
|
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++;
|
2014-06-01 16:52:41 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-01 17:02:37 -04:00
|
|
|
// might be inside the pattern
|
2014-06-01 16:52:41 -04:00
|
|
|
if ( *p == '{' ) {
|
|
|
|
state++;
|
|
|
|
} else if ( *p == '}' ) {
|
|
|
|
state--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
};
|
|
|
|
|
2014-06-01 21:48:00 -04:00
|
|
|
if (state != 0) {
|
2014-06-01 16:52:41 -04:00
|
|
|
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;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
2014-06-01 21:09:41 -04:00
|
|
|
return -1;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
2014-06-01 21:48:00 -04:00
|
|
|
info("found slug\n");
|
2014-06-01 21:09:41 -04:00
|
|
|
return 1;
|
2014-06-01 16:52:41 -04:00
|
|
|
}
|
2014-06-01 18:32:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* provide a quick way to count slugs, simply search for '{'
|
|
|
|
*/
|
2015-08-26 10:00:23 -04:00
|
|
|
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
|
|
|
}
|
2014-06-02 03:47:02 -04:00
|
|
|
return -1;
|
2014-06-01 18:32:30 -04:00
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|