// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#pragma once

#include <map>
#include <string>
#include <fstream>
#include <vector>

// Simple file archive system.

namespace FileSystem {

    class Archive {

        public:
        
          /// Constructor. Give it a file name and writable flag.
          /// Generally, writable flag should be reserved for archive
          /// creation.
          Archive(const std::string &fileName, bool writeMode = false);
		
          ~Archive(void);
		
          /// Add a file to the archive. Archive must have been created
          /// in write mode or this will fail. (assert fail)
          void addFile(const std::string &fileName, const char *data, int length);
		
          /// Load a file from the archive. Length will be stored in
          /// length. Set addNullTerminator to true for non-binary data
          /// that should have a null terminator. Value stored in length
          /// will include this extra byte if specified. Data is owned by
          /// caller. Clean it up with delete[].
          char *loadFile(const std::string &fileName, int *length, bool addNullTerminator = false);

          /// Reads the first headerLength bytes from a file instead of
          /// the whole thing.
          char *loadFileHead(const std::string &fileName, int headerLength);

          /// Get a list of all files in the archive with full path names.
          void getFileList(std::vector<std::string> &fileList);
		
          /// Check if a file exists in the archive. Use full path from
          /// archive root. Does not return true for directories.
          bool getFileExists(const std::string &fileName);
		
          /// Check if a directory exists. Note that directories will not
          /// exist on their own inside an archive. There must be
          /// something in a directory for it to be stored. (Directory
          /// information is also built off the paths of normal files in
          /// those directories.)c
          bool getDirExists(const std::string &dirName);
		
          /// Get the size of a file. Does not include additional null
          /// terminator if one is needed.
          unsigned int getFileSize(const std::string &fileName);
		
          /// Get list of files and directories in a given directory.
          void getFileListForDir(const std::string &dirName, std::vector<std::string> &fileList);

          /*

          // TODO: Implement this someday.

          // This is going to be really slow! Will defrag archive!
          void deleteFile(const std::string &fileName);
          */
		
          /// Get the file name of this archive.
          const std::string &getMyFileName(void);
		
          /// Get error condition, which could be caused by any number of
          /// things, but all basically amount to nothing useful coming
          /// out of this archive.
          bool getFailed(void);

        private:
	
          void closeArchiveFile(void);
          void openArchiveFile(void);
		
          // Maps filenames (strings) to byte offsets within the file (ints).		
          std::map<std::string, unsigned int> tableOfContents;
          std::map<std::string, bool> tableOfDirectories;
          std::map<std::string, std::vector<std::string> > directoryContents;
          std::map<std::string, std::vector<std::string> > subdirectories;
          std::string myFileName;
          std::fstream archiveFile;
          bool writeMode;
          bool failed;
          void rebuildTableOfContents(void);
	};

}
