sequential_file.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "sequential_file.h"
  2. char* sequential_file_resolve_path(
  3. Storage* storage,
  4. const char* dir,
  5. const char* prefix,
  6. const char* extension) {
  7. if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  8. return NULL;
  9. }
  10. char file_path[256];
  11. int file_index = 0;
  12. do {
  13. if(snprintf(
  14. file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, file_index, extension) <
  15. 0) {
  16. return NULL;
  17. }
  18. file_index++;
  19. } while(storage_file_exists(storage, file_path));
  20. return strdup(file_path);
  21. }
  22. bool sequential_file_open(
  23. Storage* storage,
  24. File* file,
  25. const char* dir,
  26. const char* prefix,
  27. const char* extension) {
  28. if(storage == NULL || file == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  29. return false;
  30. }
  31. char* file_path = sequential_file_resolve_path(storage, dir, prefix, extension);
  32. if(file_path == NULL) {
  33. return false;
  34. }
  35. bool success = storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  36. free(file_path);
  37. return success;
  38. }