uart_validators.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <furi.h>
  2. #include "uart_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. bool ret = true;
  18. FuriString* path = furi_string_alloc_printf(
  19. "%s/%s%s", instance->app_path_folder, text, instance->app_extension);
  20. Storage* storage = furi_record_open(RECORD_STORAGE);
  21. if(storage_common_stat(storage, furi_string_get_cstr(path), NULL) == FSE_OK) {
  22. ret = false;
  23. furi_string_printf(error, "This name\nexists!\nChoose\nanother one.");
  24. } else {
  25. ret = true;
  26. }
  27. furi_string_free(path);
  28. furi_record_close(RECORD_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. if(current_name != NULL) {
  39. instance->current_name = strdup(current_name);
  40. }
  41. return instance;
  42. }
  43. void validator_is_file_free(ValidatorIsFile* instance) {
  44. furi_assert(instance);
  45. free(instance->app_path_folder);
  46. free(instance->current_name);
  47. free(instance);
  48. }