Skip to content

Commit

Permalink
Fix path regularization; Add th::convPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ske2004 committed Jul 17, 2024
1 parent 7267e4f commit 1021669
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
41 changes: 29 additions & 12 deletions src/bindings.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -49,22 +50,20 @@ conv_path(char *path)
}
}

char *out = NULL;
const char *dir = NULL;

switch (pfx) {
case PFX_RAW: out = strdup(path); break;
case PFX_RES:
out = calloc(1, strlen(thg->res_dir) + strlen(path) + 1);
strcpy(out, thg->res_dir);
strcat(out, path);
break;
case PFX_DATA:
out = calloc(1, strlen(thg->data_dir) + strlen(path) + 1);
strcpy(out, thg->data_dir);
strcat(out, path);
break;
case PFX_RAW: dir = "./"; break;
case PFX_RES: dir = thg->res_dir; break;
case PFX_DATA: dir = thg->data_dir; break;
default: assert(0 && "Invalid PFX");
}

size_t sz = strlen(dir) + strlen(path) + 2;
char *out = calloc(1, sz);

th_regularize_path(path, dir, out, sz);

return out;
}

Expand Down Expand Up @@ -1269,6 +1268,21 @@ umth_canvas_end_scissor(UmkaStackSlot *p, UmkaStackSlot *r)
th_canvas_end_scissor();
}

/////////////////////////
// Tophat

// fn umth_conv_path(path: str): str
void
umth_conv_path(UmkaStackSlot *p, UmkaStackSlot *r)
{
char *s = conv_path(umkaGetParam(p, 0)->ptrVal);
umkaGetResult(p, r)->ptrVal = umkaMakeStr(thg->umka, s);
free(s);
}

/////////////////////////
// Transform

// fn umth_transform_rect(r: Rect, t: th::Transform): th::Quad
void
umth_transform_rect(UmkaStackSlot *p, UmkaStackSlot *r)
Expand Down Expand Up @@ -1582,6 +1596,9 @@ _th_umka_bind(void *umka)
umkaAddFunc(umka, "umth_canvas_begin_scissor_rect", &umth_canvas_begin_scissor_rect);
umkaAddFunc(umka, "umth_canvas_end_scissor", &umth_canvas_end_scissor);

// th
umkaAddFunc(umka, "umth_conv_path", &umth_conv_path);

// transform
umkaAddFunc(umka, "umth_transform_rect", &umth_transform_rect);
umkaAddFunc(umka, "umth_transform_quad", &umth_transform_quad);
Expand Down
18 changes: 13 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
#define UMPROF_IMPL
#include <umprof.h>
#ifdef _WIN32
#include <windows.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <windows.h>
#define mkdir(p, m) mkdir(p)
#define PATH_MAX 512
#else
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#include <sokol_app.h>


#ifndef TH_VERSION
#define TH_VERSION ""
#define TH_GITVER ""
Expand All @@ -50,15 +49,15 @@ warning(UmkaError *error)
int
th_init(const char *scriptpath, const char *script_src)
{
{
if (thg->res_dir == NULL) {
thg->res_dir = strdup(scriptpath);
char *fname = strrchr(thg->res_dir, '/');
ssize_t len = fname == NULL ? strlen(thg->res_dir) : strlen(fname) - 1;
thg->res_dir[strlen(thg->res_dir) - len] = '\0';
}

{
char *res_dir = thg->res_dir[0] == '\0' ? "./" : thg->res_dir;
char *res_dir = thg->res_dir;
#ifdef _WIN32
char abs[PATH_MAX];
GetFullPathNameA(res_dir, PATH_MAX - 1, abs, NULL);
Expand Down Expand Up @@ -289,6 +288,15 @@ th_main(int argc, char *argv[])
}

th_error("No module named %s\n", argv[thg->argOffset + 1]);
thg->argOffset += 2;
} else if (strcmp(argv[thg->argOffset], "-dir") == 0) {
if ((argc - thg->argOffset) < 2) {
printf("dir takes one argument\n");
exit(1);
}

thg->res_dir = strdup(argv[thg->argOffset + 1]);

thg->argOffset += 2;
} else if (strcmp(argv[thg->argOffset], "-doc") == 0) {
if ((argc - thg->argOffset) < 2) {
Expand Down
16 changes: 14 additions & 2 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ void
th_regularize_path(const char *path, const char *cur_folder, char *regularized_path, int size)
{
size_t o = 0;
bool is_slash = true;

// for now simply convert all backslashes to forward slashes and ignore `./`
for (size_t i = 0; cur_folder[i] && size; i++) {
if ((i == 0 || cur_folder[i-1] == '/' || cur_folder[i-1] == '\\') && cur_folder[i] == '.' &&
if ((i == 0 || cur_folder[i - 1] == '/' || cur_folder[i - 1] == '\\') &&
cur_folder[i] == '.' &&
(cur_folder[i + 1] == '/' || cur_folder[i + 1] == '\\')) {
i++;
is_slash = true;
continue;
}

Expand All @@ -125,11 +128,20 @@ th_regularize_path(const char *path, const char *cur_folder, char *regularized_p
} else {
regularized_path[o] = cur_folder[i];
}

is_slash = regularized_path[o] == '/';

o++;
}

if (!is_slash && size) {
regularized_path[o] = '/';
o++;
size--;
}

for (size_t i = 0; path[i] && size; i++) {
if ((i == 0 || path[i-1] == '/' || path[i-1] == '\\') && path[i] == '.' &&
if ((i == 0 || path[i - 1] == '/' || path[i - 1] == '\\') && path[i] == '.' &&
(path[i + 1] == '/' || path[i + 1] == '\\')) {
i++;
continue;
Expand Down
16 changes: 16 additions & 0 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,14 @@ const char *th_em_modulesrc[] = {
"\tplatform*: Platform\n"
")\n"
"//~~\n"
"\n"
"fn umth_conv_path(path: str): str\n"
"//~~fn convPath\n"
"// Expands the path to a platform specific path.\n"
"fn convPath*(path: str): str {\n"
"//~~\n"
"\treturn umth_conv_path(path)\n"
"}\n"
"",
"//~~\n"
"// A module for importless communication between modules. A signal is a set of\n"
Expand Down Expand Up @@ -6007,6 +6015,14 @@ const char *th_em_moduledocs[] = {
"\n"
"---------\n"
"\n"
"fn convPath\n"
"\n"
"fn convPath*(path: str): str {\n"
"\n"
"Expands the path to a platform specific path.\n"
"\n"
"---------\n"
"\n"
"",
"\n"
"\n"
Expand Down
8 changes: 8 additions & 0 deletions umka/th.um
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,11 @@ var (
platform*: Platform
)
//~~

fn umth_conv_path(path: str): str
//~~fn convPath
// Expands the path to a platform specific path.
fn convPath*(path: str): str {
//~~
return umth_conv_path(path)
}

0 comments on commit 1021669

Please sign in to comment.