Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: net/ftp/ftp_directory_listing_parser_windows.cc

Issue 465035: Split FTP LIST parsing code into individual files for each listing style. (Closed)
Patch Set: fix Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file.
4
5 #include "net/ftp/ftp_directory_listing_parser_windows.h"
6
7 #include <vector>
8
9 #include "base/string_util.h"
10
11 namespace {
12
13 bool WindowsDateListingToTime(const std::vector<string16>& columns,
14 base::Time* time) {
15 DCHECK_EQ(4U, columns.size());
16
17 base::Time::Exploded time_exploded = { 0 };
18
19 // Date should be in format MM-DD-YY[YY].
20 std::vector<string16> date_parts;
21 SplitString(columns[0], '-', &date_parts);
22 if (date_parts.size() != 3)
23 return false;
24 if (!StringToInt(date_parts[0], &time_exploded.month))
25 return false;
26 if (!StringToInt(date_parts[1], &time_exploded.day_of_month))
27 return false;
28 if (!StringToInt(date_parts[2], &time_exploded.year))
29 return false;
30 if (time_exploded.year < 0)
31 return false;
32 // If year has only two digits then assume that 00-79 is 2000-2079,
33 // and 80-99 is 1980-1999.
34 if (time_exploded.year < 80)
35 time_exploded.year += 2000;
36 else if (time_exploded.year < 100)
37 time_exploded.year += 1900;
38
39 // Time should be in format HH:MM(AM|PM)
40 if (columns[1].length() != 7)
41 return false;
42 std::vector<string16> time_parts;
43 SplitString(columns[1].substr(0, 5), ':', &time_parts);
44 if (time_parts.size() != 2)
45 return false;
46 if (!StringToInt(time_parts[0], &time_exploded.hour))
47 return false;
48 if (!StringToInt(time_parts[1], &time_exploded.minute))
49 return false;
50 string16 am_or_pm(columns[1].substr(5, 2));
51 if (EqualsASCII(am_or_pm, "PM"))
52 time_exploded.hour += 12;
53 else if (!EqualsASCII(am_or_pm, "AM"))
54 return false;
55
56 // We don't know the time zone of the server, so just use local time.
57 *time = base::Time::FromLocalExploded(time_exploded);
58 return true;
59 }
60
61 } // namespace
62
63 namespace net {
64
65 FtpDirectoryListingParserWindows::FtpDirectoryListingParserWindows() {
66 }
67
68 bool FtpDirectoryListingParserWindows::ConsumeLine(const string16& line) {
69 std::vector<string16> columns;
70 SplitString(CollapseWhitespace(line, false), ' ', &columns);
71 if (columns.size() != 4)
72 return false;
73
74 FtpDirectoryListingEntry entry;
75 entry.name = columns[3];
76
77 if (EqualsASCII(columns[2], "<DIR>")) {
78 entry.type = FtpDirectoryListingEntry::DIRECTORY;
79 entry.size = -1;
80 } else {
81 entry.type = FtpDirectoryListingEntry::FILE;
82 if (!StringToInt64(columns[2], &entry.size))
83 return false;
84 if (entry.size < 0)
85 return false;
86 }
87
88 if (!WindowsDateListingToTime(columns, &entry.last_modified))
89 return false;
90
91 entries_.push(entry);
92 return true;
93 }
94
95 bool FtpDirectoryListingParserWindows::OnEndOfInput() {
96 return true;
97 }
98
99 bool FtpDirectoryListingParserWindows::EntryAvailable() const {
100 return !entries_.empty();
101 }
102
103 FtpDirectoryListingEntry FtpDirectoryListingParserWindows::PopEntry() {
104 FtpDirectoryListingEntry entry = entries_.front();
105 entries_.pop();
106 return entry;
107 }
108
109 } // namespace net
OLDNEW
« no previous file with comments | « net/ftp/ftp_directory_listing_parser_windows.h ('k') | net/ftp/ftp_directory_listing_parser_windows_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698