MinitScript  0.9.31 PRE-BETA
FileSystemMethods.cpp
Go to the documentation of this file.
1 #include <span>
2 
10 
11 using std::span;
12 
14 
16 
21 
22 void FileSystemMethods::registerConstants(MinitScript* minitScript) {
23 }
24 
25 void FileSystemMethods::registerMethods(MinitScript* minitScript) {
26  // file system methods
27  {
28  //
29  class ScriptFileSystemComposeFileName: public MinitScript::Method {
30  private:
31  MinitScript* minitScript { nullptr };
32  public:
33  ScriptFileSystemComposeFileName(MinitScript* minitScript):
35  {
36  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
37  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
38  },
39  MinitScript::TYPE_STRING
40  ),
41  minitScript(minitScript) {
42  //
43  }
44  const string getMethodName() override {
45  return "filesystem.composeURI";
46  }
47  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
48  string pathName;
49  string fileName;
50  if (arguments.size() == 2 &&
51  MinitScript::getStringValue(arguments, 0, pathName) == true &&
52  MinitScript::getStringValue(arguments, 1, fileName) == true) {
53  returnValue.setValue(_FileSystem::composeURI(pathName, fileName));
54  } else {
55  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
56  }
57  }
58  };
59  minitScript->registerMethod(new ScriptFileSystemComposeFileName(minitScript));
60  }
61  {
62  //
63  class ScriptFileSystemGetFileSize: public MinitScript::Method {
64  private:
65  MinitScript* minitScript { nullptr };
66  public:
67  ScriptFileSystemGetFileSize(MinitScript* minitScript):
69  {
70  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
71  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
72  },
73  MinitScript::TYPE_INTEGER,
74  true
75  ),
76  minitScript(minitScript) {
77  //
78  }
79  const string getMethodName() override {
80  return "filesystem.getFileSize";
81  }
82  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
83  string pathName;
84  string fileName;
85  if (arguments.size() == 2 &&
86  MinitScript::getStringValue(arguments, 0, pathName) == true &&
87  MinitScript::getStringValue(arguments, 1, fileName) == true) {
88  try {
89  returnValue.setValue(static_cast<int64_t>(_FileSystem::getFileSize(pathName, fileName)));
90  } catch (_Exception& exception) {
91  _Console::printLine("An error occurred: " + string(exception.what()));
92  }
93  } else {
94  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
95  }
96  }
97  };
98  minitScript->registerMethod(new ScriptFileSystemGetFileSize(minitScript));
99  }
100  {
101  //
102  class ScriptFileSystemGetContentAsString: public MinitScript::Method {
103  private:
104  MinitScript* minitScript { nullptr };
105  public:
106  ScriptFileSystemGetContentAsString(MinitScript* minitScript):
108  {
109  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
110  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
111  },
112  MinitScript::TYPE_STRING,
113  true
114  ),
115  minitScript(minitScript) {
116  //
117  }
118  const string getMethodName() override {
119  return "filesystem.getContentAsString";
120  }
121  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
122  string pathName;
123  string fileName;
124  if (arguments.size() == 2 &&
125  MinitScript::getStringValue(arguments, 0, pathName) == true &&
126  MinitScript::getStringValue(arguments, 1, fileName) == true) {
127  try {
128  returnValue.setValue(_FileSystem::getContentAsString(pathName, fileName));
129  } catch (_Exception& exception) {
130  _Console::printLine("An error occurred: " + string(exception.what()));
131  }
132  } else {
133  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
134  }
135  }
136  };
137  minitScript->registerMethod(new ScriptFileSystemGetContentAsString(minitScript));
138  }
139  {
140  //
141  class ScriptFileSystemSetContentFromString: public MinitScript::Method {
142  private:
143  MinitScript* minitScript { nullptr };
144  public:
145  ScriptFileSystemSetContentFromString(MinitScript* minitScript):
147  {
148  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
149  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false },
150  { .type = MinitScript::TYPE_STRING, .name = "content", .optional = false, .reference = false, .nullable = false },
151  },
152  MinitScript::TYPE_BOOLEAN
153  ),
154  minitScript(minitScript) {
155  //
156  }
157  const string getMethodName() override {
158  return "filesystem.setContentFromString";
159  }
160  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
161  string pathName;
162  string fileName;
163  string content;
164  if (arguments.size() == 3 &&
165  MinitScript::getStringValue(arguments, 0, pathName) == true &&
166  MinitScript::getStringValue(arguments, 1, fileName) == true &&
167  MinitScript::getStringValue(arguments, 2, content) == true) {
168  try {
169  _FileSystem::setContentFromString(pathName, fileName, content);
170  returnValue.setValue(true);
171  } catch (_Exception& exception) {
172  returnValue.setValue(false);
173  _Console::printLine("An error occurred: " + string(exception.what()));
174  }
175  } else {
176  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
177  }
178  }
179  };
180  minitScript->registerMethod(new ScriptFileSystemSetContentFromString(minitScript));
181  }
182  {
183  //
184  class ScriptFileSystemGetContent: public MinitScript::Method {
185  private:
186  MinitScript* minitScript { nullptr };
187  public:
188  ScriptFileSystemGetContent(MinitScript* minitScript):
190  {
191  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
192  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
193  },
194  MinitScript::TYPE_BYTEARRAY,
195  true
196  ),
197  minitScript(minitScript) {
198  //
199  }
200  const string getMethodName() override {
201  return "filesystem.getContent";
202  }
203  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
204  string pathName;
205  string fileName;
206  if (arguments.size() == 2 &&
207  MinitScript::getStringValue(arguments, 0, pathName) == true &&
208  MinitScript::getStringValue(arguments, 1, fileName) == true) {
209  try {
210  vector<uint8_t> content;
211  _FileSystem::getContent(pathName, fileName, content);
212  returnValue.setValue(content);
213  } catch (_Exception& exception) {
214  _Console::printLine("An error occurred: " + string(exception.what()));
215  }
216  } else {
217  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
218  }
219  }
220  };
221  minitScript->registerMethod(new ScriptFileSystemGetContent(minitScript));
222  }
223  {
224  //
225  class ScriptFileSystemSetContent: public MinitScript::Method {
226  private:
227  MinitScript* minitScript { nullptr };
228  public:
229  ScriptFileSystemSetContent(MinitScript* minitScript):
231  {
232  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
233  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false },
234  { .type = MinitScript::TYPE_BYTEARRAY, .name = "content", .optional = false, .reference = false, .nullable = false },
235  },
236  MinitScript::TYPE_BOOLEAN
237  ),
238  minitScript(minitScript) {
239  //
240  }
241  const string getMethodName() override {
242  return "filesystem.setContent";
243  }
244  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
245  string pathName;
246  string fileName;
247  if (arguments.size() == 3 &&
248  MinitScript::getStringValue(arguments, 0, pathName, false) == true &&
249  MinitScript::getStringValue(arguments, 1, fileName, false) == true) {
250  try {
251  auto contentPtr = arguments[2].getByteArrayPointer();
252  if (contentPtr == nullptr) throw _ExceptionBase("Empty content byte array provided");
253  _FileSystem::setContent(pathName, fileName, *contentPtr);
254  returnValue.setValue(true);
255  } catch (_Exception& exception) {
256  returnValue.setValue(false);
257  _Console::printLine("An error occurred: " + string(exception.what()));
258  }
259  } else {
260  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
261  }
262  }
263  };
264  minitScript->registerMethod(new ScriptFileSystemSetContent(minitScript));
265  }
266  {
267  //
268  class ScriptFileSystemGetContentAsStringArray: public MinitScript::Method {
269  private:
270  MinitScript* minitScript { nullptr };
271  public:
272  ScriptFileSystemGetContentAsStringArray(MinitScript* minitScript):
274  {
275  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
276  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false },
277  },
278  MinitScript::TYPE_ARRAY,
279  true
280  ),
281  minitScript(minitScript) {
282  //
283  }
284  const string getMethodName() override {
285  return "filesystem.getContentAsStringArray";
286  }
287  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
288  string pathName;
289  string fileName;
290  if (arguments.size() == 2 &&
291  MinitScript::getStringValue(arguments, 0, pathName) == true &&
292  MinitScript::getStringValue(arguments, 1, fileName) == true) {
293  vector<string> contentAsStringArray;
294  try {
295  _FileSystem::getContentAsStringArray(pathName, fileName, contentAsStringArray);
296  //
297  returnValue.setType(MinitScript::TYPE_ARRAY);
298  for (const auto& contentAsStringArrayEntry: contentAsStringArray) {
299  returnValue.pushArrayEntry(contentAsStringArrayEntry);
300  }
301  } catch (_Exception& exception) {
302  _Console::printLine("An error occurred: " + string(exception.what()));
303  }
304  } else {
305  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
306  }
307  }
308  };
309  minitScript->registerMethod(new ScriptFileSystemGetContentAsStringArray(minitScript));
310  }
311  {
312  //
313  class ScriptFileSystemSetContentFromStringArray: public MinitScript::Method {
314  private:
315  MinitScript* minitScript { nullptr };
316  public:
317  ScriptFileSystemSetContentFromStringArray(MinitScript* minitScript):
319  {
320  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
321  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false },
322  { .type = MinitScript::TYPE_ARRAY, .name = "content", .optional = false, .reference = false, .nullable = false },
323  },
324  MinitScript::TYPE_BOOLEAN
325  ),
326  minitScript(minitScript) {
327  //
328  }
329  const string getMethodName() override {
330  return "filesystem.setContentFromStringArray";
331  }
332  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
333  string pathName;
334  string fileName;
335  if (arguments.size() == 3 &&
336  MinitScript::getStringValue(arguments, 0, pathName) == true &&
337  MinitScript::getStringValue(arguments, 1, fileName) == true &&
338  arguments[2].getType() == MinitScript::TYPE_ARRAY) {
339  vector<string> contentAsStringArray;
340  auto arrayPointer = arguments[2].getArrayPointer();
341  if (arrayPointer != nullptr) for (const auto arrayEntry: *arrayPointer) contentAsStringArray.push_back(arrayEntry->getValueAsString());
342  try {
343  _FileSystem::setContentFromStringArray(pathName, fileName, contentAsStringArray);
344  returnValue.setValue(true);
345  } catch (_Exception& exception) {
346  returnValue.setValue(false);
347  _Console::printLine("An error occurred: " + string(exception.what()));
348  }
349  } else {
350  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
351  }
352  }
353  };
354  minitScript->registerMethod(new ScriptFileSystemSetContentFromStringArray(minitScript));
355  }
356  {
357  //
358  class ScriptFileSystemIsPath: public MinitScript::Method {
359  private:
360  MinitScript* minitScript { nullptr };
361  public:
362  ScriptFileSystemIsPath(MinitScript* minitScript):
364  {
365  { .type = MinitScript::TYPE_STRING, .name = "uri", .optional = false, .reference = false, .nullable = false },
366  },
367  MinitScript::TYPE_BOOLEAN,
368  true
369  ),
370  minitScript(minitScript) {
371  //
372  }
373  const string getMethodName() override {
374  return "filesystem.isPath";
375  }
376  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
377  string uri;
378  if (arguments.size() == 1 &&
379  MinitScript::getStringValue(arguments, 0, uri, false) == true) {
380  try {
381  returnValue.setValue(_FileSystem::isPath(uri));
382  } catch (_Exception& exception) {
383  _Console::printLine("An error occurred: " + string(exception.what()));
384  }
385  } else {
386  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
387  }
388  }
389  };
390  minitScript->registerMethod(new ScriptFileSystemIsPath(minitScript));
391  }
392  {
393  //
394  class ScriptFileSystemIsDrive: public MinitScript::Method {
395  private:
396  MinitScript* minitScript { nullptr };
397  public:
398  ScriptFileSystemIsDrive(MinitScript* minitScript):
400  {
401  { .type = MinitScript::TYPE_STRING, .name = "uri", .optional = false, .reference = false, .nullable = false },
402  },
403  MinitScript::TYPE_BOOLEAN
404  ),
405  minitScript(minitScript) {
406  //
407  }
408  const string getMethodName() override {
409  return "filesystem.isDrive";
410  }
411  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
412  string uri;
413  if (arguments.size() == 1 &&
414  MinitScript::getStringValue(arguments, 0, uri) == true) {
415  returnValue.setValue(_FileSystem::isDrive(uri));
416  } else {
417  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
418  }
419  }
420  };
421  minitScript->registerMethod(new ScriptFileSystemIsDrive(minitScript));
422  }
423  {
424  class ScriptFileSystemFileExists: public MinitScript::Method {
425  private:
426  MinitScript* minitScript { nullptr };
427  public:
428  ScriptFileSystemFileExists(MinitScript* minitScript):
430  {
431  { .type = MinitScript::TYPE_STRING, .name = "uri", .optional = false, .reference = false, .nullable = false },
432  },
433  MinitScript::TYPE_BOOLEAN,
434  true
435  ),
436  minitScript(minitScript) {
437  //
438  }
439  const string getMethodName() override {
440  return "filesystem.exists";
441  }
442  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
443  string uri;
444  if (arguments.size() == 1 &&
445  MinitScript::getStringValue(arguments, 0, uri) == true) {
446  try {
447  returnValue.setValue(_FileSystem::exists(uri));
448  } catch (_Exception& exception) {
449  _Console::printLine("An error occurred: " + string(exception.what()));
450  }
451  } else {
452  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
453  }
454  }
455  };
456  minitScript->registerMethod(new ScriptFileSystemFileExists(minitScript));
457  }
458  {
459  //
460  class ScriptFileSystemGetCanonicalPath: public MinitScript::Method {
461  private:
462  MinitScript* minitScript { nullptr };
463  public:
464  ScriptFileSystemGetCanonicalPath(MinitScript* minitScript):
466  {
467  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
468  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
469  },
470  MinitScript::TYPE_STRING,
471  true
472  ),
473  minitScript(minitScript) {
474  //
475  }
476  const string getMethodName() override {
477  return "filesystem.getCanonicalURI";
478  }
479  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
480  string pathName;
481  string fileName;
482  if (arguments.size() == 2 &&
483  MinitScript::getStringValue(arguments, 0, pathName) == true &&
484  MinitScript::getStringValue(arguments, 1, fileName) == true) {
485  try {
486  returnValue.setValue(_FileSystem::getCanonicalURI(pathName, fileName));
487  } catch (_Exception& exception) {
488  _Console::printLine("An error occurred: " + string(exception.what()));
489  }
490  } else {
491  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
492  }
493  }
494  };
495  minitScript->registerMethod(new ScriptFileSystemGetCanonicalPath(minitScript));
496  }
497  {
498  //
499  class ScriptFileSystemGetCurrentWorkingPathName: public MinitScript::Method {
500  private:
501  MinitScript* minitScript { nullptr };
502  public:
503  ScriptFileSystemGetCurrentWorkingPathName(MinitScript* minitScript):
505  {},
506  MinitScript::TYPE_STRING,
507  true
508  ),
509  minitScript(minitScript) {
510  //
511  }
512  const string getMethodName() override {
513  return "filesystem.getCurrentWorkingPathName";
514  }
515  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
516  if (arguments.size() == 0) {
517  try {
519  } catch (_Exception& exception) {
520  _Console::printLine("An error occurred: " + string(exception.what()));
521  }
522  } else {
523  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
524  }
525  }
526  };
527  minitScript->registerMethod(new ScriptFileSystemGetCurrentWorkingPathName(minitScript));
528  }
529  {
530  //
531  class ScriptFileSystemChangePath: public MinitScript::Method {
532  private:
533  MinitScript* minitScript { nullptr };
534  public:
535  ScriptFileSystemChangePath(MinitScript* minitScript):
537  {
538  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
539  },
540  MinitScript::TYPE_BOOLEAN
541  ),
542  minitScript(minitScript) {
543  //
544  }
545  const string getMethodName() override {
546  return "filesystem.changePath";
547  }
548  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
549  string pathName;
550  if (arguments.size() == 1 &&
551  MinitScript::getStringValue(arguments, 0, pathName) == true) {
552  try {
553  _FileSystem::changePath(pathName);
554  returnValue.setValue(true);
555  } catch (_Exception& exception) {
556  returnValue.setValue(false);
557  _Console::printLine("An error occurred: " + string(exception.what()));
558  }
559  } else {
560  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
561  }
562  }
563  };
564  minitScript->registerMethod(new ScriptFileSystemChangePath(minitScript));
565  }
566  {
567  //
568  class ScriptFileSystemGetFileName: public MinitScript::Method {
569  private:
570  MinitScript* minitScript { nullptr };
571  public:
572  ScriptFileSystemGetFileName(MinitScript* minitScript):
574  {
575  { .type = MinitScript::TYPE_STRING, .name = "uri", .optional = false, .reference = false, .nullable = false }
576  },
577  MinitScript::TYPE_STRING
578  ),
579  minitScript(minitScript) {
580  //
581  }
582  const string getMethodName() override {
583  return "filesystem.getFileName";
584  }
585  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
586  string uri;
587  if (arguments.size() == 1 &&
588  MinitScript::getStringValue(arguments, 0, uri) == true) {
589  returnValue.setValue(_FileSystem::getFileName(uri));
590  } else {
591  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
592  }
593  }
594  };
595  minitScript->registerMethod(new ScriptFileSystemGetFileName(minitScript));
596  }
597  {
598  //
599  class ScriptFileSystemGetPathName: public MinitScript::Method {
600  private:
601  MinitScript* minitScript { nullptr };
602  public:
603  ScriptFileSystemGetPathName(MinitScript* minitScript):
605  {
606  { .type = MinitScript::TYPE_STRING, .name = "uri", .optional = false, .reference = false, .nullable = false }
607  },
608  MinitScript::TYPE_STRING
609  ),
610  minitScript(minitScript) {
611  //
612  }
613  const string getMethodName() override {
614  return "filesystem.getPathName";
615  }
616  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
617  string uri;
618  if (arguments.size() == 1 &&
619  MinitScript::getStringValue(arguments, 0, uri) == true) {
620  returnValue.setValue(_FileSystem::getPathName(uri));
621  } else {
622  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
623  }
624  }
625  };
626  minitScript->registerMethod(new ScriptFileSystemGetPathName(minitScript));
627  }
628  {
629  //
630  class ScriptFileSystemRemoveFileExtension: public MinitScript::Method {
631  private:
632  MinitScript* minitScript { nullptr };
633  public:
634  ScriptFileSystemRemoveFileExtension(MinitScript* minitScript):
636  {
637  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false }
638  },
639  MinitScript::TYPE_STRING
640  ),
641  minitScript(minitScript) {
642  //
643  }
644  const string getMethodName() override {
645  return "filesystem.removeFileExtension";
646  }
647  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
648  string fileName;
649  if (arguments.size() == 1 &&
650  MinitScript::getStringValue(arguments, 0, fileName) == true) {
651  returnValue.setValue(_FileSystem::removeFileExtension(fileName));
652  } else {
653  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
654  }
655  }
656  };
657  minitScript->registerMethod(new ScriptFileSystemRemoveFileExtension(minitScript));
658  }
659  {
660  //
661  class ScriptFileSystemRemoveFile: public MinitScript::Method {
662  private:
663  MinitScript* minitScript { nullptr };
664  public:
665  ScriptFileSystemRemoveFile(MinitScript* minitScript):
667  {
668  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
669  { .type = MinitScript::TYPE_STRING, .name = "fileName", .optional = false, .reference = false, .nullable = false },
670  },
671  MinitScript::TYPE_BOOLEAN
672  ),
673  minitScript(minitScript) {
674  //
675  }
676  const string getMethodName() override {
677  return "filesystem.removeFile";
678  }
679  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
680  string pathName;
681  string fileName;
682  if (arguments.size() == 2 &&
683  MinitScript::getStringValue(arguments, 0, pathName) == true &&
684  MinitScript::getStringValue(arguments, 1, fileName) == true) {
685  try {
686  _FileSystem::removeFile(pathName, fileName);
687  returnValue.setValue(true);
688  } catch (_Exception& exception) {
689  returnValue.setValue(false);
690  _Console::printLine("An error occurred: " + string(exception.what()));
691  }
692  } else {
693  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
694  }
695  }
696  };
697  minitScript->registerMethod(new ScriptFileSystemRemoveFile(minitScript));
698  }
699  {
700  //
701  class ScriptFileSystemCreatePath: public MinitScript::Method {
702  private:
703  MinitScript* minitScript { nullptr };
704  public:
705  ScriptFileSystemCreatePath(MinitScript* minitScript):
707  {
708  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
709  },
710  MinitScript::TYPE_BOOLEAN
711  ),
712  minitScript(minitScript) {
713  //
714  }
715  const string getMethodName() override {
716  return "filesystem.createPath";
717  }
718  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
719  string pathName;
720  if (arguments.size() == 1 &&
721  MinitScript::getStringValue(arguments, 0, pathName) == true) {
722  try {
723  _FileSystem::createPath(pathName);
724  returnValue.setValue(true);
725  } catch (_Exception& exception) {
726  returnValue.setValue(false);
727  _Console::printLine("An error occurred: " + string(exception.what()));
728  }
729  } else {
730  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
731  }
732  }
733  };
734  minitScript->registerMethod(new ScriptFileSystemCreatePath(minitScript));
735  }
736  {
737  //
738  class ScriptFileSystemRemovePath: public MinitScript::Method {
739  private:
740  MinitScript* minitScript { nullptr };
741  public:
742  ScriptFileSystemRemovePath(MinitScript* minitScript):
744  {
745  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
746  { .type = MinitScript::TYPE_STRING, .name = "recursive", .optional = false, .reference = false, .nullable = false },
747  },
748  MinitScript::TYPE_BOOLEAN
749  ),
750  minitScript(minitScript) {
751  //
752  }
753  const string getMethodName() override {
754  return "filesystem.removePath";
755  }
756  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
757  string pathName;
758  bool recursive;
759  if (arguments.size() == 2 &&
760  MinitScript::getStringValue(arguments, 0, pathName) == true &&
761  MinitScript::getBooleanValue(arguments, 1, recursive) == true) {
762  try {
763  _FileSystem::removePath(pathName, recursive);
764  returnValue.setValue(true);
765  } catch (_Exception& exception) {
766  returnValue.setValue(false);
767  _Console::printLine("An error occurred: " + string(exception.what()));
768  }
769  } else {
770  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
771  }
772  }
773  };
774  minitScript->registerMethod(new ScriptFileSystemRemovePath(minitScript));
775  }
776  {
777  //
778  class ScriptFileSystemRename: public MinitScript::Method {
779  private:
780  MinitScript* minitScript { nullptr };
781  public:
782  ScriptFileSystemRename(MinitScript* minitScript):
784  {
785  { .type = MinitScript::TYPE_STRING, .name = "fileNameFrom", .optional = false, .reference = false, .nullable = false },
786  { .type = MinitScript::TYPE_STRING, .name = "fileNameTo", .optional = false, .reference = false, .nullable = false },
787  },
788  MinitScript::TYPE_BOOLEAN
789  ),
790  minitScript(minitScript) {
791  //
792  }
793  const string getMethodName() override {
794  return "filesystem.rename";
795  }
796  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
797  string fileNameFrom;
798  string fileNameTo;
799  if (arguments.size() == 2 &&
800  MinitScript::getStringValue(arguments, 0, fileNameFrom) == true &&
801  MinitScript::getStringValue(arguments, 1, fileNameTo) == true) {
802  try {
803  _FileSystem::rename(fileNameFrom, fileNameTo);
804  returnValue.setValue(true);
805  } catch (_Exception& exception) {
806  returnValue.setValue(false);
807  _Console::printLine("An error occurred: " + string(exception.what()));
808  }
809  } else {
810  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
811  }
812  }
813  };
814  minitScript->registerMethod(new ScriptFileSystemRename(minitScript));
815  }
816  {
817  //
818  class ScriptFileSystemMove: public MinitScript::Method {
819  private:
820  MinitScript* minitScript { nullptr };
821  public:
822  ScriptFileSystemMove(MinitScript* minitScript):
824  {
825  { .type = MinitScript::TYPE_STRING, .name = "uriFrom", .optional = false, .reference = false, .nullable = false },
826  { .type = MinitScript::TYPE_STRING, .name = "uriTo", .optional = false, .reference = false, .nullable = false },
827  },
828  MinitScript::TYPE_BOOLEAN
829  ),
830  minitScript(minitScript) {
831  //
832  }
833  const string getMethodName() override {
834  return "filesystem.move";
835  }
836  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
837  string fileNameFrom;
838  string fileNameTo;
839  if (arguments.size() == 2 &&
840  MinitScript::getStringValue(arguments, 0, fileNameFrom) == true &&
841  MinitScript::getStringValue(arguments, 1, fileNameTo) == true) {
842  try {
843  _FileSystem::rename(fileNameFrom, fileNameTo);
844  returnValue.setValue(true);
845  } catch (_Exception& exception) {
846  returnValue.setValue(false);
847  _Console::printLine("An error occurred: " + string(exception.what()));
848  }
849  } else {
850  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
851  }
852  }
853  };
854  minitScript->registerMethod(new ScriptFileSystemMove(minitScript));
855  }
856  {
857  //
858  class ScriptFileSystemList: public MinitScript::Method {
859  private:
860  MinitScript* minitScript { nullptr };
861  public:
862  ScriptFileSystemList(MinitScript* minitScript):
864  {
865  { .type = MinitScript::TYPE_STRING, .name = "pathName", .optional = false, .reference = false, .nullable = false },
866  },
867  MinitScript::TYPE_ARRAY,
868  true
869  ),
870  minitScript(minitScript) {
871  //
872  }
873  const string getMethodName() override {
874  return "filesystem.list";
875  }
876  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
877  string pathName;
878  if (arguments.size() == 1 &&
879  MinitScript::getStringValue(arguments, 0, pathName, false) == true) {
880  try {
881  vector<string> files;
882  _FileSystem::list(pathName, files);
883  //
884  returnValue.setType(MinitScript::TYPE_ARRAY);
885  for (const auto& file: files) {
886  returnValue.pushArrayEntry(file);
887  }
888  } catch (_Exception& exception) {
889  _Console::printLine("An error occurred: " + string(exception.what()));
890  }
891  } else {
892  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
893  }
894  }
895  };
896  minitScript->registerMethod(new ScriptFileSystemList(minitScript));
897  }
898 }
minitscript::utilities::ExceptionBase _ExceptionBase
minitscript::utilities::Exception _Exception
#define MINITSCRIPT_METHODUSAGE_COMPLAIN(methodName)
Definition: MinitScript.h:59
MinitScript script file system methods.
void pushArrayEntry(const Variable &value)
Push entry to array.
Definition: MinitScript.h:1892
void setType(VariableType newType)
Set type.
Definition: MinitScript.h:1253
void setValue(const Variable &variable)
Set value from given variable into variable.
Definition: MinitScript.h:1618
void registerMethod(Method *method)
Register method.
static const string removeFileExtension(const string &fileName)
Remove file extension, e.g.
Definition: FileSystem.cpp:291
static bool isDrive(const string &uri)
Check if file is a drive (applies to Microsoft Windows only)
Definition: FileSystem.cpp:97
static void setContentFromString(const string &pathName, const string &fileName, const string &content)
Set content from string.
Definition: FileSystem.cpp:132
static const string composeURI(const string &pathName, const string &fileName)
Compose URI from path name and file name.
Definition: FileSystem.cpp:36
static const string getPathName(const string &uri)
Get path name.
Definition: FileSystem.cpp:277
static const string getCanonicalURI(const string &pathName, const string &fileName)
Get canonical URI from given path name and file name.
Definition: FileSystem.cpp:195
static void createPath(const string &pathName)
Create path.
Definition: FileSystem.cpp:300
static void list(const string &pathName, vector< string > &files, FileNameFilter *filter=nullptr, bool addDrives=false)
List files for given path and filter by a file name filter if not null.
Definition: FileSystem.cpp:40
static void changePath(const string &pathName)
Change path.
Definition: FileSystem.cpp:269
static void removePath(const string &pathName, bool recursive)
Remove path.
Definition: FileSystem.cpp:310
static void setContent(const string &pathName, const string &fileName, const vector< uint8_t > &content)
Set file content.
Definition: FileSystem.cpp:156
static void getContent(const string &pathName, const string &fileName, vector< uint8_t > &content)
Get file content.
Definition: FileSystem.cpp:142
static void setContentFromStringArray(const string &pathName, const string &fileName, const vector< string > &content)
Set file content as string array.
Definition: FileSystem.cpp:180
static void removeFile(const string &pathName, const string &fileName)
Remove file.
Definition: FileSystem.cpp:326
static uint64_t getFileSize(const string &pathName, const string &fileName)
Return file size of given file.
Definition: FileSystem.cpp:111
static void getContentAsStringArray(const string &pathName, const string &fileName, vector< string > &content)
Get file content as string array.
Definition: FileSystem.cpp:165
static const string getFileName(const string &uri)
Get file name.
Definition: FileSystem.cpp:284
static bool isPath(const string &uri)
Check if file is a path.
Definition: FileSystem.cpp:86
static bool exists(const string &uri)
Check if file exists.
Definition: FileSystem.cpp:101
static const string getCurrentWorkingPathName()
Get current working path name.
Definition: FileSystem.cpp:256
static const string getContentAsString(const string &pathName, const string &fileName)
Get content as string.
Definition: FileSystem.cpp:121
static void rename(const string &fileNameFrom, const string &fileNameTo)
Rename file.
Definition: FileSystem.cpp:336
static void printLine()
Print newline to console.
Definition: Console.cpp:66
std::exception Exception
Exception base class.
Definition: Exception.h:18