/*-
 * 
 * Copyright (c) 1999,2000 Qube Software, Ltd.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of the source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Any redistribution solely in binary form must conspicuously
 *    reproduce the following disclaimer in documentation provided with the
 *    binary redistribution.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'', WITHOUT ANY WARRANTIES, EXPRESS
 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  LICENSOR SHALL
 * NOT BE LIABLE FOR ANY LOSS OR DAMAGES RESULTING FROM THE USE OF THIS
 * SOFTWARE, EITHER ALONE OR IN COMBINATION WITH ANY OTHER SOFTWARE.
 * 
 * 	$Id: qsys.h,v 1.21 2000/06/22 08:43:58 nps Exp $
 */

#ifndef __hooks_qsys_h__
#define __hooks_qsys_h__

#include        <stdlib.h>
#ifdef q_unix
#include        <unistd.h>
#endif

#ifdef __cplusplus

#ifdef q_unix
#define QCDECL
#endif
#ifdef q_windows
#define QCDECL  _cdecl
#endif

extern "C" {
#endif

#include "qsys/linkage.h"
    
#ifdef __BORLANDC__
#define Q_HAVE_DIRENT
#endif

#ifdef q_windows
#define QD_MAX_PATH     256
#endif

#ifdef q_unix
#define Q_HAVE_DIRENT
#define QD_MAX_PATH	1024
#endif

#ifdef q_ps2
#define QD_MAX_PATH	1024
#endif

/* Forward Decls */
struct QTDirectory;

struct QTDiskInfo
{
    unsigned int        sectorSize_;
    unsigned int        clusterSize_;
    unsigned int        freeClusterCount_;
    unsigned int        clusterCount_;
};


#define QID_OPEN_EXISTING       1       /* must already exist */
#define QID_OPEN_CREATE         2       /* always creates */
#define QID_OPEN_MAYCREATE      4       /* create if not already */

#define QID_OPEN_MODE_ALL       1
#define QID_OPEN_NO_CACHING     0x100

#define QID_FILE_ATTRIB_REGULAR 1
#define QID_FILE_ATTRIB_DIRECTORY 2

#define QID_SEEK_SET            0
#define QID_SEEK_CUR            1
#define QID_SEEK_END            2

int EXPORT_QSYS QIF_Open(const char* file, int flags, int mode);
int EXPORT_QSYS QIF_Close(int fd);
int EXPORT_QSYS QIF_Read(int fd, void* buf, unsigned int);
int EXPORT_QSYS QIF_Write(int fd, void* buf, unsigned int);
int EXPORT_QSYS QIF_LSeek(int fd, int pos, int whence);
int EXPORT_QSYS QIF_CopyFile(const char* src, const char* dest);
int EXPORT_QSYS QIF_LastModifyTime(int fd,
                                   unsigned int* lo, 
                                   unsigned int* hi);
int EXPORT_QSYS QIF_FileSize(int fd,
                             unsigned int* lo, 
                             unsigned int* hi);
int EXPORT_QSYS QIF_DiskInfo(const char* name, struct QTDiskInfo*);
int EXPORT_QSYS QIF_DeleteFile(const char *name);

unsigned long EXPORT_QSYS QIF_CreateCriticalSection(void);
void EXPORT_QSYS QIF_EnterCriticalSection(unsigned long);
void EXPORT_QSYS QIF_ExitCriticalSection(unsigned long);
void EXPORT_QSYS QIF_DestroyCriticalSection(unsigned long);

unsigned long EXPORT_QSYS QIF_CreateMutex(void);
void EXPORT_QSYS QIF_EnterMutex(unsigned long);
void EXPORT_QSYS QIF_ExitMutex(unsigned long);
void EXPORT_QSYS QIF_DestroyMutex(unsigned long);

unsigned long EXPORT_QSYS QIF_CreateSemaphore(int maxcount);
void EXPORT_QSYS QIF_RequestSemaphore(unsigned long);
void EXPORT_QSYS QIF_ReleaseSemaphore(unsigned long, int count);
void EXPORT_QSYS QIF_DestroySemaphore(unsigned long);

/* Directory Primitives */
int EXPORT_QSYS QIF_DirectoryOpen(const char* path, struct QTDirectory* dir);
int EXPORT_QSYS QIF_DirectoryClose(struct QTDirectory* dir);
int EXPORT_QSYS QIF_DirectoryRead(struct QTDirectory* dir);
int EXPORT_QSYS QIF_DirectoryCreate(const char* path);

/* Misc */
int EXPORT_QSYS QIF_GetEnvironment(const char* name, const char** envp);
int EXPORT_QSYS QIF_ImportExternal(const char* name, unsigned long*);
int EXPORT_QSYS QIF_UnloadExternal(unsigned long);
const char EXPORT_QSYS * QIF_ImportError();
int EXPORT_QSYS QIF_Yield();
int EXPORT_QSYS QIF_GetExternalFunction(unsigned long ih,
					const char* name, void** addr);
int EXPORT_QSYS QIF_ValidatePointer(void*);
int EXPORT_QSYS QIF_Sleep(double);
int EXPORT_QSYS QIF_ProcessorCount(unsigned int*);
int EXPORT_QSYS QIF_OperatingSystem(const char** osName,
                                    int* vMajor, int* vMinor);

/* Configuration processing */
#define QID_PER_USER    0
#define QID_MACHINE     1


#ifndef q_ps2
int EXPORT_QSYS QIF_GetConfigSize(const char *key, size_t *result);
int EXPORT_QSYS QIF_GetConfigString(const char *key, char *result, 
                                    size_t len);
int EXPORT_QSYS QIF_PutConfigSize(const char *key, size_t value);
int EXPORT_QSYS QIF_PutConfigString(const char *key, char *value);
#endif

#ifdef q_windows
int EXPORT_QSYS QIF_GetWindowsConfigString(int type, const char *key,
                                           const char *value, char *result,
                                           size_t len);
int EXPORT_QSYS QIF_GetWindowsConfigSize(int type, const char *key, 
                                         const char *value, size_t *result);
#endif
/* Tasks */

#ifdef __GNUC__
#define _stdcall
#endif

typedef unsigned long (_stdcall *TaskCallback)(void*);

unsigned long EXPORT_QSYS QIF_CurrentTaskId();

/* Thread Priorities:
 * For Q we arrange thread priorites from -10 to +10. Zero is normal
 * +10 is highest and -10 is lowest.
 */
int EXPORT_QSYS QIF_CreateTask(TaskCallback,
                               void* context,
                               unsigned long* hAddr,
                               unsigned int affinityMask,
                               int priority);
                            


#ifdef DEBUG
#define QDEBUG
#endif

#ifdef QDEBUG
#define QfAssert(_x) if (!(_x)) { QfAssert__(#_x, __FILE__, __LINE__); }

void EXPORT_QSYS QfAssert__(const char*, const char*, int);

#else
#define QfAssert(_x)
#endif

/* Windoze doesn't have strcasecmp and BSD doesn't have stricmp. */
#ifdef q_unix
#define stricmp		strcasecmp
#define strnicmp	strncasecmp
#endif

#ifdef q_ps2
#define stricmp		strcasecmp
#define strnicmp	strncasecmp
#endif

#ifdef _MSC_VER

#pragma warning(disable : 4786) // truncated debug info

#endif

#ifdef __cplusplus
}
#endif

#endif /* __hooks_qsys_h__ */



