Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/glib/glib.c
2 views
1
//
2
// Created by nhp on 14-05-24.
3
//
4
5
#include <string.h>
6
#include <stdlib.h>
7
#include <math.h>
8
#include <stdio.h>
9
#include <stdarg.h>
10
11
#include "glib.h"
12
13
GString* g_string_new(gchar* initial) {
14
GString* str = g_new0(GString, 1);
15
16
if (initial != NULL) {
17
int len = strlen(initial);
18
gchar* p = malloc(len);
19
memcpy(p, initial, len);
20
str->str = p;
21
str->len = len;
22
str->allocated_len = len;
23
} else {
24
gchar* p = malloc(64);
25
str->str = p;
26
str->len = 0;
27
str->allocated_len = 64;
28
}
29
return str;
30
}
31
32
gchar* g_string_free(GString* str, gboolean free_segment) {
33
char* seg = str->str;
34
free(str);
35
if (free_segment) {
36
free(str->str);
37
return NULL;
38
}
39
return seg;
40
}
41
42
void g_string_append_printf(GString* str, const gchar* format, ...) {
43
va_list args;
44
va_start(args, format);
45
int need_len = vsnprintf(NULL, 0, format, args);
46
va_end(args);
47
48
if (str->len + need_len + 1 < str->allocated_len) {
49
gsize new_len = str->len + need_len + 1;
50
gchar* newp = realloc(str->str, new_len);
51
str->str = newp;
52
str->allocated_len = new_len;
53
str->len = new_len - 1;
54
}
55
56
gchar* temp = malloc(need_len + 1);
57
va_start(args, format);
58
vsnprintf(temp, need_len, format, args);
59
va_end(args);
60
61
strcat(str->str, temp);
62
free(temp);
63
}
64
65
gchar* g_strstr_len(const gchar* haystack, int len, const gchar* needle) {
66
if (len == -1) return strstr(haystack, needle);
67
size_t needle_len = strlen(needle);
68
for (int i = 0; i < len; i++) {
69
size_t found = 0;
70
for (int j = i; j < len; j++) {
71
if (haystack[j] == needle[j - i]) found++;
72
else break;
73
if (found == needle_len) return (gchar*) haystack + j;
74
}
75
}
76
return NULL;
77
}
78
79
gchar* g_strdup(const gchar* str) {
80
if (str == NULL) return NULL;
81
else return strdup(str);
82
}
83
84
int g_strv_length(GStrv strings) {
85
gint count = 0;
86
while (strings[count])
87
count++;
88
return count;
89
}
90
91
void g_strfreev(GStrv strings) {
92
for (int i = 0; strings[i] != NULL; i++) {
93
free(strings[i]);
94
}
95
}
96
97
// This is not good but all we're using slirp for is beaming pokemans over the internet so it's probably okay
98
gint g_rand_int_range(GRand* grand, gint min, gint max) {
99
double r = (double) rand();
100
double range = (double) (max - min);
101
double r2 = (r / (double) RAND_MAX) * range;
102
return MIN(max, ((int) r2) + min);
103
}
104
105
GRand* g_rand_new() {
106
return malloc(sizeof(GRand));
107
}
108
109
void g_rand_free(GRand* rand) {
110
free(rand);
111
}
112
113
void g_error_free(GError* error) {
114
free(error);
115
}
116
117
gboolean g_shell_parse_argv(const gchar* command_line, gint* argcp, gchar*** argvp, GError** error) {
118
const gchar* message = "Unimplemented.";
119
GError* err = malloc(sizeof(GError));
120
err->message = message;
121
*error = err;
122
return false;
123
}
124
125
gboolean g_spawn_async_with_fds(const gchar *working_directory, gchar **argv,
126
gchar **envp, GSpawnFlags flags,
127
GSpawnChildSetupFunc child_setup,
128
gpointer user_data, GPid *child_pid, gint stdin_fd,
129
gint stdout_fd, gint stderr_fd, GError **error)
130
{
131
return false;
132
}
133
134