
#ifndef CLIB_SAVAGE_H
#define CLIB_SAVAGE_H

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/resident.h>
#include <exec/ports.h>
#include <exec/tasks.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <dos/dos.h>
#include <graphics/text.h>
#include <graphics/gfx.h>
#include <savage/sysinfo.h>
#include <savage/guicontext.h>
#include <savage/visible.h>
#include <utility/tagitem.h>

/********************************************************************
 ** file related functions
 ********************************************************************/

APTR sav_ReadFile(char *file);  // read a file using exec.AllocVec
APTR sav_ReadFile2(char *file);  // read a file using savagelibs.sav_AllocVec
BOOL sav_FileExists(char *file);  // check if a file does exist or not
BOOL sav_DirExists(char *file);  // check if a directory does exist or not
struct FileInfoBlock *sav_FileGetFIB(char *file);  // get a files FIB
void sav_FileFreeFIB(struct FileInfoBlock *fib);  // free a FIB allocated with sav_FileGetFIB
long sav_FileSize(char *file);  // get the size of a file
long sav_FileType(char *file);  // get the file type (see <savage/filetype.h>)
long sav_FileSum(char *file);  // get a checksum for a file
char *sav_FileVersion(char *file);  // find the version string of a file
BOOL sav_FileWriteProtected(char *file);  // check for write protection
long sav_FileUnProtect(char *file);  // clear the write protection of a file
struct Resident *sav_GetFileRomtag(char *file);  // find a RomTag structure of a file
void sav_FreeFileRomtag(void);  // free the RomTag structure allocated with sav_GetFileRomtag
void sav_Print(char *string);  // print the string to stdout
void sav_PrintLF(void);  // print a LF to stdout
BPTR sav_CD(char *dir);  // change the current directory to dir
long sav_PathLen(char *path);  // get the length of a path
BPTR sav_MakeDir(char *dir);  // check for a dir and create it, if it still does not exist
BOOL sav_FontFile(char *file);  // check for ".font" suffix
BOOL sav_InfoFile(char *file);  // check for ".info" suffix
BOOL sav_DirWalk(char *dir, BOOL (*)(struct FileInfoBlock *, APTR), APTR usrdata);  // call entryfun(fib, usrdata) with every entry of dir
struct FileLock *sav_GetCurrentDir(void);  // returns the processes current directory
void sav_ReduceDir(char *path);  // reduce the path with one (the last) path component
long sav_GetProtection(char *file);  // returns the protection of a file

/********************************************************************
 ** gui functions
 ********************************************************************/

struct GUIContext *sav_GetGUIContext(char *screenname);  // returns the GUIContext for the given screen (see <savage/guicontext.h>)
void sav_FreeGUIContext(struct GUIContext *guic);  // free the GUIContext allocated with sav_GetGUIContext
void sav_WindowInnerDim(struct Window *window, struct Rectangle *rect);  // store windows inner dimensions into rect
void sav_CloseSharedWindow(struct Window *window);  // close a window with a shared UserPort
void sav_VisibleRect(struct Screen *screen, struct Visible *vis);  // store the visible part of an oversized screen to vis
void sav_ClearWindow(struct Window *window);  // clear windows content
APTR sav_LockWindow(struct Window *window);  // lock a window
void sav_UnlockWindow(APTR winlock);  // unlock a window which was locked with sav_LockWindow

/********************************************************************
 ** memory supporting functions
 ********************************************************************/

APTR sav_AllocVec(ULONG size, ULONG memflags);  // alloc memory and save the pointer to tasks tc_MemEntry field
void sav_FreeVec(APTR addr);  // free memory allocated with sav_AllocVec
void sav_FreeMemList(void);  // dispose the entries of tasks tc_MemEntry field which are allocated using sav_AllocVec
void sav_ClearMem(APTR addr, long size);  // fill the given memory block with zeros
APTR sav_CloneMem(APTR addr, long size);  // clones the given memory; uses exec.AllocVec for allocating the memory
APTR sav_CloneMem2(APTR addr, long size);  // clones the given memory; uses sav_AllocVec for allocating the memory

/********************************************************************
 ** list and related functions
 ********************************************************************/

void sav_InitListHeader(struct List *list);  // initializes the list header
struct Node *sav_GetHead(struct List *list);  // returns the first node or NULL if empty
struct Node *sav_GetTail(struct List *list);  // returns the last node or NULL if empty
long sav_ListLen(struct List *list);  // returns the length of a list (i.e. the number of nodes)
long sav_TailLen(struct Node *node);  // returns the number of nodes
long sav_NodeNum(struct List *list, struct Node *node);  // returns the number of the node
struct Node *sav_FindNodeNum(struct List *list, long n);  // returns the n-th node of the list or NULL
BOOL sav_ForAllNodes(struct List *list, BOOL (*)(struct Node *, APTR), APTR usrdata);  // calls nodefun(node, userdata) for every node of the list
void sav_NodeToHead(struct List *list, struct Node *node);  // moves a node to the top of the list
void sav_NodeToTail(struct List *list, struct Node *node);  // moves a node to the tail of a list
int sav_InsertNodeABC(struct List *list, struct Node *node);  // respectively to nodes name, the node will be alphabetically inserted
struct Node *sav_MakeNode(long size, char *name);  // creates a node of size "size" and (optionally) duplicates the name and uses it as ln_Name
void sav_FreeNode(struct Node *node);  // frees a node created with sav_MakeNode
void sav_FreeList(struct List *list);  // disposes all nodes (see sav_MakeNode) of a list
void sav_AppendList(struct List *list1, struct List *list2);  // appends list2 to the end of list1
struct Node *sav_FindNameNoCase(struct List *list, char *name);  // searches the list for a node, where node's name equals the given one (ignore cases!)
long sav_NumFindNameNoCase(struct List *list, char *name);  // searches the list for a node, where node's name equals the given one (ignore cases!) and returns the number of this node or -1

/********************************************************************
 ** functions for system
 ********************************************************************/

void sav_GetSysInfo(struct SysInfo *si);  // fill the SysInfo structure (see <savage/sysinfo.h>)
long sav_TotalStackSize(void);  // get the total size of the tasks stack
long sav_StackLeft(void);  // get the free stack size
BOOL sav_SetCustomStack(long size);  // set a custom stack
void sav_StackSwap(struct StackSwapStruct *sws);  // !!!!!!!!!!! fix a bug in amiga.lib StackSwap
void sav_DisposeCustomStack(void);  // remove/free a custom stack set with sav_SetCustomStack
int sav_GetSeed(void);  // get a seed vor a pseudo-random generator
struct Library *sav_FindLibrary(char *name);  // search the list of current opened libraries for a specific one
struct Device *sav_FindDevice(char *name);  // search the list of current opened devices for a specific one
long sav_ConvertVerString(char *verstr);  // extract a version number of a standard $VER string
long sav_GetReg(long reg);  // get the value of a specific cpu register (see <savage/getreg.h>)
APTR *sav_GetCurrentSP(void);  // returns the current value of the register A7 (stack pointer)
BOOL sav_ExistsDosEntry(char *name, long type);  // search the system for a assign/device/volume
struct Window *sav_QuietProcess(void);  // supress system requesters like "please insert volume..."
void sav_KPrint(char *string);  // print the string using KPrintF
BOOL sav_OpenCloseLibrary(char *libname);  // check for a library (open/close the lib)
APTR sav_ReadSP(void);  // returns the current A7
void sav_WriteSP(APTR addr);  // moves addr to A7
long sav_PutMsg(char *portname, struct Message *msg);  // searches the port and sends it a msg

/********************************************************************
 ** stupid (weird functions!)
 ********************************************************************/

long sav_BitChange(long val, long num);

/********************************************************************
 ** additional functions
 ********************************************************************/

long sav_EasyRequest(struct Window *window, char *title, char *body, char *gads, APTR args);  // see intuition.EasyRequestArgs
void sav_InitTAttr(struct TextAttr *tattr, struct TextFont *font);  // init a TextAttr structure with values of font
ULONG sav_TextLength(char *string, struct TextFont *font);  // get the size (pixels) of the string
APTR sav_StringF(char *fmtstring, APTR args);  // format a string with the given args (use exec.AllocVec() for the resulting string)
APTR sav_StringF2(char *fmtstring, APTR args);  // format a string with the given args (use savagelib.sav_AllocVec() for the resulting string)
long sav_RangeNumber(long num, long lowerrange, long upperrange);  // if num is smaller/greater than lowerrange/upperrange the cut it to size and return num
long sav_MatchPatternNoCase(char *string, char *pattern);  // matches string and the pattern; return 1 for match, 0 for no match, -1 for an error
char *sav_DupString(char *str);  // makes a copy of a string; uses exec.AllocVec for allocating the memory
char *sav_DupString2(char *str);  // makes a copy of a string, uses sav_AllocVec for allocating the memory
char *sav_BStr2CStr(BPTR str);  // takes the BPTR and copys the BSTR to a new AllocVec()`ed memory-area

#endif

