io_delegate.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2015, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <android-base/macros.h>
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "code_writer.h"
  22. #include "line_reader.h"
  23. namespace android {
  24. namespace aidl {
  25. class IoDelegate {
  26. public:
  27. IoDelegate() = default;
  28. virtual ~IoDelegate() = default;
  29. // Stores an absolute version of |path| to |*absolute_path|,
  30. // possibly prefixing it with the current working directory.
  31. // Returns false and does not set |*absolute_path| on error.
  32. static bool GetAbsolutePath(const std::string& path,
  33. std::string* absolute_path);
  34. // Returns a unique_ptr to the contents of |filename|.
  35. // Will append the optional |content_suffix| to the returned contents.
  36. virtual std::unique_ptr<std::string> GetFileContents(
  37. const std::string& filename,
  38. const std::string& content_suffix = "") const;
  39. virtual std::unique_ptr<LineReader> GetLineReader(
  40. const std::string& file_path) const;
  41. virtual bool FileIsReadable(const std::string& path) const;
  42. virtual std::unique_ptr<CodeWriter> GetCodeWriter(
  43. const std::string& file_path) const;
  44. virtual void RemovePath(const std::string& file_path) const;
  45. virtual std::vector<std::string> ListFiles(const std::string& dir) const;
  46. private:
  47. // Create the directory when path is a dir or the parent directory when
  48. // path is a file. Path is a dir if it ends with the path separator.
  49. bool CreateDirForPath(const std::string& path) const;
  50. DISALLOW_COPY_AND_ASSIGN(IoDelegate);
  51. }; // class IoDelegate
  52. } // namespace android
  53. } // namespace aidl