1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef __NSDIRINDEX_H_
7 #define __NSDIRINDEX_H_
8 
9 #include "nsString.h"
10 #include "nsCOMPtr.h"
11 #include "nsIDirIndexListener.h"
12 #include "mozilla/RefPtr.h"
13 
14 class nsIDirIndex;
15 class nsITextToSubURI;
16 
17 /* CID: {a0d6ad32-1dd1-11b2-aa55-a40187b54036} */
18 
19 class nsDirIndexParser : public nsIDirIndexParser {
20  private:
21   virtual ~nsDirIndexParser();
22 
23   nsDirIndexParser() = default;
24   nsresult Init();
25 
26  public:
27   NS_DECL_ISUPPORTS
28   NS_DECL_NSISTREAMLISTENER
29   NS_DECL_NSIREQUESTOBSERVER
30   NS_DECL_NSIDIRINDEXPARSER
31 
CreateInstance()32   static already_AddRefed<nsIDirIndexParser> CreateInstance() {
33     RefPtr<nsDirIndexParser> parser = new nsDirIndexParser();
34     if (NS_FAILED(parser->Init())) {
35       return nullptr;
36     }
37     return parser.forget();
38   }
39 
40   enum fieldType {
41     FIELD_UNKNOWN = 0,  // MUST be 0
42     FIELD_FILENAME,
43     FIELD_DESCRIPTION,
44     FIELD_CONTENTLENGTH,
45     FIELD_LASTMODIFIED,
46     FIELD_CONTENTTYPE,
47     FIELD_FILETYPE
48   };
49 
50  protected:
51   nsCOMPtr<nsIDirIndexListener> mListener;
52 
53   nsCString mEncoding;
54   nsCString mComment;
55   nsCString mBuf;
56   int32_t mLineStart{0};
57   bool mHasDescription{false};
58   int mFormat[8]{-1};
59 
60   nsresult ProcessData(nsIRequest* aRequest, nsISupports* aCtxt);
61   void ParseFormat(const char* aFormatStr);
62   void ParseData(nsIDirIndex* aIdx, char* aDataStr, int32_t lineLen);
63 
64   struct Field {
65     const char* mName;
66     fieldType mType;
67   };
68 
69   static Field gFieldTable[];
70 
71   static nsrefcnt gRefCntParser;
72   static nsITextToSubURI* gTextToSubURI;
73 };
74 
75 #endif
76