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.
4
5 #include "net/ftp/ftp_directory_listing_parser_windows.h"
6
7 #include <vector>
8
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/time/time.h"
13 #include "net/ftp/ftp_directory_listing_parser.h"
14 #include "net/ftp/ftp_util.h"
15
16 namespace net {
17
ParseFtpDirectoryListingWindows(const std::vector<base::string16> & lines,std::vector<FtpDirectoryListingEntry> * entries)18 bool ParseFtpDirectoryListingWindows(
19 const std::vector<base::string16>& lines,
20 std::vector<FtpDirectoryListingEntry>* entries) {
21 for (size_t i = 0; i < lines.size(); i++) {
22 if (lines[i].empty())
23 continue;
24
25 std::vector<base::string16> columns;
26 base::SplitString(base::CollapseWhitespace(lines[i], false), ' ', &columns);
27
28 // Every line of the listing consists of the following:
29 //
30 // 1. date
31 // 2. time
32 // 3. size in bytes (or "<DIR>" for directories)
33 // 4. filename (may be empty or contain spaces)
34 //
35 // For now, make sure we have 1-3, and handle 4 later.
36 if (columns.size() < 3)
37 return false;
38
39 FtpDirectoryListingEntry entry;
40 if (EqualsASCII(columns[2], "<DIR>")) {
41 entry.type = FtpDirectoryListingEntry::DIRECTORY;
42 entry.size = -1;
43 } else {
44 entry.type = FtpDirectoryListingEntry::FILE;
45 if (!base::StringToInt64(columns[2], &entry.size))
46 return false;
47 if (entry.size < 0)
48 return false;
49 }
50
51 if (!FtpUtil::WindowsDateListingToTime(columns[0],
52 columns[1],
53 &entry.last_modified)) {
54 return false;
55 }
56
57 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 3);
58 if (entry.name.empty()) {
59 // Some FTP servers send listing entries with empty names.
60 // It's not obvious how to display such an entry, so ignore them.
61 // We don't want to make the parsing fail at this point though.
62 // Other entries can still be useful.
63 continue;
64 }
65
66 entries->push_back(entry);
67 }
68
69 return true;
70 }
71
72 } // namespace net
73