/* $Id: DirectoryStream,v 1.1.1.1 2008/11/28 17:57:26 kiesling Exp $ -*-c-*-*/

/*
  This file is part of ctalk.
  Copyright  2005 - 2008  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/*
 *    DirectoryStream class
 */

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <dirent.h>


#define DIRECTORY_LIB_ERROR -1

FileStream class DirectoryStream;

DirectoryStream instanceMethod mkDir (char *__dirName) {
  SystemErrnoException new __e;
  returnObjectClass Integer;
  if (mkdir (__dirName, CTALK_DIRECTORY_MODE) == DIRECTORY_LIB_ERROR) {
    __e raiseException __dirName;
    methodReturnInteger(DIRECTORY_LIB_ERROR)
  }
  methodReturnSelf
/*   methodReturnFalse */
}

DirectoryStream instanceMethod rmDir (char *__dirName) {
  SystemErrnoException new __e;
  returnObjectClass Integer;
  if (rmdir (__dirName) == DIRECTORY_LIB_ERROR) {
    __e raiseException __dirName;
    methodReturnInteger(DIRECTORY_LIB_ERROR)
  }
  methodReturnSelf
/*   methodReturnFalse */
}

DirectoryStream instanceMethod chDir (char *__dirName) {
  SystemErrnoException new __e;
  returnObjectClass Integer;
  if (chdir (__dirName) == DIRECTORY_LIB_ERROR) {
    __e raiseException __dirName;
    methodReturnInteger(DIRECTORY_LIB_ERROR)
  }
  methodReturnSelf
/*   methodReturnFalse */
}

DirectoryStream instanceMethod getCwd (void) {
  char buf[FILENAME_MAX];
  returnObjectClass String;
  getcwd (buf, FILENAME_MAX);
  methodReturnString(buf)
}

DirectoryStream instanceMethod directoryList (char *__dirName, List __dList) {

  DIR *d;
  struct dirent *d_ent;
  Array new __dirs;
  Integer new __nDirs;
  Integer new __i;
  String new __dNameInternal;
  String new __entryString;
  SystemErrnoException new __e;

  returnObjectClass List;

  /*
   *  Save the directory name argument in __dNameInternal and
   *  expand if necessary.
   */
  __dNameInternal = __dirName;

  if ((__dirName == self getCwd) ||
      (__dirName == ".")) {
    __dNameInternal = self getCwd;
  }
  if (__dirName == "~") {
    __dNameInternal = self getEnv "HOME";
  }
  if (__dirName == "..") {
    __dirName = self getCwd;
    __nDirs = __dirName split ('/', __dirs);
    __dNameInternal = "";
    for (__i = 0; __i < (__nDirs - 1); __i = __i + 1) {
      __dNameInternal = __dNameInternal + "/";
      __dNameInternal = __dNameInternal + (__dirs at __i);
    }
  }

  if ((d = opendir (__dNameInternal)) == NULL) {
    __e raiseException __dNameInternal;
    methodReturnNULL
  }

  while ((d_ent = readdir (d)) != NULL)
    __dList push d_ent -> d_name;

  if (closedir (d))
    __e raiseException __dNameInternal;

  methodReturnNULL
}

