Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / ftp / ftp_directory_listing_parser_os2.cc
blob7d0ee62b485f757bc3b52bbd7ad0cff864cdf3b1
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/ftp/ftp_directory_listing_parser_os2.h"
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "net/ftp/ftp_directory_listing_parser.h"
15 #include "net/ftp/ftp_util.h"
17 namespace net {
19 bool ParseFtpDirectoryListingOS2(
20 const std::vector<base::string16>& lines,
21 std::vector<FtpDirectoryListingEntry>* entries) {
22 for (size_t i = 0; i < lines.size(); i++) {
23 if (lines[i].empty())
24 continue;
26 std::vector<base::string16> columns = base::SplitString(
27 base::CollapseWhitespace(lines[i], false), base::ASCIIToUTF16(" "),
28 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
30 // Every line of the listing consists of the following:
32 // 1. size in bytes (0 for directories)
33 // 2. type (A for files, DIR for directories)
34 // 3. date
35 // 4. time
36 // 5. filename (may be empty or contain spaces)
38 // For now, make sure we have 1-4, and handle 5 later.
39 if (columns.size() < 4)
40 return false;
42 FtpDirectoryListingEntry entry;
43 if (!base::StringToInt64(columns[0], &entry.size))
44 return false;
45 if (base::EqualsASCII(columns[1], "DIR")) {
46 if (entry.size != 0)
47 return false;
48 entry.type = FtpDirectoryListingEntry::DIRECTORY;
49 entry.size = -1;
50 } else if (base::EqualsASCII(columns[1], "A")) {
51 entry.type = FtpDirectoryListingEntry::FILE;
52 if (entry.size < 0)
53 return false;
54 } else {
55 return false;
58 if (!FtpUtil::WindowsDateListingToTime(columns[2],
59 columns[3],
60 &entry.last_modified)) {
61 return false;
64 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 4);
65 if (entry.name.empty()) {
66 // Some FTP servers send listing entries with empty names.
67 // It's not obvious how to display such an entry, so ignore them.
68 // We don't want to make the parsing fail at this point though.
69 // Other entries can still be useful.
70 continue;
73 entries->push_back(entry);
76 return true;
79 } // namespace net