blob: a53bb8f33434bcde69e7df6654dfdae585bf95f7 [file] [log] [blame]
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001/*
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
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wiley4a2884b2015-10-07 11:27:45 -070018
Elliott Hughes0a620672015-12-04 13:53:18 -080019#include <android-base/macros.h>
Christopher Wiley4a2884b2015-10-07 11:27:45 -070020
21#include <memory>
22#include <string>
Christopher Wiley054afbd2015-10-16 17:08:43 -070023#include <vector>
24
25#include "code_writer.h"
Christopher Wileyef140932015-11-03 09:29:19 -080026#include "line_reader.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070027
28namespace android {
29namespace aidl {
30
31class IoDelegate {
32 public:
33 IoDelegate() = default;
34 virtual ~IoDelegate() = default;
35
Christopher Wiley8decf952016-06-02 16:27:26 -070036 // Stores an absolute version of |path| to |*absolute_path|,
37 // possibly prefixing it with the current working directory.
38 // Returns false and does not set |*absolute_path| on error.
39 static bool GetAbsolutePath(const std::string& path,
40 std::string* absolute_path);
41
Christopher Wiley4a2884b2015-10-07 11:27:45 -070042 // Returns a unique_ptr to the contents of |filename|.
43 // Will append the optional |content_suffix| to the returned contents.
44 virtual std::unique_ptr<std::string> GetFileContents(
45 const std::string& filename,
46 const std::string& content_suffix = "") const;
47
Christopher Wileyef140932015-11-03 09:29:19 -080048 virtual std::unique_ptr<LineReader> GetLineReader(
49 const std::string& file_path) const;
50
Christopher Wiley72877ac2015-10-06 14:41:42 -070051 virtual bool FileIsReadable(const std::string& path) const;
52
Christopher Wiley054afbd2015-10-16 17:08:43 -070053 virtual std::unique_ptr<CodeWriter> GetCodeWriter(
54 const std::string& file_path) const;
55
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080056 virtual void RemovePath(const std::string& file_path) const;
57
Jiyong Parke59c3682018-09-11 23:10:25 +090058 virtual std::vector<std::string> ListFiles(const std::string& dir) const;
59
Christopher Wiley4a2884b2015-10-07 11:27:45 -070060 private:
Jiyong Park05463732018-08-09 16:03:02 +090061 // Create the directory when path is a dir or the parent directory when
62 // path is a file. Path is a dir if it ends with the path separator.
63 bool CreateDirForPath(const std::string& path) const;
64
Christopher Wiley4a2884b2015-10-07 11:27:45 -070065 DISALLOW_COPY_AND_ASSIGN(IoDelegate);
66}; // class IoDelegate
67
Christopher Wiley4a2884b2015-10-07 11:27:45 -070068} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -070069} // namespace android