add path-textfield getelm function, that supports urls

Tue, 30 Jan 2024 08:43:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 30 Jan 2024 08:43:34 +0100
changeset 9
0676408f50ad
parent 8
726b24766437
child 10
f8dfc5705516

add path-textfield getelm function, that supports urls

application/window.c file | annotate | diff | comparison | revisions
--- a/application/window.c	Mon Jan 29 18:50:04 2024 +0100
+++ b/application/window.c	Tue Jan 30 08:43:34 2024 +0100
@@ -35,6 +35,8 @@
 static UiIcon* folder_icon;
 static UiIcon* file_icon;
 
+static UiPathElm* dav_get_pathelm(const char *full_path, size_t len, size_t *ret_nelm, void* data);
+
 void window_init(void) {
 	folder_icon = ui_foldericon(16);
 	file_icon = ui_fileicon(16);
@@ -54,7 +56,7 @@
 		ui_button(obj, .icon = "Back");
 		ui_button(obj, .icon = "Forward");
 
-		ui_path_textfield(obj, .fill = UI_ON, .varname = "path");
+		ui_path_textfield(obj, .fill = UI_ON, .getpathelm = dav_get_pathelm, .varname = "path");
 
 		ui_progressspinner(obj, .value = wdata->progress);
 	}
@@ -98,3 +100,80 @@
 void window_progress(MainWindow *win, int on) {
 	ui_set(win->progress, on);
 }
+
+
+
+static UiPathElm* dav_get_pathelm(const char *full_path, size_t len, size_t *ret_nelm, void* data) {
+	cxstring fpath = cx_strn(full_path, len);
+	int protocol = 0;
+	if(cx_strcaseprefix(fpath, CX_STR("http://"))) {
+		protocol = 7;
+	} else if (cx_strcaseprefix(fpath, CX_STR("https://"))) {
+		protocol = 8;
+	}
+	
+	size_t start = 0;
+	size_t end = 0;
+	for (size_t i = protocol; i < len; i++) {
+		if (full_path[i] == '/') {
+			end = i;
+			break;
+		}
+	}
+
+	int skip = 0;
+	if (end == 0) {
+		// no '/' found or first char is '/'
+		end = len > 0 && full_path[0] == '/' ? 1 : len;
+	} else if (end + 1 <= len) {
+		skip++; // skip first '/'
+	}
+	
+
+	cxmutstr base = cx_strdup(cx_strn(full_path, end));
+	cxmutstr base_path = cx_strdup(cx_strcast(base));
+	cxstring path = cx_strsubs(fpath, end+skip);
+
+	cxstring *pathelms;
+	size_t nelm = cx_strsplit_a(cxDefaultAllocator, path, CX_STR("/"), 4096, &pathelms);
+
+	if (nelm == 0) {
+		*ret_nelm = 0;
+		return NULL;
+	}
+
+	UiPathElm* elms = (UiPathElm*)calloc(nelm + 1, sizeof(UiPathElm));
+	size_t n = nelm + 1;
+	elms[0].name = base.ptr;
+	elms[0].name_len = base.length;
+	elms[0].path = base_path.ptr;
+	elms[0].path_len = base_path.length;
+
+	int j = 1;
+	for (int i = 0; i < nelm; i++) {
+		cxstring c = pathelms[i];
+		if (c.length == 0) {
+			if (i == 0) {
+				c.length = 1;
+			}
+			else {
+				n--;
+				continue;
+			}
+		}
+
+		cxmutstr m = cx_strdup(c);
+		elms[j].name = m.ptr;
+		elms[j].name_len = m.length;
+
+		size_t elm_path_len = c.ptr + c.length - full_path;
+		cxmutstr elm_path = cx_strdup(cx_strn(full_path, elm_path_len));
+		elms[j].path = elm_path.ptr;
+		elms[j].path_len = elm_path.length;
+
+		j++;
+	}
+	*ret_nelm = n;
+
+	return elms;
+}

mercurial