• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/result.h>
24 
25 #include "code_writer.h"
26 #include "line_reader.h"
27 
28 namespace android {
29 namespace aidl {
30 
31 class IoDelegate {
32  public:
33   IoDelegate() = default;
34   virtual ~IoDelegate() = default;
35 
36   IoDelegate(const IoDelegate&) = delete;
37   IoDelegate(IoDelegate&&) = delete;
38   IoDelegate& operator=(const IoDelegate&) = delete;
39   IoDelegate& operator=(IoDelegate&&) = delete;
40 
41   // Stores an absolute version of |path| to |*absolute_path|,
42   // possibly prefixing it with the current working directory.
43   // Returns false and does not set |*absolute_path| on error.
44   static bool GetAbsolutePath(const std::string& path,
45                               std::string* absolute_path);
46 
47   // Returns a unique_ptr to the contents of |filename|.
48   // Will append the optional |content_suffix| to the returned contents.
49   virtual std::unique_ptr<std::string> GetFileContents(
50       const std::string& filename,
51       const std::string& content_suffix = "") const;
52 
53   virtual std::unique_ptr<LineReader> GetLineReader(
54       const std::string& file_path) const;
55 
56   virtual bool FileIsReadable(const std::string& path) const;
57 
58   virtual std::unique_ptr<CodeWriter> GetCodeWriter(
59       const std::string& file_path) const;
60 
61   virtual void RemovePath(const std::string& file_path) const;
62 
63   virtual android::base::Result<std::vector<std::string>> ListFiles(const std::string& dir) const;
64 
65  private:
66   // Create the directory when path is a dir or the parent directory when
67   // path is a file. Path is a dir if it ends with the path separator.
68   bool CreateDirForPath(const std::string& path) const;
69 };  // class IoDelegate
70 
71 }  // namespace aidl
72 }  // namespace android
73