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.

50 lines
1.8 KiB

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