mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
We currently use a mix of C standard includes (e.g. limits.h) and their C++ variants (e.g. climits). This migrates to a consistent style for all cases where the C++ variants are acceptable, but leaves the C equivalents in place where they are required, such as in the embedder API and other headers that may be used from C.
80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "files.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include "inlines.h"
|
|
#include "logging.h"
|
|
|
|
namespace dart_utils {
|
|
|
|
namespace {
|
|
|
|
bool ReadFileDescriptor(int fd, std::string* result) {
|
|
DEBUG_CHECK(result, LOG_TAG, "");
|
|
result->clear();
|
|
|
|
if (fd < 0) {
|
|
return false;
|
|
}
|
|
|
|
constexpr size_t kBufferSize = 1 << 16;
|
|
size_t offset = 0;
|
|
ssize_t bytes_read = 0;
|
|
do {
|
|
offset += bytes_read;
|
|
result->resize(offset + kBufferSize);
|
|
bytes_read = read(fd, &(*result)[offset], kBufferSize);
|
|
} while (bytes_read > 0);
|
|
|
|
if (bytes_read < 0) {
|
|
result->clear();
|
|
return false;
|
|
}
|
|
|
|
result->resize(offset + bytes_read);
|
|
return true;
|
|
}
|
|
|
|
bool WriteFileDescriptor(int fd, const char* data, ssize_t size) {
|
|
ssize_t total = 0;
|
|
for (ssize_t partial = 0; total < size; total += partial) {
|
|
partial = write(fd, data + total, size - total);
|
|
if (partial < 0)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool ReadFileToString(const std::string& path, std::string* result) {
|
|
return ReadFileToStringAt(AT_FDCWD, path, result);
|
|
}
|
|
|
|
bool ReadFileToStringAt(int dirfd,
|
|
const std::string& path,
|
|
std::string* result) {
|
|
int fd = openat(dirfd, path.c_str(), O_RDONLY);
|
|
bool status = ReadFileDescriptor(fd, result);
|
|
close(fd);
|
|
return status;
|
|
}
|
|
|
|
bool WriteFile(const std::string& path, const char* data, ssize_t size) {
|
|
int fd = openat(AT_FDCWD, path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
|
if (fd < 0) {
|
|
return false;
|
|
}
|
|
bool status = WriteFileDescriptor(fd, data, size);
|
|
close(fd);
|
|
return status;
|
|
}
|
|
|
|
} // namespace dart_utils
|