/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#ifndef __DIRREADER__
#define __DIRREADER__

#include "nmm/nmm_prefix.hpp"

#include <vector>
#include <string>
#include "visibility.hpp"

using namespace std;

class DLLEXPORT DirReader {

public:
  typedef struct {
    string        path;  // path
    string        name;  // filename or subdir name
    bool          isdir;
  } directory_t;
  
protected:
  vector<directory_t>   cur_dir_entries;
  vector<directory_t>   dirs;
  vector<directory_t>   regFiles;

  string                root_dir;
  string                cur_dir;
  string                backdir;
  bool                  nice_dir;

  bool myIsDir( string complete_path );
  bool myIsReg( string complete_path );

  /**
   * change directory (root prefix will be set in front of dir name)
   * reads all dir/filenames
   * \returns true if directory could be changed/read
   */
  bool readDir( string completePath );

  class cmpdirents {
    // to compare the direntries, needed by sort.
  public:
    int operator() (const directory_t& dir1, const directory_t& dir2) const
    {
      string s1 = dir1.name;
      string s2 = dir2.name;
      return (s1<s2);
    }
  };

  class cmpregents {
    // to compare the regular file entries, needed by sort.
  public:
    int operator() (const directory_t& dir1, const directory_t& dir2) const
    {
      string s1 = dir1.name;
      string s2 = dir2.name;
      return (s1<s2);
    }
  };

public:

  DirReader();
  ~DirReader();


  /**
   * set root prefix
   * \param dir root directory
   */
  void setRootDir( string dir );

  /**
   * \returns root dir prefix (ends always with a "/")
   */
  string getRootDir();

  /**
   * change directory (root prefix will be set in front of dir name)
   * reads all dir/filenames
   * \returns true if directory could be changed
   */
  bool changeDir( string dir );

  /**
   * get a array of all directory entries
   */
  vector<directory_t> getDirEntries();

  /**
   * get a array of all directories only
   */
  vector<directory_t> getDirsOnlyEntries();

  /**
   * get a array of all regular files
   */
  vector<directory_t> getRegFiles();

  /**
   * \returns current selected directory (ends always with a "/")
   */
  string getCurDir();

  /**
   * \returns current selected directory without root dir
   */
  string getCurDirWithoutRoot();

  /**
   *
   * \param nd if true all "." entries from a directory are removed,
   * all ".." entries are replaced by a string
   */
  void setNiceDir( bool nd );

  /**
   * \param name file name
   * \param len maximum characters that resulting string should have (-1 means infinite)
   * \returns a file name string without ending and with a given maximum character number
   */
  string getNiceFileName( string name, int len=-1 );

  /**
   * \param name file name
   * \param len maximum characters that resulting string should have (-1 means infinite)
   * \returns a file name string with ending and with a given maximum character number
   */
  string getFileName( string name, int len=-1 );

  /**
   * \param name dir name
   * \param len maximum characters that resulting string should have (-1 means infinite)
   * \returns a dir name string with a given maximum character number
   */
  string getNiceDirName( string name, int len=-1 );

  /**
   * removes the last "/" in a directory, but only if the directory is different
   * to "/"
   */
  void removeLastSlash(string&);

  /**
   * creates a directory
   */
  static bool makeDir(const string);

  /**
   * get suffix of filename
   */
  string getSuffix( string name );

  /**
   * overwrite internal regular files and directory list
   */
  void importDirectory(  vector<directory_t> dirs, vector<directory_t> reg_files );

};

#include "nmm/nmm_suffix.hpp"

#endif //__DIRREADER__
