xpcom/build/FileLocation.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/build/FileLocation.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozilla_FileLocation_h
     1.9 +#define mozilla_FileLocation_h
    1.10 +
    1.11 +#include "nsString.h"
    1.12 +#include "nsCOMPtr.h"
    1.13 +#include "nsAutoPtr.h"
    1.14 +#include "nsIFile.h"
    1.15 +#include "FileUtils.h"
    1.16 +
    1.17 +class nsZipArchive;
    1.18 +class nsZipItem;
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +class FileLocation
    1.23 +{
    1.24 +public:
    1.25 +  /**
    1.26 +   * FileLocation is an helper to handle different kind of file locations
    1.27 +   * within Gecko:
    1.28 +   * - on filesystems
    1.29 +   * - in archives
    1.30 +   * - in archives within archives
    1.31 +   * As such, it stores a path within an archive, as well as the archive
    1.32 +   * path itself, or the complete file path alone when on a filesystem.
    1.33 +   * When the archive is in an archive, an nsZipArchive is stored instead
    1.34 +   * of a file path.
    1.35 +   */
    1.36 +  FileLocation();
    1.37 +  ~FileLocation();
    1.38 +
    1.39 +  /**
    1.40 +   * Constructor for plain files
    1.41 +   */
    1.42 +  FileLocation(nsIFile *file);
    1.43 +
    1.44 +  /**
    1.45 +   * Constructors for path within an archive. The archive can be given either
    1.46 +   * as nsIFile or nsZipArchive.
    1.47 +   */
    1.48 +  FileLocation(nsIFile *zip, const char *path);
    1.49 +
    1.50 +  FileLocation(nsZipArchive *zip, const char *path);
    1.51 +
    1.52 +  /**
    1.53 +   * Creates a new file location relative to another one.
    1.54 +   */
    1.55 +  FileLocation(const FileLocation &file, const char *path = nullptr);
    1.56 +
    1.57 +  /**
    1.58 +   * Initialization functions corresponding to constructors
    1.59 +   */
    1.60 +  void Init(nsIFile *file);
    1.61 +
    1.62 +  void Init(nsIFile *zip, const char *path);
    1.63 +
    1.64 +  void Init(nsZipArchive *zip, const char *path);
    1.65 +
    1.66 +  /**
    1.67 +   * Returns an URI string corresponding to the file location
    1.68 +   */
    1.69 +  void GetURIString(nsACString &result) const;
    1.70 +
    1.71 +  /**
    1.72 +   * Returns the base file of the location, where base file is defined as:
    1.73 +   * - The file itself when the location is on a filesystem
    1.74 +   * - The archive file when the location is in an archive
    1.75 +   * - The outer archive file when the location is in an archive in an archive
    1.76 +   */
    1.77 +  already_AddRefed<nsIFile> GetBaseFile();
    1.78 +
    1.79 +  /**
    1.80 +   * Returns whether the "base file" (see GetBaseFile) is an archive
    1.81 +   */
    1.82 +  bool IsZip() const
    1.83 +  {
    1.84 +    return !mPath.IsEmpty();
    1.85 +  }
    1.86 +
    1.87 +  /**
    1.88 +   * Returns the path within the archive, when within an archive
    1.89 +   */
    1.90 +  void GetPath(nsACString &result) const
    1.91 +  {
    1.92 +    result = mPath;
    1.93 +  }
    1.94 +
    1.95 +  /**
    1.96 +   * Boolean value corresponding to whether the file location is initialized
    1.97 +   * or not.
    1.98 +   */
    1.99 +  operator bool() const
   1.100 +  {
   1.101 +    return mBaseFile || mBaseZip;
   1.102 +  }
   1.103 +
   1.104 +  /**
   1.105 +   * Returns whether another FileLocation points to the same resource
   1.106 +   */
   1.107 +  bool Equals(const FileLocation &file) const;
   1.108 +
   1.109 +  /**
   1.110 +   * Data associated with a FileLocation.
   1.111 +   */
   1.112 +  class Data
   1.113 +  {
   1.114 +  public:
   1.115 +    /**
   1.116 +     * Returns the data size
   1.117 +     */
   1.118 +    nsresult GetSize(uint32_t *result);
   1.119 +
   1.120 +    /**
   1.121 +     * Copies the data in the given buffer
   1.122 +     */
   1.123 +    nsresult Copy(char *buf, uint32_t len);
   1.124 +  protected:
   1.125 +    friend class FileLocation;
   1.126 +    nsZipItem *mItem;
   1.127 +    nsRefPtr<nsZipArchive> mZip;
   1.128 +    mozilla::AutoFDClose mFd;
   1.129 +  };
   1.130 +
   1.131 +  /**
   1.132 +   * Returns the data associated with the resource pointed at by the file
   1.133 +   * location.
   1.134 +   */
   1.135 +  nsresult GetData(Data &data);
   1.136 +private:
   1.137 +  nsCOMPtr<nsIFile> mBaseFile;
   1.138 +  nsRefPtr<nsZipArchive> mBaseZip;
   1.139 +  nsCString mPath;
   1.140 +}; /* class FileLocation */
   1.141 +
   1.142 +} /* namespace mozilla */
   1.143 +
   1.144 +#endif /* mozilla_FileLocation_h */

mercurial