OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/ftp/ftp_directory_listing_parser_windows.h" | 5 #include "net/ftp/ftp_directory_listing_parser_windows.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/time.h" |
| 13 #include "net/ftp/ftp_directory_listing_parser.h" |
12 #include "net/ftp/ftp_util.h" | 14 #include "net/ftp/ftp_util.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 bool WindowsDateListingToTime(const std::vector<string16>& columns, | 18 bool WindowsDateListingToTime(const std::vector<string16>& columns, |
17 base::Time* time) { | 19 base::Time* time) { |
18 DCHECK_LE(3U, columns.size()); | 20 DCHECK_LE(3U, columns.size()); |
19 | 21 |
20 base::Time::Exploded time_exploded = { 0 }; | 22 base::Time::Exploded time_exploded = { 0 }; |
21 | 23 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 67 |
66 // We don't know the time zone of the server, so just use local time. | 68 // We don't know the time zone of the server, so just use local time. |
67 *time = base::Time::FromLocalExploded(time_exploded); | 69 *time = base::Time::FromLocalExploded(time_exploded); |
68 return true; | 70 return true; |
69 } | 71 } |
70 | 72 |
71 } // namespace | 73 } // namespace |
72 | 74 |
73 namespace net { | 75 namespace net { |
74 | 76 |
75 FtpDirectoryListingParserWindows::FtpDirectoryListingParserWindows() {} | 77 bool ParseFtpDirectoryListingWindows( |
| 78 const std::vector<string16>& lines, |
| 79 std::vector<FtpDirectoryListingEntry>* entries) { |
| 80 for (size_t i = 0; i < lines.size(); i++) { |
| 81 if (lines[i].empty()) |
| 82 continue; |
76 | 83 |
77 FtpDirectoryListingParserWindows::~FtpDirectoryListingParserWindows() {} | 84 std::vector<string16> columns; |
| 85 base::SplitString(CollapseWhitespace(lines[i], false), ' ', &columns); |
78 | 86 |
79 FtpServerType FtpDirectoryListingParserWindows::GetServerType() const { | 87 // Every line of the listing consists of the following: |
80 return SERVER_WINDOWS; | 88 // |
81 } | 89 // 1. date |
| 90 // 2. time |
| 91 // 3. size in bytes (or "<DIR>" for directories) |
| 92 // 4. filename (may be empty or contain spaces) |
| 93 // |
| 94 // For now, make sure we have 1-3, and handle 4 later. |
| 95 if (columns.size() < 3) |
| 96 return false; |
82 | 97 |
83 bool FtpDirectoryListingParserWindows::ConsumeLine(const string16& line) { | 98 FtpDirectoryListingEntry entry; |
84 std::vector<string16> columns; | 99 if (EqualsASCII(columns[2], "<DIR>")) { |
85 base::SplitString(CollapseWhitespace(line, false), ' ', &columns); | 100 entry.type = FtpDirectoryListingEntry::DIRECTORY; |
| 101 entry.size = -1; |
| 102 } else { |
| 103 entry.type = FtpDirectoryListingEntry::FILE; |
| 104 if (!base::StringToInt64(columns[2], &entry.size)) |
| 105 return false; |
| 106 if (entry.size < 0) |
| 107 return false; |
| 108 } |
86 | 109 |
87 // Every line of the listing consists of the following: | 110 if (!WindowsDateListingToTime(columns, &entry.last_modified)) |
88 // | 111 return false; |
89 // 1. date | |
90 // 2. time | |
91 // 3. size in bytes (or "<DIR>" for directories) | |
92 // 4. filename (may be empty or contain spaces) | |
93 // | |
94 // For now, make sure we have 1-3, and handle 4 later. | |
95 if (columns.size() < 3) | |
96 return false; | |
97 | 112 |
98 FtpDirectoryListingEntry entry; | 113 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 3); |
99 if (EqualsASCII(columns[2], "<DIR>")) { | 114 if (entry.name.empty()) { |
100 entry.type = FtpDirectoryListingEntry::DIRECTORY; | 115 // Some FTP servers send listing entries with empty names. |
101 entry.size = -1; | 116 // It's not obvious how to display such an entry, so ignore them. |
102 } else { | 117 // We don't want to make the parsing fail at this point though. |
103 entry.type = FtpDirectoryListingEntry::FILE; | 118 // Other entries can still be useful. |
104 if (!base::StringToInt64(columns[2], &entry.size)) | 119 continue; |
105 return false; | 120 } |
106 if (entry.size < 0) | 121 |
107 return false; | 122 entries->push_back(entry); |
108 } | 123 } |
109 | 124 |
110 if (!WindowsDateListingToTime(columns, &entry.last_modified)) | |
111 return false; | |
112 | |
113 entry.name = FtpUtil::GetStringPartAfterColumns(line, 3); | |
114 if (entry.name.empty()) { | |
115 // Some FTP servers send listing entries with empty names. It's not obvious | |
116 // how to display such an entry, so we ignore them. We don't want to make | |
117 // the parsing fail at this point though. Other entries can still be useful. | |
118 return true; | |
119 } | |
120 | |
121 entries_.push(entry); | |
122 return true; | 125 return true; |
123 } | 126 } |
124 | 127 |
125 bool FtpDirectoryListingParserWindows::OnEndOfInput() { | |
126 return true; | |
127 } | |
128 | |
129 bool FtpDirectoryListingParserWindows::EntryAvailable() const { | |
130 return !entries_.empty(); | |
131 } | |
132 | |
133 FtpDirectoryListingEntry FtpDirectoryListingParserWindows::PopEntry() { | |
134 FtpDirectoryListingEntry entry = entries_.front(); | |
135 entries_.pop(); | |
136 return entry; | |
137 } | |
138 | |
139 } // namespace net | 128 } // namespace net |
OLD | NEW |