A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h
5*/
6
7#include <ctype.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define lobject_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "ldo.h"
19#include "lmem.h"
20#include "lobject.h"
21#include "lstate.h"
22#include "lstring.h"
23#include "lvm.h"
24
25
26
27const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
28
29
30/*
31** converts an integer to a "floating point byte", represented as
32** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
33** eeeee != 0 and (xxx) otherwise.
34*/
35int luaO_int2fb (unsigned int x) {
36 int e = 0; /* expoent */
37 while (x >= 16) {
38 x = (x+1) >> 1;
39 e++;
40 }
41 if (x < 8) return x;
42 else return ((e+1) << 3) | (cast_int(x) - 8);
43}
44
45
46/* converts back */
47int luaO_fb2int (int x) {
48 int e = (x >> 3) & 31;
49 if (e == 0) return x;
50 else return ((x & 7)+8) << (e - 1);
51}
52
53
54int luaO_log2 (unsigned int x) {
55 static const lu_byte log_2[256] = {
56 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
57 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
58 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
59 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
60 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
61 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
62 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
63 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
64 };
65 int l = -1;
66 while (x >= 256) { l += 8; x >>= 8; }
67 return l + log_2[x];
68
69}
70
71
72int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
73 if (ttype(t1) != ttype(t2)) return 0;
74 else switch (ttype(t1)) {
75 case LUA_TNIL:
76 return 1;
77 case LUA_TNUMBER:
78 return luai_numeq(nvalue(t1), nvalue(t2));
79 case LUA_TBOOLEAN:
80 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
81 case LUA_TLIGHTUSERDATA:
82 return pvalue(t1) == pvalue(t2);
83 default:
84 lua_assert(iscollectable(t1));
85 return gcvalue(t1) == gcvalue(t2);
86 }
87}
88
89
90int luaO_str2d (const char *s, lua_Number *result) {
91 char *endptr;
92 *result = lua_str2number(s, &endptr);
93 if (endptr == s) return 0; /* conversion failed */
94 if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
95 *result = cast_num(strtoul(s, &endptr, 16));
96 if (*endptr == '\0') return 1; /* most common case */
97 while (isspace(cast(unsigned char, *endptr))) endptr++;
98 if (*endptr != '\0') return 0; /* invalid trailing characters? */
99 return 1;
100}
101
102
103
104static void pushstr (lua_State *L, const char *str) {
105 setsvalue2s(L, L->top, luaS_new(L, str));
106 incr_top(L);
107}
108
109
110/* ROCKLUA ADDED -- Retrieves C string from TString */
111const char *luaO_getstring(const TString * ts){
112 const char *string;
113#ifdef INBINARYSTRINGS
114 if (testbits((ts)->tsv.type, TSTR_INBIN))
115 string = *(cast(const char **, (ts) + 1));
116 else
117#else
118 if (true)
119#endif
120 string = cast(const char *, (ts) + 1);
121 return string;
122}
123
124
125/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
126const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
127 int n = 1;
128 pushstr(L, "");
129 for (;;) {
130 const char *e = strchr(fmt, '%');
131 if (e == NULL) break;
132 setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
133 incr_top(L);
134 switch (*(e+1)) {
135 case 's': {
136 const char *s = va_arg(argp, char *);
137 if (s == NULL) s = "(null)";
138 pushstr(L, s);
139 break;
140 }
141 case 'c': {
142 char buff[2];
143 buff[0] = cast(char, va_arg(argp, int));
144 buff[1] = '\0';
145 pushstr(L, buff);
146 break;
147 }
148 case 'd': {
149 setnvalue(L->top, cast_num(va_arg(argp, int)));
150 incr_top(L);
151 break;
152 }
153 case 'f': {
154 setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
155 incr_top(L);
156 break;
157 }
158 case 'p': {
159 char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
160 snprintf(buff, 4*sizeof(void *) + 8, "%p", va_arg(argp, void *));
161 pushstr(L, buff);
162 break;
163 }
164 case '%': {
165 pushstr(L, "%");
166 break;
167 }
168 default: {
169 char buff[3];
170 buff[0] = '%';
171 buff[1] = *(e+1);
172 buff[2] = '\0';
173 pushstr(L, buff);
174 break;
175 }
176 }
177 n += 2;
178 fmt = e+2;
179 }
180 pushstr(L, fmt);
181 luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
182 L->top -= n;
183 return svalue(L->top - 1);
184}
185
186
187const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
188 const char *msg;
189 va_list argp;
190 va_start(argp, fmt);
191 msg = luaO_pushvfstring(L, fmt, argp);
192 va_end(argp);
193 return msg;
194}
195
196/* lua 5.2 lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ */
197/* number of chars of a literal string without the ending \0 */
198#define LL(x) (sizeof(x)/sizeof(char) - 1)
199
200#define RETS "..."
201#define PRE "[string \""
202#define POS "\"]"
203
204#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
205
206void luaO_chunkid (char *out, const char *source, size_t bufflen) {
207 size_t l = strlen(source);
208 if (*source == '=') { /* 'literal' source */
209 if (l <= bufflen) /* small enough? */
210 memcpy(out, source + 1, l * sizeof(char));
211 else { /* truncate it */
212 addstr(out, source + 1, bufflen - 1);
213 *out = '\0';
214 }
215 }
216 else if (*source == '@') { /* file name */
217 if (l <= bufflen) /* small enough? */
218 memcpy(out, source + 1, l * sizeof(char));
219 else { /* add '...' before rest of name */
220 addstr(out, RETS, LL(RETS));
221 bufflen -= LL(RETS);
222 memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
223 }
224 }
225 else { /* string; format as [string "source"] */
226 const char *nl = strchr(source, '\n'); /* find first new line (if any) */
227 addstr(out, PRE, LL(PRE)); /* add prefix */
228 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
229 if (l < bufflen && nl == NULL) { /* small one-line source? */
230 addstr(out, source, l); /* keep it */
231 }
232 else {
233 if (nl != NULL) l = nl - source; /* stop at first newline */
234 if (l > bufflen) l = bufflen;
235 addstr(out, source, l);
236 addstr(out, RETS, LL(RETS));
237 }
238 memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
239 }
240}
241