fileinfo.c 825 B

12345678910111213141516171819202122232425262728293031
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <libgen.h>
  4. #include "fileinfo.h"
  5. struct fileInfo* newFileInfo(const char *filePath) {
  6. if (!strrchr(filePath, '.')) return NULL;
  7. struct fileInfo *info = malloc(sizeof(struct fileInfo));
  8. size_t filePathLength = strlen(filePath);
  9. info->fullname = calloc(filePathLength+1, 1);
  10. strncpy(info->fullname, filePath, filePathLength);
  11. size_t basenameLength = strlen(basename(info->fullname));
  12. info->_base = calloc(basenameLength+1, 1);
  13. strncpy(info->_base, basename(info->fullname), basenameLength);
  14. char *dot = ".";
  15. info->name = strtok(info->_base, dot);
  16. info->ext = strtok(NULL, dot);
  17. if (info->name && info->ext)
  18. return info;
  19. freeFileInfo(info);
  20. return NULL;
  21. }
  22. void freeFileInfo(struct fileInfo *info) {
  23. free(info->fullname);
  24. free(info->_base);
  25. free(info);
  26. }