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

Side by Side Diff: net/ftp/ftp_directory_listing_parsers.h

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 #ifndef NET_FTP_FTP_DIRECTORY_LISTING_PARSERS_H_
6 #define NET_FTP_FTP_DIRECTORY_LISTING_PARSERS_H_
7
8 #include <queue>
9
10 #include "base/basictypes.h"
11 #include "base/string16.h"
12 #include "base/time.h"
13 #include "net/ftp/ftp_server_type_histograms.h"
14
15 namespace net {
16
17 struct FtpDirectoryListingEntry {
18 enum Type {
19 FILE,
20 DIRECTORY,
21 SYMLINK,
22 };
23
24 Type type;
25 string16 name;
26 int64 size; // File size, in bytes. -1 if not applicable.
27
28 // Last modified time, in local time zone.
29 base::Time last_modified;
30 };
31
32 class FtpDirectoryListingParser {
33 public:
34 virtual ~FtpDirectoryListingParser();
35
36 virtual FtpServerType GetServerType() const = 0;
37
38 // Adds |line| to the internal parsing buffer. Returns true on success.
39 virtual bool ConsumeLine(const string16& line) = 0;
40
41 // Called after all input has been consumed. Returns true if the parser
42 // recognizes all received data as a valid listing.
43 virtual bool OnEndOfInput() = 0;
44
45 // Returns true if there is at least one FtpDirectoryListingEntry available.
46 virtual bool EntryAvailable() const = 0;
47
48 // Returns the next entry. It is an error to call this function unless
49 // EntryAvailable returns true.
50 virtual FtpDirectoryListingEntry PopEntry() = 0;
51 };
52
53 // Parser for "ls -l"-style directory listing.
54 class FtpLsDirectoryListingParser : public FtpDirectoryListingParser {
55 public:
56 FtpLsDirectoryListingParser();
57
58 // FtpDirectoryListingParser methods:
59 virtual FtpServerType GetServerType() const { return SERVER_LS; }
60 virtual bool ConsumeLine(const string16& line);
61 virtual bool OnEndOfInput();
62 virtual bool EntryAvailable() const;
63 virtual FtpDirectoryListingEntry PopEntry();
64
65 private:
66 bool received_nonempty_line_;
67
68 std::queue<FtpDirectoryListingEntry> entries_;
69
70 DISALLOW_COPY_AND_ASSIGN(FtpLsDirectoryListingParser);
71 };
72
73 class FtpWindowsDirectoryListingParser : public FtpDirectoryListingParser {
74 public:
75 FtpWindowsDirectoryListingParser();
76
77 // FtpDirectoryListingParser methods:
78 virtual FtpServerType GetServerType() const { return SERVER_WINDOWS; }
79 virtual bool ConsumeLine(const string16& line);
80 virtual bool OnEndOfInput();
81 virtual bool EntryAvailable() const;
82 virtual FtpDirectoryListingEntry PopEntry();
83
84 private:
85 std::queue<FtpDirectoryListingEntry> entries_;
86
87 DISALLOW_COPY_AND_ASSIGN(FtpWindowsDirectoryListingParser);
88 };
89
90 // Parser for VMS-style directory listing (including variants).
91 class FtpVmsDirectoryListingParser : public FtpDirectoryListingParser {
92 public:
93 FtpVmsDirectoryListingParser();
94
95 // FtpDirectoryListingParser methods:
96 virtual FtpServerType GetServerType() const { return SERVER_VMS; }
97 virtual bool ConsumeLine(const string16& line);
98 virtual bool OnEndOfInput();
99 virtual bool EntryAvailable() const;
100 virtual FtpDirectoryListingEntry PopEntry();
101
102 private:
103 // Consumes listing line which is expected to be a directory listing entry
104 // (and not a comment etc). Returns true on success.
105 bool ConsumeEntryLine(const string16& line);
106
107 enum State {
108 STATE_INITIAL,
109
110 // Indicates that we have received the header, like this:
111 // Directory SYS$SYSDEVICE:[ANONYMOUS]
112 STATE_RECEIVED_HEADER,
113
114 // Indicates that we have received the first listing entry, like this:
115 // MADGOAT.DIR;1 2 9-MAY-2001 22:23:44.85
116 STATE_ENTRIES,
117
118 // Indicates that we have received the last listing entry.
119 STATE_RECEIVED_LAST_ENTRY,
120
121 // Indicates that we have successfully received all parts of the listing.
122 STATE_END,
123 } state_;
124
125 // VMS can use two physical lines if the filename is long. The first line will
126 // contain the filename, and the second line everything else. Store the
127 // filename until we receive the next line.
128 string16 last_filename_;
129 bool last_is_directory_;
130
131 std::queue<FtpDirectoryListingEntry> entries_;
132
133 DISALLOW_COPY_AND_ASSIGN(FtpVmsDirectoryListingParser);
134 };
135
136 } // namespace net
137
138 #endif // NET_FTP_FTP_DIRECTORY_LISTING_PARSERS_H_
OLDNEW
« no previous file with comments | « net/ftp/ftp_directory_listing_parser_windows_unittest.cc ('k') | net/ftp/ftp_directory_listing_parsers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698