]> git.lizzy.rs Git - nothing.git/blob - src/path.c
Move triangle to math "subpackage"
[nothing.git] / src / path.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4 #include "./lt.h"
5 #include "./path.h"
6 #include "./error.h"
7
8 char *base_path_folder(const char *subfolder)
9 {
10     lt_t *lt = create_lt();
11     if (lt == NULL) {
12         return NULL;
13     }
14
15     char *base_path = PUSH_LT(lt, SDL_GetBasePath(), SDL_free);
16     if (base_path == NULL) {
17         base_path = PUSH_LT(lt, SDL_strdup("./"), SDL_free);
18     }
19
20     const size_t buffer_size =
21         sizeof(char) * (strlen(base_path) + strlen(subfolder) + 1);
22
23     char *buffer = PUSH_LT(lt, malloc(buffer_size), free);
24     if (buffer == NULL) {
25         throw_error(ERROR_TYPE_LIBC);
26         RETURN_LT(lt, NULL);
27     }
28
29     if (snprintf(buffer, buffer_size,
30                  "%s%s/", base_path, subfolder) < 0) {
31         throw_error(ERROR_TYPE_LIBC);
32         RETURN_LT(lt, NULL);
33     }
34
35     buffer = RELEASE_LT(lt, buffer);
36     RETURN_LT(lt, buffer);
37 }