diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/Buffer.h | 37 | ||||
-rw-r--r-- | includes/Exceptions.h | 137 | ||||
-rw-r--r-- | includes/General.h | 7 | ||||
-rw-r--r-- | includes/Handle.h | 50 | ||||
-rw-r--r-- | includes/Image.h | 61 | ||||
-rw-r--r-- | includes/Input.h | 41 | ||||
-rw-r--r-- | includes/Output.h | 45 | ||||
-rw-r--r-- | includes/String.h | 67 |
8 files changed, 445 insertions, 0 deletions
diff --git a/includes/Buffer.h b/includes/Buffer.h new file mode 100644 index 0000000..79d0e02 --- /dev/null +++ b/includes/Buffer.h @@ -0,0 +1,37 @@ +#ifndef __BUFFER_H__ +#define __BUFFER_H__ +#ifdef __cplusplus + +#include <zlib.h> +#include <Exceptions.h> +#include <Handle.h> + +#ifndef realloc_threshold +#define realloc_threshold 256 +#endif + +class Buffer : public Handle { + public: + Buffer(); + Buffer(const Buffer &); + virtual ~Buffer(); + virtual ssize_t write(const void *buf, size_t count) throw(GeneralException); + virtual ssize_t read(void *buf, size_t count) throw (GeneralException); + virtual bool CanRead(); + virtual bool CanWrite(); + virtual String GetName(); + virtual Buffer operator=(const Buffer &); + virtual bool CanWatch(); + virtual ssize_t GetSize(); + char operator[](size_t) const; + char & operator[](size_t); + + private: + char * buffer, zero; + size_t realsiz, bufsiz, ptr; +}; + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/Exceptions.h b/includes/Exceptions.h new file mode 100644 index 0000000..ef697e7 --- /dev/null +++ b/includes/Exceptions.h @@ -0,0 +1,137 @@ +#ifndef __EXCEPTIONS_H__ +#define __EXCEPTIONS_H__ +#ifdef __cplusplus + +#include <stdio.h> +#include <unistd.h> +#include <stddef.h> +#include <string.h> +#include <stdlib.h> + +#define INLINE __inline__ + +class Base { + public: + static char * strdup(const char * s); + static void * malloc(ssize_t s); + static void * realloc(void * p, size_t s); + static void * calloc(size_t n, size_t s); + void * operator new(size_t s); + void * operator new(size_t s, void * p); + void operator delete(void * p); + static void free(void *& p); + static void free(char *& p); + static int pipe(int * p, int flag = 0); + static pid_t fork(); +}; + +class String; + +class GeneralException : public Base { + public: + GeneralException(String); + GeneralException(const GeneralException &); + ~GeneralException(); + char * GetMsg(); + + protected: + GeneralException(); + char * msg; + static char t[BUFSIZ]; +}; + +char * xstrdup(const char *); +void * xmalloc(size_t) throw (GeneralException); +void xfree(void *&); +void xfree(char *&); +void * xrealloc(void *, size_t); +int xpipe(int *, int = 0) throw (GeneralException); +pid_t xfork() throw (GeneralException); + +INLINE char * Base::strdup(const char * s) { + return xstrdup(s); +} + +INLINE void * Base::malloc(ssize_t s) { + return xmalloc(s); +} + +INLINE void * Base::realloc(void * p, size_t s) { + return xrealloc(p, s); +} + +INLINE void * Base::calloc(size_t n, size_t s) { + return xmalloc(n * s); +} + +INLINE void * Base::operator new(size_t s) { + return xmalloc(s); +} + +INLINE void * Base::operator new(size_t s, void * p) { + return memset(p, 0, s); +} + +INLINE void Base::operator delete(void * p) { + xfree(p); +} + +INLINE void Base::free(void *& p) { + xfree(p); +} + +INLINE void Base::free(char *& p) { + xfree(p); +} + +INLINE int Base::pipe(int * p, int flag) { + return xpipe(p, flag); +} + +INLINE pid_t Base::fork() { + return xfork(); +} + +class MemoryException : public GeneralException { + public: + MemoryException(ssize_t); +}; + +class TaskNotFound : public GeneralException { + public: + TaskNotFound(); +}; + +enum op_t { + IO_WRITE = 1, + IO_READ +}; + +class IOGeneral : public GeneralException { + public: + IOGeneral(String); + protected: + IOGeneral(); +}; + +class IOException : public IOGeneral { + public: + IOException(String, op_t, ssize_t); +}; + +class IOAgain : public IOGeneral { + public: + IOAgain(); +}; + +class TaskSwitch : public GeneralException { + public: + TaskSwitch(); +}; + +#include <String.h> + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/General.h b/includes/General.h new file mode 100644 index 0000000..6f423ed --- /dev/null +++ b/includes/General.h @@ -0,0 +1,7 @@ +#ifndef __GENERAL_H__ +#define __GENERAL_H__ + +#define MAX(__a,__b) ((__a)<(__b)?(__b):(__a)) +#define MIN(__a,__b) ((__a)>(__b)?(__b):(__a)) + +#endif diff --git a/includes/Handle.h b/includes/Handle.h new file mode 100644 index 0000000..3b9c9dc --- /dev/null +++ b/includes/Handle.h @@ -0,0 +1,50 @@ +#ifndef __HANDLE_H__ +#define __HANDLE_H__ +#ifdef __cplusplus + +#include <zlib.h> +#include <unistd.h> +#include <iostream.h> +#include <String.h> +#include <Exceptions.h> + +#include <sys/types.h> +#include <time.h> + +class Handle : public Base { + public: + Handle(const Handle &); + virtual ~Handle(); + virtual ssize_t read(void *buf, size_t count) throw (GeneralException); + virtual ssize_t write(const void *buf, size_t count) throw (GeneralException); + bool IsClosed(void); + bool IsNonBlock(void); + void SetNonBlock(void); + virtual bool CanRead(); + virtual bool CanWrite(); + virtual String GetName(); + virtual ssize_t GetSize(); + virtual time_t GetModif(); + void close() throw (GeneralException); + int GetHandle(); + virtual bool CanWatch(); + virtual void Dup(const Handle &); + virtual void SetZ(int) throw (GeneralException); + protected: + Handle(int h); + private: + ssize_t uwrite(const void *, size_t) throw (GeneralException); + ssize_t uread(void *, size_t); + int h; + bool closed, nonblock; + gzFile zfile; + int z; +}; + +Handle & operator<<(Handle &, const String &); +Handle & operator>>(Handle &, String &); + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/Image.h b/includes/Image.h new file mode 100644 index 0000000..8dc6788 --- /dev/null +++ b/includes/Image.h @@ -0,0 +1,61 @@ +#ifndef __IMAGE_H__ +#define __IMAGE_H__ +#ifdef __cplusplus + +#include <Buffer.h> + +enum { + FORMAT_TGA_BASIC +} format_t; + +struct Color { + Color(unsigned char aR, unsigned char aG, unsigned char aB, unsigned char aA = 255) : + R(aR), G(aG), B(aB), A(aA) { } + unsigned char R, G, B, A; +}; + +class Image : public Buffer { + public: + Image(unsigned int, unsigned int); + virtual ~Image(); + Color GetPixel(unsigned int, unsigned int); + void SetPixel(unsigned int, unsigned int, Color); + bool Prepare(unsigned int = FORMAT_TGA_BASIC); + void Fill(Color = Color(0, 0, 0)); + virtual String GetName(); + virtual bool CanWrite(); + + private: + typedef unsigned char Byte; + typedef unsigned short int Word; + typedef unsigned long int DWord; + struct TGAHeader { + Byte IDLength; + Byte ColorMapType; + Byte ImageType; + Word CM_FirstEntry; + Word CM_Length; + Byte CM_EntrySize; + Word IS_XOrigin; + Word IS_YOrigin; + Word IS_Width; + Word IS_Height; + Byte IS_Depth; + Byte IS_Descriptor; + } __attribute__((packed)); + + struct TGAFooter { + DWord ExtOffset; + DWord DevOffset; + char Sig[18]; + } __attribute__((packed)); + + unsigned int x, y; + bool r; + Color * img; +}; + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/Input.h b/includes/Input.h new file mode 100644 index 0000000..7091258 --- /dev/null +++ b/includes/Input.h @@ -0,0 +1,41 @@ +#ifndef __INPUT_H__ +#define __INPUT_H__ +#ifdef __cplusplus + +#include <sys/types.h> +#include <time.h> +#include <String.h> +#include <Handle.h> + +class Input : public Handle { + public: + Input(String = "") throw (GeneralException); + Input(const Input &); + virtual ~Input() {} + virtual bool CanWrite(); + virtual bool CanRead(); + virtual String GetName(); + virtual ssize_t GetSize(); + virtual time_t GetModif(); + + protected: + String n; + off_t size; + time_t date_modif; +}; + +class Stdin_t : public Handle { + public: + Stdin_t(); + virtual ~Stdin_t() {} + virtual bool CanWrite(); + virtual bool CanRead(); + virtual String GetName(); +}; + +extern Stdin_t Stdin; + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/Output.h b/includes/Output.h new file mode 100644 index 0000000..32d4e66 --- /dev/null +++ b/includes/Output.h @@ -0,0 +1,45 @@ +#ifndef __OUTPUT_H__ +#define __OUTPUT_H__ +#ifdef __cplusplus + +#include <String.h> +#include <Handle.h> + +class Output : public Handle { + public: + Output(String = "", int trunc = 1) throw (GeneralException); + Output(const Output &); + virtual ~Output() {} + virtual bool CanWrite(); + virtual bool CanRead(); + virtual String GetName(); + + protected: + String n; +}; + +class Stdout_t : public Handle { + public: + Stdout_t(); + virtual ~Stdout_t() {} + virtual bool CanWrite(); + virtual bool CanRead(); + virtual String GetName(); +}; + +class Stderr_t : public Handle { + public: + Stderr_t(); + virtual ~Stderr_t() {} + virtual bool CanWrite(); + virtual bool CanRead(); + virtual String GetName(); +}; + +extern Stdout_t Stdout; +extern Stderr_t Stderr; + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/includes/String.h b/includes/String.h new file mode 100644 index 0000000..b53936f --- /dev/null +++ b/includes/String.h @@ -0,0 +1,67 @@ +#ifndef __STRING_H__ +#define __STRING_H__ +#ifdef __cplusplus + +#include <iostream.h> +#include <string.h> +#include <Exceptions.h> + +class String : public Base { + public: + String(const String &); + String(const char * = ""); + String(char); + String(int); + String(unsigned int); +#ifdef USE_LONGLONG + String(long long); + String(unsigned long long); +#endif + String(double); + ~String(); + const char * set(const char *, ...); + const char * set(const String &, ...); + const char * to_charp(size_t = 0, ssize_t = -1) const; + String extract(size_t = 0, ssize_t = -1) const; + char * strdup(size_t = 0, ssize_t = -1) const; + int to_int() const; + double to_double() const; + String to_sqldate() const; + String to_sqltime() const; + String from_sqldate() const; + String from_sqltime() const; + double datedif(const String &) const; + bool is_date() const; + bool is_number() const; + bool is_float() const; + bool is_time() const; + size_t strlen() const; + ssize_t strchr(char, size_t = 0) const; + ssize_t strrchr(char) const; + ssize_t strstr(const String &) const; + int strchrcnt(char) const; + String & operator=(const String &); + String operator+(const String &) const; + String & operator+=(const String &); + bool operator!=(const String &) const; + bool operator==(const String &) const; + bool operator<=(const String &) const; + bool operator>=(const String &) const; + bool operator<(const String &) const; + bool operator>(const String &) const; + char operator[](size_t i) const; + + private: + String(int hs, const char *); + static char t[]; + char * str; + size_t siz; +}; + +ostream & operator<<(ostream &, const String &); +istream & operator>>(istream &, String &); + +#else +#error This only works with a C++ compiler +#endif +#endif |