A collection of basic/generally desirable code I use across multiple C++ projects.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.8 KiB

1 year ago
  1. #ifndef FILE_H
  2. #define FILE_H
  3. #include <stdio.h> // FILE
  4. #include <sys/types.h> // time_t
  5. #include "array.hpp"
  6. namespace File {
  7. FILE* Open(const char* path, const char* mode = "rb");
  8. FILE* Open(const char* path, size_t* outSize, const char* mode = "rb");
  9. void Close(FILE* file);
  10. size_t Size(const char* path);
  11. size_t Size(FILE* fp);
  12. // most commonly this is what you want - provide a filesystem path, read the whole file,
  13. // and return a buffer with the file's contents.
  14. // the file is opened and closed internally.
  15. // the buffer you get returned is allocated internally and must be freed by the caller.
  16. u8* Read(const char* path);
  17. // if you also want to know the size of the file.
  18. u8* Read(const char* path, size_t* outSize);
  19. // for apis that want a more incremental approach, this function will read |file|'s contents into |destination|,
  20. // which must be pre-allocated. the provided file must be closed by the caller.
  21. // returns the number of bytes read.
  22. size_t Read(FILE* fp, void* destination); // calls fseek to determine file size
  23. size_t Read(FILE* fp, void* destination, size_t size); // you know and provide the file size
  24. s32 Write(const char* path, char* data, u32 count);
  25. // writes the filenames into the provided array |outFileNames|, must be allocated ahead of time.
  26. void GetFileNamesInFolder(const char* path, Array<char*>* outFileNames);
  27. // get the last-modified timestamp on a file located at 'path'.
  28. // there's no caching done, so it's the real, current value.
  29. // specifically, it's the field 'st_mtime' on the 'stat' struct.
  30. time_t LastModified(const char* path);
  31. s32 Rename(const char* oldFilename, const char* newFilename);
  32. s32 Remove(const char* path);
  33. }
  34. #endif