validators.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <furi.h>
  2. #include "validators.h"
  3. #include <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, FuriString* 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. FuriString* path = furi_string_alloc_printf(
  18. "%s/%s%s", instance->app_path_folder, text, instance->app_extension);
  19. Storage* storage = furi_record_open(RECORD_STORAGE);
  20. const bool ret = storage_common_stat(storage, furi_string_get_cstr(path), NULL) != FSE_OK;
  21. if(!ret) {
  22. furi_string_printf(error, "This name\nexists!\nChoose\nanother one.");
  23. }
  24. furi_string_free(path);
  25. furi_record_close(RECORD_STORAGE);
  26. return ret;
  27. }
  28. ValidatorIsFile* validator_is_file_alloc_init(
  29. const char* app_path_folder,
  30. const char* app_extension,
  31. const char* current_name) {
  32. ValidatorIsFile* instance = malloc(sizeof(ValidatorIsFile));
  33. instance->app_path_folder = strdup(app_path_folder);
  34. instance->app_extension = app_extension;
  35. if(current_name != NULL) {
  36. instance->current_name = strdup(current_name);
  37. }
  38. return instance;
  39. }
  40. void validator_is_file_free(ValidatorIsFile* instance) {
  41. furi_assert(instance);
  42. free(instance->app_path_folder);
  43. free(instance->current_name);
  44. free(instance);
  45. }