/*
	ZazouMiniWebServer

	Copyright (C) 2003-2011 Xavier Garreau <xavier@xgarreau.org>

	This file is part of ZazouMiniWebServer.

    ZazouMiniWebServer 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.

    ZazouMiniWebServer 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 ZazouMiniWebServer; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
	
#ifndef _ZAZOU_OS_ABSTRACTION_LAYER
#define _ZAZOU_OS_ABSTRACTION_LAYER 1

#ifdef WIN32
// Pour les OS Windows:

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <windows.h>

#define strdup					_strdup

#define WaitForSingleThread		WaitForSingleObject
#define WaitForSingleMutex		WaitForSingleObject
#define WaitForSingleSemaphore		WaitForSingleObject
#define CloseFile			CloseHandle
#define CloseThread			CloseHandle
#define CloseProcess			CloseHandle
#define CloseMutex			CloseHandle
#define CloseSemaphore			CloseHandle

#define HANDLE_FILE				HANDLE
#define HANDLE_FIND				HANDLE
#define HANDLE_THREAD			HANDLE
#define HANDLE_PROCESS			HANDLE
#define HANDLE_MUTEX			HANDLE
#define HANDLE_SEMAPHORE		HANDLE
#endif



#ifndef WIN32
// Pour les OS non Windows:

// Includes spécifiques
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <sys/types.h>

// Constantes
#define TRUE				true
#define FALSE				false

#define ERROR_ALREADY_EXISTS		EEXIST
#define INVALID_SOCKET			-1
#define SOCKET_ERROR			-1

// Types
typedef void*				WSADATA;
typedef unsigned int			UINT;
typedef unsigned long			DWORD;
typedef long					LONG;
typedef unsigned short			WORD;
typedef unsigned char			BYTE;
typedef bool				BOOL;
typedef int				SOCKET;
typedef void*				HMODULE;
typedef char*				LPTSTR;
typedef const char*			LPCTSTR;
typedef char*				LPSTR;
typedef const char*			LPCSTR;
typedef unsigned char*			LPBYTE;
typedef void*				LPVOID;
typedef struct sockaddr_in		SOCKADDR_IN;
typedef struct sockaddr			SOCKADDR;
typedef struct sockaddr*		LPSOCKADDR;
typedef struct hostent			HOSTENT;

typedef int				HANDLE_FILE;
typedef pthread_t			HANDLE_THREAD;
typedef int 				HANDLE_PROCESS;
typedef pthread_mutex_t			HANDLE_MUTEX;

// Sections Critiques :: Types
typedef struct _CRITICAL_SECTION {
	pthread_mutex_t mutex;
} CRITICAL_SECTION;

// Sections Critiques :: Fonctions
int InitializeCriticalSection (CRITICAL_SECTION* cs);
int DeleteCriticalSection(CRITICAL_SECTION* cs);
int EnterCriticalSection(CRITICAL_SECTION* cs);
int LeaveCriticalSection(CRITICAL_SECTION* cs);

// Fichiers
#define CloseFile				close

// Mutex
#define WAIT_OBJECT_0			0
#define ReleaseMutex(m)			pthread_mutex_unlock(&(m))
#define CloseMutex(m)			pthread_mutex_destroy(&(m))
#define WaitForSingleMutex(m,t)	pthread_mutex_lock(&(m))

// Gestion du temps :: Types
typedef struct _SYSTEMTIME {
	WORD wYear;
	WORD wMonth;
	WORD wDayOfWeek;
	WORD wDay;
	WORD wHour;
	WORD wMinute;
	WORD wSecond;
	WORD wMilliseconds;
} SYSTEMTIME;

// Gestion du temps :: Fonctions
#define Sleep(ms)					usleep(1000*(ms))
void GetLocalTime (SYSTEMTIME* st, time_t when = 0);
void GetSystemTime (SYSTEMTIME* st, time_t when = 0);

// Dynamic loader :: Fonctions
HMODULE	LoadLibrary (const char* filename);
int	FreeLibrary (HMODULE module);

// errno
DWORD GetLastError (void);

// Répertoire courant
int GetCurrentDirectory (int taille, char* buf);

// Récupération du pid
DWORD GetCurrentProcessId (void);

BOOL CreateDirectory (LPCTSTR dirpath, void* dummy_security_attributes);
#endif
#endif
