feat: added sling url decomposition
This commit is contained in:
10
include/myke/util/slingurl.h
Normal file
10
include/myke/util/slingurl.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by rick on 26-03-21.
|
||||
//
|
||||
|
||||
#ifndef NEW_KERNEL_SLINGURL_H
|
||||
#define NEW_KERNEL_SLINGURL_H
|
||||
|
||||
void slingurl_decompose(const char* url);
|
||||
|
||||
#endif //NEW_KERNEL_SLINGURL_H
|
||||
@@ -19,6 +19,8 @@ size_t strlen(const char *str);
|
||||
|
||||
const char *strchr(const char *s, char c);
|
||||
|
||||
const char *strrchr(const char *s, char c);
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#ifdef ENABLE_SELF_TEST
|
||||
|
||||
#include <myke/debug/debug.h>
|
||||
#include <myke/util/slingurl.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -56,6 +57,8 @@ void exec_self_test(const char *args);
|
||||
|
||||
void smash(const char *args);
|
||||
|
||||
void slingurl(const char* args);
|
||||
|
||||
#endif
|
||||
|
||||
cmd_handler cmd_handlers[] = {
|
||||
@@ -65,6 +68,7 @@ cmd_handler cmd_handlers[] = {
|
||||
{"ide", ide},
|
||||
{"shutdown", shutdown},
|
||||
{"explode", explode},
|
||||
{"slingurl", slingurl},
|
||||
#ifdef ENABLE_SELF_TEST
|
||||
{"self-test", exec_self_test},
|
||||
{"smash", smash},
|
||||
@@ -72,6 +76,10 @@ cmd_handler cmd_handlers[] = {
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
void slingurl(const char* args) {
|
||||
slingurl_decompose(args);
|
||||
}
|
||||
|
||||
void smash(const char *args) {
|
||||
char data[16];
|
||||
memset(data, 'A', 32);
|
||||
|
||||
@@ -56,6 +56,19 @@ const char *strchr(const char *s, char c) {
|
||||
}
|
||||
}
|
||||
|
||||
const char *strrchr(const char *s, char c) {
|
||||
int index = strlen(s);
|
||||
while (1) {
|
||||
if (s[index] == c) {
|
||||
return &s[index];
|
||||
}
|
||||
if (index == 0) {
|
||||
return NULL;
|
||||
}
|
||||
index--;
|
||||
}
|
||||
}
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n) {
|
||||
uint8_t a, b;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
|
||||
71
kernel/util/slingurl.c
Normal file
71
kernel/util/slingurl.c
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Created by rick on 26-03-21.
|
||||
//
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <myke/mem/malloc.h>
|
||||
|
||||
void slingurl_decompose(const char* url) {
|
||||
uint32_t urllen = strlen(url);
|
||||
if (urllen <= 0) {
|
||||
printf("Url empty");
|
||||
return;
|
||||
}
|
||||
char* lurl = malloc(urllen + 1);
|
||||
strcpy(lurl, url);
|
||||
|
||||
// get path section
|
||||
char* path = lurl;
|
||||
|
||||
// find fist selector
|
||||
char* selector = strchr(lurl, '.');
|
||||
int nselectors = 0;
|
||||
// to lazy to deal with urls without selectors and extension
|
||||
if (selector == NULL) goto free_malloc;
|
||||
selector[0] = 0;
|
||||
selector += 1;
|
||||
|
||||
// find suffix
|
||||
char* suffix = strchr(selector, '/');
|
||||
if (suffix != NULL) {
|
||||
suffix[0] = 0;
|
||||
suffix += 1;
|
||||
}
|
||||
|
||||
// pick last "suffix" as extension
|
||||
char* extension = strrchr(selector, '.');
|
||||
if (extension == NULL) {
|
||||
// no dots in suffix section, so only an extension
|
||||
extension = selector;
|
||||
selector = NULL;
|
||||
} else {
|
||||
extension[0] = 0;
|
||||
extension += 1;
|
||||
}
|
||||
|
||||
if (selector != NULL) {
|
||||
nselectors = 1;
|
||||
char* cselector = selector;
|
||||
while ((cselector = strchr(cselector, '.')) != NULL) {
|
||||
cselector[0] = 0;
|
||||
cselector += 1;
|
||||
nselectors++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("path: %s\n", path);
|
||||
printf("selectors: ");
|
||||
char* cselector = selector;
|
||||
for (int i = 0; i < nselectors; ++i) {
|
||||
printf("%s,", cselector);
|
||||
cselector += strlen(cselector) + 1;
|
||||
}
|
||||
printf("\n");
|
||||
printf("extension: %s\n", extension);
|
||||
printf("suffix: %s\n", suffix);
|
||||
free_malloc:
|
||||
free(lurl);
|
||||
}
|
||||
Reference in New Issue
Block a user