validators.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <furi.h>
  2. #include "validators.h"
  3. #include "applications/storage/storage.h"
  4. struct ValidatorIsFile {
  5. char* app_path_folder;
  6. const char* app_extension;
  7. char* current_name;
  8. };
  9. bool validator_is_file_callback(const char* text, string_t error, void* context) {
  10. furi_assert(context);
  11. ValidatorIsFile* instance = context;
  12. if(instance->current_name != NULL) {
  13. if(strcmp(instance->current_name, text) == 0) {
  14. return true;
  15. }
  16. }
  17. bool ret = true;
  18. string_t path;
  19. string_init_printf(path, "%s/%s%s", instance->app_path_folder, text, instance->app_extension);
  20. Storage* storage = furi_record_open("storage");
  21. if(storage_common_stat(storage, string_get_cstr(path), NULL) == FSE_OK) {
  22. ret = false;
  23. string_printf(error, "This name\nexists!\nChoose\nanother one.");
  24. } else {
  25. ret = true;
  26. }
  27. string_clear(path);
  28. furi_record_close("storage");
  29. return ret;
  30. }
  31. ValidatorIsFile* validator_is_file_alloc_init(
  32. const char* app_path_folder,
  33. const char* app_extension,
  34. const char* current_name) {
  35. ValidatorIsFile* instance = malloc(sizeof(ValidatorIsFile));
  36. instance->app_path_folder = strdup(app_path_folder);
  37. instance->app_extension = app_extension;
  38. instance->current_name = strdup(current_name);
  39. return instance;
  40. }
  41. void validator_is_file_free(ValidatorIsFile* instance) {
  42. furi_assert(instance);
  43. free(instance->app_path_folder);
  44. free(instance->current_name);
  45. free(instance);
  46. }