Commits

Palmer, 2E0EOL committed 49b7a00

Add rttb.h as was when we ran against rttbproj lib/dll this time

Comments (0)

Files changed (1)

+/*
+Daybo Logic Run-Time TarBall Library And Tools
+Copyright (c) 2000-2006, David Duncan Ross Palmer, Daybo Logic
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright 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 the Daybo Logic nor the names of its contributors
+      may be used to endorse or promote products derived from this software
+      without specific prior written permission.
+
+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.
+*/
+
+#ifndef INC_LIB_RTTB_H
+#define INC_LIB_RTTB_H
+/*--------------------------------------------------------------------------*/
+/*
+  Export/import definition for easy library building.
+  RTTB_AS_DLL means either that librttb is being built as a LE/PE DLL, or
+  some other shared object format.
+*/
+
+/*
+  Visual C++ does this automatically, so we can take advantage automagically.
+*/
+#if ( defined(RTTB_EXPORTS) || defined(RTTB_IMPORTS) )
+#  ifndef RTTB_AS_DLL
+#    define RTTB_AS_DLL
+#  endif
+#endif
+
+#if ( defined(RTTB_AS_DLL) && defined(RTTB_SOURCE) && defined(__WIN32__) )
+#  define RTTBAPI(ret) __declspec(dllexport) ##ret __stdcall
+#elif ( defined(RTTB_AS_DLL) && !defined(RTTB_SOURCE) && defined(__WIN32__) )
+#  define RTTBAPI(ret) __declspec(dllimport) ##ret __stdcall
+#else
+#  define RTTBAPI(ret) ret
+#endif
+
+/*
+  Ensure all structure packing is aligned at DWORD boundries for compatibillity
+  with VB and most other 32-bit applications.
+*/
+#if ( defined(RTTB_AS_DLL) && defined(__WIN32__) )
+# include <pshpack4.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif /*__cplusplus*/
+
+/* Don't confuse this with the FILE* handle of the RTTB file, each file you
+attempt to open, whether in the RTTB or overridden from an external file
+is assigned an RTTB_HANDLE */
+typedef unsigned int RTTB_HANDLE;
+#define RTTB_NULLHANDLE (0U)
+#define RTTB_MAXHANDLE (UINT_MAX) /* This certainly doesn't mean I could open this many */
+
+#define RTTB_REVISION (0) /* Incompatible revision counter */
+#define RTTB_COPYRIGHTLENGTH (93) /* Copyright notice in containers */
+
+typedef unsigned short RTTB_ERROR; /* Error type */
+/* Possible errors: */
+#define RTTB_ERROR_NOERROR        (0)
+#define RTTB_ERROR_CANTOPEN       (1) /* The file could not be opened, it possibly doesn't exist */
+#define RTTB_ERROR_READONLY       (2) /* librttb cannot write, it's a readonly library */
+#define RTTB_ERROR_INVHANDLE      (3) /* A handle passed was invalid */
+#define RTTB_ERROR_OVERSEEK       (4) /* Attempted to seek past the end of the file is not acceptable as it will read the next file in the RTTB archive */
+#define RTTB_ERROR_UNDERSEEK      (5) /* Attempted to seek past the beginning and into a previous file in the RTTB */
+#define RTTB_ERROR_INVPARAMS      (6) /* Invalid parameters sent to function, if you're using the debug version the lib will probally crash out instead with an assert() trap */
+#define RTTB_ERROR_OPENLIMIT      (7) /* There's a limit to how many files open may open per rttb file at the same time, this does not limit the amount of files the rttb container may hold, see rttb_config.h */
+#define RTTB_ERROR_OLD            (8) /* The RTTB file is too old, rebuild the archive with a new version of makerttb */
+#define RTTB_ERROR_NEW            (9) /* The RTTB file was made by a later version of makerttb, use the makerttb supplied with librttb */
+#define RTTB_ERROR_UNEXPECTEDEOF  (10) /* If the header specified a filelength and the archive truncated prematurely this error is returned, could occur preparing the RTTB too (header damanged in some way) */
+#define RTTB_ERROR_MALLOC         (11) /* Out of memory */
+#define RTTB_ERROR_INVRTTB        (12) /* No such container */
+#define RTTB_ERROR_NOSUCHFILE     (13) /* When the file cannot be found loose or contained */
+#define RTTB_ERROR_HANDLEGEN      (14) /* Very unlikely, handle generation error, exhausted all possible numbers for a handle, I can't see you getting this error */
+#define RTTB_ERROR_UNEXPECTED     (15) /* Unexpected errors I don't expect to happen but could, they aren't the same as assert() trap bugs and are not as critical, the only one I know of is the RTL's fseek() failing on a valid, proper stream */
+#define RTTB_ERROR_EOF            (16) /* A normal EOF event, totally equivilent to stdio's EOF which is expected.  See also RTTB_ERROR_UNEXPECTEDEOF */
+/* Endian getting and setting command type */
+enum LowOrHigh_t
+{                                               
+  wtlo,
+  wthi
+};
+
+/*
+  This is a dirty hack to ensure configure will successfully
+  compile us.
+*/
+#ifndef ULONG_MAX
+# define ULONG_MAX (0xFFFFFFFFUL)
+#endif /*!ULONG_MAX*/
+
+typedef unsigned char RTTB_BYTE;
+typedef unsigned char RTTB_WORD8;
+typedef unsigned short RTTB_WORD16;
+#if ( ULONG_MAX > 0xFFFFFFFFUL ) /* 64-bit machine */
+  typedef unsigned int RTTB_WORD32;
+#else /* 16/32 */
+  typedef unsigned long RTTB_WORD32;
+#endif
+typedef unsigned int RTTB_UINT;
+/* This only applies if somebody wants to not use the prefix for types,
+generally these types clash with other types so this might not work for
+you.  Define RTTB_USING_TYPES if it works: */
+#ifdef RTTB_USING_TYPES
+# define BYTE    RTTB_BYTE
+# define WORD16  RTTB_WORD16
+# define WORD32  RTTB_WORD32
+# define UINT    RTTB_UINT
+#endif /*RTTB_USING_TYPES*/
+/* Stuff in rttb_endian.c
+   ----------------------*/
+/* Swappers, simple to operate */
+RTTBAPI(RTTB_WORD16) rttb_Swap16(const RTTB_WORD16 W);
+RTTBAPI(RTTB_WORD32) rttb_Swap32(const RTTB_WORD32 DW);
+/* Simple word makers */
+RTTBAPI(RTTB_WORD16) rttb_MakeMachWord16(const RTTB_WORD8 Hi, const RTTB_WORD8 Lo);
+RTTBAPI(RTTB_WORD32) rttb_MakeMachWord32(const RTTB_WORD16 Hi, const RTTB_WORD16 Lo);
+/* Simple word getters */
+RTTBAPI(RTTB_WORD8) rttb_GetWord8(const enum LowOrHigh_t LowOrHigh, const RTTB_WORD16 Word16);
+RTTBAPI(RTTB_WORD16) rttb_GetWord16(const enum LowOrHigh_t LowOrHigh, const RTTB_WORD32 Word32);
+RTTBAPI(int) rttb_IsBigEndian(void); /* Not used internally */
+/* Stuff in rttb.c
+   ---------------*/
+RTTBAPI(int) rttb_Setup(void); /* You must call this before anything else in the library!  It may fail, be prepared for this, such a failure would probally mean no memory left */
+RTTBAPI(void) rttb_Cleanup(void); /* Call this after you finish using the library.  The memory manager (DPCRTLMM) will go mental if you don't! */
+RTTBAPI(int) rttb_StartedCheck(void); /* Simply verifies the library is started and operational */
+RTTBAPI(long int) rttb_LocalToGlobalOffset(RTTB_HANDLE Handle, long int LocalOffset); /* Converts a file offset into a container offset. -1 on failure */
+/* The library has been in such a way, that it is safe (though questionable)
+   to shutdown the library and restart it with these functions */
+/* Stuff in fopen.c
+   ----------------*/
+/*
+  Quick notes about fopen(): Update/write modes return errors as librttb is
+  a read only interface!  ProjectedRTTB is the file which you suspect,
+  holds the file.  The order of the check is changeable on the end with LooseFirst.
+  Lots of errors can occur here so check all of them.  To check the error
+  pass a pointer to an RTTB_ERROR
+*/
+RTTBAPI(RTTB_HANDLE) rttb_fopen(const char* ProjectedRTTB, const char* FileName, const char* Mode, const int LooseFirst, RTTB_ERROR* Perr);
+/* Stuff in fclose.c
+   -----------------*/
+/*
+  Quick notes about fclose(): Closes the said file and releases it's real
+  handle (if it's loose).  If it's contained then this doesn't do much but
+  you must still close it so that it's handle is released.  When all files
+  assosiated with a container have been closed the said container is also
+  closed.  Possible returns are RTTB_ERROR_NOERROR or RTTB_ERROR_INVHANDLE.
+*/
+RTTBAPI(RTTB_ERROR) rttb_fclose(RTTB_HANDLE Handle);
+/* Stuff in fseek.c
+   ----------------*/
+/*
+  Quick notes about fseek(): The syntax is like normal fseek(), except that
+  it expects an RTTB_HANDLE as opposed to a FILE*, different error codes
+  are used too as the normal behaviour of fseek() errors is not descriptive
+  enough for what might happen here.  Generally the errors would be:
+  RTTB_ERROR_NOERROR, RTTB_ERROR_INVHANDLE,
+  RTTB_ERROR_OVERSEEK & RTTB_ERROR_UNDERSEEK.
+  RTTB_ERROR_INVPARAMS or an assert() trap could occur if, say,
+  Whence is not one of the standard idenrifiers accepted by fseek().
+*/
+RTTBAPI(RTTB_ERROR) rttb_fseek(RTTB_HANDLE Handle, long int Offset, int Whence);
+#define rttb_rewind(handle) \
+        rttb_fseek((handle), 0, SEEK_SET)
+
+/* Stuff in feof.c
+   ---------------*/
+/*
+  int rttb_feof() returns nonzero if the current offset is one less than the
+  length of the file (at the end of the file). Say if file is 10 bytes long and
+  at offset 9, that is the End Of File.  It returns zero if not so.  For simplicty
+  it returns no RTTB error codes.
+*/
+RTTBAPI(int) rttb_feof(const RTTB_HANDLE Handle);
+/* Stuff in ftell.c
+   ----------------*/
+/*
+  ftell() is a simple way to aquire the current file offset of any given
+  file.  Ofsets are returned via a pointer unlike C's ftell() to make room
+  for my error codes.
+*/
+RTTBAPI(RTTB_ERROR) rttb_ftell(const RTTB_HANDLE Handle, long int* POffset);
+/*
+  Stuff in filelength.c
+  ---------------------*/
+/*
+  This function is recommened instead of seeking to the end of the file
+  and reading the offset (as is normally the method in C), the length is
+  often cached in the header of the container and well, there's lots of
+  other reasons why this is faster, sure sometimes it does lead to a C
+  library fseek() call but only once and only if the file is loose.  It
+  works in a very similar fasion to rttb_ftell().
+*/
+RTTBAPI(RTTB_ERROR) rttb_filelength(const RTTB_HANDLE Handle, long int* PLength);
+/*
+  Stuff in fread.c
+  ----------------*/
+/*
+  This function works in the same way as a normal fread() with these
+  differences:  The extra parameter is for optionally recovering an
+  error number and may be NULL if not wanted.  An RTTB_HANDLE is expected
+  not a FILE*
+*/
+RTTBAPI(size_t) rttb_fread(void* Ptr, size_t Size, size_t N, RTTB_HANDLE Handle, RTTB_ERROR* PError);
+/* Stuff in rttb.c
+   ---------------
+*/
+/*
+  Returns length of an error message, pass error number, pass a pointer to
+  a string buffer which is at least MaxLen characters plus space for a NULL
+  terminator.  In your first call you'll generally pass NULL just to get the
+  length.
+*/
+RTTBAPI(unsigned int) rttb_GetErrorMessage(const RTTB_ERROR ErrorNumber, char* PYourString, const unsigned int MaxLen);
+/*
+  Returns the library version, major, minor and patch number
+*/
+RTTBAPI(void) rttb_GetLibVer(unsigned int* PMajor, unsigned int* PMinor, unsigned int* PPatch);
+/*
+  Stuff in rttb_fgetc.h
+  ---------------------
+*/
+/*
+  Does the same job as the ANSI C function fgetc() but takes an
+  RTTB_HANDLE as a parameter.  Remember that the function can return EOF
+  before the beginning of the file if the file contains an EOF character,
+  to avoid this use another function.
+*/
+RTTBAPI(int) rttb_fgetc(RTTB_HANDLE Handle, RTTB_ERROR* PError);
+
+#ifdef __cplusplus
+} /* Allow symbol mangling */
+#endif /*__cplusplus*/
+/*-------------------------------------------------------------------------*/
+/* If don't want to use the prefix all the time, define RTTB_USING_FUNCTIONS,
+Note to self: Keep this list up to date or stuff will break!
+Note to others: Be careful!  Document in your programs the fact that
+calls to fopen and such will silently go through RTTB! */
+#ifdef RTTB_USING_FUNCTIONS
+# define Swap16             rttb_Swap16
+# define Swap32             rttb_Swap32
+# define MakeMachWord16     rttb_MakeMachWord16
+# define MakeMachWord32     rttb_MakeMachWord32
+# define GetWord8           rttb_GetWord8
+# define GetWord16          rttb_GetWord16
+# define IsBigEndian        rttb_IsBigEndian
+# define Setup              rttb_Setup
+# define Cleanup            rttb_Cleanup
+# define StartedCheck       rttb_StartedCheck
+# define fopen              rttb_fopen
+# define fclose             rttb_fclose
+# define fseek              rttb_fseek
+# define feof               rttb_feof
+# define ftell              rttb_ftell
+# define filelength         rttb_filelength
+# define GetErrorMessage    rttb_GetErrorMessage
+# define GetLibVer          rttb_GetLibVer
+# define fread              rttb_fread
+# define fgetc              rttb_fgetc
+#endif /*RTTB_USING_FUNCTIONS*/
+/*------------------------------- C++ only --------------------------------*/
+/* The two classes represent the main objects, a container and a file,
+TRttbContainer, TRttbFile.  Every file must be assosiated with a container,
+therefore TRttbFile instantances must must be initialised with an already
+existing container class.  Attempting to destroy the container without
+first destroying all the file objects assosiated with it is illegal,
+I have some rudimentry code to try to detect this, however, it is not
+fool proof and can be tampered with.  I suggest you don't though! */
+
+#ifdef __cplusplus
+class TRttbContainer
+{
+private:
+    char* _containerName; /* Dynamic */
+    char* _defMode; /* Dynamic */
+    unsigned int _assFileCount; /* Rudimentry safety for container before file destruction */
+  void _NoContainer();
+  void _NoDefMode();
+  void _SetContainerName(const char* ContainerName);
+public:
+    bool DefLooseFirst; // Default whether to use loose first or contained first
+  TRttbContainer(const char* ContainerName);
+  ~TRttbContainer();
+  unsigned int GetContainerName(char* PBuff, size_t MaxSize);
+  void AssNotify(int Direction); /* Do not call this directly even though it's public */
+  void SetDefMode(const char* NewDefMode); /* Default mode to open files in if files are specified NULL mode (factory default="rb"), NULL passed here is ignored! */
+  unsigned int GetDefMode(char* PBuff, size_t MaxSize);
+};
+
+class TRttbFile
+{
+private:
+    RTTB_HANDLE _handle;
+    char* _fileName;
+    TRttbContainer* _container; /* The container we're in */
+  void _SetFileName(const char* NewFileName);
+  void _InitData();
+protected:
+    RTTB_ERROR lastError;
+  TRttbContainer* GetContainer();
+public:
+  TRttbFile(TRttbContainer& AssContainer, const char* FileName);
+  TRttbFile(TRttbContainer* PAssContainer, const char* FileName);
+  ~TRttbFile();
+  bool Seek(long Offset, int Whence);
+  bool Read(void* Ptr, size_t Size, size_t N);
+  bool Eof();
+  int GetC();
+  char* GetS(char* S, int size); /* Note size is not length, length = size - 1 */
+  RTTB_ERROR GetLastError();
+  unsigned int GetFileName(char* PBuff, size_t MaxLen);
+  bool IsOpen();
+  bool Close();
+  bool Open(short LooseFirst, const char* Mode); /* LooseFirst = 0 no, 1 yes, -1 use default of container, Mode set to a mode or NULL use container default */
+  bool Open(); /* For lazier programmers, like me */
+  long int FileLength(); /* If it returns -1L check the last error */
+};
+#endif /*__cplusplus*/
+/*-------------------------------------------------------------------------*/
+#endif /*!INC_LIB_RTTB_H*/