aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cla_parse.c164
-rw-r--r--src/files.c34
-rw-r--r--src/globs.c55
-rw-r--r--src/help.c79
-rw-r--r--src/lua_globs.c106
-rw-r--r--src/lua_vars.c68
-rw-r--r--src/main.c11
-rw-r--r--src/pkg_create.c120
-rw-r--r--src/str.c776
9 files changed, 1168 insertions, 245 deletions
diff --git a/src/cla_parse.c b/src/cla_parse.c
deleted file mode 100644
index c2f31fd..0000000
--- a/src/cla_parse.c
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
-
- pkgit - package it!
-
- Copyright (C) 2026 dacctal
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
-
-*/
-
-#include <stdio.h>
-#include <string.h>
-
-#include "cla_parse.h"
-
-#include "globs.h"
-#include "str.h"
-
-//#include "declare.h"
-//#include "deps_resolve.h"
-//#include "help.h"
-//#include "name_from_url.h"
-//#include "pkg_build.h"
-//#include "pkg_create.h"
-//#include "pkg_search.h"
-//#include "pkg_install.h"
-//#include "pkg_list.h"
-//#include "pkg_remove.h"
-//#include "pkgit_globals.h"
-//#include "repo_add.h"
-//#include "update.h"
-
-#define COMMAND(large, small, code) \
- if (slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(large)) \
- || slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(small))) code
-
-#define NOT_ENOUGH_ARGS(arg, next) \
- printf("%.*s Not enough arguments! Try: `pkgit %s [%s]`\n", \
- PRINT_ERROR, arg, next)
-
-void cmd_add(char **argv, int i) {
- if (argv[i + 1]) {
- printf("add repo %s", argv[i + 1]);
- //repo_add(argv[i + 1], name_from_url(argv[i + 1]));
- } else {
-// NOT_ENOUGH_ARGS(argv[i], "url");
- }
-}
-
-void cmd_build(int argc, char **argv, int i, package pkg) {
- if (argv[i + 1]) {
- for (int j = i + 1; j < argc; j++) {
- if (argv[j][0] == '-') continue;
- printf("build pkg %s", argv[j]);
- //pkg = pkg_create(argv[j]);
- //pkg_build(pkg);
- }
- } else {
- printf("build pkg .");
- //pkg = pkg_create(".");
- //pkg_build(pkg);
- }
-}
-
-void cmd_install(int argc, char **argv, int i, package pkg) {
- if (argv[i + 1]) {
- for (int j = i + 1; j < argc; j++) {
- if (argv[j][0] == '-') continue;
- printf("install pkg %s", argv[j]);
- //pkg = pkg_create(argv[j]);
- //pkg_install(pkg);
- }
- } else {
- //NOT_ENOUGH_ARGS(argv[i], "url/pkg");
- }
-}
-
-void cmd_remove(int argc, char **argv, int i, package pkg) {
- if (argv[i + 1]) {
- for (int j = i + 1; j < argc; j++) {
- if (argv[j][0] == '-') continue;
- printf("remove pkg %s", argv[j]);
- //pkg = pkg_create(argv[j]);
- //pkg_remove(pkg);
- }
- } else {
- //NOT_ENOUGH_ARGS(argv[i], "url/pkg");
- }
-}
-
-void flags_mod(char **argv, int i) {
- for (size_t j = 1; j < strlen(argv[i]); j++) {
- switch (argv[i][j]) {
- case 'q': is_verbose = 0; break;
- case 'f': is_forced = 1; break;
- default: break;
- }
- }
-}
-
-void flags_cmd(int argc, char **argv, int i, package pkg) {
- for (size_t j = 1; j < strlen(argv[i]); j++) {
- switch (argv[i][j]) {
- case 'a': cmd_add(argv, i); break;
- case 'b': cmd_build(argc, argv, i, pkg); break;
- case 'c': /*deps_resolve();*/ printf("deps_resolve\n"); break;
- case 'd': /*declare();*/ printf("declare\n"); break;
- case 'i': cmd_install(argc, argv, i, pkg); break;
- case 'r': cmd_remove(argc, argv, i, pkg); break;
- case 'u': /*update();*/ printf("update\n"); break;
- case 'l': /*pkgs_list();*/ printf("list\n"); break;
- case 's': /*search(argv[i + 1]);*/ printf("search\n"); break;
- case 'v': printf("%s\n", VERSION); break;
- case 'h': /*help();*/ printf("help\n"); break;
- default: break;
- }
- }
-}
-
-void flags_parse(int argc, char **argv, package pkg) {
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] != '-') continue;
- if (argv[i][1] == '-') {
- COMMAND("--quiet", "-q", { is_verbose = 0; });
- COMMAND("--force", "-f", { is_forced = 1; });
- } else {
- flags_mod(argv, i);
- flags_cmd(argc, argv, i, pkg);
- }
- }
-}
-
-void cmds_parse(int argc, char **argv, package pkg) {
- for (int i = 1; i < argc; i++) {
- COMMAND("--add", "a", { cmd_add(argv, i); });
- COMMAND("--build", "b", { cmd_build(argc, argv, i, pkg); });
- COMMAND("--install", "i", { cmd_install(argc, argv, i, pkg); });
- COMMAND("--remove", "r", { cmd_remove(argc, argv, i, pkg); });
- COMMAND("--update", "u", { /*update();*/ });
- COMMAND("--declare", "d", { /*declare();*/ });
- COMMAND("--list", "l", { /*pkgs_list();*/ });
- COMMAND("--search", "s", { /*search(argv[i + 1]);*/ });
- COMMAND("--version", "v", { printf("%s\n", VERSION); });
- COMMAND("--help", "h", { /*help();*/ });
- COMMAND("--check", "c", { /*deps_resolve();*/ });
- }
-}
-
-void cla_parse(int argc, char **argv) {
- if (!argv[1]) { /*help();*/ return; }
- package pkg = {0};
- flags_parse(argc, argv, pkg);
- cmds_parse(argc, argv, pkg);
-}
diff --git a/src/files.c b/src/files.c
new file mode 100644
index 0000000..8903557
--- /dev/null
+++ b/src/files.c
@@ -0,0 +1,34 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include "files.h"
+
+bool file_exists(const char *path) {
+ struct stat buffer;
+ return (stat(path, &buffer) == 0);
+}
+
+bool is_directory(const char *path) {
+ struct stat statbuf;
+ if (stat(path, &statbuf) != 0) {
+ return false;
+ }
+ return S_ISDIR(statbuf.st_mode);
+}
diff --git a/src/globs.c b/src/globs.c
index bc4315e..61d3ea6 100644
--- a/src/globs.c
+++ b/src/globs.c
@@ -18,23 +18,65 @@
*/
+#include <stdlib.h>
#include <stdio.h>
#include "globs.h"
+
+#include "files.h"
+#include "lua_vars.h"
#include "str.h"
int is_verbose = 1;
int is_forced = 0;
-char root_config_file[20] = "/etc/pkgit/init.lua";
-char root_config_dir[15] = "/etc/pkgit";
+
+int is_root_config = 0;
+str home_config_dir;
+str home_config_file;
+str home_repos_file;
+str root_config_dir;
+str root_config_file;
+str root_repos_file;
+str config_dir;
+str config_file;
+str repos_file;
+
+str prefix_dir;
+
+str bin;
+str lib;
+str include;
+str src;
void init_vars(void) {
-// char *home = getenv("HOME");
-// if (home) snprintf(home_dir, MAX_PATH_LEN, "%s", home);
-// else snprintf(home_dir, MAX_PATH_LEN, "/root");
+ home_config_dir = str_format("%s/.config/pkgit", getenv("HOME"));
+ home_config_file = str_format("%s/init.lua", home_config_dir.data);
+ home_repos_file = str_format("%s/repos.lua", home_config_dir.data);
+ root_config_dir = str_from_cstr("/etc/pkgit");
+ root_config_file = str_format("%s/init.lua", root_config_dir.data);
+ root_repos_file = str_format("%s/repos.lua", root_config_dir.data);
+ str_copy_into(&config_dir, &home_config_dir);
+ if (file_exists(root_config_file.data)) str_copy_into(&config_dir, &root_config_dir);
+ config_file = str_format("%s/init.lua", config_dir.data);
+ repos_file = str_format("%s/repos.lua", config_dir.data);
+ init_install_directories();
+}
+
+void free_vars(void) {
+ str_free(&home_config_dir);
+ str_free(&home_config_file);
+ str_free(&home_repos_file);
+ str_free(&root_config_dir);
+ str_free(&root_config_file);
+ str_free(&root_repos_file);
+ str_free(&config_dir);
+ str_free(&config_file);
+ str_free(&repos_file);
+}
+
+// old init_vars()
//
// is_root_config = file_exists(root_config);
-//
// if (is_root_config) snprintf(config_dir, MAX_PATH_LEN, "/etc/pkgit");
// else snprintf(config_dir, MAX_PATH_LEN, "%s/.config/pkgit", home_dir);
//
@@ -53,4 +95,3 @@ void init_vars(void) {
// install_directories[2] = strdup(get_install_dir("lib"));
// install_directories[3] = strdup(get_install_dir("inc"));
// install_directories[4] = strdup(get_install_dir("src"));
-}
diff --git a/src/help.c b/src/help.c
new file mode 100644
index 0000000..9bd05a2
--- /dev/null
+++ b/src/help.c
@@ -0,0 +1,79 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can REDistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <stdio.h>
+
+#include "help.h"
+
+#include "globs.h"
+
+void help(void) {
+ typedef struct {
+ const char *short_flag;
+ const char *long_flag;
+ const char *arg;
+ const char *desc;
+ } flag_t;
+
+ static const flag_t cmd_flags[] = {
+ {"-a", "--add", "[url]", "add a repo"},
+ {"-b", "--build", "[path]", "build a package"},
+ {"-d", "--declare", "", "install all packages"},
+ {"-s", "--search", "[pkgs]", "find a package from your repos"},
+ {"-i", "--install", "[pkgs, urls]", "install a package/repo"},
+ {"-r", "--remove", "[pkgs]", "remove an installed package"},
+ {"-l", "--list", "", "list all installed packages"},
+ {"-u", "--update", "", "update all installed packages"},
+ {"-h", "--help", "", "display this help message"},
+ {"-v", "--version", "", "display version number"},
+ {"-c", "--check", "", "run package checks"},
+ };
+
+ static const flag_t mod_flags[] = {
+ {"-q", "--quiet", "", "add a repo"},
+ {"-f", "--force", "", "build a package"},
+ };
+
+ if (is_verbose) {
+ printf(BOLD_MAGENTA " , \n");
+ printf(BOLD_MAGENTA " / \\ \n");
+ printf(BOLD_MAGENTA "_.--' '--._ \n");
+ printf(BOLD_MAGENTA "`'--, ,--'` " COLOR_RESET "pkgit " ITALIC GRAY "- package it!"COLOR_RESET"\n");
+ printf(BOLD_YELLOW " _- " BOLD_MAGENTA "\\ /" BOLD_YELLOW " -_ " MAGENTA VERSION COLOR_RESET "\n");
+ printf(BOLD_YELLOW "'-_ " BOLD_MAGENTA "'" BOLD_YELLOW " _-' \n");
+ printf(BOLD_YELLOW " `'-.-'` " COLOR_RESET "\n");
+ } else {
+ printf("pkgit %s%s- package it!%s\n", ITALIC, GRAY, COLOR_RESET);
+ printf("%s%s%s\n", MAGENTA, VERSION, COLOR_RESET);
+ }
+ printf("\n");
+
+ printf(RED "subcommand flags" COLOR_RESET ":\n");
+ for (size_t i = 0; i < sizeof(cmd_flags)/sizeof(cmd_flags[0]); i++) {
+ printf(COLOR_RESET "\t" GREEN "%-2s" COLOR_RESET ", " YELLOW "%-12s" BLUE "%-16s" GRAY "# %s\n" COLOR_RESET,
+ cmd_flags[i].short_flag, cmd_flags[i].long_flag, cmd_flags[i].arg, cmd_flags[i].desc);
+ }
+
+ printf("\n" RED "modifier flags" COLOR_RESET ":\n");
+ for (size_t i = 0; i < sizeof(mod_flags)/sizeof(mod_flags[0]); i++) {
+ printf(COLOR_RESET "\t" GREEN "%-2s" COLOR_RESET ", " YELLOW "%-12s" BLUE "%-16s" GRAY "# %s\n" COLOR_RESET,
+ mod_flags[i].short_flag, mod_flags[i].long_flag, mod_flags[i].arg, mod_flags[i].desc);
+ }
+}
diff --git a/src/lua_globs.c b/src/lua_globs.c
new file mode 100644
index 0000000..f030acb
--- /dev/null
+++ b/src/lua_globs.c
@@ -0,0 +1,106 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <stdio.h>
+
+#include "lua_globs.h"
+
+#include "globs.h"
+#include "files.h"
+
+lua_State *L = NULL;
+lua_State *B = NULL;
+bool config_loaded = false;
+bool bldit_loaded = false;
+
+void push_lua_path(lua_State *L, const char *new_path) {
+ lua_getglobal(L, "package");
+ lua_getfield(L, -1, "path");
+ const char *current_path = lua_tostring(L, -1);
+ if (!current_path) current_path = "";
+ lua_pop(L, 1);
+ lua_pushfstring(L, "%s;%s", current_path, new_path);
+ lua_setfield(L, -2, "path");
+ lua_pop(L, 1);
+}
+
+void init_lua_state(void) {
+ if (L != NULL) return;
+ L = luaL_newstate();
+ luaL_openlibs(L);
+ char lua_path[MAX_PATH_LEN + 20];
+ snprintf(lua_path, sizeof(lua_path), "%s/?.lua", config_dir.data);
+ push_lua_path(L, lua_path);
+ if (luaL_loadfile(L, config_file.data) || lua_pcall(L, 0, 0, 0)) {
+ printf(
+ "%s cannot run configuration script: %s\n",
+ PRINT_ERROR, lua_tostring(L, -1)
+ );
+ printf(
+ "%s to generate a configuration file, head into the",
+ PRINT_PKGIT
+ );
+ printf(
+ " root directory of the pkgit source and run `make defconfig`\n"
+ );
+ exit(EXIT_FAILURE);
+ }
+ if (file_exists(repos_file.data)) {
+ if (luaL_loadfile(L, repos_file.data) || lua_pcall(L, 0, 0, 0)) {
+ printf(
+ "%s cannot load repository file: %s\n",
+ PRINT_ERROR, lua_tostring(L, -1)
+ );
+ lua_pop(L, 1);
+ }
+ }
+ config_loaded = true;
+}
+
+void init_bldit(void) {
+ if (B != NULL) return;
+ B = luaL_newstate();
+ luaL_openlibs(B);
+ if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) {
+ if (is_verbose) printf(
+ "%s cannot run bldit script: %s\n",
+ PRINT_WARNING, lua_tostring(B, -1)
+ );
+ return;
+ }
+ bldit_loaded = true;
+}
+
+void free_lua_state(void) {
+ if (L != NULL) {
+ lua_close(L);
+ L = NULL;
+ }
+ config_loaded = false;
+}
+
+lua_State *get_lua_state(void) { return L; }
+
+void lua_isnt_type(char* variable, char* type) {
+ if (is_verbose) printf(
+ "%s init.lua: '%s' is not a %s.\n",
+ PRINT_ERROR, variable, type
+ );
+}
diff --git a/src/lua_vars.c b/src/lua_vars.c
new file mode 100644
index 0000000..3519d7a
--- /dev/null
+++ b/src/lua_vars.c
@@ -0,0 +1,68 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include "lua_vars.h"
+
+#include "lua_globs.h"
+#include "globs.h"
+
+void init_install_directories(void) {
+ init_lua_state();
+ if (!lua_istable(L, -1)) {
+ lua_getglobal(L, "install_directories");
+ }
+ if (!lua_istable(L, -1)) {
+ lua_isnt_type("install_directories", "table");
+ return;
+ }
+ //lua_pop(L, 1);
+
+ lua_getfield(L, -1, "bin");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.bin", "string");
+ } else {
+ str_copy_cstr_into(&bin, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "lib");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.lib", "string");
+ } else {
+ str_copy_cstr_into(&lib, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "include");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.include", "string");
+ } else {
+ str_copy_cstr_into(&include, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "src");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.src", "string");
+ } else {
+ str_copy_cstr_into(&src, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+}
diff --git a/src/main.c b/src/main.c
index c531487..893e944 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,12 +18,17 @@
*/
-#include <stdio.h>
-
-#include "cla_parse.h"
+//#include "cla_parse.h"
+#include "pkg_create.h"
#include "str.h"
#include "globs.h"
int main(int argc, char** argv) {
+ (void)argc;
+ init_vars();
+ str_slc arg = str_slc_from_cstr(argv[1]);
+ pkg_create(&arg);
+ //cla_parse(argc, argv);
+ free_vars();
return 0;
}
diff --git a/src/pkg_create.c b/src/pkg_create.c
new file mode 100644
index 0000000..79b4a78
--- /dev/null
+++ b/src/pkg_create.c
@@ -0,0 +1,120 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <unistd.h>
+#include <string.h>
+
+#include "pkg_create.h"
+
+#include "files.h"
+#include "globs.h"
+#include "str.h"
+
+static str get_destdir(str_slc cwd, str_slc arg) {
+ str destdir;
+ if (str_slc_first(arg) == '.') {
+ str_format_into(
+ &destdir, "%s/%s",
+ src, str_slc_from_after_delim(cwd, '/')
+ );
+ } else {
+ str_format_into(
+ &destdir, "%s/%s",
+ src, arg
+ );
+ }
+ return destdir;
+}
+
+static str get_pkgsrc(package pkg) {
+ str src_dir;
+ if (pkg.is_local == 1) {
+ str_format_into(
+ &src_dir, "%s/%s",
+ src, pkg.name
+ );
+ } else {
+ str_format_into(
+ &src_dir, "%s/%s/%s",
+ src, pkg.name, pkg.version
+ );
+ }
+ return src_dir;
+}
+
+package pkg_create(str_slc *arg) {
+ package pkg;
+ pkg.version = (str) {
+ .data = "HEAD",
+ .len = 4,
+ .cap = 4,
+ };
+ pkg.is_local = false;
+ char cwd[MAX_PATH_LEN];
+ getcwd(cwd, MAX_PATH_LEN);
+ str_slc cwd_slc = mstrslc(cwd);
+ str cwd_str = mstr(cwd);
+ str_slc new_arg;
+ new_arg.data = arg->data;
+ new_arg.len = arg->len;
+ if (!new_arg.data) exit(EXIT_FAILURE);
+ str dest_dir = get_destdir(cwd_slc, new_arg);
+ bool is_installed_locally = is_directory(dest_dir.data);
+
+ str new_arg_str = str_from_str_slc(new_arg);
+ pkg.version = str_from_after_delim(&new_arg_str, '@');
+ str_println(&pkg.version);
+ pkg.target = str_from_after_delim(&new_arg_str, ',');
+ str_println(&pkg.target);
+
+ bool is_in_repos = false;
+ //for (size_t i = 0; i < cached_repos_count; i++) {
+ // if (strcmp(new_arg, cached_repos[i].source_key) == 0) {
+ // is_in_repos = true;
+ // break;
+ // }
+ //}
+
+ if (strncmp(new_arg.data, "http", 4) == 0 || strncmp(new_arg.data, "ssh", 3) == 0) {
+ pkg.url = new_arg_str;
+ pkg.name = str_from_after_delim(&new_arg_str, '/');
+ } else if (strcmp(new_arg.data, ".") == 0) {
+ pkg.url = str_from_cstr("");
+ pkg.name = str_from_after_delim(&cwd_str, '/');
+ pkg.is_local = true;
+ } else if (is_in_repos) {
+ //for (size_t i = 0; i < cached_repos_count; i++) {
+ // if (strcmp(new_arg, cached_repos[i].source_key) == 0) {
+ // pkg.url = strdup(cached_repos[i].source_value);
+ // break;
+ // }
+ //}
+ pkg.name = new_arg_str;
+ } else if (is_installed_locally) {
+ pkg.url = str_from_cstr("");
+ pkg.name = str_from_after_delim(&dest_dir, '/');
+ pkg.is_local = true;
+ } else {
+ printf("%s '%.*s' is not a valid package\n", PRINT_ERROR, str_fmt(&new_arg));
+ exit(EXIT_FAILURE);
+ }
+ pkg.src = get_pkgsrc(pkg);
+ return pkg;
+}
diff --git a/src/str.c b/src/str.c
index 7bb424e..9e67b4a 100644
--- a/src/str.c
+++ b/src/str.c
@@ -1,109 +1,743 @@
/*
- pkgit - package it!
+pkgit - package it!
- Copyright (C) 2026 dacctal
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+Copyright (C) 2026 dacctal
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
+You should have received a copy of the GNU General Public License
+along with this program.If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "globs.h"
#include "str.h"
-void slc_print(str_slc slc) {
- for (size_t i = 0; i < slc.len; i++) {
- putchar(slc.data[i]);
+// NOTE: only use this assertion in functions where you don't depend
+// on any other str_ ones, to prevent checking twice
+#ifdef PKGIT_DEBUG
+#define assert_str_is_valid(str) \
+ do { \
+ if (!str_is_valid(str)) \
+ panic("Cannot operate on str `%s`, which is invalid!", #str); \
+ } while (0)
+#else
+#define assert_str_is_valid(str)
+#endif
+
+// XXX: INTERNAL USE ONLY
+#define _strslc(str) \
+ (str_slc) { str->data, str->len }
+#define _cstrslc(str) \
+ (str_slc) { str, strlen(str) }
+// XXX: since msvc/tcc and I presume cproc aren't smart enough to inline calls
+// where a str calls a str_slc function and vice versa, we optimize things
+// ourselves+reduce boilerplate
+#define _STRINGS_EQUAL
+
+str str_new(void) { return str_new_with_capacity(16); }
+
+str str_new_with_capacity(size_t cap) {
+ str res = {0};
+ res.cap = cap;
+ res.data = calloc(res.cap, 1);
+ check_alloc(res.data);
+ return res;
+}
+
+str str_from_cstr(const char *s) { return str_from_str_slc(_cstrslc(s)); }
+
+str mstr(const char *s) { return str_from_str_slc(_cstrslc(s)); }
+
+str str_from_str_slc(const str_slc s) {
+ str res = str_new_with_capacity(s.len + 1);
+ str_copy_str_slc_into(&res, s);
+ return res;
+}
+
+str str_format(const char *format, ...) {
+ va_list args, args_copy;
+ va_start(args, format);
+ va_copy(args_copy, args);
+
+ size_t len = vsnprintf(NULL, 0, format, args_copy);
+ str res = str_new_with_capacity(len + 1);
+ res.len = len;
+ vsnprintf(res.data, res.cap, format, args);
+
+ va_end(args);
+ return res;
+}
+
+void str_format_into(str *s, const char *format, ...) {
+ va_list args, args_copy;
+ va_start(args, format);
+ va_copy(args_copy, args);
+
+ size_t len = vsnprintf(NULL, 0, format, args_copy);
+
+ if (!str_is_valid(s)) {
+ *s = str_new_with_capacity(len + 1);
+ } else {
+ if (s->cap < len + 1)
+ str_reserve_exact(s, len + 1);
+ str_clear(s);
}
+ s->len = len;
+ vsnprintf(s->data, s->cap, format, args);
+
+ va_end(args);
}
-str_slc slc_from_cstr(char* cstr) {
- return (str_slc) {
- .data = cstr,
- .len = strlen(cstr),
- };
+void str_reserve(str *s, size_t new_cap) {
+ str_reserve_exact(s, new_cap + (new_cap >> 2));
+}
+
+void str_reserve_exact(str *s, size_t new_cap) {
+ assert_str_is_valid(s);
+ if (s->cap == new_cap)
+ return;
+ s->data = realloc(s->data, new_cap);
+ s->cap = new_cap;
+ check_alloc(s->data);
+}
+
+void str_clear(str *s) {
+ assert_str_is_valid(s);
+ memset(s->data, 0, s->cap);
+ s->len = 0;
+}
+
+void str_free(str *s) {
+ // let multiple frees be no-ops
+ if (!s->data)
+ return;
+ free(s->data);
+ s->data = NULL;
+}
+
+void str_copy_into(str *dest, const str *src) {
+ str_ncopy_cstr_into(dest, src->data, src->len);
}
-int slc_eq(str_slc a, str_slc b) {
- if (a.len != b.len) return 0;
- for (size_t i = 0; i < a.len; i++) {
- if (a.data[i] != b.data[i]) return 0;
+void str_copy_cstr_into(str *dest, const char *src) {
+ str_ncopy_cstr_into(dest, src, strlen(src));
+}
+
+void str_copy_str_slc_into(str *dest, const str_slc src) {
+ str_ncopy_cstr_into(dest, src.data, src.len);
+}
+
+void str_ncopy_into(str *dest, const str *src, size_t count) {
+ str_ncopy_cstr_into(dest, src->data, count);
+}
+
+void str_ncopy_cstr_into(str *dest, const char *src, size_t count) {
+ assert_str_is_valid(dest);
+ assert(src);
+ dest->len = count;
+ if (dest->cap < count + 1)
+ str_reserve_exact(dest, count + 1);
+ memcpy(dest->data, src, count);
+ dest->data[count] = 0;
+}
+
+void str_ncopy_str_slc_into(str *dest, const str_slc src, size_t count) {
+ str_ncopy_cstr_into(dest, src.data, count);
+}
+
+str str_dupe(const str *src) {
+ assert_str_is_valid(src);
+ str res = str_new_with_capacity(src->cap);
+ str_copy_into(&res, src);
+ return res;
+}
+
+void str_append(str *src, const str *new) {
+ str_append_str_slc(src, _strslc(new));
+}
+
+void str_append_char(str *src, char new) {
+ assert_str_is_valid(src);
+ // new char + nullterm
+ if (src->len + 2 < src->cap) {
+ // this will reserve 1.5*src->cap
+ str_reserve(src, src->cap);
}
- return 1;
+ src->data[src->len++] = new;
+ src->data[src->len] = 0;
+}
+
+void str_append_cstr(str *src, const char *new) {
+ assert(new);
+ str_append_str_slc(src, _cstrslc(new));
}
-int slc_starts_with(str_slc slc, str_slc prefix) {
- if (prefix.len > slc.len) return 0;
- for (size_t i = 0; i < prefix.len; i++) {
- if (slc.data[i] != prefix.data[i]) return 0;
+void str_append_str_slc(str *src, const str_slc new) {
+ assert_str_is_valid(src);
+ assert_str_is_valid(&new);
+ size_t len_after_append = src->len + new.len;
+ if (len_after_append + 1 > src->cap) {
+ src->cap = len_after_append + 1;
+ str_reserve(src, src->cap);
}
- return 1;
+ memcpy(src->data + src->len, new.data, new.len + 1);
+ src->len = len_after_append;
+ src->data[len_after_append] = 0;
}
-str_slc slc_take(str_slc slc, size_t n) {
- if (n > slc.len) n = slc.len;
- return (str_slc) {
- .data = slc.data,
- .len = n
- };
+char str_pop_last(str *src) {
+ char c = str_last(src);
+ src->len--;
+ return c;
+}
+
+str str_concat(const str *lhs, const str *rhs) {
+ str res = str_dupe(lhs);
+ str_append(&res, rhs);
+ return res;
+}
+
+str str_concat_cstr(const str *lhs, const char *rhs) {
+ str res = str_dupe(lhs);
+ str_append_cstr(&res, rhs);
+ return res;
+}
+
+str str_concat_str_slc(const str *lhs, const str_slc rhs) {
+ str res = str_dupe(lhs);
+ str_append_str_slc(&res, rhs);
+ return res;
+}
+
+str str_trim_left(const str *s) {
+ str res = str_dupe(s);
+ str_trim_left_in_place(&res);
+ return res;
+}
+
+str str_trim_right(const str *s) {
+ str res = str_dupe(s);
+ str_trim_right_in_place(&res);
+ return res;
+}
+
+str str_trim(const str *s) {
+ str res = str_dupe(s);
+ str_trim_in_place(&res);
+ return res;
+}
+
+str str_to_upper(const str *s) {
+ str res = str_dupe(s);
+ str_to_upper_in_place(&res);
+ return res;
+}
+
+str str_to_lower(const str *s) {
+ str res = str_dupe(s);
+ str_to_lower_in_place(&res);
+ return res;
+}
+
+void str_trim_left_in_place(str *s) {
+ assert_str_is_valid(s);
+ size_t i;
+ for (i = 0; i < s->len && isspace(s->data[i]); i++)
+ continue;
+ s->len -= i;
+ memmove(s->data, s->data + i, s->len);
+ s->data[s->len] = 0;
+}
+
+void str_trim_right_in_place(str *s) {
+ assert_str_is_valid(s);
+ if (s->len == 0)
+ return;
+ size_t end;
+ for (end = s->len - 1; isspace(s->data[end]); end--)
+ continue;
+ end++;
+ s->len = end;
+ s->data[s->len] = 0;
+}
+
+void str_trim_in_place(str *s) {
+ assert_str_is_valid(s);
+ if (s->len == 0)
+ return;
+ size_t begin, end;
+ for (begin = 0; begin < s->len && isspace(s->data[begin]); begin++)
+ continue;
+ for (end = s->len - 1; isspace(s->data[end]); end--)
+ continue;
+ end++;
+ s->len = end - begin;
+ memmove(s->data, s->data + begin, s->len);
+ s->data[s->len] = 0;
+}
+
+void str_to_upper_in_place(str *s) {
+ assert_str_is_valid(s);
+ for (size_t i = 0; i < s->len; i++)
+ s->data[i] = toupper(s->data[i]);
+}
+
+void str_to_lower_in_place(str *s) {
+ assert_str_is_valid(s);
+ for (size_t i = 0; i < s->len; i++)
+ s->data[i] = tolower(s->data[i]);
+}
+
+bool str_equal(const str *lhs, const str *rhs) {
+ return str_slc_equal(_strslc(lhs), _strslc(rhs));
+}
+
+bool str_equal_cstr(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal(_strslc(lhs), _cstrslc(rhs));
+}
+
+bool str_equal_str_slc(const str *lhs, const str_slc rhs) {
+ return str_slc_equal(_strslc(lhs), rhs);
+}
+
+int str_compare(const str *lhs, const str *rhs) {
+ return str_slc_compare(_strslc(lhs), _strslc(rhs));
+}
+
+int str_compare_cstr(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_compare(_strslc(lhs), _cstrslc(rhs));
+}
+
+int str_compare_str_slc(const str *lhs, const str_slc rhs) {
+ return str_slc_compare(_strslc(lhs), rhs);
+}
+
+bool str_equal_case_insensitive(const str *lhs, const str *rhs) {
+ return str_slc_equal_case_insensitive(_strslc(lhs), _strslc(rhs));
+}
+
+bool str_equal_cstr_case_insensitive(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal_case_insensitive(_strslc(lhs), _cstrslc(rhs));
+}
+
+bool str_equal_str_slc_case_insensitive(const str *lhs, const str_slc rhs) {
+ return str_slc_equal_case_insensitive(_strslc(lhs), rhs);
+}
+
+int str_compare_case_insensitive(const str *lhs, const str *rhs) {
+ return str_slc_compare_case_insensitive(_strslc(lhs), _strslc(rhs));
+}
+
+int str_compare_cstr_case_insensitive(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_compare_case_insensitive(_strslc(lhs), _cstrslc(rhs));
+}
+
+int str_compare_str_slc_case_insensitive(const str *lhs, const str_slc rhs) {
+ return str_slc_compare_case_insensitive(_strslc(lhs), rhs);
+}
+
+str str_slice_to_str(const str *src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(_strslc(src), begin, end);
+ return str_from_str_slc(slc);
+}
+
+str str_slice_from_cstr(const char *src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(_cstrslc(src), begin, end);
+ return str_from_str_slc(slc);
+}
+
+str_slc str_slice(const str *src, ptrdiff_t begin, ptrdiff_t end) {
+ return str_slc_slice(_strslc(src), begin, end);
+}
+
+str str_slice_from_str_slc(const str_slc src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(src, begin, end);
+ return str_from_str_slc(slc);
}
-str_slc slc_drop(str_slc slc, size_t n) {
- if (n > slc.len) n = slc.len;
- return (str_slc) {
- .data = slc.data + n,
- .len = slc.len - n
+size_t str_to_double_pro(const str *src, double *res) {
+ return str_slc_to_double_pro(_strslc(src), res);
+}
+
+size_t str_to_int64_pro(const str *src, int64_t *res, int base) {
+ return str_slc_to_int64_pro(_strslc(src), res, base);
+}
+
+bool str_to_double(const str *src, double *res) {
+ return str_to_double_pro(src, res) == src->len;
+}
+
+bool str_to_int64(const str *src, int64_t *res) {
+ return str_to_int64_pro(src, res, 10) == src->len;
+}
+
+char str_at(const str *s, size_t i) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (i >= s->len)
+ panic("Out of bounds string access at index %zu (length: %zu)", i,
+ s->len);
+#endif
+ return s->data[i];
+}
+
+char str_first(const str *s) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (!s->len)
+ panic("Attempted to get the first character of an empty string");
+#endif
+ return *s->data;
+}
+
+char str_last(const str *s) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (!s->len)
+ panic("Attempted to get the last character of an empty string");
+#endif
+ return s->data[s->len - 1];
+}
+
+size_t str_find_char(const str *s, char c) {
+ return str_slc_find_char(_strslc(s), c);
+}
+
+size_t str_find_char_right(const str *s, char c) {
+ return str_slc_find_char_right(_strslc(s), c);
+}
+
+void str_print(const str *s) { printf("%.*s", str_fmt(s)); }
+
+void str_println(const str *s) { printf("%.*s\n", str_fmt(s)); }
+
+void str_fprint(const str *s, FILE *f) { fprintf(f, "%.*s", str_fmt(s)); }
+
+void str_fprintln(const str *s, FILE *f) { fprintf(f, "%.*s\n", str_fmt(s)); }
+
+str str_read_line_from_file(FILE *f) {
+ size_t len = 0;
+ char c;
+ for (len = 0; (c = fgetc(f)) != EOF && c != '\n'; len++)
+ continue;
+ fseek(f, -len - 1, SEEK_SET);
+ str res = str_new_with_capacity(len + 1);
+ size_t bytes_read = fread(res.data, 1, len, f);
+ assert(bytes_read == len);
+ res.len = len;
+ return res;
+}
+
+str str_read_chars_from_file(FILE *f, size_t count) {
+ str res = str_new_with_capacity(count + 1);
+ size_t bytes_read = fread(res.data, 1, count, f);
+ assert(bytes_read == count);
+ res.len = count;
+ return res;
+}
+
+str str_read_entire_file(FILE *f) {
+ fseek(f, 0, SEEK_END);
+ size_t len = ftell(f);
+ rewind(f);
+ str res = str_new_with_capacity(len + 1);
+ size_t bytes_read = fread(res.data, 1, len, f);
+ assert(bytes_read == len);
+ res.len = len;
+ return res;
+}
+
+bool str_write_to_file(const str *s, FILE *f) {
+ return str_slc_write_to_file(_strslc(s), f);
+}
+
+// === str_slc implementation ===
+
+str_slc str_slc_from_str(const str *s) { return _strslc(s); }
+
+str_slc str_slc_from_cstr(const char *s) { return _cstrslc(s); }
+
+str_slc mstrslc(const char *s) { return (str_slc){s, strlen(s)}; }
+
+str str_slc_concat(const str_slc lhs, const str_slc rhs) {
+ str res = str_new();
+ str_append_str_slc(&res, lhs);
+ str_append_str_slc(&res, rhs);
+ return res;
+}
+
+str str_slc_concat_cstr(const str_slc lhs, const char *rhs) {
+ str res = str_new();
+ str_append_str_slc(&res, lhs);
+ str_append_cstr(&res, rhs);
+ return res;
+}
+
+bool str_slc_equal(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ return (lhs.len == rhs.len) && memcmp(lhs.data, rhs.data, lhs.len) == 0;
+}
+
+bool str_slc_equal_cstr(const str_slc lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal(lhs, _cstrslc(rhs));
+}
+
+bool str_slc_equal_case_insensitive(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len != rhs.len)
+ return false;
+ for (size_t i = 0; i < lhs.len; i++)
+ if (tolower(lhs.data[i]) != tolower(rhs.data[i]))
+ return false;
+ return true;
+}
+
+bool str_slc_equal_cstr_case_insensitive(const str_slc lhs, const char *rhs) {
+ return str_slc_equal_case_insensitive(lhs, _cstrslc(rhs));
+}
+
+int str_slc_compare(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len < rhs.len)
+ return -1;
+ else if (lhs.len > rhs.len)
+ return 1;
+ for (size_t i = 0; i < lhs.len; i++)
+ if (lhs.data[i] != rhs.data[i])
+ return (uint8_t)lhs.data[i] - (uint8_t)rhs.data[i];
+ return 0;
+}
+
+int str_slc_compare_cstr(const str_slc lhs, const char *rhs) {
+ return str_slc_compare(lhs, _cstrslc(rhs));
+}
+
+int str_slc_compare_case_insensitive(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len < rhs.len)
+ return -1;
+ else if (lhs.len > rhs.len)
+ return 1;
+ char cl, cr;
+ for (size_t i = 0; i < lhs.len; i++)
+ if ((cl = tolower(lhs.data[i])) != (cr = tolower(rhs.data[i])))
+ return (uint8_t)cl - (uint8_t)cr;
+ return 0;
+}
+
+int str_slc_compare_cstr_case_insensitive(const str_slc lhs, const char *rhs) {
+ return str_slc_compare_case_insensitive(lhs, _cstrslc(rhs));
+}
+
+str_slc str_slc_slice(const str_slc src, ptrdiff_t begin, ptrdiff_t end) {
+ assert_str_is_valid(&src);
+ if (begin > end)
+ return (str_slc){src.data, 0};
+ if (end < 0)
+ end = 0;
+ else if (end > (ptrdiff_t)src.len)
+ end = src.len;
+ if (begin < 0)
+ begin = 0;
+ else if (begin > (ptrdiff_t)src.len)
+ begin = src.len;
+ str_slc res = {
+ .data = src.data + begin,
+ .len = end - begin,
};
+ return res;
}
-str_slc slc_trim(str_slc slc) {
- while (slc.len > 0 && isspace((unsigned char)slc.data[0])) {
- slc = slc_drop(slc, 1);
- }
- while (slc.len > 0 && isspace((unsigned char)slc.data[slc.len - 1])) {
- slc = slc_take(slc, slc.len - 1);
- }
- return slc;
+// Equivalent to str_slice_from_str_slc
+str str_slc_slice_to_str(const str_slc s, ptrdiff_t begin, ptrdiff_t end) {
+ return str_slice_from_str_slc(s, begin, end);
}
-str_slc slc_split(str_slc *slc, char delimiter) {
- size_t i = 0;
- while(i < slc->len && slc->data[i] != delimiter) {
- i++;
- }
+str str_slc_to_lower(const str_slc s) {
+ str res = str_from_str_slc(s);
+ str_to_lower_in_place(&res);
+ return res;
+}
- str_slc result = slc_take(*slc, i);
- if (i < slc->len) {
- *slc = slc_drop(*slc, i + i);
- } else {
- *slc = slc_drop(*slc, i);
- }
- return result;
+str str_slc_to_upper(const str_slc s) {
+ str res = str_from_str_slc(s);
+ str_to_upper_in_place(&res);
+ return res;
}
-void slc_cat(str_slc first, str_slc second, str_slc *new_slc) {
- if (new_slc->len < first.len + second.len) {
- printf("fatal: slc_cat length of new slice is too small\n");
- exit(EXIT_FAILURE);
- }
- for (size_t i = 0; i < first.len; i++) {
- new_slc->data[i] = first.data[i];
- }
- for (size_t i = first.len; i < new_slc->len; i++) {
- new_slc->data[i] = second.data[i-first.len];
+str_slc str_slc_trim_left(const str_slc s) {
+ assert_str_is_valid(&s);
+ size_t i;
+ for (i = 0; i < s.len && isspace(s.data[i]); i++)
+ continue;
+ return (str_slc){s.data + i, s.len - i};
+}
+
+str_slc str_slc_trim_right(const str_slc s) {
+ if (s.len == 0)
+ return s;
+ size_t end;
+ for (end = s.len - 1; isspace(s.data[end]); end--)
+ continue;
+ end++;
+ return (str_slc){s.data, end};
+}
+
+str_slc str_slc_trim(const str_slc s) {
+ if (s.len == 0)
+ return s;
+ size_t begin, end;
+ for (begin = 0; begin < s.len && isspace(s.data[begin]); begin++)
+ continue;
+ for (end = s.len - 1; isspace(s.data[end]); end--)
+ continue;
+ end++;
+ return (str_slc){s.data + begin, end - begin};
+}
+
+size_t str_slc_to_double_pro(const str_slc src, double *res) {
+ assert_str_is_valid(&src);
+ errno = 0;
+ char *endptr = NULL;
+ const char *nptr = src.data;
+ double num = strtod(nptr, &endptr);
+ size_t diff = endptr - nptr;
+ *res = num;
+ return diff;
+}
+
+size_t str_slc_to_int64_pro(const str_slc src, int64_t *res, int base) {
+ assert_str_is_valid(&src);
+ errno = 0;
+ char *endptr = NULL;
+ const char *nptr = src.data;
+ int64_t num = strtoll(nptr, &endptr, base);
+ size_t diff = endptr - nptr;
+ *res = num;
+ return diff;
+}
+
+bool str_slc_to_double(const str_slc src, double *res) {
+ return str_slc_to_double_pro(src, res) == src.len;
+}
+
+bool str_slc_to_int64(const str_slc src, int64_t *res) {
+ return str_slc_to_int64_pro(src, res, 10) == src.len;
+}
+
+bool str_slc_write_to_file(const str_slc s, FILE *f) {
+ return s.len == fwrite(s.data, 1, s.len, f);
+}
+
+char str_slc_first(const str_slc s) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (!s.len)
+ panic("Attempted to get the first character of an empty string");
+#endif
+ return *s.data;
+}
+
+char str_slc_last(const str_slc s) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (!s.len)
+ panic("Attempted to get the last character of an empty string");
+#endif
+ return s.data[s.len - 1];
+}
+
+char str_slc_at(const str_slc s, size_t i) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (i >= s.len)
+ panic("Out of bounds string access at index %zu (length: %zu)", i,
+ s.len);
+#endif
+ return s.data[i];
+}
+
+void str_slc_print(const str_slc s) { printf("%.*s", str_fmt(&s)); }
+
+void str_slc_println(const str_slc s) { printf("%.*s\n", str_fmt(&s)); }
+
+void str_slc_fprint(const str_slc s, FILE *f) {
+ fprintf(f, "%.*s", str_fmt(&s));
+}
+
+void str_slc_fprintln(const str_slc s, FILE *f) {
+ fprintf(f, "%.*s\n", str_fmt(&s));
+}
+
+ptrdiff_t str_slc_find_char(const str_slc s, char c) {
+ size_t i;
+ for (i = 0; i < s.len; i++)
+ if (s.data[i] == c)
+ break;
+ return (i == s.len) ? 0 : i;
+}
+
+ptrdiff_t str_slc_find_char_right(const str_slc s, char c) {
+ if (s.len == 0)
+ return 0;
+ for (ptrdiff_t i = s.len - 1; i >= 0; i--) {
+ if (s.data[i] == c)
+ return (size_t)i;
}
+ return s.len;
+}
+
+str str_from_after_delim(str *arg, char delimiter) {
+ size_t i = str_find_char_right(arg, delimiter);
+ str res = str_slice_to_str(arg, i + 1, arg->len);
+ str_println(&res);
+ return res;
+}
+
+str str_from_before_delim(str *arg, char delimiter) {
+ size_t i = str_find_char(arg, delimiter);
+ str res = str_slice_to_str(arg, i + 1, arg->len);
+ str_println(&res);
+ return res;
+}
+
+str_slc str_slc_from_after_delim(str_slc arg, char delimiter) {
+ size_t i = str_slc_find_char_right(arg, delimiter);
+ str_slc res = str_slc_slice(arg, i + 1, arg.len);
+ str_slc_println(res);
+ return res;
+}
+
+str_slc str_slc_from_before_delim(str_slc arg, char delimiter) {
+ size_t i = str_slc_find_char(arg, delimiter);
+ str_slc res = str_slc_slice(arg, i + 1, arg.len);
+ str_slc_println(res);
+ return res;
}