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.

53 lines
1.9 KiB

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