fileinfo.c 703 B

1234567891011121314151617181920212223242526272829
  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. info->fullname = malloc(strlen(filePath)+1);
  9. strcpy(info->fullname, filePath);
  10. info->_base = malloc(strlen(basename(info->fullname))+1);
  11. strcpy(info->_base, basename(info->fullname));
  12. char *dot = ".";
  13. info->name = strtok(info->_base, dot);
  14. info->ext = strtok(NULL, dot);
  15. if (info->name && info->ext)
  16. return info;
  17. freeFileInfo(info);
  18. return NULL;
  19. }
  20. void freeFileInfo(struct fileInfo *info) {
  21. free(info->fullname);
  22. free(info->_base);
  23. free(info);
  24. }