moved utility functions in src/lib/utils

This commit is contained in:
cozis
2022-08-16 21:48:59 +02:00
parent 3209afb513
commit 0e94f644ab
6 changed files with 91 additions and 70 deletions
+1 -43
View File
@@ -52,48 +52,6 @@ static int bin_print(Runtime *runtime, Object **argv, unsigned int argc, Object
return 0; return 0;
} }
// Returns the length written in buff (not considering the zero byte)
static size_t getCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize)
{
const char *path = Runtime_GetCurrentScriptAbsolutePath(runtime);
if(path == NULL) {
if(getcwd(buff, sizeof(buffsize)) == NULL)
return 0;
return strlen(buff);
}
// This following block is a custom implementation
// of [dirname], which doesn't write into the input
// string and is way buggier. It will for sure give
// problems in the future!!
size_t dir_len;
{
// This is buggy code!!
size_t path_len = strlen(path);
assert(path_len > 0); // Not empty
assert(path[0] == '/'); // Is absolute
assert(path[path_len-1] != '/'); // Doesn't end with a slash.
size_t popped = 0;
while(path[path_len-1-popped] != '/')
popped += 1;
assert(path_len > popped);
dir_len = path_len - popped;
assert(dir_len < path_len);
assert(path[dir_len-1] == '/');
}
if(dir_len >= buffsize)
return 0;
memcpy(buff, path, dir_len);
buff[dir_len] = '\0';
return dir_len;
}
static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Error *error) static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Error *error)
{ {
assert(argc == 1); assert(argc == 1);
@@ -128,7 +86,7 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
} }
strcpy(full_path, path); strcpy(full_path, path);
} else { } else {
size_t written = getCurrentScriptFolder(runtime, full_path, sizeof(full_path)); size_t written = Runtime_GetCurrentScriptFolder(runtime, full_path, sizeof(full_path));
if(written == 0) { if(written == 0) {
Error_Report(error, 1, "Internal buffer is too small"); Error_Report(error, 1, "Internal buffer is too small");
return -1; return -1;
+46
View File
@@ -28,7 +28,10 @@
** +--------------------------------------------------------------------------+ ** +--------------------------------------------------------------------------+
*/ */
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "../utils/path.h"
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/stack.h" #include "../utils/stack.h"
#include "runtime.h" #include "runtime.h"
@@ -56,6 +59,49 @@ struct xRuntime {
Heap *heap; Heap *heap;
}; };
// Returns the length written in buff (not considering the zero byte)
size_t Runtime_GetCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize)
{
const char *path = Runtime_GetCurrentScriptAbsolutePath(runtime);
if(path == NULL) {
if(getcwd(buff, sizeof(buffsize)) == NULL)
return 0;
return strlen(buff);
}
// This following block is a custom implementation
// of [dirname], which doesn't write into the input
// string and is way buggier. It will for sure give
// problems in the future!!
size_t dir_len;
{
// This is buggy code!!
size_t path_len = strlen(path);
assert(path_len > 0); // Not empty
assert(Path_IsAbsolute(path)); // Is absolute
assert(path[path_len-1] != '/'); // Doesn't end with a slash.
size_t popped = 0;
while(path[path_len-1-popped] != '/')
popped += 1;
assert(path_len > popped);
dir_len = path_len - popped;
assert(dir_len < path_len);
assert(path[dir_len-1] == '/');
}
if(dir_len >= buffsize)
return 0;
memcpy(buff, path, dir_len);
buff[dir_len] = '\0';
return dir_len;
}
const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime) const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime)
{ {
Executable *exe = Runtime_GetCurrentExecutable(runtime); Executable *exe = Runtime_GetCurrentExecutable(runtime);
+1
View File
@@ -51,6 +51,7 @@ Object* Runtime_GetBuiltins(Runtime *runtime);
void Runtime_SetBuiltins(Runtime *runtime, Object *builtins); void Runtime_SetBuiltins(Runtime *runtime, Object *builtins);
int Runtime_GetCurrentIndex(Runtime *runtime); int Runtime_GetCurrentIndex(Runtime *runtime);
Executable *Runtime_GetCurrentExecutable(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime);
size_t Runtime_GetCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize);
const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime); const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime);
Snapshot *Snapshot_New(Runtime *runtime); Snapshot *Snapshot_New(Runtime *runtime);
void Snapshot_Free(Snapshot *snapshot); void Snapshot_Free(Snapshot *snapshot);
+36
View File
@@ -0,0 +1,36 @@
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "path.h"
_Bool Path_IsAbsolute(const char *path)
{
return path[0] == '/';
}
const char *Path_MakeAbsolute(const char *path, char *buff, size_t buffsize)
{
assert(path != NULL);
if(Path_IsAbsolute(path))
return path; // It's already absolute.
if(getcwd(buff, buffsize) == NULL)
return NULL;
size_t written = strlen(buff);
assert(buff[written-1] != '/');
if(written+1 >= buffsize)
return NULL; // No space for the / following the cwd.
buff[written++] = '/';
size_t n = strlen(path);
if(written + n >= buffsize)
return NULL;
memcpy(buff + written, path, n);
buff[written + n] = '\0';
return buff;
}
+5
View File
@@ -0,0 +1,5 @@
#ifndef PATH_H
#define PATH_H
_Bool Path_IsAbsolute(const char *path);
const char *Path_MakeAbsolute(const char *path, char *buff, size_t buffsize);
#endif /* PATH_H */
+2 -27
View File
@@ -36,6 +36,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include "source.h" #include "source.h"
#include "path.h"
struct xSource { struct xSource {
char *name; char *name;
@@ -82,32 +83,6 @@ const char *Source_GetAbsolutePath(Source *src)
return NULL; return NULL;
} }
const char *makePathAbsolute(const char *path, char *buff, size_t buffsize)
{
if(path[0] == '/')
// It's already absolute.
return path;
if(getcwd(buff, buffsize) == NULL)
return NULL;
size_t written = strlen(buff);
assert(buff[written-1] != '/');
if(written+1 >= buffsize)
return NULL; // No space for the / following the cwd.
buff[written++] = '/';
size_t n = strlen(path);
if(written + n >= buffsize)
return NULL;
memcpy(buff + written, path, n);
buff[written + n] = '\0';
return buff;
}
Source *Source_FromFile(const char *file, Error *error) Source *Source_FromFile(const char *file, Error *error)
{ {
assert(file != NULL); assert(file != NULL);
@@ -118,7 +93,7 @@ Source *Source_FromFile(const char *file, Error *error)
} }
char maybe[1024]; char maybe[1024];
const char *abs_path = makePathAbsolute(file, maybe, sizeof(maybe)); const char *abs_path = Path_MakeAbsolute(file, maybe, sizeof(maybe));
if(abs_path == NULL) { if(abs_path == NULL) {
Error_Report(error, 0, "Internal buffer is too small"); Error_Report(error, 0, "Internal buffer is too small");
return NULL; return NULL;