X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FPath.cpp;h=4d179218f78c7781d7b5cfc0a4be6abb306ff283;hb=049ffbbdf2a43d5529cb56b6bb696d20d28ff217;hp=15edf0ddbbd8a4886cef70244aec7b2f8fddcdd7;hpb=d8b23109b26f2070e21d5515ed2472ca9ab0111c;p=oota-llvm.git diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 15edf0ddbbd..4d179218f78 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -308,7 +308,30 @@ const_iterator &const_iterator::operator++() { return *this; } -const_iterator &const_iterator::operator--() { +bool const_iterator::operator==(const const_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Position == RHS.Position; +} + +ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { + return Position - RHS.Position; +} + +reverse_iterator rbegin(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Position = Path.size(); + return ++I; +} + +reverse_iterator rend(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Component = Path.substr(0, 0); + I.Position = 0; + return I; +} + +reverse_iterator &reverse_iterator::operator++() { // If we're at the end and the previous char was a '/', return '.' unless // we are the root path. size_t root_dir_pos = root_dir_start(Path); @@ -335,19 +358,11 @@ const_iterator &const_iterator::operator--() { return *this; } -bool const_iterator::operator==(const const_iterator &RHS) const { - return Path.begin() == RHS.Path.begin() && +bool reverse_iterator::operator==(const reverse_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Component == RHS.Component && Position == RHS.Position; } -bool const_iterator::operator!=(const const_iterator &RHS) const { - return !(*this == RHS); -} - -ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { - return Position - RHS.Position; -} - const StringRef root_path(StringRef path) { const_iterator b = begin(path), pos = b, @@ -525,14 +540,24 @@ void native(const Twine &path, SmallVectorImpl &result) { native(result); } -void native(SmallVectorImpl &path) { +void native(SmallVectorImpl &Path) { #ifdef LLVM_ON_WIN32 - std::replace(path.begin(), path.end(), '/', '\\'); + std::replace(Path.begin(), Path.end(), '/', '\\'); +#else + for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) { + if (*PI == '\\') { + auto PN = PI + 1; + if (PN < PE && *PN == '\\') + ++PI; // increment once, the for loop will move over the escaped slash + else + *PI = '/'; + } + } #endif } const StringRef filename(StringRef path) { - return *(--end(path)); + return *rbegin(path); } const StringRef stem(StringRef path) { @@ -846,6 +871,40 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting) { return create_directory(P, IgnoreExisting); } +std::error_code copy_file(const Twine &From, const Twine &To) { + int ReadFD, WriteFD; + if (std::error_code EC = openFileForRead(From, ReadFD)) + return EC; + if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) { + close(ReadFD); + return EC; + } + + const size_t BufSize = 4096; + char *Buf = new char[BufSize]; + int BytesRead = 0, BytesWritten = 0; + for (;;) { + BytesRead = read(ReadFD, Buf, BufSize); + if (BytesRead <= 0) + break; + while (BytesRead) { + BytesWritten = write(WriteFD, Buf, BytesRead); + if (BytesWritten < 0) + break; + BytesRead -= BytesWritten; + } + if (BytesWritten < 0) + break; + } + close(ReadFD); + close(WriteFD); + delete[] Buf; + + if (BytesRead < 0 || BytesWritten < 0) + return std::error_code(errno, std::generic_category()); + return std::error_code(); +} + bool exists(file_status status) { return status_known(status) && status.type() != file_type::file_not_found; } @@ -893,7 +952,7 @@ void directory_entry::replace_filename(const Twine &filename, file_status st) { } /// @brief Identify the magic in magic. - file_magic identify_magic(StringRef Magic) { +file_magic identify_magic(StringRef Magic) { if (Magic.size() < 4) return file_magic::unknown; switch ((unsigned char)Magic[0]) { @@ -1031,7 +1090,7 @@ std::error_code identify_magic(const Twine &Path, file_magic &Result) { char Buffer[32]; int Length = read(FD, Buffer, sizeof(Buffer)); - if (Length < 0) + if (close(FD) != 0 || Length < 0) return std::error_code(errno, std::generic_category()); Result = identify_magic(StringRef(Buffer, Length));