

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//_/_/
//_/_/  This file is part of CMSDK version 2.0.0.170915_Beta4 (15 September 2017)
//_/_/  
//_/_/  developed by
//_/_/  
//_/_/  Communicative Machines Ltd. 
//_/_/    http://cmlabs.com/
//_/_/  Copyright (c) 2017
//_/_/  
//_/_/  in collaboration with 
//_/_/  
//_/_/  Icelandic Institute for Intelligent Machines ses
//_/_/    Menntavegur 1
//_/_/    101 Reykjavik
//_/_/    ICELAND
//_/_/    http://www.iiim.is/
//_/_/
//_/_/  and is released under the following
//_/_/  
//_/_/  EXTENDED BSD License
//_/_/
//_/_/  Redistribution and use in source and binary forms, with or without
//_/_/  modification, is permitted provided that the following conditions 
//_/_/  are met:
//_/_/
//_/_/  - Redistributions of source code must retain the above copyright 
//_/_/    and collaboration notice, this list of conditions and the following 
//_/_/    disclaimer.
//_/_/
//_/_/  - Redistributions in binary form must reproduce the above copyright 
//_/_/    notice, this list of conditions and the following disclaimer in the 
//_/_/    documentation and/or other materials provided with the distribution.
//_/_/
//_/_/  - Neither the name of its copyright holders nor the names of its 
//_/_/    contributors may be used to endorse or promote products derived from 
//_/_/    this software without specific prior written permission.
//_/_/
//_/_/  - CADIA Clause: The license granted in and to the software under this 
//_/_/    agreement is a limited-use license. The software may not be used in 
//_/_/    furtherance of: (i) intentionally causing bodily injury or severe 
//_/_/    emotional harm to any person; (ii) invading the personal privacy or 
//_/_/    violating the human rights of any person; or (iii) committing or 
//_/_/    preparing for any act of war. 
//_/_/
//_/_/  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//_/_/  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//_/_/  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//_/_/  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
//_/_/  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//_/_/  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//_/_/  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//_/_/  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//_/_/  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//_/_/  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//_/_/  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//_/_/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/*

	Implementation of POSIX directory browsing functions and types for Win32.

	Kevlin Henney (mailto:kevlin@acm.org), March 1997.

	Copyright Kevlin Henney, 1997. All rights reserved.

	Permission to use, copy, modify, and distribute this software and its
	documentation for any purpose is hereby granted without fee, provided
	that this copyright and permissions notice appear in all copies and
	derivatives, and that no charge may be made for the software and its
	documentation except to cover cost of distribution.
	
	This software is supplied "as is" without express or implied warranty.

	But that said, if there are any problems please get in touch.

*/

#include "direntwin.h"

namespace cmlabs {


#if defined(POCKETPC)
//	#define CONSTCHAR unsigned short
	#define CONSTCHAR WCHAR
#else
	#define CONSTCHAR char
#endif

#if defined(POCKETPC)
	int errno;
#endif

#ifdef CYGWIN
	int errno;
#endif

struct DIR
{
	HANDLE				handle; /* -1 for failed rewind */
	WIN32_FIND_DATA		info;
	struct dirent		result; /* d_name null iff first time */
	CONSTCHAR *name;
};

DIR *opendir(const char *name)
{
	DIR *dir = 0;

	if(name && name[0])
	{
		size_t base_length = strlen(name);
		const char *all = /* the root directory is a special case... */
			strchr("/\\", name[base_length - 1]) ? "*" : "/*";

		if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
		   (dir->name = (CONSTCHAR*) malloc(base_length + strlen(all) + 1)) != 0)
		{
			utils::strcpyavail((char*)dir->name, name, (uint32)base_length + (uint32)strlen(all) + 1, true);
			strcat_s((char*)dir->name, base_length + (uint32)strlen(all) + 1, all);

			if((dir->handle = FindFirstFile(dir->name, &dir->info)) != INVALID_HANDLE_VALUE)
			{
				dir->result.d_name = 0;
			}
			else /* rollback */
			{
				free((void*)dir->name);
				free(dir);
				dir = 0;
			}
		}
		else /* rollback */
		{
			free(dir);
			dir   = 0;
			errno = 12; // ENOMEM;
		}
	}
	else
	{
		errno = 22; // EINVAL;
	}

	return dir;
}

int closedir(DIR *dir)
{
	int result = -1;

	if(dir)
	{
		if(dir->handle != INVALID_HANDLE_VALUE)
		{
			result = FindClose(dir->handle);
		}

		free((void*)dir->name);
		free(dir);
	}

	if(result == -1) /* map all errors to EBADF */
	{
		errno = 9; // EBADF;
	}

	return result;
}

struct dirent *readdir(DIR *dir)
{
	struct dirent *result = 0;

	if(dir && dir->handle != INVALID_HANDLE_VALUE)
	{
		if((!dir->result.d_name) || (FindNextFile(dir->handle, &dir->info)))
		{
			result         = &dir->result;
			result->d_name = (char*) dir->info.cFileName;
		}
	}
	else
	{
		errno = 9; // EBADF;
	}

	return result;
}

void rewinddir(DIR *dir)
{
	if(dir && dir->handle != INVALID_HANDLE_VALUE)
	{
		FindClose(dir->handle);
		dir->handle = FindFirstFile(dir->name, &dir->info);
		dir->result.d_name = 0;
	}
	else
	{
		errno = 9; // EBADF;
	}
}


} // namespace cmlabs
