Line data Source code
1 : #include "steam_types.h"
2 :
3 4 : oid_amount_list_t oid_amount_list_from_string(const char* text)
4 : {
5 4 : oid_amount_list_t list = { 0 };
6 :
7 4 : if (!text || *text == '\0')
8 : {
9 2 : return list;
10 : }
11 :
12 2 : char* buffer = strdup(text);
13 2 : if (!buffer)
14 : {
15 0 : return list;
16 : }
17 :
18 2 : size_t capacity = 8;
19 2 : list.entries = (oid_amount_t*)calloc(capacity, sizeof(oid_amount_t));
20 :
21 2 : if (!list.entries)
22 : {
23 0 : free(buffer);
24 0 : return list;
25 : }
26 :
27 2 : char* saveptr = NULL;
28 2 : char* token = strtok_r(buffer, ";", &saveptr);
29 :
30 8 : while (token != NULL)
31 : {
32 : /* trim leading spaces */
33 6 : while (isspace((unsigned char)*token))
34 : {
35 0 : token++;
36 : }
37 :
38 : /* trim trailing spaces */
39 6 : char* end = token + strlen(token) - 1;
40 6 : while (end > token && isspace((unsigned char)*end))
41 : {
42 0 : *end = '\0';
43 0 : end--;
44 : }
45 :
46 6 : if (*token != '\0')
47 : {
48 6 : int id = 0;
49 6 : int qty = 1;
50 :
51 6 : char* x = strchr(token, 'x');
52 6 : if (x != NULL)
53 : {
54 4 : *x = '\0';
55 4 : id = atoi(token);
56 4 : qty = atoi(x + 1);
57 : }
58 : else
59 : {
60 2 : id = atoi(token);
61 : }
62 :
63 6 : if (id > 0 && qty > 0)
64 : {
65 6 : if (list.count >= capacity)
66 : {
67 0 : capacity *= 2;
68 0 : oid_amount_t* resized = (oid_amount_t*)realloc(list.entries, capacity * sizeof(oid_amount_t));
69 :
70 0 : if (!resized)
71 : {
72 0 : break;
73 : }
74 :
75 0 : list.entries = resized;
76 : }
77 :
78 6 : list.entries[list.count].itemdefid = id;
79 6 : list.entries[list.count].quantity = qty;
80 6 : list.count++;
81 : }
82 : }
83 :
84 6 : token = strtok_r(NULL, ";", &saveptr);
85 : }
86 :
87 2 : free(buffer);
88 2 : return list;
89 : }
90 :
91 1 : char* oid_amount_list_to_string(const oid_amount_list_t* list)
92 : {
93 1 : if (!list || list->count == 0)
94 : {
95 0 : return strdup("");
96 : }
97 :
98 1 : size_t bufsize = list->count * 16;
99 1 : char* out = (char*)malloc(bufsize);
100 :
101 1 : if (!out)
102 : {
103 0 : return NULL;
104 : }
105 :
106 1 : out[0] = '\0';
107 :
108 4 : for (size_t i = 0; i < list->count; ++i)
109 : {
110 : char part[32];
111 :
112 3 : if (list->entries[i].quantity != 1)
113 : {
114 2 : snprintf(part, sizeof(part), "%d x %d", list->entries[i].itemdefid, list->entries[i].quantity);
115 : }
116 : else
117 : {
118 1 : snprintf(part, sizeof(part), "%d", list->entries[i].itemdefid);
119 : }
120 :
121 3 : if (i > 0)
122 : {
123 2 : strncat(out, ";", bufsize - strlen(out) - 1);
124 : }
125 :
126 3 : strncat(out, part, bufsize - strlen(out) - 1);
127 : }
128 :
129 1 : return out;
130 : }
131 :
132 2 : void oid_amount_list_free(oid_amount_list_t* list)
133 : {
134 2 : if (!list)
135 : {
136 0 : return;
137 : }
138 :
139 2 : if (list->entries != NULL)
140 : {
141 2 : free(list->entries);
142 2 : list->entries = NULL;
143 : }
144 :
145 2 : list->count = 0;
146 : }
147 :
148 5 : int oid_itemdefid_valid(const oid_itemdefid_t id, int workshop_allowed)
149 : {
150 5 : if (id.value <= 0 || (!workshop_allowed && id.value >= 1000000))
151 : {
152 3 : return 0;
153 : }
154 :
155 2 : return 1;
156 : }
157 :
158 6 : oid_itemdefid_t oid_itemdefid_from_int(const int32_t val)
159 : {
160 6 : oid_itemdefid_t id = { val };
161 6 : return id;
162 : }
163 :
164 1 : int32_t oid_itemdefid_to_int(const oid_itemdefid_t id)
165 : {
166 1 : return id.value;
167 : }
168 :
169 0 : oid_item_type_t oid_item_type_from_string(const char* str)
170 : {
171 0 : if (!str) return OID_ITEM_TYPE_UNKNOWN;
172 0 : if (!strcmp(str, "item")) return OID_ITEM_TYPE_ITEM;
173 0 : if (!strcmp(str, "bundle")) return OID_ITEM_TYPE_BUNDLE;
174 0 : if (!strcmp(str, "generator")) return OID_ITEM_TYPE_GENERATOR;
175 0 : if (!strcmp(str, "playtimegenerator")) return OID_ITEM_TYPE_PLAYTIME_GENERATOR;
176 0 : if (!strcmp(str, "tag_generator")) return OID_ITEM_TYPE_TAG_GENERATOR;
177 0 : return OID_ITEM_TYPE_UNKNOWN;
178 : }
179 :
180 0 : const char* oid_item_type_to_string(const oid_item_type_t type)
181 : {
182 0 : switch (type)
183 : {
184 0 : case OID_ITEM_TYPE_ITEM: return "item";
185 0 : case OID_ITEM_TYPE_BUNDLE: return "bundle";
186 0 : case OID_ITEM_TYPE_GENERATOR: return "generator";
187 0 : case OID_ITEM_TYPE_PLAYTIME_GENERATOR: return "playtimegenerator";
188 0 : case OID_ITEM_TYPE_TAG_GENERATOR: return "tag_generator";
189 0 : default: return "unknown";
190 : }
191 : }
192 :
193 0 : const oid_color_t oid_color_from_hex(const char* hex)
194 : {
195 0 : oid_color_t color = {0, 0, 0, 0};
196 :
197 0 : if (!hex || strlen(hex) != 6)
198 : {
199 0 : return color;
200 : }
201 :
202 0 : for (size_t i = 0; i < 6; ++i)
203 : {
204 0 : if (!isxdigit((unsigned char)hex[i]))
205 : {
206 0 : return color;
207 : }
208 : }
209 :
210 : unsigned int rgb;
211 0 : if (sscanf(hex, "%06x", &rgb) == 1)
212 : {
213 0 : color.r = (rgb >> 16) & 0xFF;
214 0 : color.g = (rgb >> 8) & 0xFF;
215 0 : color.b = rgb & 0xFF;
216 0 : color.valid = 1;
217 : }
218 :
219 0 : return color;
220 : }
221 :
222 0 : void oid_color_to_hex(const oid_color_t color, char* buffer)
223 : {
224 0 : if (!buffer)
225 : {
226 0 : return;
227 : }
228 :
229 0 : snprintf(buffer, 7, "%02X%02X%02X", color.r, color.g, color.b);
230 : }
|