SkOSFile.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. // TODO: add unittests for all these operations
  8. #ifndef SkOSFile_DEFINED
  9. #define SkOSFile_DEFINED
  10. #include <stdio.h>
  11. #include "include/core/SkString.h"
  12. enum SkFILE_Flags {
  13. kRead_SkFILE_Flag = 0x01,
  14. kWrite_SkFILE_Flag = 0x02
  15. };
  16. FILE* sk_fopen(const char path[], SkFILE_Flags);
  17. void sk_fclose(FILE*);
  18. size_t sk_fgetsize(FILE*);
  19. size_t sk_fwrite(const void* buffer, size_t byteCount, FILE*);
  20. void sk_fflush(FILE*);
  21. void sk_fsync(FILE*);
  22. size_t sk_ftell(FILE*);
  23. /** Maps a file into memory. Returns the address and length on success, NULL otherwise.
  24. * The mapping is read only.
  25. * When finished with the mapping, free the returned pointer with sk_fmunmap.
  26. */
  27. void* sk_fmmap(FILE* f, size_t* length);
  28. /** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise.
  29. * The mapping is read only.
  30. * When finished with the mapping, free the returned pointer with sk_fmunmap.
  31. */
  32. void* sk_fdmmap(int fd, size_t* length);
  33. /** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap.
  34. * The length parameter must be the same as returned from sk_fmmap.
  35. */
  36. void sk_fmunmap(const void* addr, size_t length);
  37. /** Returns true if the two point at the exact same filesystem object. */
  38. bool sk_fidentical(FILE* a, FILE* b);
  39. /** Returns the underlying file descriptor for the given file.
  40. * The return value will be < 0 on failure.
  41. */
  42. int sk_fileno(FILE* f);
  43. /** Returns true if something (file, directory, ???) exists at this path,
  44. * and has the specified access flags.
  45. */
  46. bool sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0);
  47. // Returns true if a directory exists at this path.
  48. bool sk_isdir(const char *path);
  49. // Like pread, but may affect the file position marker.
  50. // Returns the number of bytes read or SIZE_MAX if failed.
  51. size_t sk_qread(FILE*, void* buffer, size_t count, size_t offset);
  52. // Create a new directory at this path; returns true if successful.
  53. // If the directory already existed, this will return true.
  54. // Description of the error, if any, will be written to stderr.
  55. bool sk_mkdir(const char* path);
  56. class SkOSFile {
  57. public:
  58. class Iter {
  59. public:
  60. Iter();
  61. Iter(const char path[], const char suffix[] = nullptr);
  62. ~Iter();
  63. void reset(const char path[], const char suffix[] = nullptr);
  64. /** If getDir is true, only returns directories.
  65. Results are undefined if true and false calls are
  66. interleaved on a single iterator.
  67. */
  68. bool next(SkString* name, bool getDir = false);
  69. static const size_t kStorageSize = 40;
  70. private:
  71. SkAlignedSStorage<kStorageSize> fSelf;
  72. };
  73. };
  74. #endif