MCPcopy Create free account
hub / github.com/aardappel/treesheets / AddFile

Function AddFile

lobster/src/file.cpp:117–362  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117void AddFile(NativeRegistry &nfr) {
118
119nfr("scan_folder", "folder,divisor", "SI", "S]?I]?",
120 "returns two vectors representing all elements in a folder, the first vector containing all"
121 " names, the second vector containing sizes (or -1 if a directory)."
122 " Specify 1 as divisor to get sizes in bytes, 1024 for kb etc. Values > 0x7FFFFFFF will be"
123 " clamped in 32-bit builds. Returns nil if folder couldn't be scanned.",
124 [](VM &vm, Value &fld, Value &divisor) {
125 vector<pair<string, int64_t>> dir;
126 auto ok = ScanDirAbs(fld.sval()->strv(), dir);
127 if (!ok) {
128 vm.Push(Value());
129 return Value();
130 }
131 if (divisor.ival() <= 0) divisor.setival(1);
132 auto nlist = (LVector *)vm.NewVec(0, 0, TYPE_ELEM_VECTOR_OF_STRING);
133 auto slist = (LVector *)vm.NewVec(0, 0, TYPE_ELEM_VECTOR_OF_INT);
134 for (auto &[name, size] : dir) {
135 nlist->Push(vm, Value(vm.NewString(name)));
136 if (size >= 0) {
137 size /= divisor.ival();
138 if (sizeof(intp) == sizeof(int) && size > 0x7FFFFFFF) size = 0x7FFFFFFF;
139 }
140 slist->Push(vm, Value(size));
141 }
142 vm.Push(Value(nlist));
143 return Value(slist);
144 });
145
146nfr("read_file", "file,textmode", "SI?", "S?",
147 "returns the contents of a file as a string, or nil if the file can't be found."
148 " you may use either \\ or / as path separators",
149 [](VM &vm, Value &file, Value &textmode) {
150 string buf;
151 auto l = LoadFile(file.sval()->strv(), &buf, 0, -1, !textmode.True());
152 if (l < 0) return Value();
153 auto s = vm.NewString(buf);
154 return Value(s);
155 });
156
157nfr("write_file", "file,contents,textmode", "SSI?", "B",
158 "creates a file with the contents of a string, returns false if writing wasn't possible",
159 [](VM &, Value &file, Value &contents, Value &textmode) {
160 auto ok = WriteFile(file.sval()->strv(), !textmode.True(), contents.sval()->strv());
161 return Value(ok);
162 });
163
164nfr("ensure_size", "string,size,char,extra", "SkIII?", "S",
165 "ensures a string is at least size characters. if it is, just returns the existing"
166 " string, otherwise returns a new string of that size (with optionally extra bytes"
167 " added), with any new characters set to"
168 " char. You can specify a negative size to mean relative to the end, i.e. new"
169 " characters will be added at the start. ",
170 [](VM &vm, Value &str, Value &size, Value &c, Value &extra) {
171 auto asize = abs(size.ival());
172 return str.sval()->len >= asize
173 ? str
174 : Value(vm.ResizeString(str.sval(), asize + extra.ival(), c.intval(), size.ival() < 0));

Callers

nothing calls this directly

Calls 15

ScanDirAbsFunction · 0.85
WriteFileFunction · 0.85
absFunction · 0.85
ParseSchemasFunction · 0.85
GenerateTextFunction · 0.85
strvMethod · 0.80
svalMethod · 0.80
ivalMethod · 0.80
setivalMethod · 0.80
NewVecMethod · 0.80
NewStringMethod · 0.80
ResizeStringMethod · 0.80

Tested by

no test coverage detected