LlamaLib  v2.0.2
Cross-platform library for local LLMs
Loading...
Searching...
No Matches
defs.h
Go to the documentation of this file.
1
4
7
10
11#pragma once
12
13#include <sys/stat.h>
14
15#include <string>
16
17#include "json.hpp"
18#ifdef _WIN32
19#include <windows.h>
20#endif
21
22#ifdef _WIN32
23#ifdef UNDREAMAI_EXPORTS
24#define UNDREAMAI_API __declspec(dllexport)
25#else
26#define UNDREAMAI_API __declspec(dllimport)
27#endif
28#else
29#define UNDREAMAI_API
30#endif
31
32using json = nlohmann::ordered_json;
33
34typedef void (*CharArrayFn)(const char *);
35
36#ifdef _WIN32
37const char SEP = '\\';
38#else
39const char SEP = '/';
40#endif
41
42inline std::string join_paths(const std::string &a, const std::string &b)
43{
44 if (a.empty())
45 return b;
46 if (b.empty())
47 return a;
48 if (a.back() == SEP)
49 return a + b;
50 return a + SEP + b;
51}
52
53inline bool file_exists(const std::string &path)
54{
55 struct stat s;
56 return stat(path.c_str(), &s) == 0;
57}
58
59inline bool is_file(const std::string &path)
60{
61 struct stat s;
62 return stat(path.c_str(), &s) == 0 && s.st_mode & S_IFREG;
63}
64
65inline bool is_directory(const std::string &path)
66{
67 struct stat s;
68 return stat(path.c_str(), &s) == 0 && s.st_mode & S_IFDIR;
69}
70
71inline bool always_false()
72{
73 return false;
74}
75
76inline std::string args_to_command(int argc, char **argv)
77{
78 std::string command = "";
79 for (int i = 1; i < argc; ++i)
80 {
81 command += argv[i];
82 if (i < argc - 1)
83 command += " ";
84 }
85 return command;
86}
87
88static inline const char *stringToCharArray(const std::string &input)
89{
90 char *content = new char[input.length() + 1];
91 strcpy(content, input.c_str());
92 return content;
93}
94
95extern "C"
96{
97 UNDREAMAI_API void CharArray_Delete(char *object);
98}
99
100inline void CharArray_Delete(char *str)
101{
102 delete[] str;
103}